Reputation: 951
I want to use Flask app for getting request to send message Bale users.
this code is work! BaleBot respond user message and Flask app respond request. but bot.send_message
in def send_to_user
not work!
for example if i send a message (any thing) in Bale to my bot, his respond.
and if i send a request to Flask such curl -X POST 127.0.0.1:5000/send/user -d 'bale_id=1533910422&access_hash=6644828719985087310&message=text'
, Flask was respond.
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)
Upvotes: 2
Views: 362
Reputation: 951
with help by @ehsan-barkhordar this code wrote and it`s correct:
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))
Upvotes: 5
Reputation: 4291
Generally you have two options. First, using a shared database between two programs, that one get requests and write to database however the other one read database and send messages. Second, you would use actor in pykka if you didn't want to use a database. It implement actors in Thread and use them in order to send message while getting a request. here is a good doc about it: https://www.pykka.org/en/latest/
Upvotes: 2