Добрый день! Помогите пожалуйста как мне правильно записать несколько CHAT_ID в код, за ранее спасибо!!! Код (C++): #include <ESP8266WiFi.h> #include <WiFiClientSecure.h> #include <UniversalTelegramBot.h> // Wifi network station credentials #define WIFI_SSID "D9" #define WIFI_PASSWORD "хххх" // Telegram BOT Token (Get from Botfather) #define BOT_TOKEN "хххх" #define CHAT_ID "ххххх" const unsigned long BOT_MTBS = 1000; // mean time between scan messages X509List cert(TELEGRAM_CERTIFICATE_ROOT); WiFiClientSecure secured_client; UniversalTelegramBot bot(BOT_TOKEN, secured_client); unsigned long bot_lasttime; // last time messages' scan has been done const int ledPin = LED_BUILTIN; int ledStatus = 0; void handleNewMessages(int numNewMessages) { Serial.print("handleNewMessages "); Serial.println(numNewMessages); for (int i=0; i<numNewMessages; i++) { // Chat id of the requester String chat_id = String(bot.messages[i].chat_id); if (chat_id != CHAT_ID){ bot.sendMessage(chat_id, "Unauthorized user", ""); continue; } String text = bot.messages[i].text; String from_name = bot.messages[i].from_name; if (from_name == "") from_name = "Guest";
Код (C++): #define CHAT_ID_1 "ххххх" #define CHAT_ID_2 "ххххх" #define CHAT_ID_3 "ххххх" Далее перебор через case. Если упали в default, значит Код (C++): bot.sendMessage(chat_id, "Unauthorized user", "");
написал вот так Код (C++): #include <ESP8266WiFi.h> #include <WiFiClientSecure.h> #include <UniversalTelegramBot.h> // Wifi network station credentials #define WIFI_SSID "D9" #define WIFI_PASSWORD "хххх" // Telegram BOT Token (Get from Botfather) #define BOT_TOKEN "хххх" //#define CHAT_ID "хххх" int CHAT_ID #define CHAT_ID_1 "ххх" #define CHAT_ID_2 "1234567890" #define CHAT_ID_3 "2345678901" const unsigned long BOT_MTBS = 1000; // mean time between scan messages X509List cert(TELEGRAM_CERTIFICATE_ROOT); WiFiClientSecure secured_client; UniversalTelegramBot bot(BOT_TOKEN, secured_client); unsigned long bot_lasttime; // last time messages' scan has been done const int ledPin = D0; int ledStatus = 0; const int ledPin2 = LED_BUILTIN; int ledStatus2 = 0; void handleNewMessages(int numNewMessages) { Serial.print("handleNewMessages "); Serial.println(numNewMessages); for (int i = 0; i < numNewMessages; i++) { switch (CHAT_ID) { case CHAT_ID_1: String chat_id = String(bot.messages[i].chat_id); break; case CHAT_ID_2: String chat_id = String(bot.messages[i].chat_id); break; case CHAT_ID_3: String chat_id = String(bot.messages[i].chat_id); break; default: bot.sendMessage(chat_id, "Unauthorized user", ""); но выскакивает ошибка expected initializer before 'const' что я сделал не так???
в самом начале любой книжки написано: любое действие в языке си заканчивается точкой с запятой. Есть исключения, но не будем сейчас об этом.
Не получается Вашим методом все реализовать, пытаюсь с помощью массива, вроде бы все получается но есть проблема с типами данных, так как некоторые ID могут быть типом int а некоторые могут быть long long, если ID имеет тип int все замечательно. Код (C++): int chatID_acces[] = {1618033154, 5271692259, 5162498360}; здесь первый ID проходит а остальные нет. сравнивание Код (C++): String text = bot.messages[i].text; Serial.println(m_id); String from_name = bot.messages[i].from_name; if (from_name == "") from_name = "Guest"; int i2=0; do{ if (!protection || String(chatID_acces[i2]) == chat_id) { if (text == "/control") { String statusMessage; for (int i=0; i<quantity; i++) { digitalRead(pin[i])?statusMessage+=on_symbol:statusMessage+=off_symbol; statusMessage+=buttons[i]; statusMessage+='\n'; } и Код (C++): if (i2==((sizeof(chatID_acces)/sizeof(int))-1)&&text == "/start"||(sizeof(chatID_acces)/sizeof(int))==0&&text == "/start") { bot.sendMessage(chat_id, "No access, Chat ID: "+chat_id, ""); } } i2++; }while (i2<(sizeof(chatID_acces)/sizeof(int))); }