Skip to content

Instantly share code, notes, and snippets.

@witnessmenow
Created August 19, 2021 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save witnessmenow/6d5e67c917cd7370841014d0adbc5492 to your computer and use it in GitHub Desktop.
Save witnessmenow/6d5e67c917cd7370841014d0adbc5492 to your computer and use it in GitHub Desktop.
/*******************************************************************
A telegram bot for your ESP32 that responds
with whatever message you send it.
Parts:
ESP32 D1 Mini stlye Dev board* - http://s.click.aliexpress.com/e/C6ds4my
(or any ESP32 board)
= Affilate
If you find what I do useful and would like to support me,
please consider becoming a sponsor on Github
https://github.com/sponsors/witnessmenow/
Written by Brian Lough
YouTube: https://www.youtube.com/brianlough
Tindie: https://www.tindie.com/stores/brianlough/
Twitter: https://twitter.com/witnessmenow
*******************************************************************/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <Thermal_Printer.h>
static uint8_t ucBuf[48 * 384];
#define WIDTH 384
#define HEIGHT 240
// Wifi network station credentials
#define WIFI_SSID "OnePlus7t"
#define WIFI_PASSWORD "easypass"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN ""
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
void printMessage(String message){
if (tpScan("MTP-2",5))
{
Serial.println((char *)"Found a printer!, connecting...");
if (tpConnect())
{
Serial.println((char *)"Testing plain text printing");
tpSetFont(0, 0, 0, 0, 0);
tpPrint((char *)message.c_str());
tpPrint((char *)"\r");
Serial.println((char *)"Disconnecting");
tpDisconnect();
Serial.println((char *)"Done!");
}
}
}
void handleNewMessages(int numNewMessages)
{
for (int i = 0; i < numNewMessages; i++)
{
printMessage(bot.messages[i].text);
//bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
}
}
void setup()
{
Serial.begin(115200);
Serial.println();
// attempt to connect to Wifi network:
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment