2026年2月21日 星期六

ESP32 Telegram Chat Action(聊天狀態) 的功能

ESP32 Telegram   Chat Action(聊天狀態) 的功能



//*******************************************************************/
/*******************************************************************
*  An example of bot that echos back any messages received,
*  including ones from channels
*                                                                          
*  written by Brian Lough
*******************************************************************/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

// Wifi network station credentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "7738940254:AAHbrWu9ovb1BKPQyWsbNSjNxfCGCrEWU-o"

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
bool Start = false;

void handleNewMessages(int numNewMessages)
{
  Serial.println("handleNewMessages");
  Serial.println(String(numNewMessages));

  for (int i = 0; i < numNewMessages; i++)
  {
    String chat_id = 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 == "/send_test_action")
    {
      bot.sendChatAction(chat_id, "typing");
      delay(4000);
      bot.sendMessage(chat_id, "Did you see the action message?");

      // You can't use own message, just choose from one of bellow

      //typing for text messages
      //upload_photo for photos
      //record_video or upload_video for videos
      //record_audio or upload_audio for audio files
      //upload_document for general files
      //find_location for location data

      //more info here - https://core.telegram.org/bots/api#sendchataction
    }

    if (text == "/start")
    {
      String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
      welcome += "This is Chat Action Bot example.\n\n";
      welcome += "/send_test_action : to send test chat action message\n";
      bot.sendMessage(chat_id, welcome);
    }
  }
}

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



這段程式碼是一個進階版的 Telegram 機器人範例,主要展示了 Chat Action(聊天狀態) 的功能(例如:顯示「對方正在輸入中...」)。

以下為您逐行解說:


1. 引入庫與基本參數設定

C++
#include <WiFi.h>              // ESP32 WiFi 連線庫
#include <WiFiClientSecure.h>  // HTTPS 加密傳輸庫(Telegram 規定使用)
#include <UniversalTelegramBot.h> // Telegram Bot 核心庫
  • WIFI_SSID: 設定為 Wokwi-GUEST,這是為了在 Wokwi 線上模擬器執行的設定。

  • BOT_TOKEN: 機器人的身份證,用來連接 Telegram 伺服器。

  • BOT_MTBS: 每隔 1000 毫秒(1秒)檢查一次新訊息,避免頻繁請求導致被伺服器封鎖。


2. 核心處理邏輯:handleNewMessages

當機器人收到訊息時,會進入這個函式處理:

C++
void handleNewMessages(int numNewMessages) {
  for (int i = 0; i < numNewMessages; i++) {
    String chat_id = bot.messages[i].chat_id; // 取得發送者的 ID
    String text = bot.messages[i].text;       // 取得訊息內容
    String from_name = bot.messages[i].from_name; // 取得發送者暱稱
    if (from_name == "") from_name = "Guest";   // 如果沒暱稱則顯示 Guest

    // --- 測試聊天動作功能 ---
    if (text == "/send_test_action") {
      bot.sendChatAction(chat_id, "typing"); // 在手機端顯示「機器人正在輸入中...」
      delay(4000); // 模擬處理過程,停 4 秒
      bot.sendMessage(chat_id, "Did you see the action message?"); // 發送正式訊息
    }

    // --- 起始畫面 ---
    if (text == "/start") {
      String welcome = "Welcome..., " + from_name + ".\n";
      welcome += "/send_test_action : to send test chat action message\n";
      bot.sendMessage(chat_id, welcome); // 傳送歡迎詞與指令列表
    }
  }
}

小知識sendChatAction 可以提升使用者體驗,讓使用者知道機器人正在處理(如:typing 正在輸入、upload_photo 正在上傳圖片)。


3. 初始化設定:setup

這部分只在開發板啟動時執行一次。

C++
void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD); // 連接 WiFi
  secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // 設定安全憑證

  while (WiFi.status() != WL_CONNECTED) { delay(500); } // 等待網路連線

  // 重要:取得正確的網路時間
  configTime(0, 0, "pool.ntp.org"); 
  time_t now = time(nullptr);
  while (now < 24 * 3600) { // 如果時間不正確,HTTPS 連線會失效
    delay(100);
    now = time(nullptr);
  }
}

4. 主迴圈:loop

程式會在這裡不斷循環運行。

C++
void loop() {
  // 檢查是否超過了設定的檢查間隔 (1秒)
  if (millis() - bot_lasttime > BOT_MTBS) {
    // 取得新訊息的數量,並更新訊息索引
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while (numNewMessages) { // 如果有新訊息
      handleNewMessages(numNewMessages); // 呼叫處理函式
      numNewMessages = bot.getUpdates(bot.last_message_received + 1); // 再次確認
    }
    bot_lasttime = millis(); // 更新計時器
  }
}

總結

這段程式碼比一般的 Echo Bot 多了:

  1. 使用者辨識:會抓取 from_name(使用者姓名)。

  2. 指令觸發:使用了 if (text == "/start") 這種結構來製作選單。

  3. 互動感:利用 sendChatAction 讓機器人看起來像「真人」在打字,這在處理耗時任務(如感測器讀取或圖片生成)時非常有用。

沒有留言:

張貼留言

ESP32 Telegram 指令選單(Bot Commands)

ESP32 Telegram 指令選單(Bot Commands)  /*******************************************************************  ***********************************...