關於Telegram LongPolling Versus Webhook
在Telegram取得使用者訊息官方提供兩種方法。
1.LongPolling - getUpdates
2.Webhook
LongPolling - getUpdates
1 2
| Long Polling 是指程式間隔一定時間透過 getUpdates(上面所使用的方法)取得訊息, 缺點是浪費資源、不夠即時,所以適合在程式還沒有 deploy,在 develop 和 test 階段時使用。
|
Webhook
1 2
| Webhook 是指向 Telegram 設定一組 callback url,只要當使用者傳送訊息給 Chatbot, Telegram 就會把message連同 metada 透過 url 傳給 web server。適合在程式已經 deploy,有固定 url 的 production 環境使用。
|
Message with text using curl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| curl --tlsv1.2 -v -k -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{ "update_id":10000, "message":{ "date":1441645532, "chat":{ "last_name":"Test Lastname", "id":1111111, "first_name":"Test", "username":"Test" }, "message_id":1365, "from":{ "last_name":"Test Lastname", "id":1111111, "first_name":"Test", "username":"Test" }, "text":"/start" } }' "https://YOUR.BOT.URL:YOURPORT/"
|