telegramruby

telegram机器人服务最简入门

2017-03-30  本文已影响1742人  6593443e12c1

0. 前言

本篇博客最基础要求

  1. 大家能翻墙
  2. 1点点英文基础

1. 概念

Telegram 是一个专注于安全和速度,基于云的移动和桌面消息应用。

Telegram Bot Architecture 机器人架构

着重看架构中的webhooks小节

2. 步骤

2.1 技术栈

全文全部采用ruby

2.2 基础环境

  1. 安装ruby

    参考 安装ruby

    > gem install bundler
    

  2. 下载示例代码

    > git clone https://github.com/watsy0007/telegram_webhook.git
    > cd telegram_webhook
    > bundle install
    > rackup config.ru
    

  3. 端口转发

    因为 ngrok 被墙的原因,使用 localtunnel 代替

    > npm install -g localtunnel
    > lt --port 9292 #返回远程https地址
    your url is: https://xxxxxxxxxxxxx.localtunnel.me
    

2.3 申请机器人

  1. 登录telegram

  2. 添加 @BotFather 帐号

  3. 创建机器人

    输入/newbot根据提示新建机器人

    watsy0007, [27.03.17 14:46]
    /newbot
    
    BotFather, [27.03.17 14:46]
    Alright, a new bot. How are we going to call it? Please choose a name for your bot.
    
    watsy0007, [27.03.17 14:46]
    project
    
    BotFather, [27.03.17 14:46]
    Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
    
    watsy0007, [27.03.17 14:46]
    watsy_program_bot
    
    BotFather, [27.03.17 14:46]
    Done! Congratulations on your new bot. You will find it at t.me/watsy_program_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:
    305067117:BBH5pDuY2ceOE0YAFvtxTajjOSmVJJUX-Ux
    
    For a description of the Bot API, see this page: https://core.telegram.org/bots/api
    
  4. 设置webhooks

    设置webhooks请求url格式为

    https://api.telegram.org/bot<**telegram_bot_token_from_botfather**>/setWebhook?url=callback_url
    

    替换 bot token和 callback_url(localtunnel返回地址)

    https://api.telegram.org/bot305067117:BBH5pDuY2ceOE0YAFvtxTajjOSmVJJUX-Ux/setWebhook?url=https://xxxxxxxxxxxxx.localtunnel.me
    

    复制到浏览器中访问得到

    {
    ok: true,
    result: true,
    description: "Webhook was set"
    }
    

2.4 测试

telegram中添加 @watsy_program_bot

随便发1句话

2.5 部署

=-=随意找个vps部署

配置dns解析以后记得配置https

上一篇 下一篇

猜你喜欢

热点阅读