Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27474

How to use Telegram API to send a message

$
0
0
Hello, I'm trying to create a simple telegram bot using its API.
I'm trying to look online but there are only old c# examples wich aren't still good.
I've found in the official documentation examples in c# ( I assume) like this one example-bot.html And translating it step by step I'm starting with their first quickstart
So I've translate that code in :

Code:

Option Strict On
    Option Explicit On
    Imports Telegram.Bot
    Public Class Form1
        Private Async Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
            Dim botClient As New TelegramBotClient("{ID }")
       
            Dim bot As Object = Await botClient.GetMeAsync()
            MessageBox.Show($"Hello, World! I am user {Me.Id} and my name is {Me.FirstName}.")
        End Sub
    End class

but it already gives me 2 errors:
Id and Firstname are not member of Form1.
So I've tried to move forward with the more full example and I've translate it with an online tool which doesn't seems to translate c# to vb.net completely.

Code:

Imports Telegram.Bot
 Imports Telegram.Bot.Exceptions
 Imports Telegram.Bot.Extensions.Polling
 Imports Telegram.Bot.Types
 Imports Telegram.Bot.Types.Enums
   
 Dim botClient As var =  New TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}")
   
 Imports var cts = New CancellationTokenSource()
   
 ' StartReceiving does not block the caller thread. Receiving is done on the ThreadPool.
 var receiverOptions = New ReceiverOptions
 {
    AllowedUpdates =
    {
         
    }
  ' receive all update types
   
 }
   
 botClient.StartReceiving(
    HandleUpdateAsync,
    HandleErrorAsync,
    receiverOptions,
    Dim cts.Token) As cancellationToken:
   
   
 Dim me As var =  await botClient.GetMeAsync()
   
 Console.WriteLine($"Start listening for @{me.Username}")
 Console.ReadLine()
   
 ' Send cancellation request to stop bot
 cts.Cancel()
   
 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.MessageNot .Type <> MessageType.Text Then
        Return
    End If
   
    Dim chatId As var =  update.Message.Chat.Id
    Dim messageText As var =  update.Message.Text
   
    Console.WriteLine($"Received a '{messageText}' message in chat {chatId}.")
   
    ' Echo received message text
    Message sentMessage = await botClient.SendTextMessageAsync(
        chatId: chatId,
        text: "You said:\n" + messageText,
        Dim cancellationToken) As cancellationToken:
 End Function
   
 Private Function HandleErrorAsync(ByVal botClient As ITelegramBotClient, ByVal exception As Exception, ByVal cancellationToken As CancellationToken) As Task
    var ErrorMessage = exception switch
    {
        ApiRequestException apiRequestException
            => $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
        _ => exception.ToString()
    }
   
   
    Console.WriteLine(ErrorMessage)
    Return Task.CompletedTask
 End Function

I'm ending up having lots of errors. Also i don't really need all those things, I just need a button that send whatever is written in a textbox. All the rest is useles to me. Can somebody help me to translate it?
Thanks

Viewing all articles
Browse latest Browse all 27474