用于开发的库telegram bots
telegram-botup的Python项目详细描述
电报botup
用于快速开发和简单部署Telegram机器人的库。她包括:
- dispatcher用于处理程序注册和接收传入更新
- form用于使用电报api
- sender-异步请求的工作进程
- 其他用途、类型等
功能
- 完全支持类型和方法Telegram API
- 内置的sender-一个任务一个任务地发出类似请求的辅助进程
- 同步和异步工作之间的简单切换
- 使用wsgi进行简单部署
- 内置命令行工具
- 支持使用socks5代理和代理url
安装
$ pip install telegram-botup
示例
from botup import Dispatcher, Form
from botup.types import InputFile
from flask import Flask, request
from my_func import get_random_image
from config import TOKEN
from config import redis_connection as rdb
app = Flask(__name__)
dispatcher = Dispatcher()
form = Form(token=TOKEN, connection=rdb)
def start_handler(chat_id, update):
form.push(
func=form.send_message,
chat_id=chat_id,
text='Hi!\nTo get image press to /image'
)
def send_image(chat_id, update):
path = get_random_image()
cache = rdb.get(f'cache:{path}')
if cache:
input_file = InputFile(file_id=cache)
form.push(
func=form.send_photo,
chat_id=chat_id,
photo=input_file.as_dict()
)
else:
input_file = InputFile(path=path)
resp = form.push(
func=form.send_photo,
chat_id=chat_id,
photo=input_file.as_dict()
).wait()
rdb.set(f'cache:{path}', resp.photo[-1].file_id)
dispatcher.register_command_handler('/image', send_image)
dispatcher.register_command_handler('*', start_handler)
@app.route(f'/{TOKEN}', methods=['POST'])
def index():
try:
req = request.get_json()
dispatcher.handle(req)
except Exception as exc:
import traceback
print(traceback.format_exc())
return "!", 200
Q/A
- 如何设置webhook?
$ botup set_webhook --help
- 如何在没有请求的情况下测试bot?
在form上使用fake_mode=true初始化
form = Form(..., fake_mode=True, ...)
或者在Sender上使用--fake模式启动
$ botup run_sender --token $TOKEN --fake-mode