telegram自动发信息_创建telegram 机器人 并发送消息

使用telegram的原因:利用该app的机器人定时发一些报表或者一些预警信息

telegram是国外常用的聊天工具,功能很强大,除了聊天还可以玩游戏,传文件,视频,声音,投票,群组。telegram有个聊天机器人可以自动发消息,也是挺有意思的。它还开放了api,可以自己创建bot,通过api发送消息,视频,声音,文件等功能。

telegram有两种api,一种是bot api,一种是telegram api。bot api是基于http访问,telegram api是基于mtproto访问,访问需要加密,相对要复杂一些。下面主要介绍一下bot api的这种方式通过http直接发消息到制定的群内

先要创建一个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.

我再输入 wmsgBot。这是这个机器人的名字。

它会反馈

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

这名字已经被用了。我们换一个

xxx_wmsg_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中发送消息。随便多发几条,等下通过接口直接获取一下,拿一下群id。拿到群id后就可以用程序自动发消息了。

然后访问

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",

"entities" : [{

"type" : "bot_command",

"offset" : 0,

"length" : 6

}

]

}

},

]

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

{

"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",

"entities":[{

"type":"bot_command",

"offset":0,

"length":6

}

]

}

},

]

}

这里,我们看到有个id,指的就是当前group的id。我们记下它 。然后执行以下curl。

botXXX:YYYY 指的是bot+token,一定要加上bot前缀

chat_id就是上面的id,注意是负数,必须有-

发送内容是=send msg

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

1

curl-XPOST"https://api.telegram.org/botXXX:YYYY/sendMessage"-d"chat_id=-zzzzzzzzzz&text=send msg"

或者在浏览器地址输入:

https://api.telegram.org/botXXX:YYYY/sendMessage?chat_id=-zzzzzzzzzz&text=send msg

执行完,这个group就收到消息了。

api git:

https://github.com/unreal4u/telegram-api/graphs/contributors 只支持php7.

https://github.com/unreal4u/telegram-api/wiki

Post Views:

1,634

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值