FlipDot_Telegram/src/Telegram.cpp
2026-01-21 10:29:23 +01:00

167 lines
5.4 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;
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Guest";
if (text == "/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 = "";
if (text.length() > cmdList[i].command.length() + 1)
{
// Arguments are on space behind the command string
Arguments = text.substring(cmdList[i].command.length() + 1);
}
if (cmdList[i].functionPointer != nullptr)
{
String DisplayText = cmdList[i].functionPointer(Arguments);
if (DisplayText.isEmpty())
{
bot.sendMessage(chat_id, "Display text is empty!!");
}
else
{
String Answer = "Display text is \"";
Answer += DisplayText;
Answer += "\".";
bot.sendMessage(chat_id, Answer);
}
break;
}
}
}
if ((text == "/display_start") || (text == "/start") || (text == "/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);
}
}
}
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);
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 I'm online.");
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;
}
}