สามารถอ่านเพิ่มเติมได้ที่บทความนี้ @YuTTYL
ESP32 library for sending and receiving Telegram messages.
You can install this library directly through the Arduino Library Manager. Follow these steps:
- Open the Arduino IDE.
- Navigate to Sketch > Include Library > Manage Libraries....
- In the Library Manager, search for "TelegramESP32".
- Click on the library in the list.
- Click the "Install" button.
- ArduinoJson
- WiFiClientSecure
- HTTPClient
#include <Arduino.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include "TelegramESP32.h"
const char* deviceName = "WeatherStation#1";
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* BOT_TOKEN = "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11";
const char* CHAT_ID = "-100xxxxxxxxxxxx";
TelegramESP32 telegram(BOT_TOKEN);
void handleMessage(String& message) {
if (message == "/temp") {
float temp = 25.5;
telegram.sendMessage(String(deviceName) +"\nTemperature: " + String(temp) + "°C");
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
telegram.begin();
telegram.setMessageCallback(handleMessage);
telegram.addChat(CHAT_ID, TelegramESP32::CHANNEL, "Notification");
// telegram.addChat("123456", TelegramESP32::PRIVATE, "Admin");
// telegram.addChat("-200xxx", TelegramESP32::GROUP, "Team");
char wifiIP[50];
snprintf(wifiIP, sizeof(wifiIP), "Device Name: %s\nIP: %s", deviceName, WiFi.localIP().toString().c_str());
telegram.sendMessageToChat("Notification",wifiIP);
}
void loop() {
telegram.loop();
}addChat(id, name): Add chat for sending messagessendMessage(message): Send to first added chatsendMessageToChat(name, message): Send to specific chatbroadcast(message): Send to all chatssetMessageCallback(callback): Handle incoming messagessetMessageInterval(ms): Set minimum interval between messages
MAX_CHATS: 5 chats maximumBUFFER_SIZE: 256 bytes message buffer- Default message interval: 1000ms