Добавляем Телеграм-бота. Умеет отвечать на сообщения из чата и настраиваться.

This commit is contained in:
2022-05-01 00:38:30 +03:00
parent 83c5d74d7a
commit bdaf6c1832
5 changed files with 241 additions and 61 deletions

View File

@@ -56,9 +56,8 @@
"widget": "", "widget": "",
"page": "", "page": "",
"descr": "", "descr": "",
"int": "1", "int": "0",
"val": "0", "val": "0"
"dtFormat": ""
}, },
{ {
"header": "Модули управления" "header": "Модули управления"
@@ -106,9 +105,23 @@
"volume": 20 "volume": 20
}, },
{ {
"name": "8. Таймер", "name": "8. Телеграм-Бот",
"num": 8, "num": 8,
"type": "Writing", "type": "Writing",
"subtype": "Telegram",
"id": "tg",
"widget": "",
"page": "",
"descr": "",
"int": 10,
"token": "",
"autos": 1,
"receiveMsg": 0
},
{
"name": "9. Таймер",
"num": 9,
"type": "Writing",
"subtype": "Timer", "subtype": "Timer",
"id": "timer", "id": "timer",
"widget": "", "widget": "",
@@ -124,8 +137,8 @@
"header": "Сенсоры" "header": "Сенсоры"
}, },
{ {
"name": "9. Датчик напряжения ADS1115", "name": "10. Датчик напряжения ADS1115",
"num": 9, "num": 10,
"type": "Reading", "type": "Reading",
"subtype": "Ads1115", "subtype": "Ads1115",
"id": "Ads3", "id": "Ads3",
@@ -137,12 +150,12 @@
"gain": "3/4x", "gain": "3/4x",
"plus": 0, "plus": 0,
"multiply": 1, "multiply": 1,
"round": 100, "round": 2,
"int": 10 "int": 10
}, },
{ {
"name": "10. Cенсор температуры AHT20", "name": "11. Cенсор температуры AHT20",
"num": 10, "num": 11,
"type": "Reading", "type": "Reading",
"subtype": "Aht20t", "subtype": "Aht20t",
"id": "Temp20", "id": "Temp20",
@@ -154,8 +167,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "11. Cенсор влажности AHT20", "name": "12. Cенсор влажности AHT20",
"num": 11, "num": 12,
"type": "Reading", "type": "Reading",
"subtype": "Aht20h", "subtype": "Aht20h",
"id": "Hum20", "id": "Hum20",
@@ -167,8 +180,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "12. Аналоговый сенсор", "name": "13. Аналоговый сенсор",
"num": 12, "num": 13,
"type": "Reading", "type": "Reading",
"subtype": "AnalogAdc", "subtype": "AnalogAdc",
"id": "t", "id": "t",
@@ -183,8 +196,8 @@
"int": 15 "int": 15
}, },
{ {
"name": "13. Cенсор температуры Bme280", "name": "14. Cенсор температуры Bme280",
"num": 13, "num": 14,
"type": "Reading", "type": "Reading",
"subtype": "Bme280t", "subtype": "Bme280t",
"id": "tmp3", "id": "tmp3",
@@ -196,8 +209,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "14. Cенсор давления Bme280", "name": "15. Cенсор давления Bme280",
"num": 14, "num": 15,
"type": "Reading", "type": "Reading",
"subtype": "Bme280p", "subtype": "Bme280p",
"id": "Press3", "id": "Press3",
@@ -209,8 +222,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "15. Cенсор влажности Bme280", "name": "16. Cенсор влажности Bme280",
"num": 15, "num": 16,
"type": "Reading", "type": "Reading",
"subtype": "Bme280h", "subtype": "Bme280h",
"id": "Hum3", "id": "Hum3",
@@ -222,8 +235,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "16. Cенсор температуры Bmp280", "name": "17. Cенсор температуры Bmp280",
"num": 16, "num": 17,
"type": "Reading", "type": "Reading",
"subtype": "Bmp280t", "subtype": "Bmp280t",
"id": "tmp3", "id": "tmp3",
@@ -235,8 +248,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "17. Cенсор давления Bmp280", "name": "18. Cенсор давления Bmp280",
"num": 17, "num": 18,
"type": "Reading", "type": "Reading",
"subtype": "Bmp280p", "subtype": "Bmp280p",
"id": "Press3", "id": "Press3",
@@ -248,8 +261,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "18. Cенсор температуры dht11", "name": "19. Cенсор температуры dht11",
"num": 18, "num": 19,
"type": "Reading", "type": "Reading",
"subtype": "Dht1122t", "subtype": "Dht1122t",
"id": "tmp3", "id": "tmp3",
@@ -261,8 +274,8 @@
"senstype": "dht11" "senstype": "dht11"
}, },
{ {
"name": "19. Cенсор влажности dht11", "name": "20. Cенсор влажности dht11",
"num": 19, "num": 20,
"type": "Reading", "type": "Reading",
"subtype": "Dht1122h", "subtype": "Dht1122h",
"id": "Hum3", "id": "Hum3",
@@ -274,8 +287,8 @@
"senstype": "dht11" "senstype": "dht11"
}, },
{ {
"name": "20. Cенсор температуры ds18b20", "name": "21. Cенсор температуры ds18b20",
"num": 20, "num": 21,
"type": "Reading", "type": "Reading",
"subtype": "Ds18b20", "subtype": "Ds18b20",
"id": "dstmp", "id": "dstmp",
@@ -289,8 +302,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "21. Cенсор температуры GY21", "name": "22. Cенсор температуры GY21",
"num": 21, "num": 22,
"type": "Reading", "type": "Reading",
"subtype": "GY21t", "subtype": "GY21t",
"id": "tmp4", "id": "tmp4",
@@ -301,8 +314,8 @@
"int": 15 "int": 15
}, },
{ {
"name": "22. Cенсор влажности GY21", "name": "23. Cенсор влажности GY21",
"num": 22, "num": 23,
"type": "Reading", "type": "Reading",
"subtype": "GY21h", "subtype": "GY21h",
"id": "Hum4", "id": "Hum4",
@@ -313,8 +326,8 @@
"int": 15 "int": 15
}, },
{ {
"name": "23. Cенсор температуры HDC1080", "name": "24. Cенсор температуры HDC1080",
"num": 23, "num": 24,
"type": "Reading", "type": "Reading",
"subtype": "Hdc1080t", "subtype": "Hdc1080t",
"id": "Temp1080", "id": "Temp1080",
@@ -326,8 +339,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "24. Cенсор влажности HDC1080", "name": "25. Cенсор влажности HDC1080",
"num": 24, "num": 25,
"type": "Reading", "type": "Reading",
"subtype": "Hdc1080h", "subtype": "Hdc1080h",
"id": "Hum1080", "id": "Hum1080",
@@ -339,8 +352,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "25. Cенсор температуры MAX6675", "name": "26. Cенсор температуры MAX6675",
"num": 25, "num": 26,
"type": "Reading", "type": "Reading",
"subtype": "Max6675t", "subtype": "Max6675t",
"id": "maxtmp", "id": "maxtmp",
@@ -353,8 +366,8 @@
"CLK": 14 "CLK": 14
}, },
{ {
"name": "26. Датчик CO2 MHZ-19 UART", "name": "27. Датчик CO2 MHZ-19 UART",
"num": 26, "num": 27,
"type": "Reading", "type": "Reading",
"subtype": "Mhz19uart", "subtype": "Mhz19uart",
"id": "co2uart", "id": "co2uart",
@@ -372,8 +385,8 @@
"ABC": 1 "ABC": 1
}, },
{ {
"name": "27. Датчик CO2 MHZ-19 PWM", "name": "28. Датчик CO2 MHZ-19 PWM",
"num": 27, "num": 28,
"type": "Reading", "type": "Reading",
"subtype": "Mhz19pwm", "subtype": "Mhz19pwm",
"id": "co2pwm", "id": "co2pwm",
@@ -387,8 +400,8 @@
"int": 300 "int": 300
}, },
{ {
"name": "28. Cенсор температуры от MHZ-19 UART", "name": "29. Cенсор температуры от MHZ-19 UART",
"num": 28, "num": 29,
"type": "Reading", "type": "Reading",
"subtype": "Mhz19temp", "subtype": "Mhz19temp",
"id": "Mhz19temp", "id": "Mhz19temp",
@@ -404,8 +417,8 @@
"int": 30 "int": 30
}, },
{ {
"name": "29. Рабочий диапазон от MHZ-19 UART", "name": "30. Рабочий диапазон от MHZ-19 UART",
"num": 29, "num": 30,
"type": "Reading", "type": "Reading",
"subtype": "Mhz19range", "subtype": "Mhz19range",
"id": "Mhz19range", "id": "Mhz19range",
@@ -422,8 +435,8 @@
"int": 30 "int": 30
}, },
{ {
"name": "30. Автокалибровка от MHZ-19 UART", "name": "31. Автокалибровка от MHZ-19 UART",
"num": 30, "num": 31,
"type": "Reading", "type": "Reading",
"subtype": "Mhz19ABC", "subtype": "Mhz19ABC",
"id": "Mhz19ABC", "id": "Mhz19ABC",
@@ -437,8 +450,8 @@
"int": 30 "int": 30
}, },
{ {
"name": "31. Датчик пыли SDS011 PM25", "name": "32. Датчик пыли SDS011 PM25",
"num": 31, "num": 32,
"type": "Reading", "type": "Reading",
"subtype": "Sds011_25", "subtype": "Sds011_25",
"id": "pmuart25", "id": "pmuart25",
@@ -455,8 +468,8 @@
"period": 300 "period": 300
}, },
{ {
"name": "32. Датчик пыли SDS011 PM10", "name": "33. Датчик пыли SDS011 PM10",
"num": 32, "num": 33,
"type": "Reading", "type": "Reading",
"subtype": "Sds011_10", "subtype": "Sds011_10",
"id": "pmuart10", "id": "pmuart10",
@@ -473,8 +486,8 @@
"period": 300 "period": 300
}, },
{ {
"name": "33. Cенсор температуры Sht20", "name": "34. Cенсор температуры Sht20",
"num": 33, "num": 34,
"type": "Reading", "type": "Reading",
"subtype": "Sht20t", "subtype": "Sht20t",
"id": "tmp2", "id": "tmp2",
@@ -485,8 +498,8 @@
"round": 1 "round": 1
}, },
{ {
"name": "34. Cенсор влажности Sht20", "name": "35. Cенсор влажности Sht20",
"num": 34, "num": 35,
"type": "Reading", "type": "Reading",
"subtype": "Sht20h", "subtype": "Sht20h",
"id": "Hum2", "id": "Hum2",
@@ -500,8 +513,8 @@
"header": "Экраны" "header": "Экраны"
}, },
{ {
"name": "35. LCD экран 2004", "name": "36. LCD экран 2004",
"num": 35, "num": 36,
"type": "Reading", "type": "Reading",
"subtype": "Lcd2004", "subtype": "Lcd2004",
"id": "Lcd", "id": "Lcd",
@@ -515,8 +528,8 @@
"id2show": "id датчика" "id2show": "id датчика"
}, },
{ {
"name": "36. LCD экран 1602", "name": "37. LCD экран 1602",
"num": 36, "num": 37,
"type": "Reading", "type": "Reading",
"subtype": "Lcd2004", "subtype": "Lcd2004",
"id": "Lcd", "id": "Lcd",

View File

@@ -7,6 +7,7 @@ void* getAPI_Variable(String subtype, String params);
void* getAPI_ButtonIn(String subtype, String params); void* getAPI_ButtonIn(String subtype, String params);
void* getAPI_ButtonOut(String subtype, String params); void* getAPI_ButtonOut(String subtype, String params);
void* getAPI_Mp3(String subtype, String params); void* getAPI_Mp3(String subtype, String params);
void* getAPI_Telegram(String subtype, String params);
void* getAPI_Timer(String subtype, String params); void* getAPI_Timer(String subtype, String params);
void* getAPI_Ads1115(String subtype, String params); void* getAPI_Ads1115(String subtype, String params);
void* getAPI_Aht20(String subtype, String params); void* getAPI_Aht20(String subtype, String params);
@@ -32,6 +33,7 @@ if ((tmpAPI = getAPI_Variable(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_ButtonIn(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_ButtonIn(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_ButtonOut(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_ButtonOut(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Telegram(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Timer(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Timer(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Ads1115(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Ads1115(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Aht20(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Aht20(subtype, params)) != nullptr) return tmpAPI;

View 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 {
//_myBot.sendMessage(_chatID, "ID: " + chipId + ", Name: " + jsonReadStr(configSetupJson, F("name")));
_myBot.sendMessage(_chatID, "ID: " + chipId);
_myBot.sendMessage(_chatID, F("Wrong order, use /all to get all values, /get_id to get value, or /set_id_value to set value"));
}
}
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);
#ifdef ESP32
_myBot.useDNS(true);
#endif
_myBot.setTelegramToken(_token);
_myBot.enableUTF8Encoding(true);
if (_myBot.testConnection()) {
SerialPrint("I", F("Telegram"), F("Connected"));
} else {
SerialPrint("E", F("Telegram"), F("Not connected"));
}
SerialPrint("I", F("Telegram"), F("Telegram Init"));
}
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));
}
}
// if (_ticker && (value.valD > 0 || _unfin) && !_pause) regEvent(value.valD, "Telegram tick");
}
IoTValue execute(String command, std::vector<IoTValue> &param) {
// if (command == "stop") {
// _pause = true;
// } else if (command == "reset") {
// _pause = false;
// value.valD = _initValue;
// if (_initValue) value.valD = value.valD + 1;
// } else if (command == "continue") {
// _pause = false;
// } else if (command == "int") {
// if (param.size()) {
// setInterval(param[0].valD);
// }
// }
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;
}
}

View File

@@ -0,0 +1,17 @@
[
{
"name": "Телеграм-Бот",
"num": 31,
"type": "Writing",
"subtype": "Telegram",
"id": "tg",
"widget": "",
"page": "",
"descr": "",
"int": 10,
"token": "",
"autos": 1,
"receiveMsg": 0
}
]

View File

@@ -0,0 +1,7 @@
[env:esp8266_4mb]
lib_deps =
CTBot @2.1.9
[env:esp32_4mb]
lib_deps =
CTBot @2.1.9