# nonebot-adapter-telegram
(施工中)自己用的非官方nonebot2 telegram adapter,代码全靠糊
开发中代码没有经过清理和优化,不能与官方版本共存
当前仅支持有限类型的消息解析和发送(接受私聊/群聊文字/图片,发送私聊/群聊文字/图片/语音,入群事件)
如果使用webhook工作方式需要公网ip或者frp
演示bot[@aya_od_bot](https://t.me/aya_od_bot)
## 使用方法
如果要试毒的话
真的要的话
```shell
pip install nonebot-adapter-antelegram
```
## 上路
一、新建项目文件夹
二、在nonebot2的配置文件中配置以下选项
```shell
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
```python
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
```python
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](https://github.com/ColdThunder11/nonebridge)是一个实验性~~玩具性~~项目,可以将本适配器简单的Telegram消息转换为Onebot v11消息直接分发给原先为Onebot v11适配器编写的插件,详情请参考该项目README。
Raw data
{
"_id": null,
"home_page": "https://github.com/ColdThunder11/nonebot-adapter-telegram",
"name": "nonebot-adapter-antelegram",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8.1,<4.0.0",
"maintainer_email": "",
"keywords": "bot,telegram",
"author": "ColdThunder11",
"author_email": "lslyj27761@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e6/29/4b2a1750ef38dacf7d25907ee6541959b17f723623474e9a7019f4a1e066/nonebot_adapter_antelegram-0.2.0.dev12.tar.gz",
"platform": null,
"description": "# nonebot-adapter-telegram\n\uff08\u65bd\u5de5\u4e2d\uff09\u81ea\u5df1\u7528\u7684\u975e\u5b98\u65b9nonebot2 telegram adapter\uff0c\u4ee3\u7801\u5168\u9760\u7cca \n\u5f00\u53d1\u4e2d\u4ee3\u7801\u6ca1\u6709\u7ecf\u8fc7\u6e05\u7406\u548c\u4f18\u5316\uff0c\u4e0d\u80fd\u4e0e\u5b98\u65b9\u7248\u672c\u5171\u5b58 \n\u5f53\u524d\u4ec5\u652f\u6301\u6709\u9650\u7c7b\u578b\u7684\u6d88\u606f\u89e3\u6790\u548c\u53d1\u9001\uff08\u63a5\u53d7\u79c1\u804a/\u7fa4\u804a\u6587\u5b57/\u56fe\u7247\uff0c\u53d1\u9001\u79c1\u804a/\u7fa4\u804a\u6587\u5b57/\u56fe\u7247/\u8bed\u97f3\uff0c\u5165\u7fa4\u4e8b\u4ef6\uff09 \n\u5982\u679c\u4f7f\u7528webhook\u5de5\u4f5c\u65b9\u5f0f\u9700\u8981\u516c\u7f51ip\u6216\u8005frp \n\u6f14\u793abot[@aya_od_bot](https://t.me/aya_od_bot) \n## \u4f7f\u7528\u65b9\u6cd5\n\u5982\u679c\u8981\u8bd5\u6bd2\u7684\u8bdd \n\u771f\u7684\u8981\u7684\u8bdd \n```shell\npip install nonebot-adapter-antelegram\n```\n## \u4e0a\u8def\n\u4e00\u3001\u65b0\u5efa\u9879\u76ee\u6587\u4ef6\u5939 \n\u4e8c\u3001\u5728nonebot2\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u4ee5\u4e0b\u9009\u9879 \n```shell\nbot_token=your_bot_token #telegram bot token\uff0c\u9700\u8981\u4e8b\u5148\u7533\u8bf7\uff0c\u53c2\u8003https://core.telegram.org/bots#3-how-do-i-create-a-bot\ntelegram_bot_api_server_addr=https://api.telegram.org #\u53ef\u9009\uff0c\u5e94\u8be5\u5927\u6982\u4e5f\u53ef\u4ee5\u66ff\u6362\u4e3a\u53cd\u4ee3\u7684\u57df\u540d\uff0c\u4e0d\u8bbe\u7f6e\u9ed8\u8ba4\u5b98\u65b9\ntelegram_bot_api_proxy=proxy_server_addr #\u53ef\u9009\uff0c\u4ee3\u7406\u670d\u52a1\u5668\u5730\u5740\n\n#\u5982\u679c\u9700\u8981\u4f7f\u7528webhook\u65b9\u5f0f\u63a5\u53d7\u6d88\u606f\uff0c\u8fdb\u884c\u5982\u4e0b\u8bbe\u7f6e\uff08\u63a8\u8350\u4f46\u662f\u9ebb\u70e6\uff09\ndriver=~fastapi\nhost=127.0.0.1 # \u914d\u7f6e NoneBot \u76d1\u542c\u7684 IP / \u4e3b\u673a\u540d \nport=xxxxx # \u914d\u7f6e NoneBot \u76d1\u542c\u7684\u7aef\u53e3 \nwebhook_host=https://your_domain # \u914d\u7f6etelegram webhook\u57df\u540d\uff0c\u7531\u4e8etelegram\u8981\u6c42webhook\u5730\u5740\u5fc5\u987b\u4e3ahttps\uff0c\u9700\u8981\u81ea\u884c\u914d\u7f6e\u53cd\u5411\u4ee3\u7406\uff0c\u4e5f\u53ef\u4ee5\u53c2\u8003telegram\u6587\u6863\u81ea\u5efa\u672c\u5730bot api\uff0c\u672c\u5730api\u65e0\u9700https \n\n#\u5982\u679c\u9700\u8981\u957f\u8f6e\u8bad\u65b9\u5f0f\u63a5\u53d7\u6d88\u606f\uff0c\u8fdb\u884c\u5982\u4e0b\u8bbe\u7f6e\uff08\u63a8\u8350\uff0c\u957f\u8f6e\u8be2\u6bd4\u8f6e\u8bad\u63a5\u53d7\u6d88\u606f\u66f4\u53ca\u65f6\uff0c\u8d44\u6e90\u5360\u7528\u66f4\u5c0f\uff09\ndriver=~httpx\ntelegram_polling_interval=0 #\u4e0d\u4f7f\u7528\u8f6e\u8bad\ntelegram_long_polling_timeout=20 #\u957f\u8f6e\u8bad\u8d85\u65f6\u65f6\u95f4\n\n#\u5982\u679c\u9700\u8981\u8f6e\u8bad\u65b9\u5f0f\u63a5\u53d7\u6d88\u606f\uff0c\u8fdb\u884c\u5982\u4e0b\u8bbe\u7f6e\uff08\u4e0d\u63a8\u8350\uff0c\u4ec5\u5efa\u8bae\u8c03\u8bd5\u7f51\u7edc\u65f6\u4f7f\u7528\uff09\ndriver=~httpx\ntelegram_polling_interval=5 #\u8f6e\u8bad\u95f4\u9694\ntelegram_long_polling_timeout=0 #\u4e0d\u4f7f\u7528\u957f\u8f6e\u8bad\n\n#\u6ce8\u610f\uff1a\u4f7f\u7528driver=~fastapi+~httpx\u65f6\u4f1a\u4f7f\u7528httpx\u4f5c\u4e3aDriver\u5e76\u542f\u52a8fastapi\uff0c\u7528\u4e8e\u9700\u8981\u8f6e\u8bad\u4e14\u540c\u65f6\u542f\u52a8fastapi\u670d\u52a1\u5668\u7684\u60c5\u51b5\n\n```\n\u4e09\u3001\u914d\u7f6ewebhook\u53cd\u4ee3\uff08\u4ec5\u4f7f\u7528webhook\u65b9\u5f0f\u9700\u8981\uff09 \n\u5c06webhook\u57df\u540d\u89e3\u6790\u5230\u672c\u673a\uff0c\u7528\u4f60\u559c\u6b22\u7684\u65b9\u5f0f\u914d\u7f6e\u53cd\u4ee3\u5c06webhook\u57df\u540d\u7684\u6d41\u91cf\u8f6c\u53d1\u5230nonebot2\u7684\u76d1\u542c\u7aef\u53e3\uff08\u5982\u679c\u4e0d\u4f7f\u7528\u672c\u5730bot api\uff09 \n\u56db\u3001\u5b89\u88c5redis\uff08\u63a8\u8350\uff09 \n\u4e3a\u4e86\u66f4\u597d\u7684\u4f7f\u7528\u4f53\u9a8c\uff0c\u6211\u4eec\u63a8\u8350\u60a8\u5b89\u88c5redis\u4ee5\u542f\u7528\u90e8\u5206\u7f13\u5b58\u529f\u80fd\uff0c\u4e0d\u4f7f\u7528redis\u4f1a\u5bfc\u81f4\u90e8\u5206\u529f\u80fd\u5931\u6548 \n\u4e94\u3001\u5f00\u59cb\u5199\u673a\u5668\u4eba\uff08\u6478\u9c7c\uff09 \n\n## \u5df2\u77e5\u95ee\u9898\uff08\u77ed\u65f6\u95f4\u5185\u5e76\u4e0d\u4f1a\u89e3\u51b3\uff09 \n\u4ec5\u652f\u6301\u63a5\u53d7\u6709\u9650\u79cd\u7c7b\u7684\u6d88\u606f \n\u4ec5\u652f\u6301\u53d1\u9001\u6709\u9650\u79cd\u7c7b\u7684\u6d88\u606f \n\u6709\u4ebf\u70b9\u70b9\u5c0fbug \n~~\u53ef\u80fd\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898~~ \n\n## \u6700\u7b80\u5355\u7684\u4f8b\u5b50\nbot.py\n```python\nimport nonebot\nfrom nonebot.adapters.telegram.adapter import Adapter\n\nnonebot.init()\ndriver = nonebot.get_driver()\ndriver.register_adapter(Adapter)\nnonebot.load_plugin(\"plugins.echo\")\n\nif __name__ == \"__main__\":\n nonebot.run() \n```\nplugins/echo.py\n```python\nfrom nonebot.plugin import on_command\nfrom nonebot.adapters.telegram import Bot, MessageEvent, Message, MessageSegment\nfrom nonebot.rule import to_me\n\necho = on_command(\"echo\",to_me())\n\n@echo.handle()\nasync def echo_escape(bot: Bot, event: MessageEvent):\n await bot.send(event, event.get_message())\n\n#await bot.send(event, \"114514\") #\u53d1\u9001\u6587\u5b57\n#await bot.send(event, MessageSegment.photo(pic_url)) #\u53d1\u9001\u56fe\u7247 \u652f\u6301file:///\uff0cbase64://\uff0cbytes\uff0cfile_id\uff0curl(\u7531Telegram\u670d\u52a1\u5668\u4e0b\u8f7d) \n#\n```\n\u8fd0\u884c\u673a\u5668\u4eba\uff0c\u5411bot\u79c1\u804a\u53d1\u9001/echo 123\uff0cbot\u4f1a\u5c06\u6d88\u606f\u539f\u6837\u91cd\u65b0\u53d1\u9001 \n\n## \u4eceOnebot v11\u63d2\u4ef6\u8fc1\u79fb\n[nonebridge](https://github.com/ColdThunder11/nonebridge)\u662f\u4e00\u4e2a\u5b9e\u9a8c\u6027~~\u73a9\u5177\u6027~~\u9879\u76ee\uff0c\u53ef\u4ee5\u5c06\u672c\u9002\u914d\u5668\u7b80\u5355\u7684Telegram\u6d88\u606f\u8f6c\u6362\u4e3aOnebot v11\u6d88\u606f\u76f4\u63a5\u5206\u53d1\u7ed9\u539f\u5148\u4e3aOnebot v11\u9002\u914d\u5668\u7f16\u5199\u7684\u63d2\u4ef6\uff0c\u8be6\u60c5\u8bf7\u53c2\u8003\u8be5\u9879\u76eeREADME\u3002",
"bugtrack_url": null,
"license": "MIT",
"summary": "Another unofficial Telegram adapter for nonebot2",
"version": "0.2.0.dev12",
"split_keywords": [
"bot",
"telegram"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d5fb2dea12bab7e05876eeed3b99abbdab5cca01d3a0c8d421601332a5bda4b5",
"md5": "27138541bb9f73ecffd40c82e41bc58e",
"sha256": "0a56cc69b3181b8a300c723d85d57501f3520bec23d25909d7fb41af2ca5521d"
},
"downloads": -1,
"filename": "nonebot_adapter_antelegram-0.2.0.dev12-py3-none-any.whl",
"has_sig": false,
"md5_digest": "27138541bb9f73ecffd40c82e41bc58e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8.1,<4.0.0",
"size": 45040,
"upload_time": "2023-01-17T13:36:53",
"upload_time_iso_8601": "2023-01-17T13:36:53.902394Z",
"url": "https://files.pythonhosted.org/packages/d5/fb/2dea12bab7e05876eeed3b99abbdab5cca01d3a0c8d421601332a5bda4b5/nonebot_adapter_antelegram-0.2.0.dev12-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e6294b2a1750ef38dacf7d25907ee6541959b17f723623474e9a7019f4a1e066",
"md5": "8a8ef793f6cbacc33d7edd0dfc19506e",
"sha256": "6cdad6d5a182af74388b5c90329eafb8067f623c1e2628f324b780ecd0711487"
},
"downloads": -1,
"filename": "nonebot_adapter_antelegram-0.2.0.dev12.tar.gz",
"has_sig": false,
"md5_digest": "8a8ef793f6cbacc33d7edd0dfc19506e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8.1,<4.0.0",
"size": 43612,
"upload_time": "2023-01-17T13:36:55",
"upload_time_iso_8601": "2023-01-17T13:36:55.258789Z",
"url": "https://files.pythonhosted.org/packages/e6/29/4b2a1750ef38dacf7d25907ee6541959b17f723623474e9a7019f4a1e066/nonebot_adapter_antelegram-0.2.0.dev12.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-17 13:36:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "ColdThunder11",
"github_project": "nonebot-adapter-telegram",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "nonebot-adapter-antelegram"
}