建立telegram機器人教程

telegram是國外常用的聊天功能,功能很強大,除了聊天還可以玩游戲,傳檔案,視訊,聲音,投票,群組。 當然,這些跟qq,微信都很類似了。 但是它畢竟是國外最常用的IM之一。 telegram有個聊天機器人可以自動發訊息,也是挺有意思的。 它還開放了api,可以自己建立bot,通過api傳送訊息,視訊,聲音,檔案等功能。 下面,我簡單介紹下怎么新建一個bot

telegram有兩種api,一種是bot api,一種是telegram api。 bot api是基于http訪問,telegram api是基于mtproto訪問,訪問需要加密,相對要復雜一些。 后者也可以實現傳送訊息等功能。 這里我只說第一種bot api。

首先要建立一個bot。 訪問:https://telegram.me/botfather

它會提示你用telegram開啟。 然后你就開啟了botfarther的聊天對話框。 輸入

/newbot

回車傳送。 botfarther會反饋

Alright, a new bot. How are we going to call it? Please choose a name for your bot.

輸入你要建立的bot名字。 例如david_bot,回車傳送

Good. Now let’s choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.

再輸入 DavidBot。 這是這個機器人的名字。

Sorry, this username is already taken. Please try something different.

這名字已經被用了。 我們換一個

GZ_David_Bot

系統反饋:

BotFather, [16.02.17 14:23]
Done! Congratulations on your new bot. You will find it at t.me/Gz_David_Bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you’ve finished creating your cool bot, ping our
Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

Use this token to access the HTTP API:
xxx:xxx

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

這里它生成了api token。 我們記下它。 以后api請求都會用到。 如果需要幫助,輸入

/help

輸入/token 可以重新生成一個token。
/revoke 可以撤銷一個token

我們需要用這個bot來傳送訊息,首先需要建立一個group,加入一些人,同時將這個bot也加進去。 然后在這個group中傳送訊息。 類似 /hello @GZ_David_Bot

然后訪問
https://api.telegram.org/botxxx:xxx/getUpdates

我們會獲取到一個json

{
"ok" : true,
"result" : [{
"update_id" : xxx,
"message" : {
"message_id" : 4,
"from" : {
"id" : xxx,
"first_name" : "david",
"last_name" : "huang",
"username" : "davidhuang"
},
"chat" : {
"id" : -xxx,
"title" : "bot",
"type" : "group",
"all_members_are_administrators" : true
},
"date" : xxx,
"text" : "/hello @GZ_David_Bot",
"entities" : [{
"type" : "bot_command",
"offset" : 0,
"length" : 6
}
]
}
},
]
}

這里,我們看到有個id,指的就是當前group的id。 我們記下它 。 然后執行以下curl。
botXXX:YYYY 指的是bot token,一定要加上bot前綴
chat_id就是上面的id,注意是負數,必須有-

傳送內容是=my sample text

curl -X POST “https://api.telegram.org/botXXX:YYYY/sendMessage” -d “chat_id=-zzzzzzzzzz&text=my sample text”

執行完,這個group就收到訊息了。

api git:
https://github.com/unreal4u/telegram-api/graphs/contributors
但是悲劇的是,它只支持php7.
https://github.com/unreal4u/telegram-api/wiki

另外,也可以通過webhook api獲取聊天記錄等功能。 但是這個api要求我們的站點必須是https證書

還有個bot sdk,它只要php5.5就可以了
https://telegram-bot-sdk.readme.io/reference#forwardmessage
https://github.com/irazasyed/telegram-bot-sdk
https://telegram-bot-sdk.readme.io/v2.0/reference
sdk 文件:
https://telegram-bot-sdk.readme.io/docs
安裝
composer require irazasyed/telegram-bot-sdk

這個sdk包含了所有的telegram bot api,例如支援command,keyboard,webhook

參考文件:
https://fullmeter.com/blog/?p=14
http://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id-ruby-gem-telegram-bot
https://core.telegram.org/bots
https://core.telegram.org/bots/api

贊(0)
版權聲明:本文采用知識共享 署名4.0國際許可協議 [BY-NC-SA] 進行授權, 轉載請注明出處。
文章名稱:《建立telegram機器人教程》
文章鏈接:http://www.lrracing.com/11821.html
【聲明】:國外主機測評僅分享信息,不參與任何交易,也非中介,所有內容僅代表個人觀點,均不作直接、間接、法定、約定的保證,讀者購買風險自擔。一旦您訪問國外主機測評,即表示您已經知曉并接受了此聲明通告。
【關于安全】:任何 IDC商家都有倒閉和跑路的可能,備份永遠是最佳選擇,服務器也是機器,不勤備份是對自己極不負責的表現,請保持良好的備份習慣。