Another unofficial Telegram adapter for nonebot2
Project description
nonebot-adapter-telegram
(施工中)自己用的非官方nonebot2 telegram adapter,代码全靠糊
开发中代码没有经过清理和优化,不能与官方版本共存
当前仅支持有限类型的消息解析和发送(接受私聊/群聊文字/图片,发送私聊/群聊文字/图片/语音,入群事件)
如果使用webhook工作方式需要公网ip或者frp
演示bot@aya_od_bot
使用方法
如果要试毒的话
真的要的话
pip install nonebot-adapter-antelegram
上路
一、新建项目文件夹
二、在nonebot2的配置文件中配置以下选项
bot_token=your_bot_token #telegram bot token,需要事先申请,参考https://core.telegram.org/bots#3-how-do-i-create-a-bot
telegram_bot_api_server_addr=https://api.telegram.org #可选,应该大概也可以替换为反代的域名,不设置默认官方
telegram_bot_api_proxy=proxy_server_addr #可选,代理服务器地址
#如果需要使用webhook方式接受消息,进行如下设置(推荐但是麻烦)
driver=~fastapi
host=127.0.0.1 # 配置 NoneBot 监听的 IP / 主机名
port=xxxxx # 配置 NoneBot 监听的端口
webhook_host=https://your_domain # 配置telegram webhook域名,由于telegram要求webhook地址必须为https,需要自行配置反向代理,也可以参考telegram文档自建本地bot api,本地api无需https
#如果需要长轮训方式接受消息,进行如下设置(推荐,长轮询比轮训接受消息更及时,资源占用更小)
driver=~httpx
telegram_polling_interval=0 #不使用轮训
telegram_long_polling_timeout=20 #长轮训超时时间
#如果需要轮训方式接受消息,进行如下设置(不推荐,仅建议调试网络时使用)
driver=~httpx
telegram_polling_interval=5 #轮训间隔
telegram_long_polling_timeout=0 #不使用长轮训
#注意:使用driver=~fastapi+~httpx时会使用httpx作为Driver并启动fastapi,用于需要轮训且同时启动fastapi服务器的情况
三、配置webhook反代(仅使用webhook方式需要)
将webhook域名解析到本机,用你喜欢的方式配置反代将webhook域名的流量转发到nonebot2的监听端口(如果不使用本地bot api)
四、安装redis(推荐)
为了更好的使用体验,我们推荐您安装redis以启用部分缓存功能,不使用redis会导致部分功能失效
五、开始写机器人(摸鱼)
已知问题(短时间内并不会解决)
仅支持接受有限种类的消息
仅支持发送有限种类的消息
有亿点点小bug
可能存在内存泄漏问题
最简单的例子
bot.py
import nonebot
from nonebot.adapters.telegram.adapter import Adapter
nonebot.init()
driver = nonebot.get_driver()
driver.register_adapter(Adapter)
nonebot.load_plugin("plugins.echo")
if __name__ == "__main__":
nonebot.run()
plugins/echo.py
from nonebot.plugin import on_command
from nonebot.adapters.telegram import Bot, MessageEvent, Message, MessageSegment
from nonebot.rule import to_me
echo = on_command("echo",to_me())
@echo.handle()
async def echo_escape(bot: Bot, event: MessageEvent):
await bot.send(event, event.get_message())
#await bot.send(event, "114514") #发送文字
#await bot.send(event, MessageSegment.photo(pic_url)) #发送图片 支持file:///,base64://,bytes,file_id,url(由Telegram服务器下载)
#
运行机器人,向bot私聊发送/echo 123,bot会将消息原样重新发送
从Onebot v11插件迁移
nonebridge是一个实验性玩具性项目,可以将本适配器简单的Telegram消息转换为Onebot v11消息直接分发给原先为Onebot v11适配器编写的插件,详情请参考该项目README。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for nonebot_adapter_antelegram-0.2.0.dev10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a34e4b7e23d3a224220a70932029ebd3c1ad08feeabdf4268d762a09812ce1e |
|
MD5 | 02b8fd7849bcdd055a591d8b4bf5ae0e |
|
BLAKE2b-256 | 7ff49302adcc1e722cd941dce1d0bae4be5f25f23eee7cfb7ce601e31d7095e5 |
Hashes for nonebot_adapter_antelegram-0.2.0.dev10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e806ce3338fca0745ba10da7ef9547c613388f0cad0ab02717c90b45243f4168 |
|
MD5 | 45be2239b5edb6b99fc07d2c62dd900e |
|
BLAKE2b-256 | 1071a28d88b3108edb49dda1fc8c73f97163e5f76596ec06648c6c1e885447e2 |