奇诺分享 | ccino.org

  • 首页
  • VPS
    • VPS申请
    • VPS配置
    • 科学上网
  • 网站建设
    • WordPress
  • 程序猿
    • 开发工具
    • 微服务
    • 容器
    • 分布式
    • 数据库
  • 杂项
  • 关于
  • Privacy Policy
生活不只是眼前的苟且,还有诗和远方!
  1. 首页
  2. 未分类
  3. 正文

创建telegram 机器人

2017年12月19日 5209点热度 2人点赞 10条评论

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

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

这里,我们看到有个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

本文出自:http://blog.csdn.net/mochong/article/details/67636712

相关文章

  • 电影菜鸟入门教程

  • 想在数字时代少接几个推销电话,你需要记住 18 件事

  • 使用 VPS 搭建自己的 Telegram RSS 中文订阅机器人

  • 各平台全面解决 Chrome 浏览器太占内存问题无敌方案

  • 免费申请Google Voice美国电话号码

标签: Telegram
最后更新:2018年2月25日

奇诺分享 | ccino.org

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

  • 中青看点是真的吗

    文章大有看点,支持!

    2017年12月19日
    回复
    • 奇诺分享 | ccino.org

      @中青看点是真的吗 谢谢,大家觉得有用就好。

      2017年12月20日
      回复
      • 只若初见

        @奇诺分享 | ccino.org 你好能帮我写个机器人吗,有相应报酬

        2018年2月6日
        回复
        • 奇诺分享 | ccino.org

          @只若初见 您好:要让您失望了,最近事情太多没精力哦~~~

          2018年2月6日
          回复
      • 追风

        @奇诺分享 | ccino.org 没搞明白 卡住了

        2018年2月19日
        回复
        • 奇诺分享 | ccino.org

          @追风 新年好,到哪一步卡住了?过年在外,估计帮不了你什么。^_^

          2018年2月19日
          回复
  • 夏日博客

    还真没用过 telegram。

    2017年12月20日
    回复
    • 奇诺分享 | ccino.org

      @夏日博客 恩,我最近是想找个平台发送网站告警,所以研究了下telegram。

      2017年12月20日
      回复
  • 菏泽夜生活论坛

    博客大好,让人忘不了!

    2017年12月28日
    回复
    • 奇诺分享 | ccino.org

      @菏泽夜生活论坛 谢谢,您觉得有用就好。

      2017年12月28日
      回复
  • razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
    取消回复

    此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据。

    COPYRIGHT © 2022 奇诺分享 | ccino.org. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang