mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-06-10 20:09:19 +03:00
Убираем нумерацию папок модулей
This commit is contained in:
141
src/modules/exec/Telegram/Telegram.cpp
Normal file
141
src/modules/exec/Telegram/Telegram.cpp
Normal file
@@ -0,0 +1,141 @@
|
||||
#include "Global.h"
|
||||
#include "classes/IoTItem.h"
|
||||
|
||||
#include "CTBot.h"
|
||||
|
||||
String uint64ToString(uint64_t input) {
|
||||
String result = "";
|
||||
uint8_t base = 10;
|
||||
|
||||
do {
|
||||
char c = input % base;
|
||||
input /= base;
|
||||
|
||||
if (c < 10)
|
||||
c +='0';
|
||||
else
|
||||
c += 'A' - 10;
|
||||
result = c + result;
|
||||
} while (input);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
class Telegram : public IoTItem {
|
||||
private:
|
||||
CTBot _myBot;
|
||||
String _token;
|
||||
bool _autos;
|
||||
bool _receiveMsg;
|
||||
int64_t _chatID;
|
||||
|
||||
String _prevMsg = "";
|
||||
|
||||
void telegramMsgParse(String msg) {
|
||||
if (msg.indexOf("set") != -1) {
|
||||
msg = deleteBeforeDelimiter(msg, "_");
|
||||
generateOrder(selectToMarker(msg, "_"), selectToMarkerLast(msg, "_"));
|
||||
_myBot.sendMessage(_chatID, "order done");
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + uint64ToString(_chatID) + ", msg: " + String(msg));
|
||||
} else if (msg.indexOf("get") != -1) {
|
||||
msg = deleteBeforeDelimiter(msg, "_");
|
||||
IoTItem* item = findIoTItem(msg);
|
||||
if (item) {
|
||||
_myBot.sendMessage(_chatID, item->getValue());
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + uint64ToString(_chatID) + ", msg: " + String(msg));
|
||||
}
|
||||
} else if (msg.indexOf("all") != -1) {
|
||||
String list = returnListOfParams();
|
||||
_myBot.sendMessage(_chatID, list);
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + uint64ToString(_chatID) + "\n" + list);
|
||||
} else if (msg.indexOf("help") != -1) {
|
||||
_myBot.sendMessage(_chatID, "ID: " + chipId);
|
||||
_myBot.sendMessage(_chatID, "chatID: " + uint64ToString(_chatID));
|
||||
_myBot.sendMessage(_chatID, F("Wrong order, use /all to get all values, /get_id to get value, or /set_id_value to set value"));
|
||||
} else {
|
||||
setValue(msg);
|
||||
}
|
||||
}
|
||||
|
||||
String 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;
|
||||
}
|
||||
|
||||
public:
|
||||
Telegram(String parameters): IoTItem(parameters) {
|
||||
jsonRead(parameters, "token", _token);
|
||||
jsonRead(parameters, "autos", _autos);
|
||||
jsonRead(parameters, "receiveMsg", _receiveMsg);
|
||||
|
||||
String tmp;
|
||||
jsonRead(parameters, "chatID", tmp);
|
||||
_chatID = atoll(tmp.c_str());
|
||||
|
||||
#ifdef ESP32
|
||||
_myBot.useDNS(true);
|
||||
#endif
|
||||
|
||||
_myBot.setTelegramToken(_token);
|
||||
_myBot.enableUTF8Encoding(true);
|
||||
}
|
||||
|
||||
void doByInterval() {
|
||||
if (_receiveMsg) {
|
||||
TBMessage msg;
|
||||
if (_myBot.getNewMessage(msg)) {
|
||||
SerialPrint("->", F("Telegram"), "chat ID: " + uint64ToString(msg.sender.id) + ", msg: " + msg.text);
|
||||
if (_autos) {
|
||||
_chatID = msg.sender.id;
|
||||
}
|
||||
telegramMsgParse(String(msg.text));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void sendTelegramMsg(bool often, String msg) {
|
||||
if (often) {
|
||||
_myBot.sendMessage(_chatID, msg);
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + uint64ToString(_chatID) + ", msg: " + msg);
|
||||
} else {
|
||||
if (_prevMsg != msg) {
|
||||
_prevMsg = msg;
|
||||
_myBot.sendMessage(_chatID, msg);
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + uint64ToString(_chatID) + ", msg: " + msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
~Telegram() {
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
void* getAPI_Telegram(String subtype, String param) {
|
||||
if (subtype == F("Telegram")) {
|
||||
return new Telegram(param);
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
46
src/modules/exec/Telegram/modinfo.json
Normal file
46
src/modules/exec/Telegram/modinfo.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"menuSection": "Исполнительные устройства",
|
||||
|
||||
"configItem": [{
|
||||
"name": "Телеграм-Бот",
|
||||
"type": "Writing",
|
||||
"subtype": "Telegram",
|
||||
"id": "tg",
|
||||
"widget": "",
|
||||
"page": "",
|
||||
"descr": "",
|
||||
"int": 10,
|
||||
|
||||
"token": "",
|
||||
"autos": 1,
|
||||
"receiveMsg": 0,
|
||||
"chatID": ""
|
||||
}],
|
||||
|
||||
"about": {
|
||||
"authorName": "Ilya Belyakov",
|
||||
"authorContact": "https://t.me/Biveraxe",
|
||||
"authorGit": "https://github.com/biveraxe",
|
||||
"specialThanks": "",
|
||||
"moduleName": "Telegram",
|
||||
"moduleVersion": "1.0",
|
||||
"moduleDesc": "Добавляет возможность отправлять сообщения от имени бота контакту в Телеграм-чате и получать команды.",
|
||||
"propInfo": {
|
||||
"token": "Токен для авторизации бота в системе Telegram",
|
||||
"autos": "Автоматически(1) или нет(0) запоминать ChatID по входящим сообщениям. Т.е. бот будет информировать тех, кто последний прислал сообщение.",
|
||||
"receiveMsg": "Обрабатывать(1) или нет(0) входящие сообщения.",
|
||||
"chatID": "ИД диалога с контактом. Необходим для отправки сообщений именно вам."
|
||||
}
|
||||
},
|
||||
|
||||
"defActive": false,
|
||||
|
||||
"devices": {
|
||||
"esp32_4mb": [
|
||||
"CTBot @2.1.9"
|
||||
],
|
||||
"esp8266_4mb": [
|
||||
"CTBot @2.1.9"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user