Telegram Bot API Server作弊条

老房东
7 min readDec 9, 2020

--

很久以前看到Telegram的更新说明里提到了Run Your Own Bot API Server这个话题,与清风老湿聊了聊,YY了一下这东西很牛,并没有任何想法真的去用用它。今天因为一个特别的原因,刚好用了一下,把一些东西留在这里防止以后忘了。

Photo by Todd Quackenbush on Unsplash

编译一个Bot API Server

对,你没看错,这货需要自己来编译,就是因为这,所以这前我没想试试,因为我的bot都跑在Raspberry PI上,这个编译在我的MBP上就花了十多分钟,想想用PI来编译就觉得蛋疼。

首先你可以访问一个很友好的页面,它会让你选择操作系统和编译选项来帮你生成一个编译环境的设置和编译步骤:

我在我的Mac上真是一路顺利,编译时间十多分钟的样子吧。其实编译后,这货就一个可执行文件。可以把这个文件复制到任何你喜欢的地方去,还是挺绿色的。

运行Bot API Server

运行需要先去 https://my.telegram.org ,登录后,点API development tools可以看到你的api-id和api-hash,这个要记下来,留在后面备用。

我们可以先试第一种启动(用上面的api-id和api-hash替换里面的<arg>):

telegram-bot-api --api-id=<arg> --api-hash=<arg>

接下来我们来试试发一个video(里面的<chat_id>和<token>做你的替换):

curl -v -F chat_id="<chat_id>" -F supports_streaming=true -F video="@my.mp4" -F caption="my.mp4" http://localhost:8081/bot<token>/sendVideo

注意,不要用https,要用http。这是传统把一个文件读出来,发到一个chat_id里。接下来我们启动local模式:

telegram-bot-api --api-id=<arg> --api-hash=<arg> --local

在官方的说法中,如果启动了local模式就可以:

  • Download files without a size limit.
  • Upload files up to 2000 MB.
  • Upload files using their local path and the file URI scheme.
  • Use an HTTP URL for the webhook.
  • Use any local IP address for the webhook.
  • Use any port for the webhook.
  • Set max_webhook_connections up to 100000.
  • Receive the absolute local path as a value of the file_path field without the need to download the file after a getFile request.

这里让我很喜欢的就是让bot可以发2G的文件和使用file URI了。对了,为了方便 ,我们可以给server加上log,可以使用-v参数,我到处没有找到文档,这个-v里应该是什么,后来找了代码的文档:

才看到这样的提示:

New value of the verbosity level for logging. Value 0 corresponds to fatal errors, value 1 corresponds to errors, value 2 corresponds to warnings and debug warnings, value 3 corresponds to informational, value 4 corresponds to debug, value 5 corresponds to verbose debug, value greater than 5 and up to 1023 can be used to enable even more logging.

所以你可以设置个4或5看到文件上传的log了(我建议3,刚好适合我们看到文件上传的状态,没有太多的无用信息)。最后我使用了这样的参数来启动API Server:

telegram-bot-api --api-id=<arg> --api-hash=<arg> --local -l /var/logs/tgserver.log -v 3

试着用local Server发Video

你可以用之前的代码发送2G的文件了:

curl -v -F chat_id="<chat_id>" -F video="@my.mp4" -F supports_streaming=true -F caption="my.mp4" http://localhost:8081/bot<token>/sendVideo

但是这样做,真的很蛋疼,因为你会发现会把先文件发到local server再由local server转去telegram server。所以你可以试试这个:

curl -v -F chat_id="<chat_id>" -F video="file:///home/hd/my.mp4" -F supports_streaming=true -F caption="my.mp4" http://localhost:8081/bot<token>/sendVideo

仔细看,就会发现运行后会返回这样的信息:

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8081 (#0)
> POST /bot<you_token>/sendVideo HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Length: 558
> Content-Type: multipart/form-data; boundary=------------------------f7dec8eebe228600
>
* We are completely uploaded and fine

这就说明,local server直接从本地取文件发送了。你可以tail下log,看到它在后台正在使劲的发送中。

tail -F tgserver.log 

备忘

其实看到We are completely uploaded and fine就可以^c,发送下一个文件了。local server总会发完。我看了下log,其实local server只会发送一个文件,不会同时发送多个。

如果你不断的提交文件,local并不会按你的提交顺序发送,是发送完当前文件后发送最新接到的请求(这个让我比较烦,其实我是想让它按我提交的顺序发送来着)。

--

--

老房东

Kayak浪 重剑配件残手 Python早期用户 FreeBSD远古鼓吹手 智能家居忽悠 苹果脑残粉