hua0512 / stream-rec

Automatic streaming record tool. 虎牙/抖音/斗鱼/Twitch/PandaTV直播,弹幕自动录制
MIT License
390 stars 32 forks source link

Upload the records to telegram channel #67

Open a186232641 opened 1 month ago

garyvalue commented 5 days ago

做个记录 使用mproto API等其他第三方库上传大文件需使用普通账户或者高级账户,并有可能会遇到限速情况 建议使用 telegram-bot-api 自建本地bot-api 服务器上传 https://github.com/tdlib/telegram-bot-api

参考解决办法是 使用docker部署telegram-bot-api https://hub.docker.com/r/aiogram/telegram-bot-api 通过 http://localhost:8081 使用 curl 发送文件 示例 curl -F "chat_id=$CHAT_ID" -F "document=@$ARCHIVE_NAME" "$LOCAL_API_URL/bot$BOT_TOKEN/sendDocument"

garyvalue commented 4 days ago

关于Bot API的简要 Telegram Bot 通过 HTTP 请求与 Telegram 服务器进行通信。 Telegram Bot 的 API 是这个接口的规范,即一个 很长的列表,包括方法和数据类型,我们一般把它称为参考。 它定义了 Telegram Bot 所能做的一切。 你可以在资源标签下 Telegram 的部分找到它的链接。

请求的步骤可以被可视化为这样:

你的 bot <———HTTP———> Bot API <———MTProto———> Telegram 换句话说:当你的 bot 发送消息时,它将以 HTTP 请求的形式发送到一个 Bot API服务器。 这个服务器托管在 api.telegram.org。 它会把请求转换成 Telegram 的本地协议,称为 MTProto,并向 Telegram 的后端发送请求,后者负责将信息发送给用户。

类似地,每当用户返回响应时,会采取相反的路径。

当你运行你的 bot 时,你需要决定如何通过 HTTP 连接发送 update。 这可以通过 长轮询或 webhook来实现。

你也可以自己托管 Bot API 服务器。 这主要用于发送大文件或减少延迟

运行一个本地 Bot API 服务器 使用官方Bot API 不可以下载大小超过 20 MB 的文件也不能上传超过 50 MB 的文件。 一些文件类型有着更加严格的限制。 运行自己的 Bot API 服务器有两大优势。

你的 bot 可以发送和接收大文件。 你的 bot 可能减少了网络延迟

Bot API 服务器实际上并不包含你的 bot。 它只负责转发请求、在 HTTP 和 MTProto 之间进行转换等。 Bot API 服务器可能位于阿姆斯特丹,但 Telegram 服务器却分布在三个不同地点: 阿姆斯特丹,荷兰 迈阿密, 佛罗里达, 美国 新加坡 当 Bot API 服务器向 Telegram 服务器发送请求时,可能不得不绕半个地球发送数据。 这种情况是否发生取决于 bot 本身的数据中心。 bot 的数据中心与创建 bot 的用户的数据中心相同。 用户的数据中心取决于许多因素,包括用户所在位置。

其他优点 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 API Server 首先需要编译一个Bot API Server,首先你可以访问一个很友好的页面,它会让你选择操作系统和编译选项来帮你生成一个编译环境的设置和编译步骤,大概需要十几分钟 也可以使用docker镜像部署容器,例如 https://hub.docker.com/r/aiogram/telegram-bot-api

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

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

telegram-bot-api --api-id=<arg> --api-hash=<arg> 接下来我们来试试发一个video(里面的做你的替换):

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

你可以设置个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

local server只会发送一个文件,不会同时发送多个。

如果你不断的提交文件,local并不会按你的提交顺序发送,是发送完当前文件后发送最新接到的请求

上传视频文件 bot api上传视频需要自己指定视频缩略图,视频长宽,视频长度

参考文章 https://grammy.dev/zh/guide/api https://hdcola.medium.com/telegram-bot-api-server%E4%BD%9C%E5%BC%8A%E6%9D%A1-301d40bd65ba https://core.telegram.org/tdlib https://core.telegram.org/bots/api#available-methods https://blog.jialezi.net/?post=168