Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable_web_page_preview + disable_notification + deleteMessage() #260

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ Here is a list of features that this library covers. (Note: The examples link to
| Feature | Description | Usage | Example |
| ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| _Receiving Messages_ | Your bot can read messages that are sent to it. This is useful for sending commands to your arduino such as toggle and LED | `int getUpdates(long offset)` <br><br> Gets any pending messages from Telegram and stores them in **bot.messages** . Offset should be set to **bot.last_message_received** + 1. Returns the numbers new messages received. | [FlashLED](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/FlashLED/FlashLED.ino) or any other example |
| _Sending messages_ | Your bot can send messages to any Telegram or group. This can be useful to get the arduino to notify you of an event e.g. Button pressed etc (Note: bots can only message you if you messaged them first) | `bool sendMessage(String chat_id, String text, String parse_mode = "")` <br><br> Sends the message to the chat_id. Returns if the message sent or not. | [EchoBot](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/EchoBot/EchoBot.ino#L51) or any other example |
| _Sending messages_ | Your bot can send messages to any Telegram or group. This can be useful to get the arduino to notify you of an event e.g. Button pressed etc (Note: bots can only message you if you messaged them first) | `bool sendMessage(String chat_id, String text, String parse_mode = "", int message_id = 0, bool disable_web_page_preview = false, bool disable_notification = false)` <br><br> Sends the message to the chat_id. Returns if the message sent or not. | [EchoBot](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/EchoBot/EchoBot.ino#L51) or any other example |
| _Reply Keyboards_ | Your bot can send [reply keyboards](https://camo.githubusercontent.com/2116a60fa614bf2348074a9d7148f7d0a7664d36/687474703a2f2f692e696d6775722e636f6d2f325268366c42672e6a70673f32) that can be used as a type of menu. | `bool sendMessageWithReplyKeyboard(String chat_id, String text, String parse_mode, String keyboard, bool resize = false, bool oneTime = false, bool selective = false)` <br><br> Send a keyboard to the specified chat_id. parse_mode can be left blank. Will return true if the message sends successfully. | [ReplyKeyboard](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/CustomKeyboard/ReplyKeyboardMarkup/ReplyKeyboardMarkup.ino) |
| _Inline Keyboards_ | Your bot can send [inline keyboards](https://camo.githubusercontent.com/55dde972426e5bc77120ea17a9c06bff37856eb6/68747470733a2f2f636f72652e74656c656772616d2e6f72672f66696c652f3831313134303939392f312f324a536f55566c574b61302f346661643265323734336463386564613034). <br><br>Note: URLS & callbacks are supported currently | `bool sendMessageWithInlineKeyboard(String chat_id, String text, String parse_mode, String keyboard)` <br><br> Send a keyboard to the specified chat_id. parse_mode can be left blank. Will return true if the message sends successfully. | [InlineKeyboard](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/CustomKeyboard/InlineKeyboardMarkup/InlineKeyboardMarkup.ino) |
| _Deleting messages_ | Your bot can delete messages | `bool deleteMessage(String chat_id, int message_id = 0)` <br><br>Deletes the message by message_id from the selected chat. Will return true if the message was successfully deleted. | [DeleteMessage](examples/ESP8266/DeleteMessage/DeleteMessage.ino) |
| _Send Photos_ | It is possible to send phtos from your bot. You can send images from the web or from the arduino directly (Only sending from an SD card has been tested, but it should be able to send from a camera module) | Check the examples for more info | [From URL](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/SendPhoto/PhotoFromURL/PhotoFromURL.ino)<br><br>[Binary from SD](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/SendPhoto/PhotoFromSD/PhotoFromSD.ino)<br><br>[From File Id](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/SendPhoto/PhotoFromFileID/PhotoFromFileID.ino) |
| _Chat Actions_ | Your bot can send chat actions, such as _typing_ or _sending photo_ to let the user know that the bot is doing something. | `bool sendChatAction(String chat_id, String chat_action)` <br><br> Send a the chat action to the specified chat_id. There is a set list of chat actions that Telegram support, see the example for details. Will return true if the chat actions sends successfully. |
| _Location_ | Your bot can receive location data, either from a single location data point or live location data. | Check the example. | [Location](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/Location/Location.ino) |
Expand Down
178 changes: 178 additions & 0 deletions examples/ESP8266/DeleteMessage/DeleteMessage.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

// Wifi network station credentials
#define WIFI_SSID "YOUR_SSID"
#define WIFI_PASSWORD "YOUR_PASSWORD"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

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, 2500);
unsigned long bot_lasttime;

void setup()
{
Serial.begin(115200);

connectToWifi();
synchronizeTime();
}

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();
}
}

void handleNewMessages(int numNewMessages)
{
for (int i = 0; i < numNewMessages; i++)
{
telegramMessage message = bot.messages[i];

Serial.println("Received: " + message.text);

String text = message.text;
String from_name = message.from_name;
int message_id = getMessageId(message);

if (from_name == "")
{
from_name = "Guest";
}

if (text == "/options")
{
// Memory pool for JSON object tree.
//
// Inside the brackets, 400 is the size of the pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use https://arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<400> keyboard;

StaticJsonDocument<100> menuItem0;
menuItem0["text"] = "Some button";
menuItem0["callback_data"] = "/custom_action";
keyboard[0].add(menuItem0);

StaticJsonDocument<100> menuItem1;
menuItem1["text"] = "Go to Google";
menuItem1["url"] = "https://www.google.com";
keyboard[1].add(menuItem1);

StaticJsonDocument<100> menuItem2;
menuItem2["text"] = "Close";
menuItem2["callback_data"] = "/cancel";
keyboard[2].add(menuItem2);

bot.sendMessageWithInlineKeyboard(
message.chat_id,
"Choose from one of the following options",
"",
getKeyboardJson(keyboard),
message_id
);
}
else if (text == "/custom_action")
{
Serial.println(
message_id != 0 ? "The keyboard will be replaced with test text." : "Only test text will be displayed."
);

bot.sendMessage(message.chat_id, "Lorem Ipsum", "", message_id);
}
else if (text == "/cancel")
{
Serial.println(
message_id != 0 ? "Keyboard will be deleted." : "Action will be ignored."
);

bot.deleteMessage(message.chat_id, message_id);
}
else if (text == "/start")
{
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
welcome += "This is example of Inline Keyboard Markup and deletion of the previous message.\n\n";
welcome += "/options : returns the inline keyboard\n";
welcome += "/custom_action : returns test text\n";

bot.sendMessage(message.chat_id, welcome, "");
}
}
}

void connectToWifi()
{
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
Serial.print(" ");

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setTrustAnchors(&cert);

while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
Serial.println();

Serial.print("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());
}

void synchronizeTime()
{
configTime(0, 0, "pool.ntp.org");

Serial.print("Time synchronization ");

time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println();

Serial.print("Current time (unixtime): ");
Serial.println(now);
}

int getMessageId(telegramMessage message)
{
if (message.type == "callback_query")
{
Serial.println("callback_query detected. The message_id will be passed.");
}

return message.type == "callback_query" ? message.message_id : 0;
}

String getKeyboardJson(JsonDocument& keyboard)
{
String keyboardJson = "";
serializeJson(keyboard, keyboardJson);
return keyboardJson;
}
Loading