#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define BOT_TOKEN "6790819455:AAFPbxovw34s3H49b-bhhnk0XtU5cZ4CJjk" //<----------------- Вставьте сюда ваш токен бота
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done
String chatID_acces[] = {"03475932552", "0347597552", "03475973132"}; // Chat ID, которым разрешен доступ, игнорируется, если protection = 0.
//"6233273659"
String chatID_accesp[] = {"840347910", "5230738681", "nnn"};//"840347910"
//, "6233273659"
void handleNewMessages(int numNewMessages)
{
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Guest";
int i2=0;
do{
if (chatID_acces[i2] == chat_id) {
if (text == "/start") {
String welcome = "Приветствую " + from_name + ".\n";
welcome += "Вы Админ.\n\n";
String keyboardStart = "[[{ \"text\" : \"👨🏻💻Telegram автора👨🏻💻\", \"url\" : \"https://t.me/Kriko11\" }]]";
bot.sendMessageWithInlineKeyboard(chat_id, welcome, "", keyboardStart);
}
break;
}
if (chatID_accesp[i2] == chat_id) {
if (text == "/start") {
String welcome = "Приветствую " + from_name + ".\n";
welcome += "Вы пользователь.\n\n";
String keyboardStart = "[[{ \"text\" : \"👨🏻💻Telegram автора👨🏻💻\", \"url\" : \"https://t.me/Kriko11\" }]]";
bot.sendMessageWithInlineKeyboard(chat_id, welcome, "", keyboardStart);
}
break;
}
if (i2==((sizeof(chatID_acces)/sizeof(int))-1)&&text == "/start"||(sizeof(chatID_acces)/sizeof(int))==0&&text == "/start") {
if (i2==((sizeof(chatID_accesp)/sizeof(int))-1)&&text == "/start"||(sizeof(chatID_accesp)/sizeof(int))==0&&text == "/start") {
String keyboardStart = "[[{ \"text\" : \"👨🏻💻Telegram автора👨🏻💻\", \"url\" : \"https://t.me/Kriko11\" }]]";
bot.sendMessageWithInlineKeyboard(chat_id, "Вы гость, ваш chat_id: " + chat_id, "", keyboardStart);
break;
}
}
i2++;
}while (i2<(sizeof(chatID_acces)/sizeof(int)));
}
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop()
{
if (millis() - bot_lasttime > BOT_MTBS)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages)
{
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
}
//bot.sendMessage("6233273659", "Котел скоро потухнет. Подкинь дровишек браток!!!", "");
}