You have a Bot Telegram but you do not know how to read messages?

Telegram, what do I need?
1- you must have a bot telegram,
This post show you how to create a bot and send messages
2- Bot token to access the HTTP API
it is available after creating the bot from BotFather bot Telegram
Visual Studio, what do I need?
3- Telegram.Bot Package
open visual studio and Create a new project console,
open Package Manager and type Install-Package Telegram.Bot -Version 13.4.0
the package will be installed

4- Any code:
Copy and Paste this code into Module1.vb file

Imports System.IO
Imports System.Text
Imports System.Timers
Imports IS_tg_sender.IS_tg_sender
Imports Telegram.Bot.Args

    Sub initbot()      
        Dim YourBotTokenHere = "insert your bot token string here"     
        bott = New Telegram.Bot.TelegramBotClient(YourBotTokenHere)

        AddHandler bott.OnUpdate, AddressOf OnUpdate
        AddHandler bott.OnMessage, AddressOf OnMessageReceived
        AddHandler bott.OnMessageEdited, AddressOf OnMessageEdited
        AddHandler bott.OnCallbackQuery, AddressOf OnCallbackQuery
        AddHandler bott.OnInlineQuery, AddressOf BotOnInlineQueryReceived
        AddHandler bott.OnInlineResultChosen, AddressOf BotOnChosenInlineResultReceived
        AddHandler bott.OnReceiveError, AddressOf BotOnReceiveError

        If Not bott.IsReceiving Then
            bott.StartReceiving()
        End If

        Console.WriteLine("Bot Started")
       
    End Sub

you insert on YourBotTokenHere variable your token (you read point two)
we have these Events Handler but,

    Private Sub BotOnReceiveError(sender As Object, e As ReceiveErrorEventArgs)
        Console.WriteLine("BotOnReceiveError")        
    End Sub

    Private Sub BotOnChosenInlineResultReceived(sender As Object, e As ChosenInlineResultEventArgs)
        Console.WriteLine("BotOnChosenInlineResultReceived")
    End Sub

    Private Sub BotOnInlineQueryReceived(sender As Object, e As InlineQueryEventArgs)
        Console.WriteLine("BotOnInlineQueryReceived")
    End Sub

    Private Sub OnCallbackQuery(sender As Object, e As CallbackQueryEventArgs)
        Console.WriteLine("OnCallbackQuery")
    End Sub

    Private Sub OnMessageEdited(sender As Object, e As MessageEventArgs)
        Console.WriteLine(e.Message.Text)
    End Sub

    Private Sub OnUpdate(sender As Object, e As UpdateEventArgs)
         Console.WriteLine("OnUpdate")
    End Sub

I use only OnMessageReceived


    Private Async Sub OnMessageReceived(sender As Object, e As MessageEventArgs)
        Dim te As String = e.Message.From.Id.ToString & " " &
                            e.Message.From.FirstName & " " &
                            e.Message.From.Id & " " &
                            e.Message.From.IsBot & " " &
                            e.Message.From.LastName & " " & e.Message.From.Username


        Dim ID As String = e.Message.From.Id.ToString ' it is your id
        If e.Message.Type.Equals(Telegram.Bot.Types.Enums.MessageType.TextMessage) Then
            Select Case e.Message.Text
                Case "/now" 'response with the date and time
                    Await sendMessage(ID, "it's:  " & Now.ToString)
                Case "/myid" 'response with yor name and ID
                    Await sendMessage(ID, e.Message.From.FirstName & " code:" & e.Message.From.Id.ToString)
            End Select
            log(e.Message.Text & " ==> " & te)
        Else
            Dim t = "Sorry, I don't support this message type!"
            log(e.Message.Text & " ==> " & te)
            Await sendMessage(ID, t)

        End If
    End Sub

and sendmessage function:

    Public Async Function sendMessage(ByVal destID As String, ByVal text As String) As Task

        Try
            Await bott.SendTextMessageAsync(destID, text, Telegram.Bot.Types.Enums.ParseMode.Markdown, False, False, 0, Nothing) 

        Catch e As Exception
        End Try        
    End Function

any log console:

   Private Sub log(txt As String)
        Dim s = Now.ToString & " -- " & txt & vbCrLf
        Console.WriteLine(s)
        Dim folder As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
        folder = If(folder.EndsWith("\"), folder, folder & "\") & "IsNotifier\Log\"
        If Not Directory.Exists(folder) Then
            Directory.CreateDirectory(folder)
        End If
        folder = folder & Now.Month.ToString.PadLeft(2, "0") & Now.Year & ".txt"
        File.AppendAllText(folder, s)
    End Sub

5- How to use.

    Sub Main()
        initbot()
        Console.ReadLine()
    End Sub

6- Compile and run console application.
TGS0
7- Open telegram application and go to your created bot,
Type /now and press enter
Type /myid and press enter
this it is result on Telegram App:
TGS

Advertisements