代码之家  ›  专栏  ›  技术社区  ›  Zaaferani

如何使用Flask应用程序和BaleBot向用户发送消息

  •  1
  • Zaaferani  · 技术社区  · 6 年前

    这个密码是有效的!BaleBot响应用户消息和烧瓶应用程序响应请求。但是 bot.send_message 在里面 def send_to_user

    例如,如果我给我的机器人发送了一条信息(任何东西),他的回应。 如果我向你提出要求 curl -X POST 127.0.0.1:5000/send/user -d 'bale_id=1533910422&access_hash=6644828719985087310&message=text' ,烧瓶有反应。

    import os
    
    import asyncio
    from flask import Flask
    from flask import request
    from balebot.handlers import *
    from balebot.filters import *
    from balebot.models.base_models import Peer
    from balebot.models.messages import *
    from balebot.updater import Updater
    from balebot.config import Config
    from threading import Thread
    
    import zbxbale_settings
    
    Config.log_level = zbxbale_settings.LOG_LEVEL
    
    updater = Updater(token=zbxbale_settings.bale_key, loop=asyncio.get_event_loop())
    # updater = Updater(token=zbxbale_settings.bale_key)
    dispatcher = updater.dispatcher
    bale_bot = updater.bot
    
    
    def success(result, err):
        print("success : ", result)
        print("success : ", err)
    
    
    def failure(result, err):
        print("failure : ", result)
    
    
    @dispatcher.command_handler("/start")
    def start_command(bot, update):
        message = update.get_effective_message()
        bot.respond(update, message, success_callback=success, failure_callback=failure)
    
    
    @dispatcher.default_handler()
    def default_handler(bot, update):
        message = update.get_effective_message()
        message.text += " not recognized"
        bot.respond(update, message, success_callback=success, failure_callback=failure)
    
    
    app = Flask(__name__)
    
    
    @app.route("/")
    def hello():
        return "Hello"
    
    
    @app.route("/send/user", methods=['POST'])
    def send_to_user():
        bale_id = request.form['bale_id']
        access_hash = request.form['access_hash']
        message = request.form['message']
        p = Peer('User', bale_id, access_hash)
        m = TextMessage(message)
        bale_bot.send_message(m, p, success_callback=success, failure_callback=failure)
        return "Hello World!"
    
    
    @app.route("/send/group", methods=['POST'])
    def send_to_group():
        bale_id = request.form['bale_id']
        access_hash = request.form['access_hash']
        return "Hello World!"
    
    
    from multiprocessing import Process
    
    
    p = Process(target=app.run, args=())
    q = Process(target=updater.run, args=())
    
    q.start()
    p.start()
    # q.join()
    # p.join()
    # app.run()
    # updater.run()
    
    p = Peer('User', "1533910422", "6644828719985087310")
    m = TextMessage("salam")
    updater.bot.send_message(m, p, success_callback=success, failure_callback=failure)
    
    2 回复  |  直到 5 年前
        1
  •  4
  •   Ehsan    6 年前

    在帮助下 @ehsan-barkhordar

    import pykka
    
    import asyncio
    from flask import Flask
    from flask import request
    from balebot.handlers import *
    from balebot.filters import *
    from balebot.models.base_models import Peer
    from balebot.models.messages import *
    from balebot.updater import Updater
    
    updater = Updater(token="token", loop=asyncio.get_event_loop())
    dispatcher = updater.dispatcher
    bale_bot = updater.bot
    
    
    def success(result, err):
        print("success : ", result)
        print("success : ", err)
    
    
    def failure(result, err):
        print("failure : ", result)
    
    
    @dispatcher.command_handler("/start")
    def start_command(bot, update):
        message = update.get_effective_message()
        bot.respond(update, message, success_callback=success, failure_callback=failure)
    
    
    app = Flask(__name__)
    
    
    class ServerActor(pykka.ThreadingActor):
        def __init__(self, flask_app):
            super(ServerActor, self).__init__()
            self.flask_app = flask_app
    
        def on_receive(self, message):
            print(message)
    
        def run(self):
            self.flask_app.run(host='0.0.0.0', port=5050)
    
        def shutdown_server(self):
            func = request.environ.get('werkzeug.server.shutdown')
            if func is None:
                raise RuntimeError('Not running with the Werkzeug Server')
            func()
    
    
    @app.route("/", methods=['GET'])
    def send_to_user():
        message = "hello"
        m = TextMessage(message)
        bale_bot.send_message(m, Peer('User', "1533910422", "6644828719985087310"), success_callback=success,
                              failure_callback=failure)
        return "message sent"
    
    
    # class SMStarter():
    def start(notification_sender, server_actor_ref):
        server_proxy = server_actor_ref.proxy()
        future = server_proxy.run()
        notification_sender.run()
        notification_sender.stop()
    
    
    start(notification_sender=updater, server_actor_ref=ServerActor.start(flask_app=app))
    
        2
  •  2
  •   Ehsan    6 年前

    第二,如果不想使用数据库,可以使用pykka中的actor。它在线程中实现actor,并使用它们在获取请求时发送消息。 这里有个好医生: https://www.pykka.org/en/latest/

    推荐文章