我在Herroku上的sleekxmpp聊天机器人的message
回调在发送一条消息时被调用2次,而该消息仅应执行一次。
我在Heroku上运行了一个python和sleekxmpp聊天机器人。它设置为单个Web进程dyno,并连接到本地XMPP服务器(在我的本地网络中运行)。该漫游器成功连接并接收了我发送给它的消息。但是,它的message
回调会在收到每条消息时执行两次。
我已经验证了只有一条消息到达XMPP服务器。我还确保该应用程序不会在多个测功机上缩放。
正在研究中,我找不到信息来指出解决问题的方向。
import json
import logging
import threading
import sleekxmpp
from flask import Flask, request
def xmpp_run():
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data Forms
xmpp.register_plugin('xep_0060') # PubSub
xmpp.register_plugin('xep_0199') # XMPP Ping
xmpp.register_plugin('xep_0071') # Lightweight text markup in message
if xmpp.connect((XMPPSERVER, 5222)):
xmpp.process(block=True)
print("Done")
else:
print("Unable to connect.")
""" XMPP Bot """
class XMPPBot(sleekxmpp.ClientXMPP):
def __init__(self, jid, password):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
self.add_event_handler("session_start", self.start)
self.add_event_handler("message", self.message)
def start(self, event):
self.send_presence()
self.get_roster()
def message(self, msg):
app.logger.debug('Inside XMPPBot.message method')
app.logger.debug('msg: ' + str(msg))
app = Flask(__name__)
app.debug = True
xmpp = XMPPBot('username', 'password')
threading.Thread(target=xmpp_run).start()
logging.basicConfig(level=logging.INFO,
format='%(levelname)-8s %(message)s')
XMPPSERVER = 'x.x.x.x'
@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
app.logger.debug('Received webhook request')
if request.method == 'GET':
app.logger.debug('GET request received')
""" Some initialization code runs here """
elif request.method == 'POST':
request_json = request.get_json(force=True)
app.logger.debug('Incoming POST request: ' + json.dumps(request_json))
""" Some processing code runs here """
return ''
我希望在发送消息时,XMPPBot.message()
方法只会执行一次。相反,它将执行两次。重新部署后,对于收到的前几条消息,通常会执行4次,然后转到2。
上面的代码在每次执行时记录消息Inside XMPPBot.message method
。这是日志文件的输出:
2019-07-19T15:19:51.769859+00:00 app[web.1]: --------------------------------------------------------------------------------
2019-07-19T15:19:51.769878+00:00 app[web.1]: DEBUG in main [/app/main.py:36]:
2019-07-19T15:19:51.769880+00:00 app[web.1]: Inside XMPPBot.message method
2019-07-19T15:19:51.769881+00:00 app[web.1]: --------------------------------------------------------------------------------
2019-07-19T15:19:51.769887+00:00 app[web.1]: DEBUG Inside XMPPBot.message method
2019-07-19T15:19:51.770036+00:00 app[web.1]: --------------------------------------------------------------------------------
2019-07-19T15:19:51.770038+00:00 app[web.1]: DEBUG in main [/app/main.py:37]:
2019-07-19T15:19:51.770044+00:00 app[web.1]: msg: <message xml:lang="en" to="x@xxx/1234512345" from="y@xxx/12341234" type="chat" id="123456"><active xmlns="http://jabber.org/protocol/chatstates" /><body>hi</body></message>
2019-07-19T15:19:51.770045+00:00 app[web.1]: --------------------------------------------------------------------------------
2019-07-19T15:19:51.770070+00:00 app[web.1]: DEBUG msg: <message xml:lang="en" to="x@xxx/1234512345" from="y@xxx/12341234" type="chat" id="123456"><active xmlns="http://jabber.org/protocol/chatstates" /><body>hi</body></message>
2019-07-19T15:19:51.770568+00:00 app[web.1]: --------------------------------------------------------------------------------
2019-07-19T15:19:51.770569+00:00 app[web.1]: DEBUG in main [/app/main.py:36]:
2019-07-19T15:19:51.770570+00:00 app[web.1]: Inside XMPPBot.message method
2019-07-19T15:19:51.770573+00:00 app[web.1]: --------------------------------------------------------------------------------
2019-07-19T15:19:51.770611+00:00 app[web.1]: DEBUG Inside XMPPBot.message method
2019-07-19T15:19:51.770842+00:00 app[web.1]: --------------------------------------------------------------------------------
2019-07-19T15:19:51.770843+00:00 app[web.1]: DEBUG in main [/app/main.py:37]:
2019-07-19T15:19:51.770844+00:00 app[web.1]: msg: <message xml:lang="en" to="x@xxx/1234512345" from="y@xxx/12341234" type="chat" id="123456"><active xmlns="http://jabber.org/protocol/chatstates" /><body>hi</body></message>
2019-07-19T15:19:51.770847+00:00 app[web.1]: --------------------------------------------------------------------------------
2019-07-19T15:19:51.770877+00:00 app[web.1]: DEBUG msg: <message xml:lang="en" to="x@xxx/1234512345" from="y@xxx/12341234" type="chat" id="123456"><active xmlns="http://jabber.org/protocol/chatstates" /><body>hi</body></message>