B4J Question jTelegramBot Error.

iz0ndg

Active Member
Licensed User
B4J 8.10
JDK 11.0.1
jTelegramBot 0.31

I have an error running example bot :
In /start command :
B4X:
MessageEntities: [MessageEntity{type=bot_command, offset=0, length=6, url='null', user=null}]
MessageText: /start
main._appstart (java line: 57)
java.lang.RuntimeException: NegativeResponseException{httpResponseCode=400, errorCode=400, description='Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 12'} io.fouad.jtb.core.exceptions.NegativeResponseException: Error Code = 400 | Description = Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 12
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:120)
    at anywheresoftware.b4a.BA$3.run(BA.java:247)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:153)
    at b4j.example.main._appstart(main.java:57)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at b4j.example.main.main(main.java:28)
Caused by: NegativeResponseException{httpResponseCode=400, errorCode=400, description='Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 12'} io.fouad.jtb.core.exceptions.NegativeResponseException: Error Code = 400 | Description = Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 12
    at io.fouad.jtb.core.utils.HttpClient.readResponse(HttpClient.java:360)
    at io.fouad.jtb.core.utils.HttpClient.sendHttpPost(HttpClient.java:318)
    at io.fouad.jtb.core.JTelegramBot.sendMessage(JTelegramBot.java:328)
    at de.donmanfred.JTBwrapper.sendMessage(JTBwrapper.java:295)
    at b4j.example.main._jtb_onmessagereceived(main.java:293)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    ... 12 more

It seems to be due to the 'parsemode' in the 'sendmessage' command, if it is 'MARKDOWN' it goes into error, if it is 'HTML' no

This goes wrong:
B4X:
    else If message.Text = "/start" Then
        jtb.sendMessage(jtb.byId(from.Id),$" Hello ${chat.Username}
`Command   | Result
----------|-------------------------
/location | Returns a Locationobject
/document | Returns a Document
/photo    | Returns a Photo
/audio    | Returns a Audiofile
/url      | Returns a URL with Preview
/plainurl | Returns a URL without Preview
/buttons  | Shows some buttons To Click on
`
Thank you For choosing Me :-)"$,"MARKDOWN",False,False,message.MessageId,Null)

This no error :
B4X:
    else If message.Text = "/start" Then
        jtb.sendMessage(jtb.byId(from.Id),$" Hello ${chat.Username}
`Command   | Result
----------|-------------------------
/location | Returns a Locationobject
/document | Returns a Document
/photo    | Returns a Photo
/audio    | Returns a Audiofile
/url      | Returns a URL with Preview
/plainurl | Returns a URL without Preview
/buttons  | Shows some buttons To Click on
`
Thank you For choosing Me :-)"$,"HTML",False,False,message.MessageId,Null) ' <---- Change in HTML

The same in all send messages.

Thanks for help
 

DonManfred

Expert
Licensed User
how are you sending the /start to your bot?
 
Upvote 0

iz0ndg

Active Member
Licensed User
On the telegram app, new message, I search for the bot, click on it, the word "join" (in italian "Unisciti") appears at the bottom, click and you can write the message .... / start .... and the bot go to crash.
 
Upvote 0

DonManfred

Expert
Licensed User
i fear this is the wrong way.

The jTelegramBot does not provide any bot-commands.

Start a new conversation/chat with the bot should work.

I generated a my telegram link for my bot, opened it and my browser just opens a chat with my bot (in Telegram App) where i can send /start
 
Upvote 0

iz0ndg

Active Member
Licensed User
i just searched for my bot in telegram app, clicked on the botname and started a Chat with him....

Telegram automatically sends a /start in the chat and my Bot responds with a markdown.

I guess you are doing something wrong

Hi Don, the problem is not the bot, it works!
After some tests it seems that the problem is the printing of "from.Username" in the answer together with "MARKDOWN".
I have tried several times and replacing it with "from.FirstName" it all works.
I don't know why, with HTML it works with MARKDOWN it goes wrong.
Also in the responses for the "/buttons" command the same thing .... :rolleyes:

Anyway thanks for the support, you're great!👍
 
Upvote 0

iz0ndg

Active Member
Licensed User
Seems to be underscore in the Username ....(My username is "Firstname_Lastname")
Seems to be underscore in the field "text" of sendmessage...
if I replace it in the string it works
B4X:
        Dim username As String
        username = from.Username.Replace("_"," ")
        Log("Username :" & username)
        jtb.sendMessage(jtb.byId(from.Id),$"Thank you ${username} for clicking the button ${cbquery.Data} :-)"$,"MARKDOWN",False,False,msg.MessageId,Null)
🤔
 
Last edited:
Upvote 0
Top