telegram robot

telegram foreign commonly used chat function, function is very powerful, it can also play games in addition to chat, file transfer, video, voice, vote, group. Of course, those with qq, very similar to the micro-channel. But it is after all one of the most popular IM abroad. telegram had a chat robot can automatically send an announcement, it is also quite interesting. It also opened api, you can create your own bot, sending a message through the api, video, sound files and other functions. Under Here, I briefly introduce how to create a new bot

There are two telegram api, one is bot api, one is the telegram api. bot api is http-based access, telegram api is based mtproto access, access requires encryption, is relatively complicated. The latter may also be implemented to send messages and so on. Here I only said first bot api.

Start by creating a bot. Access
https://telegram.me/botfather
it will prompt you with a telegram open. Then you open the botfarther chat box. Input
/ newbot
Enter transmission. botfarther will feedback
Alright, a new bot. How are we going to call it? Please choose a name for your bot.

Enter the name you want to create bot. For example david_bot, send a carriage return

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

I then enter DavidBot. This is the name of the robot.

它会反馈
Sorry, this username is already taken. Please try something different.

This name has been used. We change

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

Here it generated api token. We note of it. After api request will be used. If you need help, input / help

Input / token can regenerate a token.
/ revoke a token can be revoked

We need to use this bot to send a message, you first need to create a group, add some people, but the bot will be added to the list. Then sends the message in this group. Similar / hello @GZ_David_Bot

Then visit
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
}
]
}
},
]
}

Here, we see a id, refers to the current group of id. We note of it. Then do the following curl.
botXXX: YYYY refers to the bot + token, be sure to add the prefix bot
chat_id is above id, attention is negative, there must be -
send content = my sample text

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

Executed, this group would receive the news.

Guess you like

Origin www.cnblogs.com/php-no-2/p/11207717.html