Ultima modifica:
Salve, sto cercando di implementare le api di telegram in particolare sono interessato ad inviare un messaggio. Dalla documentazione ufficiale in c# ho trovato Questo esempio ma è in c# per console e vorrei convertirlo in vb.net WinForms. Il codice ha bisogno del package Telegram.Bot scaricabile da NuGet. Ho già provato ad usare tools online per la conversione, ma questa non avviene come previsto.
Questo è il codice che sono riuscito a convertire per il momento:
Qualcuno potrebbe aiutarmi? Vi ringrazio
Questo è il codice che sono riuscito a convertire per il momento:
Codice:
Imports Telegram.Bot
Imports Telegram.Bot.Exceptions
Imports Telegram.Bot.Extensions.Polling
Imports Telegram.Bot.Types
Imports Telegram.Bot.Types.Enums
Private Async Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim botClient As New TelegramBotClient("My token ID")
Dim bot = Await botClient.GetMeAsync()
MessageBox.Show($"Hello, World! I am user {bot.Id} and my name is {bot.FirstName}.")
Message sentMessage = Await botClient.SendTextMessageAsync()
End Sub
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()
MsgBox($"Start listening for @{me.Username}")
' 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 String = update.Message.Chat.Id
Dim messageText As String = update.Message.Text
MsgBox($"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()
}
MsgBox(ErrorMessage)
Return Task.CompletedTask
End Function
Qualcuno potrebbe aiutarmi? Vi ringrazio