Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from cotestatnt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
cotestatnt authored Jul 31, 2020
2 parents 77f6504 + a8dd9a7 commit 752e698
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 21 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Take a look at the examples provided in the [examples folder](https://github.com
### Reference
[Here how to use the library](https://github.com/cotestatnt/AsyncTelegram/blob/master/REFERENCE.md).

+ 1.0.5 Added possibility to forward a messege to a puclic channel (bot must be one of admins) or to a specific user
+ 1.0.4 Fixed ArduinoJson ARDUINOJSON_DECODE_UNICODE define
+ 1.0.3 Bug fixes
+ 1.0.2 Added method for update Telegram server fingerprint (with online service https://www.grc.com/fingerprints.htm )
+ 1.0.1 Now is possible assign a callback function for every "inline keyboard button"
+ 1.0.0 Initial version
Expand Down
54 changes: 40 additions & 14 deletions examples/echoBot/echoBot.ino
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
/*
Name: echoBot.ino
Name: echoBot.ino
Created: 20/06/2020
Author: Tolentino Cotesta <[email protected]>
Description: a simple example that check for incoming messages
and reply the sender with the received message
and reply the sender with the received message.
The message will be forwarded also in a public channel
and to a specific userid.
*/
#include <Arduino.h>
#include "AsyncTelegram.h"
AsyncTelegram myBot;

const char* ssid = "XXXXXXXX"; // REPLACE mySSID WITH YOUR WIFI SSID
const char* pass = "XXXXXXXX"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
const char* ssid = "XXXXXXXX"; // REPLACE mySSID WITH YOUR WIFI SSID
const char* pass = "XXXXXXXX"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
const char* token = "XXXXXXXXXXXXXXXXXXXX"; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
const uint8_t LED = 4;

// Name of public channel (your bot must be in admin group)
const char* channel = "@tolentino_cotesta";

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
// initialize the Serial
Serial.begin(115200);
Serial.println("Starting TelegramBot...");
Expand All @@ -31,13 +35,17 @@ void setup() {
}

// Set the Telegram bot properies
myBot.setUpdateTime(2000);
myBot.setTelegramToken(token);

// Check if all things are ok
Serial.print("\nTest Telegram connection... ");
myBot.begin() ? Serial.println("OK") : Serial.println("NOK");

//if( myBot.updateFingerPrint())
// Serial.println("Telegram fingerprint updated");
myBot.setUpdateTime(2000);
myBot.setTelegramToken(token);

// Check if all things are ok
Serial.print("\nTest Telegram connection... ");
myBot.begin() ? Serial.println("OK") : Serial.println("NOK");

Serial.print("Bot name: @");
Serial.println(myBot.userName);
}

void loop() {
Expand All @@ -53,6 +61,24 @@ void loop() {

// if there is an incoming message...
if (myBot.getNewMessage(msg)){
myBot.sendMessage(msg, msg.text);

// Send a message to your public channel
String message ;
message += "Message from @";
message += myBot.userName;
message += ":\n";
message += msg.text;
Serial.println(message);
myBot.sendToChannel(channel, message, true);

// Send a message to specific user who has started your bot
// Target user can find it's own userid with the bot @JsonDumpBot
// https://t.me/JsonDumpBot
int32_t userid = 1234567890;
myBot.sendToUser(userid, msg.text);

// echo the received message
myBot.sendMessage(msg, msg.text);
}
}

2 changes: 2 additions & 0 deletions examples/lightBot/lightBot.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void setup() {
}

// Set the Telegram bot properies
//if( myBot.updateFingerPrint())
// Serial.println("Telegram fingerprint updated");
myBot.setUpdateTime(1000);
myBot.setTelegramToken(token);

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=AsyncTelegram
version=1.0.4
version=1.0.5
author=Tolentino Cotesta <[email protected]>
maintainer=Tolentino Cotesta <[email protected]>
sentence=Simple Arduino Telegram BOT library for ESP8266 and ESP32
Expand Down
34 changes: 33 additions & 1 deletion src/AsyncTelegram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ bool AsyncTelegram::begin(){
}



// Blocking getMe function (we wait for a reply from Telegram server)
bool AsyncTelegram::getMe(TBUser &user) {
String response((char *)0);
Expand Down Expand Up @@ -367,6 +368,7 @@ bool AsyncTelegram::getMe(TBUser &user) {
user.username = root["result"]["username"];
user.lastName = root["result"]["last_name"];
user.languageCode = root["result"]["language_code"];
userName = user.username ;
return true;
}

Expand Down Expand Up @@ -401,7 +403,7 @@ void AsyncTelegram::sendMessage(const TBMessage &msg, const char* message, Strin
}


void AsyncTelegram::sendMessage(const TBMessage &msg, String message, String keyboard) {
void AsyncTelegram::sendMessage(const TBMessage &msg, String &message, String keyboard) {
return sendMessage(msg, message.c_str(), keyboard);
}

Expand All @@ -416,6 +418,36 @@ void AsyncTelegram::sendMessage(const TBMessage &msg, const char* message, Reply
}


void AsyncTelegram::sendToUser(const int32_t userid, String &message, String keyboard) {
TBMessage msg;
msg.sender.id = userid;
return sendMessage(msg, message.c_str(), "");
}


void AsyncTelegram::sendToChannel(const char* &channel, String &message, bool silent) {
if (sizeof(message) == 0)
return;
String param((char *)0);
param.reserve(512);
DynamicJsonDocument root(BUFFER_BIG);

root["chat_id"] = channel;
root["text"] = message;
if(silent)
root["silent"] = true;

serializeJson(root, param);
sendCommand("sendMessage", param.c_str());

#if DEBUG_MODE > 0
serialLog("SEND message:\n");
serializeJsonPretty(root, Serial);
serialLog("\n");
#endif
}


void AsyncTelegram::endQuery(const TBMessage &msg, const char* message, bool alertMode) {
if (sizeof(msg.callbackQueryID) == 0)
return;
Expand Down
20 changes: 15 additions & 5 deletions src/AsyncTelegram.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AsyncTelegram
// true if no error occurred
bool begin(void);


// reset the connection between ESP8266 and the telegram server (ex. when connection was lost)
// returns
// true if no error occurred
Expand All @@ -99,11 +99,18 @@ class AsyncTelegram
// (in json format or using the inlineKeyboard/ReplyKeyboard class helper)

void sendMessage(const TBMessage &msg, const char* message, String keyboard = "");
void sendMessage(const TBMessage &msg, String message, String keyboard = "");
void sendMessage(const TBMessage &msg, String &message, String keyboard = "");

void sendMessage(const TBMessage &msg, const char* message, InlineKeyboard &keyboard);
void sendMessage(const TBMessage &msg, const char* message, ReplyKeyboard &keyboard);


// Send message to a channel. This bot must be in the admin group
void sendToChannel(const char* &channel, String &message, bool silent) ;

// Send message to a specific user. In order to work properly two conditions is needed:
// - You have to find the userid (for example using the bot @JsonBumpBot https://t.me/JsonDumpBot)
// - User has to start your bot in it's own client. For example send a message with @<your bot name>
void sendToUser(const int32_t userid, String &message, String keyboard = "") ;

// terminate a query started by pressing an inlineKeyboard button. The steps are:
// 1) send a message with an inline keyboard
Expand Down Expand Up @@ -141,17 +148,20 @@ class AsyncTelegram
// pollingTime: interval time in milliseconds
void setUpdateTime(uint32_t pollingTime);

String userName ;

private:
char* m_token;
const char* m_token;
const char* m_botName;
int32_t m_lastUpdate = 0;
uint32_t m_lastUpdateTime;
uint32_t m_minUpdateTime = 2000;

bool m_useDNS = false;
bool m_UTF8Encoding = false;
uint8_t m_fingerprint[20];

TBUser m_user;

InlineKeyboard m_inlineKeyboard; // last inline keyboard showed in bot

// Struct for store telegram server reply and infos about it
Expand Down

0 comments on commit 752e698

Please sign in to comment.