Skip to content

Instantly share code, notes, and snippets.

@yakutozcan
Created June 6, 2017 19:08
Show Gist options
  • Save yakutozcan/e07551607a59d2a64642e8d4d7633662 to your computer and use it in GitHub Desktop.
Save yakutozcan/e07551607a59d2a64642e8d4d7633662 to your computer and use it in GitHub Desktop.
/*******************************************************************
* An example of bot that receives commands and turns on and off *
* an LED. *
* *
* written by Giacarlo Bacchio (Gianbacchio on Github) *
* adapted by Brian Lough *
*******************************************************************/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Initialize Wifi connection to the router
char ssid[] = "özcan"; // WiFi Adi SSID
char password[] = "efsanesifre!"; // WiFi şifresi
// Initialize Telegram BOT
#define BOTtoken "************" //Bot Token (Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1500; //Gelen mesaj tarama sıklığı
long Bot_lasttime; //last time messages' scan has been done
bool Start = false;
const int ledPin = LED_BUILTIN;
int ledStatus = 1;
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 == "/ledon") {
digitalWrite(ledPin, LOW); // Led Açık
ledStatus = 1;
bot.sendMessage(chat_id, "Led Acildi", "");
}
if (text == "/ledoff") {
ledStatus = 0;
digitalWrite(ledPin, HIGH); // Led Kapali
bot.sendMessage(chat_id, "Led Kapandi", "");
}
if (text == "/status") {
if(ledStatus){
bot.sendMessage(chat_id, "Led Acik", "");
} else {
bot.sendMessage(chat_id, "Led Kapali", "");
}
}
if (text == "/start") {
String welcome = "TLed, " + from_name + ".\n";
welcome += "/ledon : Ledi Ac \n";
welcome += "/ledoff : Ledi Kapat\n";
welcome += "/status : Led Durumu\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
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());
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment