shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
150 stars 34 forks source link

Added firstName and lastName to getNewMessage() #11

Closed ivan140 closed 5 years ago

ivan140 commented 5 years ago

Hello!

I added some missing assignments to the fields of the TBUser object.

It seems the field _firstname is required but _lastname and username are optional. (https://core.telegram.org/bots/api#user) Due to this the following reply by the bot works: bot.sendMessage(m.sender.id, (String)m.sender.id + "\n" + m.sender.firstName); but the following doesn't: bot.sendMessage(m.sender.id, (String)m.sender.id + "\n" + m.sender.lastName);

Not sure how to approach this minor issue.

shurillu commented 5 years ago

Hello Ivan,

I have merged your commit, thank you: soon I'll made another release with it.

Due to this the following reply by the bot works: bot.sendMessage(m.sender.id, (String)m.sender.id + "\n" + m.sender.firstName); but the following doesn't: bot.sendMessage(m.sender.id, (String)m.sender.id + "\n" + m.sender.lastName);

This weird behavior is due to the String class: you can chain/concatenate with the + (plus) operator only String objects. So, you have to convert (typecast) to String the \n because in this form is an array of char (double quotation means "array of chars" even when there is only a character). In other words if you try:

bot.sendMessage(m.sender.id, (String)m.sender.id + (String)"\n" + m.sender.firstName); bot.sendMessage(m.sender.id, (String)m.sender.id + (String)"\n" + m.sender.lastName);

it should work. Let me know!

Regards, Stefano

ivan140 commented 5 years ago

Hi! I just tested it: the concatenation without the cast works just fine. There must be an other problem. Here's the code snippet I tested with:

if (m.text.indexOf("/id") > -1) {
          bot.sendMessage(m.sender.id, "firstName:");
          bot.sendMessage(m.sender.id, (String)m.sender.id + "\n" + m.sender.firstName);
          bot.sendMessage(m.sender.id, "lastName:");
          bot.sendMessage(m.sender.id, (String)m.sender.id + (String)"\n" + m.sender.lastName);
}

And here's the response: grafik

So it seems, that sender.lastName is not initialized and produces some unclear behavior.

shurillu commented 5 years ago

Hello Ivan, i found the issue: it involves with the json string handling. To obtain a string, I do (for example - the sender username):

message.sender.username = root["result"][0]["message"]["from"]["username"].asString();

Now the asString() method work just fine when there is something to return (a valid string) but it do not work so well when there is no string (an empty field, like the last name field for example). Changing the asString() method - deprecated, check https://github.com/bblanchon/ArduinoJson/issues/552 - with as<String>() all work flawlessy.

I've already committed the changes in the master branch: could you try it? Let me know, regards

Stefano

ivan140 commented 5 years ago

Hello, Just checked out master. For the following code:

if (m.text.indexOf("/id") > -1) {
          bot.sendMessage(m.sender.id, "firstName:");
          bot.sendMessage(m.sender.id, (String)m.sender.id + "\n" + m.sender.firstName);
          bot.sendMessage(m.sender.id, "lastName:");
          bot.sendMessage(m.sender.id, (String)m.sender.id + (String)"\n" + m.sender.lastName);
          bot.sendMessage(m.sender.id, "username:");
          bot.sendMessage(m.sender.id, (String)m.sender.id + (String)"\n" + m.sender.username);
 }

I now get: grafik

So something I'd expect. Thank you for the fix!

Best regards Ivan