Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using vb.net visual studio 2022 with telegram bot v18 plugin
I'm trying to get messages, ID, ... out of telegram using the new telegram v18 bot. As the bot.startreceiving has subs with arguments that are not in the startreceiving it is not going good. Tried to translate it from C# - but got errors I don't know how to solve. Can anyone tell me what I need to change for this to get it running good?

This is what I got:
VB.NET
Imports Telegram.Bot
Imports Telegram.Bot.Exceptions
Imports Telegram.Bot.Polling
Imports Telegram.Bot.Args
Imports Telegram.Bot.Extensions.Polling
Imports Telegram.Bot.Types
Imports Telegram.Bot.Types.Enums
Imports Telegram.Bot.Types.ReplyMarkups

Public Class Form1

    Public TelegramBotToken As String = "YOUR TELEGRAM TOKEN HERE"      

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim bot1 = New TelegramBotClient(TelegramBotToken)
        Dim receiverOptions1 = New ReceiverOptions
        bot.startreceiving(UpdateHandler, ErrorHandler, receiverOptions1)
    End Sub

    Private Sub ErrorHandler(arg1 As ITelegramBotClient, arg2 As Exception, arg3 As CancellationToken)
        Throw New NotImplementedException()
    End Sub

    Private Async Sub UpdateHandler(bot As ITelegramBotClient, update As Update, arg3 As CancellationToken)
        If (update.Type = UpdateType.Message) Then
            If (update.Message.Type = MessageType.Text) Then
                Dim Text As String = update.Message.Text
                Dim ID1 As String = update.Message.Chat.Id
                Dim username As String = update.Message.Chat.Username
            End If
        End If
    End Sub
End Class

the bot.startreceiving(UpdateHandler, ErrorHandler, receiverOptions1) gives error as UpdateHandler needs args ..

If it needs to be done totally differently, please let me also know

What I have tried:

tried to run this without args, was searching how to get it another way, but can't find how to get it ok.

I think I need some addhandler, but I don't know it very good to know what to put there ... Please help me out

I guess it needs to be done with an addhandler, but I don't find how.
Posted
Updated 7-Nov-22 12:33pm
v4
Comments
Graeme_Grant 31-Oct-22 18:26pm    
My car won't start. We can't see your screen and what the error is. Please update your question with more information like the error message being returned either by the runtime or the Telegram API.

The method you are calling is:
C#
public static void StartReceiving(
    this ITelegramBotClient botClient,
    Func<ITelegramBotClient, Update, CancellationToken, Task> updateHandler,
    Func<ITelegramBotClient, Exception, CancellationToken, Task> pollingErrorHandler,
    ReceiverOptions? receiverOptions = default,
    CancellationToken cancellationToken = default
)

Your UpdateHandler and ErrorHandler methods need to return a Task. You have declared them as Async Sub, which will not work, and should be avoided wherever possible.
Avoid async void methods | You’ve Been Haacked[^]

Change your methods to:
VB.NET
Private Function ErrorHandler(arg1 As ITelegramBotClient, arg2 As Exception, arg3 As CancellationToken) As Task
    Throw New NotImplementedException()
End Function

Private Async Function UpdateHandler(bot As ITelegramBotClient, update As Update, arg3 As CancellationToken) As Task
    If (update.Type = UpdateType.Message) Then
        If (update.Message.Type = MessageType.Text) Then
            Dim Text As String = update.Message.Text
            Dim ID1 As String = update.Message.Chat.Id
            Dim username As String = update.Message.Chat.Username
        End If
    End If
End Function
 
Share this answer
 
Comments
Member 12328320 6-Nov-22 19:41pm    
Hi Richard,
Thanks for this.

I'm not so familiar with this at all ...

I now got:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim bot1 = New TelegramBotClient(TelegramBotToken)
Dim receiverOptions1 = New ReceiverOptions
bot1.StartReceiving(UpdateHandler, ErrorHandler, receiverOptions1)

End Sub

Private Sub ErrorHandler(arg1 As ITelegramBotClient, arg2 As Exception, arg3 As CancellationToken)
Throw New NotImplementedException()
End Sub

Private Async Function UpdateHandler(bot As ITelegramBotClient, update As Update, arg3 As CancellationToken) As Task
If (update.Type = UpdateType.Message) Then
If (update.Message.Type = MessageType.Text) Then
Dim Text As String = update.Message.Text
Dim ID1 As String = update.Message.Chat.Id
Dim username As String = update.Message.Chat.Username
End If
End If
End Function


And I got errors on: bot1.StartReceiving(UpdateHandler, ErrorHandler, receiverOptions1)
This is line 197 in my program.

I got these errors when compiling:
Severity Code Description Project File Line Suppression State
Error BC30455 Argument not specified for parameter 'bot' of 'Private Function UpdateHandler(bot As ITelegramBotClient, update As Update, arg3 As CancellationToken) As Task'. MAP C:\VB\MAP\MAP\Form1.vb 197 Active
Error BC30455 Argument not specified for parameter 'update' of 'Private Function UpdateHandler(bot As ITelegramBotClient, update As Update, arg3 As CancellationToken) As Task'. MAP C:\VB\MAP\MAP\Form1.vb 197 Active
Error BC30455 Argument not specified for parameter 'arg3' of 'Private Function UpdateHandler(bot As ITelegramBotClient, update As Update, arg3 As CancellationToken) As Task'. MAP C:\VB\MAP\MAP\Form1.vb 197 Active
Error BC30455 Argument not specified for parameter 'arg1' of 'Private Sub ErrorHandler(arg1 As ITelegramBotClient, arg2 As Exception, arg3 As CancellationToken)'. MAP C:\VB\MAP\MAP\Form1.vb 197 Active
Error BC30455 Argument not specified for parameter 'arg2' of 'Private Sub ErrorHandler(arg1 As ITelegramBotClient, arg2 As Exception, arg3 As CancellationToken)'. MAP C:\VB\MAP\MAP\Form1.vb 197 Active
Error BC30455 Argument not specified for parameter 'arg3' of 'Private Sub ErrorHandler(arg1 As ITelegramBotClient, arg2 As Exception, arg3 As CancellationToken)'. MAP C:\VB\MAP\MAP\Form1.vb 197 Active

Can you help me out with it ? looks like I need to put arguments on updatehandler and errorhandler in the bot.startreceiving - but I don't know which to put there...
Thanks already for the help !!!!
Richard Deeming 7-Nov-22 3:36am    
Once again: Your UpdateHandler and ErrorHandler methods need to return a Task.
got it all now !!
Thanks !!

calling is:
bot1.StartReceiving(AddressOf UpdateHandler, AddressOf ErrorHandler, receiverOptions1)

and those 2 indeed as task
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900