The robot created Telegram

1. Create Telegram robot Bot

The first step :

Log Telegram, and find @BotFather, use / new bot command and take a nickname for your user name and follow the instructions Bot (Bot username necessary to end bot). At this point BotFather will give you a very important thing --Bot token, Take good care of it!

By BotFather commands, you can set your avatar as a robot, command, description, etc., as shown below.

Step two :

At this point you message to the robot, he will not return to you, because there is no written code behind to support his run. The following two links are defined in many ways Bot API provides all Bot can be called. https://core.telegram.org/bots/api and https://core.telegram.org/bots.

In fact, when our Bot can already get users to send messages, we Xianxiang Bot just send in something (of course, this time it will not reply), and then enter in your browser: https: //api.telegram. ORG / BOT (token) / getUpdates
(token) or above Please take into token. The browser will display our content and chat robot, here is provided by Telegram Bot platform getUpdates () method to get.

The third step :

有两种互相排斥的方式来接收你的机器人的更新,一方面是getUpdates方法,另一方面是webhook。传入的更新将存储在服务器上,直到机器人以任何一种方式接收它们,但是它们的保存时间不会超过24小时。这种方法虽然可以或取讯息,但是要做到Bot随时使用,频繁调用getUpdates方法会非常浪费资源。因此我们使用另一种方法设置Webhook,即向 Telegram 设定一组 callback url,只要有用户向你的Bot发送讯息,Telegram 就会把用户讯息连同元数据 传送到你设定的 url 。

Telegram要求callback url所在的服务器必需要有SSL证书,而我们目前并没有去搭建服务器。所以,打开浏览器,输入http://script.google.com 新建脚本。直接使用gs开发就可以啦,谷歌的服务器自然是有SSL证书的。关于Telegram Bot所支持的程序语言,虽然官方手册的案例上并没有gs,但我们打开GitHub,搜索telegram-bot-gscript,还是能找到案例的哦。如:

function doPost(e){
  var estringa = JSON.parse(e.postData.contents);
  var payload = identificar(estringa);
  var data = {
    "method": "post",
    "payload": payload
  }
  UrlFetchApp.fetch("https://api.telegram.org/botYOUR-API-HERE/", data);}
function identificar(e){
  if (e.message.text){
    var mensaje = {
      "method": "sendMessage",
      "chat_id": e.message.chat.id,
      "text": e.message.text,
    } 
  }
  else if (e.message.sticker){
    var mensaje = {
      "method": "sendSticker",
      "chat_id": e.message.chat.id,
      "sticker": e.message.sticker.file_id
    }
   }
  else if (e.message.photo){
    var array = e.message.photo;
    var text = array[1];
    var mensaje = {
      "method": "sendPhoto",
      "chat_id": e.message.chat.id,
      "photo": text.file_id
    }
   }
    else {
    var mensaje = {
      "method": "sendMessage",
      "chat_id": e.message.chat.id,
      "text": "Try other stuff"
    }
   }
  return mensaje}

把案例中的代码复制到上面新建的脚本当中,保存并点击左上角的发布-部署为网络应用,把权限改为任何人甚至匿名,确定。就得到了具有SSL凭证的callback url。

第四步

打开浏览器,把上面或取的token和callback url放到下边网址对应的位置
https://api.telegram.org/bot(token)/setwebhook?url=callback url。随后浏览器便会返回:

这时,给机器人发消息就可以发现,机器人会返回相同的内容。

第五步

本地写运行Bot的脚本文件。下载编译器,如VSCode。首先安装node-telegram-bot-api库,命令行中输入npm install node-telegram-bot-api,同时输入npm install config。在这里插入图片描述


我们开发的bot是怎么知道用户发送了哪些命令?

Telegram bot有两种获取用户发送命令的方式,一种是polling模式,也就是轮询。我们的bot需要每隔一段时间,就向Telegram服务器发送请求,询问最近用户发过来了哪些命令。这种方式的好处就是便于在本地调试,我们刚才的代码使用的就是这种模式。坏处就是每隔一段时间就要主动发送请求,即使最近可能没有任何用户发送命令。另外一种模式就是webhook,我们需要给bot设置一个webhook地址,比如说https://hentai.com/bot123。这样,每次当用户向bot输入命令时,Telegram就会把这次的命令转发到https://hentai.com/bot123,因此,我们需要在https://hentai.com/bot123部署我们的bot。这种模式的好处就是可以及时响应用户的命令,坏处就是本地调试麻烦,可能需要ngrock这种内网穿透工具。同时在线上部署时,我们还需要有自己的域名并且要支持https!!!

添加按钮、内联键盘



效果图如下:

发送漂亮的消息、粗体、斜体、表格、按钮

Telegram提供了两种解析模式,一种是Markdown和html,他们基本相同。可以设置回复的格式为Markdown,disable_web_page_preview :true,表示对于链接关闭提前预览模式。


下图是,对与上面定义的应用,加粗只对英文起作用。等宽字体很有用,可以用来编辑整齐的表格。

处理特定命令

回声机器人实现后,现在想要机器人能够处理特定的命令,该怎么办呢?可以在botFather中设置命令,之后在代码中写处理命令的逻辑。如下图,是代码处理特定命令。Body.message.text.indexOf(“/help”) === 0 这个判断条件表示,用户的输入是以/help 开头即可。注:如果用户输入的文本里面有空格,也没关系,telegram会自动解析处理。

下面是优化后的代码,去除冗余的代码。\n是换行,可以再消息显示中换行。当然也可以用switch,后面会介绍。

对于,其他语言,如python、java、php等可以到telegram 官网telegram 官网,查看更多的源代码,进行学习!

发布了26 篇原创文章 · 获赞 3 · 访问量 1395

Guess you like

Origin blog.csdn.net/Ydecube/article/details/104036176