Skip to content

Commit f8837fa

Browse files
committed
WIP initial working version of supporting channel post #37
1 parent 3f101b3 commit f8837fa

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -312,24 +312,10 @@ int UniversalTelegramBot::getUpdates(long offset) {
312312
int resultArrayLength = root["result"].size();
313313
if (resultArrayLength > 0) {
314314
int newMessageIndex = 0;
315+
//Step through all results
315316
for (int i=0; i < resultArrayLength; i++) {
316-
JsonObject& message = root["result"][i]["message"];
317-
int update_id = root["result"][i]["update_id"];
318-
if (last_message_received != update_id) {
319-
last_message_received = update_id;
320-
String text = message["text"];
321-
String date = message["date"];
322-
String chat_id = message["chat"]["id"];
323-
String from_id = message["from"]["id"];
324-
String from_name = message["from"]["first_name"];
325-
326-
messages[newMessageIndex].update_id = update_id;
327-
messages[newMessageIndex].text = text;
328-
messages[newMessageIndex].date = date;
329-
messages[newMessageIndex].chat_id = chat_id;
330-
messages[newMessageIndex].from_id = from_id;
331-
messages[newMessageIndex].from_name = from_name;
332-
317+
JsonObject& result = root["result"][i];
318+
if (processResult(result, newMessageIndex)) {
333319
newMessageIndex++;
334320
}
335321
}
@@ -349,6 +335,56 @@ int UniversalTelegramBot::getUpdates(long offset) {
349335
}
350336
}
351337

338+
bool UniversalTelegramBot::processResult(JsonObject& result, int messageIndex) {
339+
int update_id = result["update_id"];
340+
// Check have we already dealt with this message (this shouldn't happen!)
341+
if (last_message_received != update_id) {
342+
last_message_received = update_id;
343+
344+
String text;
345+
String date;
346+
String chat_id;
347+
String chat_title;
348+
String from_id;
349+
String from_name;
350+
String type;
351+
352+
353+
if (result.containsKey("message")) {
354+
JsonObject& message = result["message"];
355+
type = "message";
356+
from_id = message["from"]["id"].as<String>();
357+
from_name = message["from"]["first_name"].as<String>();
358+
359+
text = message["text"].as<String>();
360+
date = message["date"].as<String>();
361+
chat_id = message["chat"]["id"].as<String>();
362+
chat_title = message["chat"]["title"].as<String>();
363+
}
364+
else if (result.containsKey("channel_post")) {
365+
JsonObject& message = result["channel_post"];
366+
type = "channel_post";
367+
368+
text = message["text"].as<String>();
369+
date = message["date"].as<String>();
370+
chat_id = message["chat"]["id"].as<String>();
371+
chat_title = message["chat"]["title"].as<String>();
372+
}
373+
374+
messages[messageIndex].update_id = update_id;
375+
messages[messageIndex].text = text;
376+
messages[messageIndex].date = date;
377+
messages[messageIndex].chat_id = chat_id;
378+
messages[messageIndex].chat_title = chat_title;
379+
messages[messageIndex].from_id = from_id;
380+
messages[messageIndex].from_name = from_name;
381+
messages[messageIndex].type = type;
382+
383+
return true;
384+
}
385+
return false;
386+
}
387+
352388
/***********************************************************************
353389
* SendMessage - function to send message to telegram *
354390
* (Arguments to pass: chat_id, text to transmit and markup(optional)) *

src/UniversalTelegramBot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ typedef byte (*GetNextByte)();
3737
struct telegramMessage{
3838
String text;
3939
String chat_id;
40+
String chat_title;
4041
String from_id;
4142
String from_name;
4243
String date;
44+
String type;
4345
int update_id;
4446
};
4547

@@ -86,6 +88,7 @@ class UniversalTelegramBot
8688
//JsonObject * parseUpdates(String response);
8789
String _token;
8890
Client *client;
91+
bool processResult(JsonObject& result, int messageIndex);
8992
const int maxMessageLength = 1300;
9093
bool _debug = false;
9194
};

0 commit comments

Comments
 (0)