Hỏi đáp về Telegram Bot

  1. Làm thế nào để tạo Telegram Bot?
  2. Telegram Bot làm việc như thế nào?
  3. Điều kiện để có thể tạo đường dây nóng webhook?
  4. SSL certificate và self-signed certificate
  5. Đăng ký webhook với Telegram server như thế nào?
  6. Đăng ký nhiều webhook trên một webserver
  7. Viết script cho webhook thế nào?
  8. Chat id


1. Làm thế nào để tạo Telegram Bot?

Gởi yêu cầu cho BotFather.

BotFather là một bot dịch vụ được Telegram tạo sẵn, gõ /help để biết các lệnh sai khiến BotFather, gõ /newbot để tạo một Telegram Bot

You can control me by sending these commands:

/newbot – create a new bot
/mybots – edit your bots [beta]
/mygames – edit your games (https://core.telegram.org/bots/games) [beta]

Edit Bots
/setname – change a bot’s name
/setdescription – change bot description
/setabouttext – change bot about info
/setuserpic – change bot profile photo
/setcommands – change the list of commands
/deletebot – delete a bot

Bot Settings
/token – generate authorization token
/revoke – revoke bot access token
/setinline – toggle inline mode (https://core.telegram.org/bots/inline)
/setinlinegeo – toggle inline location requests (https://core.telegram.org/bots/inline#location-based-results)
/setinlinefeedback – change inline feedback (https://core.telegram.org/bots/inline#collecting-feedback) settings
/setjoingroups – can your bot be added to groups?
/setprivacy – toggle privacy mode (https://core.telegram.org/bots#privacy-mode) in groups

Games
/newgame – create a new game (https://core.telegram.org/bots/games)
/listgames – get a list of your games
/editgame – edit a game
/deletegame – delete an existing game

2. Telegram Bot làm việc như thế nào?

  • Người dùng gởi thông điệp từ ứng dụng Telegram (hay từ ứng dụng của mình, xem thí dụ gởi bằng curl bên dưới)
  • Telegram server lưu thông điệp, thông tin người gởi, người nhận…
  • Bot hoặc định kỳ kiểm tra với Telegram server xem có tin mới không để tải về, hoặc thiết kế một đường dây nóng Webhook để Telegram server tự động gởi thông tin cho Bot mỗi khi có tin mới
  • Bot tiếp nhận thông tin và xử lý theo chương trình đã định trước, sau đó gởi hồi đáp.

3. Điều kiện để có thể tạo đường dây nóng webhook?

  • Có domain động hay tĩnh trỏ đến máy chủ web
  • Có giao thức SSL
  • Có script để tiếp nhận và xử lý thông tin từ Telegram server

4. SSL certificate và self-signed certificate

Ngoài các chứng chỉ CA do các tổ chức chứng thực cấp, Telegram chấp nhận các chứng chỉ CA tự cấp do ứng dụng openssl tạo ra trên máy người dùng.

openssl req -newkey rsa:2048 -sha256 -nodes -keyout /path/to/nginx.key -x509 -days 365 -out /path/to/nginx.crt -subj "/C=VN/ST=HCM/L=HCM City/O=LNT Company/CN=your_domain"

5. Đăng ký webhook với Telegram server như thế nào?

Mỗi bot được Telegram cung cấp một định danh gọi là token có dạng user:password

Đăng ký webhook bằng cách mở url trên browser hay chạy lệnh curl trên RPi

  • https://api.telegram.org/your_token/setWebhook?url=https://your_domain/your_bot.php&certificate=@your_ssl_certificate
  • curl -4 -F ‘url=your_domain/your_bot.php’ -F ‘certificate=@your_ssl_certificate’ https://api.telegram.org/your_token/setWebhook

Nếu webserver đã có SSL rồi (thí dụ dùng Let’s Encrypt) thì không cần tham số -F ‘certificate=your_ssl_certificate’ 

6. Đăng ký nhiều webhook trên một webserver

Có thể đăng ký nhiều webhook để xử lý các luồng chat khác nhau, hay các công việc khác nhau.

  • Telegram chấp nhận 2 port ssl là 4438443, vì vậy có thể đăng ký 2 webhook tương ứng với 2 port này. Tất nhiên webserver phải cấu hình ssl trên cả hai port này.
  • Dùng nhiều domain name cho webserver, mỗi domain name trỏ đến một virtual host. Vậy có bao nhiêu domain name thì có thể tạo bấy nhiêu webhook. Nếu dùng Let’s Encrypt thì chỉ được cấp CA cho tối đa 4 domain của một website.

7. Viết script cho webhook thế nào?

Script có thể viết bằng nhiều ngôn ngữ khác nhau.

Telegram server gởi đến script một chuỗi JSON. Script phải có thể nhận dữ liệu thô và giải mã chuỗi JSON để lấy các thông tin cần thiết.

Sau đây là minh họa bằng PHP.

$content = file_get_contents('php://input');
$update = json_decode($content, true);
$msg_text = $update['message']['text'];
$chat_id = $update['message']['chat']['id'];

Từ chuỗi JSON $content mà Telegram server gởi đến script, ta đã trích ra được $msg_text$chat_id. Xử lý $msg_text thế nào tùy theo nhu cầu và tạo ra chuỗi hồi báo $data để gởi về $chat_id

# Chuẩn bị hồi báo qua curl
$cmd = 'https://api.telegram.org/your_token/sendMessage';
$postFields = ['chat_id'=>$chat_id, 'parse_mode'=>'Markdown', 'text'=>$data];
# Gởi hồi báo bằng curl
$ch = curl_init($cmd);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_exec($ch);
curl_close($ch)

8. Chat Id

Chat Id được hiểu là id của một luồng chat, giúp 2 user gởi message về đúng nơi. Tuy nhiên, Telegram chỉ tạo một luồng chát giữa 2 user nên chat id là một hằng số chỉ phụ thuộc vào 2 user.

  • Nếu dùng Bot để tự động thông báo một chiều Bot –> User cho user cố định thì chỉ cần biết chat id, không cần thiết kế và đăng ký webhook cho Bot (khỏi tốn port SSL và domain name cho webhook). Thí dụ khi cctv phát hiện có người qua lại thì gởi thông báo cho chủ sở hữu.
  • Nếu dùng Bot để trả lời một yêu cầu của user: User –> Bot –> User (thí dụ như Bot từ điển), khi đó webhook có vai trò tra cứu CSDL để trả lại ngữ nghĩa của từ được user hỏi, trường hợp này cần phân tích chat_id từ dữ liệu Telegram server gởi cho webhook để trả lời đúng đối tượng.

Chú thích:

Tham khảo Bài Telegram và Bài Telegram Bot

Comments Off on Hỏi đáp về Telegram Bot

Filed under Software

Comments are closed.