
今天配置好了strongswan,手机也可以科学上网了。为了进一步充分利用服务器,建了一个 Telegram 机器人,实现 LaTeX 公式转图片的小功能。服务器用 Nginx + Flask ,由于要求 HTTPS 连接,使用Let ’ s Encrypt申请免费 SSL 证书。
保持良好习惯,使用独立 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 找到机器人老爹@BotFather请求创建新 Bot ,输入指令/newbot,选好name和username之后,老爹会返回一串 Token :
Use this token to access the HTTP API: 187512456:A*****************-***************s 到这里就算申请完毕了,不需要备案审核,也不需要 300 块。关于 Bot 的说明和 API 文档可以从官方获取(About,API)。这里采用设定 WebHook 的方式,被动响应用户指令。
Telegram 要求设定的 WebHook 地址为 HTTPS ,因此需要申请 SSL 证书。
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 端口没有打开……
已经有 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。
根据官网配置即可:
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 shierji 2016-02-28 22:49:54 +08:00 ikev1 没啥意思啊 不如 ocserv |
2 edfward 2016-02-29 09:22:02 +08:00 感觉 slack 也挺适合做机器人的 |