How to send a message through Telegram API's?

Mattia Fanti 356 Reputation points
2022-03-02T14:27:24.423+00:00

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

shot-example_bot.jpg

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 the following 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.

shot-text_msg.jpg

Translated to vb.net is:

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

What Am I doing wrong?

Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Mattia Fanti 356 Reputation points
    2022-03-03T13:50:59.907+00:00

    I solved!
    To address my question the only code I needed into a button was:

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim bot = New TelegramBotClient("token id")
            Await bot.SendTextMessageAsync(
                       "chat id",
                        "Text")
        End Sub
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.