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.

Leave a Reply

Your email address will not be published. Required fields are marked *