Skip to content

Commit 047f74f

Browse files
authored
Merge pull request witnessmenow#14 from Nzbuu/message_from_name
Return user's first-name with message
2 parents 7c94fbf + 6d55106 commit 047f74f

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

examples/101/FlashledBot/FlashledBot.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ void handleNewMessages(int numNewMessages) {
5454
}
5555
}
5656
if (text == "/start") {
57-
String welcome = "Welcome from FlashLedBot, your personal Bot on Arduino 101\n";
57+
String from_name = message.from_name;
58+
if (from_name == "") from_name = "Anonymous";
59+
60+
String welcome = "Welcome, " + from_name + ", from FlashLedBot, your personal Bot on Arduino 101\n";
5861
welcome = welcome + "/ledon : to switch the Led ON \n";
5962
welcome = welcome + "/ledoff : to switch the Led OFF \n";
6063
welcome = welcome + "/status : Returns current status of LED \n";

examples/ESP8266/FlashledBot/FlashledBot.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ void handleNewMessages(int numNewMessages) {
5454
}
5555
}
5656
if (text == "/start") {
57-
String welcome = "Welcome from FlashLedBot, your personal Bot on ESP8266\n";
57+
String from_name = message.from_name;
58+
if (from_name == "") from_name = "Anonymous";
59+
60+
String welcome = "Welcome, " + from_name + ", from FlashLedBot, your personal Bot on ESP8266\n";
5861
welcome = welcome + "/ledon : to switch the Led ON\n";
5962
welcome = welcome + "/ledoff : to switch the Led OFF\n";
6063
welcome = welcome + "/status : Returns current status of LED\n";

src/UniversalTelegramBot.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,22 @@ int UniversalTelegramBot::getUpdates(long offset) {
171171
if(resultArrayLength > 0) {
172172
int newMessageIndex = 0;
173173
for(int i=0; i < resultArrayLength; i++){
174+
JsonObject& message = root["result"][i]["message"];
174175
int update_id = root["result"][i]["update_id"];
175176
if(last_message_received != update_id) {
176177
last_message_received = update_id;
177-
String text = root["result"][i]["message"]["text"];
178-
String date = root["result"][i]["message"]["date"];
179-
String chat_id = root["result"][i]["message"]["chat"]["id"];
180-
String from_id = root["result"][i]["message"]["from"]["id"];
178+
String text = message["text"];
179+
String date = message["date"];
180+
String chat_id = message["chat"]["id"];
181+
String from_id = message["from"]["id"];
182+
String from_name = message["from"]["first_name"];
181183

182184
messages[newMessageIndex].update_id = update_id;
183185
messages[newMessageIndex].text = text;
184186
messages[newMessageIndex].date = date;
185187
messages[newMessageIndex].chat_id = chat_id;
186188
messages[newMessageIndex].from_id = from_id;
189+
messages[newMessageIndex].from_name = from_name;
187190

188191
newMessageIndex++;
189192
}

src/UniversalTelegramBot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct telegramMessage{
3636
String text;
3737
String chat_id;
3838
String from_id;
39+
String from_name;
3940
String date;
4041
int update_id;
4142
};

0 commit comments

Comments
 (0)