Files
IoTManager/src/modules/exec/Telegram_v2/Telegram_v2.cpp

362 lines
12 KiB
C++
Raw Normal View History

2023-09-27 23:00:44 +03:00
#include "Global.h"
#include "classes/IoTItem.h"
// #define FB_NO_UNICODE
// #define FB_NO_URLENCODE
// #define FB_NO_OTA
// #define FB_DYNAMIC
// #include <GyverGFX.h>
// #include <CharPlot.h>
2023-10-02 21:29:02 +03:00
// #include "esp_camera.h"
2023-09-27 23:00:44 +03:00
#include <FastBot.h>
#include <vector>
// FastBot _myBot;
FastBot *_myBot = nullptr;
FastBot *instanceBot()
{
if (!_myBot)
{
_myBot = new FastBot();
// ot->begin();
}
return _myBot;
}
String _token;
String _chatID;
bool _autos;
2023-10-02 21:29:02 +03:00
bool _initSD;
2023-09-27 23:00:44 +03:00
class Telegram_v2 : public IoTItem
{
private:
bool _receiveMsg;
String _prevMsg = "";
2023-10-02 21:29:02 +03:00
bool _useLed = false;
2023-09-27 23:00:44 +03:00
public:
Telegram_v2(String parameters) : IoTItem(parameters)
{
jsonRead(parameters, "token", _token);
jsonRead(parameters, "autos", _autos);
jsonRead(parameters, "receiveMsg", _receiveMsg);
jsonRead(parameters, "chatID", _chatID);
instanceBot();
_myBot->attach(telegramMsgParse);
#ifdef ESP32
// _myBot->useDNS(true);
#endif
_myBot->setToken(_token);
// _myBot->enableUTF8Encoding(true);
_myBot->setChatID(_chatID);
// _myBot->showMenuText("help","help",false);
}
void loop()
{
2023-10-07 09:40:45 +03:00
if (_receiveMsg && isNetworkActive())
2023-09-27 23:00:44 +03:00
{
_myBot->tick();
}
// Далее вызов doByInterval для обработки комманд
2023-10-07 09:40:45 +03:00
IoTItem::loop();
2023-09-27 23:00:44 +03:00
}
void doByInterval()
{
}
IoTValue execute(String command, std::vector<IoTValue> &param)
{
2023-10-07 09:40:45 +03:00
if (!isNetworkActive()) return {};
2023-09-27 23:00:44 +03:00
if (command == "sendMsg")
{
if (param.size())
{
String strTmp;
if (param[0].isDecimal)
strTmp = param[0].valD;
else
strTmp = param[0].valS;
sendTelegramMsg(false, strTmp);
}
}
else if (command == "sendOftenMsg")
{
if (param.size())
{
String strTmp;
if (param[0].isDecimal)
strTmp = param[0].valD;
else
strTmp = param[0].valS;
sendTelegramMsg(true, strTmp);
}
}
else if (command == "sendPinMsg")
{
if (param.size())
{
String strTmp;
if (param[0].isDecimal)
strTmp = param[0].valD;
else
strTmp = param[0].valS;
_myBot->sendMessage(strTmp, _chatID);
_myBot->pinMessage(_myBot->lastBotMsg());
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ",pin msg: " + strTmp);
}
}
else if (command == "editMsg")
{
if (param.size())
{
String strTmp;
if (param[0].isDecimal)
strTmp = param[0].valD;
else
strTmp = param[0].valS;
_myBot->editMessage(_myBot->lastBotMsg(), strTmp);
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ",edit msg: " + strTmp);
}
}
else if (command == "sendFile")
{
if (param.size() && !param[0].isDecimal)
{
// String path = filepath(filename);
auto file = FileFS.open(param[0].valS, FILE_READ);
if (!file)
{
2023-10-03 22:47:11 +03:00
SerialPrint("E", F("Telegram"), "Fail send file: " + param[0].valS);
2023-09-27 23:00:44 +03:00
return {};
}
// File file = LittleFS.open(param[0].valS, "r"); // /test.png
// selectToMarkerLast(msg.text, "_")
uint8_t res = _myBot->sendFile(file, (FB_FileType)param[1].valD, selectToMarkerLast(param[0].valS, "/"), _chatID);
file.close();
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", sendFile: " + param[0].valS + " res: " + String(res));
}
}
else if (command == "editFile")
{
if (param.size() && !param[0].isDecimal)
{
// String path = filepath(filename);
auto file = FileFS.open(param[0].valS, FILE_READ);
if (!file)
{
2023-10-03 22:47:11 +03:00
SerialPrint("E", F("Telegram"), "Fail edit file: " + param[0].valS);
2023-09-27 23:00:44 +03:00
return {};
}
// File file = LittleFS.open(param[0].valS, "r"); // /test.png
// selectToMarkerLast(msg.text, "_")
uint8_t res = _myBot->editFile(file, (FB_FileType)param[1].valD, selectToMarkerLast(param[0].valS, "/"), _myBot->lastBotMsg(), _chatID);
file.close();
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", editFile: " + param[0].valS + " res: " + String(res));
}
}
return {};
}
void static telegramMsgParse(FB_msg &msg)
{
// FB_msg msg;
SerialPrint("->", F("Telegram"), "chat ID: " + msg.chatID + ", msg: " + msg.text);
// _myBot->setChatID(_chatID);
if (_autos)
{
_chatID = msg.chatID;
}
if (msg.text.indexOf("set") != -1)
{
msg.text = deleteBeforeDelimiter(msg.text, "_");
generateOrder(selectToMarker(msg.text, "_"), selectToMarkerLast(msg.text, "_"));
_myBot->replyMessage("order done", msg.messageID, _chatID);
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", msg: " + String(msg.text));
}
else if (msg.text.indexOf("get") != -1)
{
msg.text = deleteBeforeDelimiter(msg.text, "_");
IoTItem *item = findIoTItem(msg.text);
if (item)
{
_myBot->replyMessage(item->getValue(), msg.messageID, _chatID);
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", msg: " + String(msg.text));
}
}
else if (msg.text.indexOf("all") != -1)
{
// String list = returnListOfParams();
String out;
std::vector<float> vctr;
for (std::list<IoTItem *>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it)
{
if ((*it)->iAmLocal)
{
if (it == IoTItems.begin())
{
out = "get_" + (*it)->getID();
}
else
{
out = out + " \n " + "get_" + (*it)->getID();
}
vctr.push_back(atoff((*it)->getValue().c_str()));
// _myBot->sendMessage((*it)->getID() + ": " + (*it)->getValue(), _chatID);
}
}
_myBot->showMenuText("select Id", out, true);
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + "\n" + out);
// _myBot->sendMessage(CharPlot<LINE_X2>(&vctr[0], vctr.size(), 5), _chatID);
// SerialPrint("<-", F("Telegram"), CharPlot<LINE_X2>(&vctr[0], vctr.size(), 10));
}
else if (msg.text.indexOf("file") != -1 && msg.chatID == _chatID)
{
msg.text = deleteBeforeDelimiter(msg.text, "_");
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", get file: " + String(msg.text));
auto file = FileFS.open(selectToMarker(msg.text, "_"), FILE_READ); // /test.png
if (!file)
{
2023-10-03 22:47:11 +03:00
SerialPrint("E", F("Telegram"), "Fail send file: " + selectToMarker(msg.text, "_"));
2023-09-27 23:00:44 +03:00
return;
}
int type = atoi(selectToMarkerLast(msg.text, "_").c_str());
_myBot->sendFile(file, (FB_FileType)type, selectToMarker(msg.text, "_"), _chatID);
file.close();
}
else if (msg.isFile)
{
if (msg.text.indexOf("download") != -1 && msg.chatID == _chatID)
{
2023-10-03 22:47:11 +03:00
downloadFile(msg);
}
else if (msg.text.indexOf("nextion") != -1 && msg.chatID == _chatID)
{
2023-11-13 23:55:05 +03:00
for (std::list<IoTItem *>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) {
if ((*it)->getSubtype() == "NextionUpload") {
(*it)->uploadNextionTlgrm(msg.fileUrl);
2023-09-27 23:00:44 +03:00
}
}
2023-11-13 23:55:05 +03:00
}
2023-09-27 23:00:44 +03:00
}
else if (msg.text.indexOf("help") != -1)
{
_myBot->sendMessage("ID: " + chipId, _chatID);
_myBot->sendMessage("chatID: " + _chatID, _chatID);
_myBot->sendMessage(F("Wrong order, use /all to get all values, /get_id to get value, /set_id_value to set value, or /file_name_type or send file msg=download"), _chatID);
}
else
{
// setValue(msg.text);
}
}
2023-10-03 22:47:11 +03:00
2023-09-27 23:00:44 +03:00
void sendTelegramMsg(bool often, String msg)
{
2023-10-07 09:40:45 +03:00
if (!isNetworkActive()) return;
2023-09-27 23:00:44 +03:00
if (often)
{
_myBot->sendMessage(msg, _chatID);
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", msg: " + msg);
}
else
{
if (_prevMsg != msg)
{
_prevMsg = msg;
_myBot->sendMessage(msg, _chatID);
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", msg: " + msg);
}
}
}
2023-10-02 21:29:02 +03:00
void sendFoto(uint8_t *buf, uint32_t length, const String &name)
{
2023-10-07 09:40:45 +03:00
if (!isNetworkActive()) return;
2023-10-02 21:29:02 +03:00
_myBot->sendFile(buf, length, FB_PHOTO, name, _chatID);
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", send foto from esp-cam");
}
void editFoto(uint8_t *buf, uint32_t length, const String &name)
{
2023-10-07 09:40:45 +03:00
if (!isNetworkActive()) return;
2023-10-02 21:29:02 +03:00
_myBot->editFile(buf, length, FB_PHOTO, name, _myBot->lastBotMsg(), _chatID);
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", edit foto from esp-cam");
}
2023-10-03 22:47:11 +03:00
int static downloadFile(FB_msg &msg)
{
int _size = 0;
String path = '/' + msg.fileName; // вида /filename.xxx
auto file = FileFS.open(path, FILE_WRITE); // открываем для записи
// _myBot->sendMessage("Downloading from: " + _chatID + ", file: " + String(msg.fileName), _chatID);
if (file)
{ // файл открылся/создался
HTTPClient http;
2023-09-27 23:00:44 +03:00
2023-10-03 22:47:11 +03:00
#ifdef ESP8266 // esp8266 требует SSl
BearSSL::WiFiClientSecure client;
client.setInsecure();
http.begin(client, msg.fileUrl); // пингуем файл
#else // esp32 сама умеет SSL
http.begin(msg.fileUrl); // пингуем файл
#endif
if (http.GET() == HTTP_CODE_OK)
{ // файл доступен
// загружаем в память. Результат > 0 - успешно
_size = http.writeToStream(&file);
}
http.end(); // закрываем соединение
file.close(); // закрываем файл
if (_size == 0)
{
SerialPrint("E", F("Telegram"), "download error file url: " + msg.fileUrl);
_myBot->sendMessage(F("Download Fail"), _chatID);
}
else
{
SerialPrint("<-", F("Telegram"), "download from: " + _chatID + ", file: " + msg.fileName + " size = " + String(_size) + " byte");
_myBot->sendMessage("Download Ok, size = " + String(_size) + " byte", _chatID);
}
}
else
{
SerialPrint("E", F("Telegram"), F("file write error"));
_myBot->sendMessage(F("file write error"), _chatID);
}
2023-10-07 09:40:45 +03:00
return _size;
2023-10-03 22:47:11 +03:00
}
IoTItem *getTlgrmDriver()
{
2023-10-02 21:29:02 +03:00
return this;
}
2023-10-03 22:47:11 +03:00
~Telegram_v2()
{
2023-10-02 21:29:02 +03:00
tlgrmItem = nullptr;
2023-09-27 23:00:44 +03:00
};
};
void *getAPI_Telegram_v2(String subtype, String param)
{
if (subtype == F("Telegram_v2"))
{
return new Telegram_v2(param);
}
else
{
return nullptr;
}
}