sochix / TLSharp

Telegram client library implemented in C#
1k stars 380 forks source link

How can I get the bot keyboard button list and click the button? #299

Open AndrewChan0513 opened 7 years ago

AndrewChan0513 commented 7 years ago

Just like this button :

http://stackoverflow.com/questions/38376354/how-can-i-show-keyboard-buttons-in-a-telegram-bot-integrated-with-api-ai

image

AND inline keyboard button :

https://core.telegram.org/bots/2-0-intro

image

AndrewChan0513 commented 7 years ago

I found the bot Button is just pass the text only. So I just need to get the message from bot and get the button text : image

But when I SendRequestAsync using : TLMessagesSlice it have a Exception.

            var dialogs = await client.GetUserDialogsAsync();
            var chat = dialogs.users.lists
                .Where(c => c.GetType() == typeof(TLUser))
                .Cast<TLUser>()
                .FirstOrDefault(c => c.username == "villagegamebot");

            var inputPeer = new TLInputPeerUser() { user_id = chat.id };

            var res = await client.SendRequestAsync<TLMessagesSlice>(new TLRequestGetHistory() { peer = inputPeer });

chat var : image inputPeer : image

Exception :

System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=PEER_ID_INVALID Source=TLSharp.Core StackTrace: 於 TLSharp.Core.Network.MtProtoSender.HandleRpcResult(UInt64 messageId, Int32 sequence, BinaryReader messageReader, TLMethod request) 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\Network\MtProtoSender.cs: 行 288 於 TLSharp.Core.Network.MtProtoSender.processMessage(UInt64 messageId, Int32 sequence, BinaryReader messageReader, TLMethod request) 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\Network\MtProtoSender.cs: 行 193 於 TLSharp.Core.Network.MtProtoSender.d9.MoveNext() 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\Network\MtProtoSender.cs: 行 144 --- 先前擲回例外狀況之位置中的堆疊追蹤結尾 --- 於 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 於 TLSharp.Core.TelegramClient.d151.MoveNext() 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\TelegramClient.cs: 行 153 --- 先前擲回例外狀況之位置中的堆疊追蹤結尾 --- 於 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() 於 WindowsFormsApplication3.Form1.d__82.MoveNext() 於 C:\Users\andrewchan\Documents\Visual Studio 2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs: 行 490 --- 先前擲回例外狀況之位置中的堆疊追蹤結尾 --- 於 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter.GetResult() 於 WindowsFormsApplication3.Form1.d__108.MoveNext() 於 C:\Users\andrewchan\Documents\Visual Studio 2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs: 行 890 InnerException:

sochix commented 7 years ago

Seems like you couldn't get messages from bot with gethistory method. You should explore telegram api for the different method

10 нояб. 2016 г. 7:37 пользователь "AndrewChan0513" < notifications@github.com> написал:

I found the bot Button is just pass the text only. So I just need to get the message from bot and get the button text : [image: image] https://cloud.githubusercontent.com/assets/20835419/20164652/1ca0ffea-a741-11e6-98fe-146e01eea8b7.png

But when I SendRequestAsync using : TLMessagesSlice https://core.telegram.org/constructor/messages.messagesSlice it have a Exception.

        var dialogs = await client.GetUserDialogsAsync();
        var chat = dialogs.users.lists
            .Where(c => c.GetType() == typeof(TLUser))
            .Cast<TLUser>()
            .FirstOrDefault(c => c.username == "villagegamebot");

        var inputPeer = new TLInputPeerUser() { user_id = chat.id };

        var res = await client.SendRequestAsync<TLMessagesSlice>(new TLRequestGetHistory() { peer = inputPeer });

chat var : [image: image] https://cloud.githubusercontent.com/assets/20835419/20164713/b064e476-a741-11e6-9ca1-519275f97b71.png inputPeer : [image: image] https://cloud.githubusercontent.com/assets/20835419/20164766/43315a3c-a742-11e6-846f-ded325ed3fe0.png

Exception :

