linvi / tweetinvi

Tweetinvi, an intuitive Twitter C# library for the REST and Stream API. It supports .NET, .NETCore, UAP (Xamarin)...
MIT License
1.01k stars 221 forks source link

How to create a custom query to insert quick reply buttons in direct messages #639

Closed lcumra closed 6 years ago

lcumra commented 6 years ago

Hi, Linvi!

I'm working with tweetinvi to create a DM chatbot, and I'd find it great to have some quick reply buttons in DM, which doesn't seem so hard on Twitter's repository but it turns out to be a nightmare when I try to implement it. Is there already a way to do so through tweetinvi? any hint on how I might do, maybe with a custom query?

Thank you so much!

linvi commented 6 years ago

Hello,

The new DM endpoints will be implemented in version 2.3. In the meantime you can indeed use Tweetinvi custom queries.

Documentation : https://github.com/linvi/tweetinvi/wiki/Custom-Queries

Cheers, Linvi

lcumra commented 6 years ago

Great!

For the time being, I tried

Auth.SetUserCredentials(consumerKey, consumerSecret, userAccessToken, userAccessSecret);
            var authenticatedUser = User.GetAuthenticatedUser();

and I got


 // works 
TwitterAccessor.ExecuteGETQueryReturningJson("https://api.twitter.com/1.1/users/show.json?screen_name=tweetinviapi"); 

now the DM stuff... This json, with twurl works and buttons appear as expected.


                var js =
                    "{ 'event': { 'type': 'message_create', 'message_create': { 'target': { 'recipient_id': '1234567 }, 'sender_id': '9876543', 'message_data': { 'text': 'do you like buttons?', 'quick_reply': { 'type': 'options', 'options': [ { 'label': 'Ieeee', 'metadata': 'external_id_1', 'description': 'Cool!' }, { 'label': 'WOW!', 'metadata': 'external_id_2', 'description': 'Great Scott!!!!' } ] } } } } }";

                var data = JsonConvert.DeserializeObject(js);
                var encoded = WebUtility.HtmlEncode(data.ToString());
                var url = "https://api.twitter.com/1.1/direct_messages/events/new.json";

// returns false 
                var test1 = TwitterAccessor.TryPOSTJsonContent(url, js);                 

// both test 2 and 2 return 
// error 401 Unauthorized

                var test2 = TwitterAccessor.ExecutePOSTQueryReturningJson($"{url}?{encoded}"); // error 401 Unauthorized
                var test3 = TwitterAccessor.ExecutePOSTQuery<IMessageDTO> ($"{url}?{encoded}");

Do you spot any mistake I might fix? Thank you again!