Results 1 to 10 of 10

Thread: How to use Telegram API to send a message

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    How to use Telegram API to send a message

    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How to use Telegram API to send a message

    Are Id and FirstName TextBoxes?
    It’d be Me.Id.Text and Me.FirstName.Text if they are…

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Re: How to use Telegram API to send a message

    No they aren't textboxes. I don't know what they are, I supposed the API should get them from the token ID

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How to use Telegram API to send a message

    The error says they’re undefined variables if they’re not textboxes

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Re: How to use Telegram API to send a message

    I've removed those two and just used a normal string. I'm getting the error Telegram.Bot.Exceptions.ApiRequestException: 'Not Found'

  6. #6
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    579

    Re: How to use Telegram API to send a message

    So you got this example C# code:
    C# Code:
    1. using Telegram.Bot;
    2.  
    3. var botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}");
    4.  
    5. var me = await botClient.GetMeAsync();
    6. Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");

    and...
    Quote Originally Posted by matty95srk View Post
    ...
    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
    So you translated this:
    C# Code:
    1. var me = await botClient.GetMeAsync();
    to this:
    VB.NET Code:
    1. Dim bot As Object = Await botClient.GetMeAsync()

    Now the obvious change is the variable me from C# became bot in VB.NET as Me is reserved word. Also why you declare bot as Object when you can just use Dim bot = await botClient.GetMeAsync() that will return the proper object type of User (Telegram.Bot.Types.User).

    It is not clear why you don't use the new variable name bot in next line and you keep using Me (as in original C# code) which is used in completely different context in VB apps:
    VB.NET Code:
    1. MessageBox.Show($"Hello, World! I am user {Me.Id} and my name is {Me.FirstName}.")

    Just fix your code like this:
    VB.NET Code:
    1. Dim bot = Await botClient.GetMeAsync()
    2. MessageBox.Show($"Hello, World! I am user {bot.Id} and my name is {bot.FirstName}.")

    Also insisting to use WinForms apps for tests while "translating" C# console apps and mixing the logic inside Button_Click event handlers is another way to mess things.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Re: How to use Telegram API to send a message

    Quote Originally Posted by peterst View Post
    So you got this example C# code:
    C# Code:
    1. using Telegram.Bot;
    2.  
    3. var botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}");
    4.  
    5. var me = await botClient.GetMeAsync();
    6. Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");

    and...


    So you translated this:
    C# Code:
    1. var me = await botClient.GetMeAsync();
    to this:
    VB.NET Code:
    1. Dim bot As Object = Await botClient.GetMeAsync()

    Now the obvious change is the variable me from C# became bot in VB.NET as Me is reserved word. Also why you declare bot as Object when you can just use Dim bot = await botClient.GetMeAsync() that will return the proper object type of User (Telegram.Bot.Types.User).

    It is not clear why you don't use the new variable name bot in next line and you keep using Me (as in original C# code) which is used in completely different context in VB apps:
    VB.NET Code:
    1. MessageBox.Show($"Hello, World! I am user {Me.Id} and my name is {Me.FirstName}.")

    Just fix your code like this:
    VB.NET Code:
    1. Dim bot = Await botClient.GetMeAsync()
    2. MessageBox.Show($"Hello, World! I am user {bot.Id} and my name is {bot.FirstName}.")

    Also insisting to use WinForms apps for tests while "translating" C# console apps and mixing the logic inside Button_Click event handlers is another way to mess things.

    I stated I used an online converter to convert c# to vb.net. I don't know c#. I didn't know that was c# console, but now I'm working on winforms.

    Buy the way I'm getting the same error Telegram.Bot.Exceptions.ApiRequestException: 'Not Found'
    on Dim bot = Await botClient.GetMeAsync() . I'm sure the token id is correct.

    Edit:
    it doesn't return any error in the case I use my token id bewteen "" and not in "{ id }".So i assume the first step is okay.
    Last edited by matty95srk; Feb 27th, 2022 at 05:17 PM.

  8. #8
    New Member
    Join Date
    Sep 2021
    Posts
    5

    Re: How to use Telegram API to send a message

    Same with me.. i can't .. i want to it.. language vb.net .. not c# please convert https://telegrambots.github.io/book/1/example-bot.html

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,734

    Re: How to use Telegram API to send a message

    Try one of the free online converters for C# to VB.Net:
    https://converter.telerik.com/

  10. #10
    New Member
    Join Date
    Mar 2022
    Posts
    2

    Re: How to use Telegram API to send a message

    I’ve done this in a small VB WinForms app. On the button click I just create the client and send the textbox text: Dim bot = New TelegramBotClient("YOUR_TOKEN"); Await bot.SendTextMessageAsync(chatId:=New ChatId(CLng(txtChatId.Text)), text:=txtMsg.Text). The only tricky part was the chatId being numeric; I grabbed it once from an Update I logged after messaging the bot, but @userinfobot works too. After that, GetMeAsync and sends were fine.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width