V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
irainy
V2EX  ›  分享创造

Python 自建 Telegram Bot

  •  1
     
  •   irainy · 2016-02-28 20:11:48 +08:00 · 1269 次点击
    这是一个创建于 2992 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原文: Python 自建 Telegram Bot

    今天配置好了strongswan,手机也可以科学上网了。为了进一步充分利用服务器,建了一个 Telegram 机器人,实现 LaTeX 公式转图片的小功能。服务器用 Nginx + Flask ,由于要求 HTTPS 连接,使用Let ’ s Encrypt申请免费 SSL 证书。

    1. Python3 虚拟环境

    保持良好习惯,使用独立 Python 虚拟环境:

    python3 -m venv venv3
    source venv3/bin/activate
    pip install flask
    pip install uwsgi
    
    # Ubuntu 14.04 系统有问题,导致 apt-get install python3-venv 找不到
    # 如下方法解决
    python3 -m venv --without-pip venv3
    source venv3/bin/activate
    curl https://bootstrap.pypa.io/get-pip.py | python
    deactivate
    source venv3/bin/activate
    

    2. 创建 Telegram Bot

    找到机器人老爹@BotFather请求创建新 Bot ,输入指令/newbot,选好nameusername之后,老爹会返回一串 Token :

    Use this token to access the HTTP API:
    187512456:A*****************-***************s
    

    到这里就算申请完毕了,不需要备案审核,也不需要 300 块。关于 Bot 的说明和 API 文档可以从官方获取(AboutAPI)。这里采用设定 WebHook 的方式,被动响应用户指令。

    Telegram 要求设定的 WebHook 地址为 HTTPS ,因此需要申请 SSL 证书。

    3. Let's Encrypt

    git clone https://github.com/letsencrypt/letsencrypt
    cd letsencrypt
    sudo chmod g+x letsencrypt-auto
    
    # Nginx 
    ./letsencrypt-auto certonly --email=[email protected] -d YOURDOMAIN.COM -d SUB.YOURDOMAIN.COM
    

    要求服务器 IP 与域名指向的 IP 一致,刚开始一直返回下面的错误:

    IMPORTANT NOTES:
     - The following errors were reported by the server:
    
       Domain: *.*.*
       Type:   connection
       Detail: Failed to connect to host for DVSNI challenge
    

    后来发现是 443 端口没有打开……

    4. Flask & telegram

    已经有 Python 封装好的 telegram API :python-telegram-bot,直接下载:

    pip install python-telegram-bot
    

    设置 WebHook 地址:

    import telegram
    bot = telegram.Bot(token = "TOKEN")
    bot.setWebhook("https://webhook.url")
    

    我的@MathModeBot代码在https://github.com/rainyear/MathModeBot

    5. 配置 Nginx + uwsgi + Flask

    根据官网配置即可:

    server {
      listen 443;
      server_name example.com;
    
      ssl on;
      ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    
      location / { try_files $uri @yourapplication; }
      location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi.sock;
      }
    }
    

    启动 uwsgi :

    uwsgi -s /tmp/uwsgi.sock -w main:app --logformat="%(method) %(uri) %(uagent)"
    

    参考

    1. Setup LetsEncrypt On Linux
    2. Simple-Echo-Telegram-Bot
    3. venv doesn't create activate script python3
    第 1 条附言  ·  2016-02-28 21:50:30 +08:00
    添加了一个 `/fuli` 指令~
    2 条回复    2016-02-29 09:22:02 +08:00
    shierji
        1
    shierji  
       2016-02-28 22:49:54 +08:00
    ikev1 没啥意思啊 不如 ocserv
    edfward
        2
    edfward  
       2016-02-29 09:22:02 +08:00
    感觉 slack 也挺适合做机器人的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   982 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:40 · PVG 05:40 · LAX 14:40 · JFK 17:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.