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:
- Create a new Telegram bot by contacting the BotFather on Telegram. The BotFather will provide you with an API token for your bot.
- 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.
- 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.