|
-
Feb 27th, 2022, 03:37 PM
#1
Thread Starter
Lively Member
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
-
Feb 27th, 2022, 03:57 PM
#2
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2022, 04:06 PM
#3
Thread Starter
Lively Member
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
-
Feb 27th, 2022, 04:19 PM
#4
Re: How to use Telegram API to send a message
The error says they’re undefined variables if they’re not textboxes
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2022, 04:25 PM
#5
Thread Starter
Lively Member
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'
-
Feb 27th, 2022, 05:04 PM
#6
Re: How to use Telegram API to send a message
So you got this example C# code:
C# Code:
using Telegram.Bot;
var botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}");
var me = await botClient.GetMeAsync();
Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
and...
 Originally Posted by matty95srk
...
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:
var me = await botClient.GetMeAsync();
to this:
VB.NET Code:
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:
MessageBox.Show($"Hello, World! I am user {Me.Id} and my name is {Me.FirstName}.")
Just fix your code like this:
VB.NET Code:
Dim bot = Await botClient.GetMeAsync()
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.
-
Feb 27th, 2022, 05:09 PM
#7
Thread Starter
Lively Member
Re: How to use Telegram API to send a message
 Originally Posted by peterst
So you got this example C# code:
C# Code:
using Telegram.Bot;
var botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}");
var me = await botClient.GetMeAsync();
Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
and...
So you translated this:
C# Code:
var me = await botClient.GetMeAsync();
to this:
VB.NET Code:
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:
MessageBox.Show($"Hello, World! I am user {Me.Id} and my name is {Me.FirstName}.")
Just fix your code like this:
VB.NET Code:
Dim bot = Await botClient.GetMeAsync()
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.
-
Nov 1st, 2022, 12:41 AM
#8
New Member
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
-
Nov 1st, 2022, 09:07 AM
#9
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/
-
Sep 15th, 2025, 04:12 AM
#10
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|