1

I'm using Telegram API and translated that example in vb.net. That code get users messages from telegram client and reply back: Screnshot

The code is:

Imports System
Imports System.Threading
Imports Telegram.Bot
Imports Telegram.Bot.Exceptions
Imports Telegram.Bot.Extensions.Polling
Imports Telegram.Bot.Types
Imports Telegram.Bot.Types.Enums
Imports Telegram.Bot.Types.ReplyMarkups
Public Class Form1
   Public chatId As String
   Public messageText As String
   Public botClient = New TelegramBotClient("My personal Token ID")
   Public cts = New CancellationTokenSource()
   Public update as Update

   Sub Main(args As String())
       DoWork().Wait()
   End Sub

   Private Async Function DoWork() As Task
       Dim receiverOptions = New ReceiverOptions()

       botClient.StartReceiving(receiverOptions, cts.Token)

       Dim _me = Await botClient.GetMeAsync()
       Console.WriteLine($"Start listening for @{_me.Username}")
       Console.ReadLine()

       ' Send cancellation request to stop bot
       cts.Cancel()
   End Function

   Private Function HandleErrorAsync(ByVal botClient As ITelegramBotClient, ByVal exception As Exception, ByVal cancellationToken As CancellationToken) As Task
       Dim ErrorMessage = exception.ToString()
       Console.WriteLine(ErrorMessage)
       Return Task.CompletedTask
   End Function

   Async Function HandleUpdateAsync(ByVal botClient As ITelegramBotClient, ByVal update As Update, ByVal cancellationToken As CancellationToken) As Task
       ' Only process Message updates: https://core.telegram.org/bots/api#message
       If update.Type <> UpdateType.Message Then
           Return
       End If
       ' Only process text messages
       If update.Message.Type <> MessageType.Text Then
           Return
       End If
       chatId = update.Message.Chat.Id
       messageText = update.Message.Text

       Console.WriteLine($"Received a '{messageText}' message in chat {chatId}.")

       ' Echo received message text
       Dim msg = "You said:" & vbLf & messageText
       Dim sentMessage = Await botClient.SendTextMessageAsync(ChatId, msg, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, cancellationToken)
       Dim Message = Await botClient.SendTextMessageAsync(chatId, "hello world", cts)
   End Function

I'm calling the function DoWork using a button to connect the client. I Want now to call the HandleUpdateAsync through a button, but I don't know why this code thrown an Invalid Cast:-

HandleUpdateAsync(botClient, update, cts)

I also getting an error: System.MissingMemberException: 'Impossible to find the public member 'SendTextMessageAsync' of type 'TelegramBotClient'.' when I try to implement SendingMessage inside a button. Translated to vb.net is:

 Dim Message = Await botClient.SendTextMessageAsync(chatId, "hello world", cts)

What Am I doing wrong?

0 Answers0