193 lines
6.7 KiB
C++
193 lines
6.7 KiB
C++
#include <Telegram.h>
|
|
#include "secrets.h"
|
|
#include <ArduinoJson.h>
|
|
|
|
const char *Telegram::BOT_TOKEN = TELEGRAM_BOT_TOKEN;
|
|
const unsigned long Telegram::BOT_MTBS = 1000; // mean time between scan messages
|
|
|
|
void Telegram::handleNewMessages(int numNewMessages)
|
|
{
|
|
Serial.println("handleNewMessages");
|
|
Serial.println(String(numNewMessages));
|
|
|
|
for (int i = 0; i < numNewMessages; i++)
|
|
{
|
|
String chat_id = bot.messages[i].chat_id;
|
|
String text = bot.messages[i].text;
|
|
|
|
#ifdef DEBUGGER_TELEGRAM_ID
|
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe von ") + bot.messages[i].from_name + "(" + bot.messages[i].from_id + ")" +
|
|
"im Chat " + bot.messages[i].chat_title + "(" + bot.messages[i].chat_id + ")" +
|
|
"folgenden Text empfangen:\n\n" + text);
|
|
#endif
|
|
|
|
String from_name = bot.messages[i].from_name;
|
|
if (from_name == "")
|
|
from_name = "Guest";
|
|
|
|
if (text.startsWith("/send_test_action"))
|
|
{
|
|
bot.sendChatAction(chat_id, "typing");
|
|
delay(4000);
|
|
bot.sendMessage(chat_id, "Did you see the action message?");
|
|
|
|
// You can't use own message, just choose from one of bellow
|
|
|
|
// typing for text messages
|
|
// upload_photo for photos
|
|
// record_video or upload_video for videos
|
|
// record_audio or upload_audio for audio files
|
|
// upload_document for general files
|
|
// find_location for location data
|
|
|
|
// more info here - https://core.telegram.org/bots/api#sendchataction
|
|
}
|
|
|
|
for (uint32_t i = 0; i < cmdListCount; i++)
|
|
{
|
|
if (text.startsWith(cmdList[i].command))
|
|
{
|
|
String Arguments = "";
|
|
auto posOfFirstSpace = text.indexOf(' ');
|
|
if ((posOfFirstSpace > 0) && (text.length() > posOfFirstSpace + 1))
|
|
{
|
|
// Arguments are on space behind the command string
|
|
Arguments = text.substring(posOfFirstSpace + 1);
|
|
}
|
|
|
|
if (cmdList[i].functionPointer != nullptr)
|
|
{
|
|
String DisplayText = cmdList[i].functionPointer(Arguments);
|
|
if (DisplayText.isEmpty())
|
|
{
|
|
bot.sendMessage(chat_id, "Display text ist leer!!");
|
|
#ifdef DEBUGGER_TELEGRAM_ID
|
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe an ") + chat_id + " folgenden Text geschrieben:\n\n" + "Display text ist leer!!");
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
String Answer = "Display zeigt \"";
|
|
Answer += DisplayText;
|
|
Answer += "\".";
|
|
bot.sendMessage(chat_id, Answer);
|
|
#ifdef DEBUGGER_TELEGRAM_ID
|
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe an ") + chat_id + " folgenden Text geschrieben:\n\n" + Answer);
|
|
#endif
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (text.startsWith("/start") || text.startsWith("/help"))
|
|
{
|
|
String welcome = "Willkommen beim Tür-Display-Bot, " + from_name + ".\n\n";
|
|
welcome += "Hier kommen die möglichen Kommandos, die ich verstehe:\n";
|
|
|
|
for (uint32_t i = 0; i < cmdListCount; i++)
|
|
{
|
|
welcome += cmdList[i].command + "\n";
|
|
}
|
|
|
|
bot.sendMessage(chat_id, welcome);
|
|
#ifdef DEBUGGER_TELEGRAM_ID
|
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe an ") + chat_id + " folgenden Text geschrieben:\n\n" + welcome);
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
|
|
Telegram::Telegram(WiFiClientSecure &sec_client, TelegramCommand commandList[], uint32_t commandListCount)
|
|
: secured_client(sec_client), bot(BOT_TOKEN, sec_client), cmdList(commandList), cmdListCount(commandListCount)
|
|
{ }
|
|
|
|
Telegram::~Telegram() {}
|
|
|
|
void Telegram::init()
|
|
{
|
|
secured_client.setInsecure();
|
|
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
|
|
secured_client.setHandshakeTimeout(5);
|
|
// secured_client.setTimeout(5);
|
|
|
|
Serial.println("clearing old messages");
|
|
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
|
|
bot.maxMessageLength = 2500;
|
|
while (numNewMessages)
|
|
{
|
|
for (int i = 0; i < numNewMessages; i++)
|
|
{
|
|
String chat_id = bot.messages[i].chat_id;
|
|
String text = bot.messages[i].text;
|
|
String from_name = bot.messages[i].from_name;
|
|
|
|
String txt = String("ignored '") + text + String("' from ") + from_name +String("(") + chat_id + String(")");
|
|
|
|
Serial.println(txt);
|
|
}
|
|
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
|
|
}
|
|
|
|
JsonDocument doc;
|
|
JsonArray commandsJsonArray = doc.to<JsonArray>();
|
|
String jsonString;
|
|
|
|
// add all the commands to the list
|
|
for (uint32_t i = 0; i < cmdListCount; i++)
|
|
{
|
|
auto cmdObj = commandsJsonArray.add<JsonObject>();
|
|
|
|
// remove the prepending slash as it is not needed here
|
|
String tmp = cmdList[i].command;
|
|
tmp.remove(0,1);
|
|
cmdObj["command"] = tmp;
|
|
cmdObj["description"] = cmdList[i].description;
|
|
}
|
|
|
|
// add generic commands to the list
|
|
auto helpObj = commandsJsonArray.add<JsonObject>();
|
|
helpObj["command"] = "help";
|
|
helpObj["description"] = "Zeigt einen Hilfstext an";
|
|
// auto startObj = commandsJsonArray.add<JsonObject>();
|
|
// startObj["command"] = "start";
|
|
// startObj["description"] = "Startet die Konversation";
|
|
|
|
serializeJson(doc, jsonString);
|
|
bot.setMyCommands(jsonString);
|
|
|
|
bot.sendMessage(TELEGRAM_BOT_OWNER, "Hi, ich bin online und brauche einen neuen Zustand.");
|
|
#ifdef DEBUGGER_TELEGRAM_ID
|
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, "Hi DEBUGGER, ich bin online und brauche einen neuen Zustand.");
|
|
#endif
|
|
bot_lasttime = millis();
|
|
}
|
|
|
|
void Telegram::cyclic(unsigned long now)
|
|
{
|
|
if (now - bot_lasttime > BOT_MTBS)
|
|
{
|
|
Serial.print(bot.last_message_received);
|
|
Serial.println(" checking");
|
|
|
|
auto stat = WiFi.status();
|
|
Serial.println("WifiStat= ");
|
|
Serial.println(stat);
|
|
|
|
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
|
|
|
|
while (numNewMessages)
|
|
{
|
|
handleNewMessages(numNewMessages);
|
|
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
|
|
}
|
|
|
|
bot_lasttime = now;
|
|
}
|
|
}
|
|
|
|
int Telegram::sendMessage(const String& chat_id, const String& text)
|
|
{
|
|
return bot.sendMessage(chat_id, text);
|
|
}
|