Android Question Unable to send a telegram message to a private channel

toby

Well-Known Member
Licensed User
Longtime User
I've tried unsuccessfully to to send a message to a private Telegram channel with the following code.

I've double-checked the following
1. the bot is an admin with post message permission
2. bot token is correct
3. chat id is correct.


B4X:
Sub SendMsgToPrivateChannel
    Dim token As String = "1234567890:ABCDEF1R0-gtm1oBBi0ABCDEFGHIJkLMopr" 'Replace with your actual bot token
    Dim chatID As String =""-1001234567890" ' Replace with your actual private channel ID
    Dim messageText As String = "Hello world!"

    Dim url As String = $"https://api.telegram.org/bot${token}/sendMessage"$

    Dim params As Map
    params.Initialize
    params.Put("chat_id", chatID)
    params.Put("text", messageText)

    Dim j As HttpJob
    j.Initialize("SendTelegram", Me)
    j.PostMultipart(url, params, Null)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("✅ Test message sent successfully.")
        ToastMessageShow("Test message sent successfully!", True )
    Else
        Log("❌ Error sending message: " & j.ErrorMessage)
        ToastMessageShow("Failed to send message: " & j.ErrorMessage, True)
    End If
    j.Release
End Sub

When execution reaches line If j.Success Then
The error log shows:
ResponseError. Reason: , Response:

Could someone kindly help me to make it work, please?

TIA
 

Jones Hone

Active Member
Licensed User
Longtime User
I have used b4j to send it, the code is as follows. For your reference.
I used POST to send, but it didn't work, so I used GET instead.
b4x sample:
        Dim Url As String
        Dim http As OkHttpClient
        http.InitializeAcceptAll("Http")
        Dim aReq As OkHttpRequest
        Dim ChatId As String = "1234567890"
        Dim su As StringUtils
        Dim msg As String = "Hello This Is Test"
        Url = "https://api.telegram.org"
        Url = Url & "/bot....token......../sendMessage"
        Url = Url & "?chat_id=" & ChatId & "&parse_mode=HTML&text=" & "<blockquote>" & msg & "</blockquote>"
        aReq.InitializeGet(Url)
        http.Execute(aReq,1000)
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
My solution is to use PostString instead of PostMultPart as follows
B4X:
public Sub SendPrivateChannelMessage(botToken As String, channelId As String, msg As String)
    Dim url As String = $"https://api.telegram.org/bot${botToken}/sendMessage"$

    Dim postData As String = $"chat_id=${channelId}&text=${EscapeMessage(msg)}"$

    Dim j As HttpJob
    j.Initialize("SendTelegram", Me)
    j.PostString(url, postData)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
   
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log($"✅ message sent successfully. ${j.GetString} "$)
        ToastMessageShow("Message sent successfully!", True )
    Else
        Log("❌ Error sending message: " & j.ErrorMessage)
        ToastMessageShow("Failed to send message: " & j.ErrorMessage, True)
    End If
    j.Release
End Sub

private Sub EscapeMessage(msg As String) As String
    Return msg.Replace(" ", "%20").Replace("!", "%21").Replace("#", "%23").Replace("&", "%26")
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…