Commit dea7b45c authored by diy_bloke's avatar diy_bloke

Add new file

parents
/*******************************************************************
* How to use basic dunctiond of a Telegrambot
* A completely changed example based on a sketch from
* Vadim Sinitski
*******************************************************************/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Initialize Wifi connection to the router
char ssid[] = "NETGEAR1"; // your network SSID (name)
char password[] = "SECRET"; // your network key
// Initialize Telegram BOT
#define BOTtoken "XXXXXXXXX:YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" // your Bot Token (Get from Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done
bool Start = false;
const int ledPin = 2;
int ledStatus = 0;
String test_photo_url = "https://img.srgcdn.com/e/w:750/cjh2NWp4S0FiNWxJQVNsQ3JUU28uanBn.jpg"; // can insert any picture URL
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(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";
if (text == "/get_photo") {
bot.sendPhoto(chat_id, test_photo_url, "Who");
}
//-------------
//add more commands here
//-------------
if (text == "/ledon") {
digitalWrite(ledPin, LOW); // turn the LED on (HIGH is the voltage level)
ledStatus = 1;
bot.sendMessage(chat_id, "Led is ON", "");
}
if (text == "/ledoff") {
ledStatus = 0;
digitalWrite(ledPin, HIGH); // turn the LED off (LOW is the voltage level)
bot.sendMessage(chat_id, "Led is OFF", "");
}
if (text == "/status") {
if (ledStatus) {
bot.sendMessage(chat_id, "Led is ON", "");
} else {
bot.sendMessage(chat_id, "Led is OFF", "");
}
}
if (text == "/start") {
String welcome = "Welcome to Telegram Bot, " + from_name + ".\n";
welcome += "This will show several properies.\n\n";
welcome += "/get_photo : getting photo\n";
welcome += "/ledon : to switch the Led ON\n";
welcome += "/ledoff : to switch the Led OFF\n";
welcome += "/status : Returns current status of LED\n";
bot.sendMessage(chat_id, welcome, "");
}
}
}
void setup() {
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was Previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
client.setInsecure(); // if yr core cannot handle secure connections
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
delay(10);
digitalWrite(ledPin, HIGH); // initialize pin as off
}
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();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment