mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
tlgrm_v2 alfa ver
This commit is contained in:
311
src/modules/exec/Telegram_v2/Telegram_v2.cpp
Normal file
311
src/modules/exec/Telegram_v2/Telegram_v2.cpp
Normal file
@@ -0,0 +1,311 @@
|
||||
#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>
|
||||
#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;
|
||||
|
||||
class Telegram_v2 : public IoTItem
|
||||
{
|
||||
private:
|
||||
bool _receiveMsg;
|
||||
String _prevMsg = "";
|
||||
|
||||
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()
|
||||
{
|
||||
if (_receiveMsg)
|
||||
{
|
||||
_myBot->tick();
|
||||
}
|
||||
// Далее вызов doByInterval для обработки комманд
|
||||
if (enableDoByInt)
|
||||
{
|
||||
currentMillis = millis();
|
||||
difference = currentMillis - prevMillis;
|
||||
if (difference >= _interval)
|
||||
{
|
||||
prevMillis = millis();
|
||||
this->doByInterval();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void doByInterval()
|
||||
{
|
||||
}
|
||||
|
||||
IoTValue execute(String command, std::vector<IoTValue> ¶m)
|
||||
{
|
||||
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)
|
||||
{
|
||||
SerialPrint("X", F("Telegram"), "Fail send file: " + param[0].valS);
|
||||
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)
|
||||
{
|
||||
SerialPrint("X", F("Telegram"), "Fail edit file: " + param[0].valS);
|
||||
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)
|
||||
{
|
||||
SerialPrint("X", F("Telegram"), "Fail send file: " + selectToMarker(msg.text, "_"));
|
||||
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)
|
||||
{
|
||||
String path = '/' + msg.fileName; // вида /filename.xxx
|
||||
auto file = FileFS.open(path, FILE_WRITE); // открываем для записи
|
||||
if(!_myBot->downloadFile(file, msg.fileUrl))
|
||||
{
|
||||
SerialPrint("X", F("Telegram"), "download from: error write" );
|
||||
return;
|
||||
}
|
||||
SerialPrint("<-", F("Telegram"), "download from: " + _chatID + ", file: " + String(msg.fileUrl));
|
||||
_myBot->sendMessage("download from: " + _chatID + ", file: " + String(msg.fileName), _chatID);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
/*
|
||||
String static returnListOfParams()
|
||||
{
|
||||
String out;
|
||||
for (std::list<IoTItem *>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it)
|
||||
{
|
||||
if ((*it)->iAmLocal)
|
||||
out = out + (*it)->getID() + ": " + (*it)->getValue() + "\n";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
*/
|
||||
void sendTelegramMsg(bool often, String msg)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
~Telegram_v2(){
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
void *getAPI_Telegram_v2(String subtype, String param)
|
||||
{
|
||||
if (subtype == F("Telegram_v2"))
|
||||
{
|
||||
return new Telegram_v2(param);
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
91
src/modules/exec/Telegram_v2/modinfo.json
Normal file
91
src/modules/exec/Telegram_v2/modinfo.json
Normal file
@@ -0,0 +1,91 @@
|
||||
{
|
||||
"menuSection": "executive_devices",
|
||||
|
||||
"configItem": [{
|
||||
"global": 0,
|
||||
"name": "Телеграм-Бот v2",
|
||||
"type": "Writing",
|
||||
"subtype": "Telegram_v2",
|
||||
"id": "tg",
|
||||
"widget": "",
|
||||
"page": "",
|
||||
"descr": "",
|
||||
"int": 10,
|
||||
|
||||
"token": "",
|
||||
"autos": 1,
|
||||
"receiveMsg": 0,
|
||||
"chatID": ""
|
||||
}],
|
||||
|
||||
"about": {
|
||||
"authorName": "Mikhail Bubnov",
|
||||
"authorContact": "https://t.me/Mit4bmw",
|
||||
"authorGit": "https://github.com/Mit4el",
|
||||
"specialThanks": "",
|
||||
"moduleName": "Telegram_v2",
|
||||
"moduleVersion": "1.0",
|
||||
"usedRam": {
|
||||
"esp32_4mb": 37,
|
||||
"esp8266_4mb": 37
|
||||
},
|
||||
"title": "Телеграм-Бот v2",
|
||||
"moduleDesc": "Добавляет возможность отправлять сообщения от имени бота контакту в Телеграм-чате и получать команды.",
|
||||
"propInfo": {
|
||||
"token": "Токен для авторизации бота в системе Telegram",
|
||||
"autos": "Автоматически(1) или нет(0) запоминать ChatID по входящим сообщениям. Т.е. бот будет информировать тех, кто последний прислал сообщение.",
|
||||
"receiveMsg": "Обрабатывать(1) или нет(0) входящие сообщения.",
|
||||
"chatID": "ИД диалога с контактом. Необходим для отправки сообщений именно вам."
|
||||
},
|
||||
"funcInfo": [
|
||||
{
|
||||
"name": "sendMsg",
|
||||
"descr": "Отправить сообщение без повторений.",
|
||||
"params": ["Сообщение, может быть строкой, числом или ИД другого элемента для получения значения"]
|
||||
},
|
||||
{
|
||||
"name": "sendOftenMsg",
|
||||
"descr": "Отправить сообщение в любом случае, даж если отправляли такое ранее.",
|
||||
"params": ["Сообщение, может быть строкой, числом или ИД другого элемента для получения значения"]
|
||||
},
|
||||
{
|
||||
"name": "sendPinMsg",
|
||||
"descr": "Отправить закрепленное сообщение в любом случае, даж если отправляли такое ранее.",
|
||||
"params": ["Сообщение, может быть строкой, числом или ИД другого элемента для получения значения"]
|
||||
},
|
||||
{
|
||||
"name": "editMsg",
|
||||
"descr": "Отредактировать последнее отправленное ботом сообщение.",
|
||||
"params": ["Сообщение, может быть строкой, числом или ИД другого элемента для получения значения"]
|
||||
},
|
||||
{
|
||||
"name": "sendFile",
|
||||
"descr": "Отправить файл в телеграмм, с указанием типа файла: 0-фото, 1-аудио, 2-документ, 3-видео, 4-анимация, 5-голос",
|
||||
"params": ["Путь к файлу (/test.png)", "Тип файла/информации (число)"]
|
||||
},
|
||||
{
|
||||
"name": "editFile",
|
||||
"descr": "Отредактировать последний отправленный файл, с указанием типа файла: 0-фото, 1-аудио, 2-документ, 3-видео, 4-анимация, 5-голос",
|
||||
"params": ["Путь к файлу (/test.png)", "Тип файла/информации (число)"]
|
||||
}
|
||||
|
||||
]
|
||||
},
|
||||
|
||||
"defActive": false,
|
||||
|
||||
"usedLibs": {
|
||||
"esp32_4mb": [
|
||||
"gyverlibs/FastBot"
|
||||
],
|
||||
"esp32s2_4mb": [
|
||||
"gyverlibs/FastBot"
|
||||
],
|
||||
"esp32_16mb": [
|
||||
"gyverlibs/FastBot"
|
||||
],
|
||||
"esp8266_4mb": [
|
||||
"gyverlibs/FastBot"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user