Posted on Leave a comment

Using VB.net to get Telegram Contact from Group

To get Telegram contacts from a group using VB.NET, you can utilize the Telegram.Bot library, which provides a wrapper around the Telegram Bot API. Here’s an example of how you can achieve this:

1. Install the Telegram.Bot package via NuGet in your VB.NET project.

2. Import the necessary namespaces in your code file:

Imports Telegram.Bot
Imports Telegram.Bot.Args
Imports Telegram.Bot.Types
Imports Telegram.Bot.Types.Enums

3. Set up the Telegram Bot client and authenticate with your bot token:

Dim botToken As String = "YOUR_BOT_TOKEN"
Dim botClient As New TelegramBotClient(botToken)

4. Create an event handler for the OnMessage event to receive messages from the group:

AddHandler botClient.OnMessage, AddressOf OnMessageReceived

5. Implement the event handler to process incoming messages:

Private Sub OnMessageReceived(sender As Object, e As MessageEventArgs)
    Dim message As Message = e.Message

    If message.Type = MessageType.Text Then
        ' Process text messages
        Dim chatId As Long = message.Chat.Id
        Dim senderName As String = message.From.Username
        Dim messageText As String = message.Text

        ' Check if the message is from the group
        If message.Chat.Type = ChatType.Group Then
            ' Access the list of group members
            Dim groupChat As Chat = CType(message.Chat, GroupChat)
            Dim members As User() = groupChat.ChatMembers.ToArray()

            ' Process the members' information
            For Each member As User In members
                Dim memberId As Integer = member.Id
                Dim memberUsername As String = member.Username
                Dim memberFirstName As String = member.FirstName
                Dim memberLastName As String = member.LastName

                ' Do something with the member information
                ' For example, add it to a list or store it in a database
            Next
        End If
    End If
End Sub

6. Start the bot client to begin receiving messages:

botClient.StartReceiving()

Remember to replace "YOUR_BOT_TOKEN" with your actual bot token obtained from the BotFather on Telegram. You also need to adapt the code to store or process the retrieved member information as per your requirements.

Please note that the example assumes you already have a bot created on Telegram and added it to the desired group.

Posted on Leave a comment

Get Telegram ChatID by Telegram Bot using VB.net

To get the Telegram User ID using VB.NET, you can make use of the Telegram Bot API. Follow the steps below to accomplish this:

  1. Create a new Telegram bot by contacting the BotFather on Telegram. The BotFather will provide you with an API token for your bot.
  2. Add the Telegram.Bot NuGet package to your VB.NET project. Right-click on your project in Visual Studio, select “Manage NuGet Packages,” search for “Telegram.Bot,” and install it.
  3. Import the required namespaces in your code file:
 Public Sub GetChatIDJson()
        ' Telegram bot token
        Dim botToken As String = Bot_ApiToken

        ' Make HTTP request to get updates
        Dim apiUrl As String = $"https://api.telegram.org/bot{botToken}/getUpdates"
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(apiUrl), HttpWebRequest)
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

        ' Read response stream
        Using reader As New StreamReader(response.GetResponseStream())
            Dim jsonResponse As String = reader.ReadToEnd()

            'Parse JSON response
            Dim parsedResponse As JObject = JObject.Parse(jsonResponse)
            Dim resultArray As JArray = DirectCast(parsedResponse("result"), JArray)

            'Iterate over updates
            For Each update As JObject In resultArray
                Dim chatId As Long = update("message")("chat")("id").ToObject(Of Long)()
                Console.WriteLine("Chat ID: " & chatId)
                'UserChatID = chatId
            Next

        End Using

        Console.ReadLine()
    End Sub

Remember to replace “YOUR_BOT_TOKEN” with the actual token obtained from the BotFather.