Добрый вечер!! помогите изменить код, на просторах нашел рабочий код, но не могу его переделать на работу с массивом CHAT_ID.
#include <ESP8266WiFi.h>
001 | #include <WiFiClientSecure.h> |
002 | #include <UniversalTelegramBot.h> |
005 | #define WIFI_SSID "xxx" |
006 | #define WIFI_PASSWORD "xxx" |
008 | #define BOT_TOKEN "xxx" |
010 | const unsigned long BOT_MTBS = 1000; |
012 | X509List cert(TELEGRAM_CERTIFICATE_ROOT); |
013 | WiFiClientSecure secured_client; |
014 | UniversalTelegramBot bot(BOT_TOKEN, secured_client); |
015 | unsigned long bot_lasttime; |
017 | const int ledPin = LED_BUILTIN; |
020 | void handleNewMessages( int numNewMessages) |
022 | Serial .print( "handleNewMessages " ); |
023 | Serial .println(numNewMessages); |
025 | for ( int i = 0; i < numNewMessages; i++) |
027 | String chat_id = bot.messages[i].chat_id; |
028 | String text = bot.messages[i].text; |
030 | String from_name = bot.messages[i].from_name; |
034 | if (text == "/ledon" ) |
036 | digitalWrite(ledPin, LOW); |
038 | bot.sendMessage(chat_id, "Led is ON" , "" ); |
041 | if (text == "/ledoff" ) |
044 | digitalWrite(ledPin, HIGH); |
045 | bot.sendMessage(chat_id, "Led is OFF" , "" ); |
048 | if (text == "/status" ) |
052 | bot.sendMessage(chat_id, "Led is ON" , "" ); |
056 | bot.sendMessage(chat_id, "Led is OFF" , "" ); |
060 | if (text == "/start" ) |
062 | String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n" ; |
063 | welcome += "This is Flash Led Bot example.\n\n" ; |
064 | welcome += "/ledon : to switch the Led ON\n" ; |
065 | welcome += "/ledoff : to switch the Led OFF\n" ; |
066 | welcome += "/status : Returns current status of LED\n" ; |
067 | bot.sendMessage(chat_id, welcome, "Markdown" ); |
075 | Serial .begin(115200); |
078 | pinMode(ledPin, OUTPUT); |
080 | digitalWrite(ledPin, HIGH); |
083 | configTime(0, 0, "pool.ntp.org" ); |
084 | secured_client.setTrustAnchors(&cert); |
085 | Serial .print( "Connecting to Wifi SSID " ); |
086 | Serial .print(WIFI_SSID); |
087 | WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
088 | while (WiFi.status() != WL_CONNECTED) |
093 | Serial .print( "\nWiFi connected. IP address: " ); |
094 | Serial .println(WiFi.localIP()); |
097 | Serial .print( "Retrieving time: " ); |
098 | time_t now = time(nullptr); |
099 | while (now < 24 * 3600) |
110 | if (millis() - bot_lasttime > BOT_MTBS) |
112 | int numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
114 | while (numNewMessages) |
116 | Serial .println( "got response" ); |
117 | handleNewMessages(numNewMessages); |
118 | numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
121 | bot_lasttime = millis(); |
вы прям так уверены. что любой знает, где тут должен быть этот массив?
вы прям так уверены. что любой знает, где тут должен быть этот массив?
отсюда пытаюсь взять строки https://randomnerdtutorials.com/telegram-control-esp32-esp8266-nodemcu-outputs/
вы прям так уверены. что любой знает, где тут должен быть этот массив?
001
#include <ESP8266WiFi.h>
002
#include <WiFiClientSecure.h>
003
#include <UniversalTelegramBot.h>
004
005
// Wifi network station credentials
006
#define WIFI_SSID "D9"
007
#define WIFI_PASSWORD "хххх"
008
// Telegram BOT Token (Get from Botfather)
009
#define BOT_TOKEN "хххх"
010
#define CHAT_ID "XXXXXXXXXX"
011
012
013
const
unsigned
long
BOT_MTBS = 1000;
// mean time between scan messages
014
015
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
016
WiFiClientSecure secured_client;
017
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
018
unsigned
long
bot_lasttime;
// last time messages' scan has been done
019
020
const
int
ledPin = LED_BUILTIN;
021
int
ledStatus = 0;
022
023
void
handleNewMessages(
int
numNewMessages)
024
{
025
Serial
.print(
"handleNewMessages "
);
026
Serial
.println(numNewMessages);
027
028
for
(
int
i = 0; i < numNewMessages; i++)
029
{
030
String chat_id = String(bot.messages[i].chat_id);
031
if
(chat_id != CHAT_ID){
032
bot.sendMessage(chat_id,
"Unauthorized user"
,
""
);
033
continue
;
034
035
String from_name = bot.messages[i].from_name;
036
if
(from_name ==
""
)
037
from_name =
"Guest"
;
038
039
if
(text ==
"/ledon"
)
040
{
041
digitalWrite(ledPin, LOW);
// turn the LED on (HIGH is the voltage level)
042
ledStatus = 1;
043
bot.sendMessage(chat_id,
"Led is ON"
,
""
);
044
}
045
046
if
(text ==
"/ledoff"
)
047
{
048
ledStatus = 0;
049
digitalWrite(ledPin, HIGH);
// turn the LED off (LOW is the voltage level)
050
bot.sendMessage(chat_id,
"Led is OFF"
,
""
);
051
}
052
053
if
(text ==
"/status"
)
054
{
055
if
(ledStatus)
056
{
057
bot.sendMessage(chat_id,
"Led is ON"
,
""
);
058
}
059
else
060
{
061
bot.sendMessage(chat_id,
"Led is OFF"
,
""
);
062
}
063
}
064
065
if
(text ==
"/start"
)
066
{
067
String welcome =
"Welcome to Universal Arduino Telegram Bot library, "
+ from_name +
".\n"
;
068
welcome +=
"This is Flash Led Bot example.\n\n"
;
069
welcome +=
"/ledon : to switch the Led ON\n"
;
070
welcome +=
"/ledoff : to switch the Led OFF\n"
;
071
welcome +=
"/status : Returns current status of LED\n"
;
072
bot.sendMessage(chat_id, welcome,
"Markdown"
);
073
}
074
}
075
}
076
077
078
void
setup
()
079
{
080
Serial
.begin(115200);
081
Serial
.println();
082
083
pinMode(ledPin, OUTPUT);
// initialize digital ledPin as an output.
084
delay(10);
085
digitalWrite(ledPin, HIGH);
// initialize pin as off (active LOW)
086
087
// attempt to connect to Wifi network:
088
configTime(0, 0,
"pool.ntp.org"
);
// get UTC time via NTP
089
secured_client.setTrustAnchors(&cert);
// Add root certificate for api.telegram.org
090
Serial
.print(
"Connecting to Wifi SSID "
);
091
Serial
.print(WIFI_SSID);
092
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
093
while
(WiFi.status() != WL_CONNECTED)
094
{
095
Serial
.print(
"."
);
096
delay(500);
097
}
098
Serial
.print(
"\nWiFi connected. IP address: "
);
099
Serial
.println(WiFi.localIP());
100
101
// Check NTP/Time, usually it is instantaneous and you can delete the code below.
102
Serial
.print(
"Retrieving time: "
);
103
time_t now = time(nullptr);
104
while
(now < 24 * 3600)
105
{
106
Serial
.print(
"."
);
107
delay(100);
108
now = time(nullptr);
109
}
110
Serial
.println(now);
111
}
112
113
void
loop
()
114
{
115
if
(millis() - bot_lasttime > BOT_MTBS)
116
{
117
int
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
118
119
while
(numNewMessages)
120
{
121
Serial
.println(
"got response"
);
122
handleNewMessages(numNewMessages);
123
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
124
}
125
126
bot_lasttime = millis();
127
}
128
}
всегда цитируйте сообщение об ошибках ПОЛНОСТЬЮ!
Строка 028 кода из первого поста потеряна в коде из #3
1
String text = bot.messages[i].text;
От этого ошибка. Дальше не смотрел.
Все получилось спасибо, еще вопросик а как добавить несколько CHAT_ID???