System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=PEER_ID_INVALID Source=TLSharp.Core StackTrace: 於 TLSharp.Core.Network.MtProtoSender.HandleRpcResult(UInt64 messageId, Int32 sequence, BinaryReader messageReader, TLMethod request) 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\Network\MtProtoSender.cs: 行 288 於 TLSharp.Core.Network.MtProtoSender.processMessage(UInt64 messageId, Int32 sequence, BinaryReader messageReader, TLMethod request) 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\Network\MtProtoSender.cs: 行 193 於 TLSharp.Core.Network.MtProtoSender.d9.MoveNext() 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\Network\MtProtoSender.cs: 行 144 --- 先前擲回例外狀況之位置中的堆疊追蹤結尾 --- 於 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter. HandleNonSuccessAndDebuggerNotification(Task task) 於 TLSharp.Core.TelegramClient.d151.MoveNext() 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\TelegramClient.cs: 行 153 --- 先前擲回例外狀況之位置中的堆疊追蹤結尾 --- 於 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter. HandleNonSuccessAndDebuggerNotification(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() 於 WindowsFormsApplication3.Form1.d82.MoveNext() 於 C:\Users\andrewchan\Documents\Visual Studio 2015\Projects\ WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs: 行 490 --- 先前擲回例外狀況之位置中的堆疊追蹤結尾 --- 於 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter. HandleNonSuccessAndDebuggerNotification(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter.GetResult() 於 WindowsFormsApplication3.Form1.d108.MoveNext() 於 C:\Users\andrewchan\Documents\Visual Studio 2015\Projects\ WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs: 行 890 InnerException:

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sochix/TLSharp/issues/299#issuecomment-259599933, or mute the thread https://github.com/notifications/unsubscribe-auth/ABd-YpnCP-akfLnSP8AAuq2odZucmmT_ks5q8p-AgaJpZM4KuM-o .

AndrewChan0513 commented 7 years ago

I've changed to use TLInputPeerChat :

            var dialogs = await client.GetUserDialogsAsync();
            var chat = dialogs.users.lists
                .Where(c => c.GetType() == typeof(TLUser))
                .Cast<TLUser>()
                .FirstOrDefault(c => c.username == "villagegamebot");

            var inputPeer = new TLInputPeerChat() { chat_id = chat.id };

            var res = await client.SendRequestAsync<TLMessagesSlice>(new TLRequestGetHistory() { peer = inputPeer });

But it still trough exception :

System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=PEER_ID_INVALID Source=TLSharp.Core StackTrace: 於 TLSharp.Core.Network.MtProtoSender.HandleRpcResult(UInt64 messageId, Int32 sequence, BinaryReader messageReader, TLMethod request) 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\Network\MtProtoSender.cs: 行 288 於 TLSharp.Core.Network.MtProtoSender.processMessage(UInt64 messageId, Int32 sequence, BinaryReader messageReader, TLMethod request) 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\Network\MtProtoSender.cs: 行 193 於 TLSharp.Core.Network.MtProtoSender.d9.MoveNext() 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\Network\MtProtoSender.cs: 行 144 --- 先前擲回例外狀況之位置中的堆疊追蹤結尾 --- 於 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 於 TLSharp.Core.TelegramClient.d151.MoveNext() 於 Y:\TLSharp-master\TLSharp-master\TLSharp.Core\TelegramClient.cs: 行 153 --- 先前擲回例外狀況之位置中的堆疊追蹤結尾 --- 於 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() 於 WindowsFormsApplication3.Form1.d__82.MoveNext() 於 C:\Users\andrewchan\Documents\Visual Studio 2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs: 行 490 --- 先前擲回例外狀況之位置中的堆疊追蹤結尾 --- 於 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 於 System.Runtime.CompilerServices.TaskAwaiter.GetResult() 於 WindowsFormsApplication3.Form1.d__108.MoveNext() 於 C:\Users\andrewchan\Documents\Visual Studio 2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs: 行 890 InnerException:

In getHistory method the peer parameter is support user or group :

image

InputPeer have inputPeerChat and it's using chat_id also:

image

AndrewChan0513 commented 7 years ago

I've review TeleSharp.TL.Messages.TLRequestGetHistory method is using -1347868602 ( afa92846 ) : image

It can accept inputPeerChat too.

aarani commented 7 years ago

Telegram Documention is Outdated so don't use it.

eko24ive commented 6 years ago

Any updates ?

nguyentuan3691 commented 6 years ago

Any updates for this topic?

Erinio commented 6 years ago

@tuandoi1 @eko24ive

Made a quick code on getting the buttons and url from them 👍 Since i didn't see any help thought i would try!

                    TLInputPeerUser peer = new TLInputPeerUser {UserId = user.Id, AccessHash = user.AccessHash.Value};
                    TLAbsMessages tlAbsMessages = await client.GetHistoryAsync(peer, 0, -1, 100);

                    var tlMessages = (TLMessages) tlAbsMessages;

                    foreach (TLAbsMessage tlAbsMessage in tlMessages.Messages)
                    {
                        if (!(tlAbsMessage is TLMessage message)) continue;
                        if (!(message.ReplyMarkup is TLReplyInlineMarkup replyMarkup)) continue;

                        TLVector<TLKeyboardButtonRow> rows = replyMarkup.Rows;
                        Console.WriteLine($"Rows {rows.Count}");

                        foreach (TLKeyboardButtonRow keyRow in rows)
                        {
                            TLVector<TLAbsKeyboardButton> buttons = keyRow.Buttons;

                            Console.WriteLine($"Buttons {buttons.Count}");
                            if (buttons.Count <= 0) continue;

                            foreach (TLAbsKeyboardButton keyBtn in buttons)
                            {
                                var key = (TLKeyboardButtonUrl) keyBtn;
                                Console.WriteLine(key.Url);
                            }
                        }
                    }

This works fine for me! It can be optimized just say thank you if you use it or share code if you made it more simple 👍

nguyentuan3691 commented 6 years ago

@Erinio Thanks for repply.

I want click to button. I can help me.

Many thanks

Erinio commented 6 years ago
 TLVector<TLAbsKeyboardButton> buttons = keyRow.Buttons;

                            Console.WriteLine($"Buttons {buttons.Count}");
                            if (buttons.Count <= 0) continue;

                            foreach (TLAbsKeyboardButton keyBtn in buttons)
                            {
                                var key = (TLKeyboardButtonUrl) keyBtn;
                                Console.WriteLine(key.Url);
                            }

That part is for the button data (Text, Url) etc. go ahead and check it. @tuandoi1

eko24ive commented 6 years ago

@Erinio thank you, I will try

jamesjsc commented 5 years ago

@Erinio thank you, I will try

Any updates?