用于开发的库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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用户界面java,使用gui连接到另一台计算机/服务器的文件系统   运行sbt的ubuntu返回错误:“javahome需要<path>参数”   java如何在Android中处理许多ImageView而不出现内存问题?   查询中非法字符的java相同URL失败   安卓取消引用可能会产生“java”。lang.NullPointerException'   java中的indexoutofboundsexception“java.lang.ArrayIndexOutOfBoundsException”错误   xml Java将dom保存到文件>文件在程序结束后由另一个进程打开   Java的垃圾收集器是如何工作的?   Java如何筛选值(列表)   java处理字符串我怎样才能像在真实的书籍中一样在上面部分生成“小数字”呢?   java SonarQube是否有一个API来获取所有项目分析的一部分?   java startActivity(intent)什么都不做   JAVAutil。扫描器类Java   java如何从Firebase更新电子邮件?UpdateMail方法已被弃用   java Hibernate。如何正确组织带有注释的onetomany关系?   在java中获得卷标和驱动器号之间的映射(而不是FileSystemView)的解决方法是什么   java查找文件的路径