如何将通过HTTPPOST从Python Flask脚本接收的数据传递到单独的Python脚本进行处理



好了,伙计们,这是我的问题。

我正在开发一款带有打包机器人的Slack应用程序,允许用户在Slack中玩游戏。我已经成功地构建了机器人程序,并根据API指南将其与应用程序打包在一起。一旦我发现了交互式消息功能,我就决定实现上述功能,以便对游戏进行更用户友好的处理。

交互式消息功能允许您使用按钮发布消息,用户可以单击按钮来调用操作。我的机器人脚本,我们称之为bot.py,它会提示用户(使用Slack chat.postMessage函数)一条消息,其中包含一些可供选择的按钮。这个脚本有一个类(我知道它应该更模块化,但一切都很及时),它打开了一个网络套接字,通过Slack RTM API进行通信。因此,当脚本运行时,它总是在如下所示的通道中"侦听"来自用户的命令:@botname command。脚本中调用这种"始终侦听"状态的部分如下所示:

#bot.py
...
if slack_client.rtm_connect():
print("MYBOT v1.0 connected and running!")
while True:
command, channel, user = self.parse_slack_output(slack_client.rtm_read())
if command and channel:
if channel not in self.channel_ids_to_name.keys():
#this (most likely) means that this channel is a PM with the bot
self.handle_private_message(command, user)
else:
self.handle_command(command, channel, user)
time.sleep(READ_WEBSOCKET_DELAY)
else:
print("Connection failed. Invalid Slack token or bot ID?")

这一切都很好。现在,假设用户已经使用一个命令成功创建了一个游戏实例并开始玩。在某个时刻,用户会被提示选择一套王牌套装:

#bot.py
...
attachments =[{
"title":"Please select index for trump suit:",
"fallback":"Your interface does not support interactive messages.",
"callback_id":"prompt_trump_suit", 
"attachment_type":"default", 
"actions":
[{"name":"diamonds","text":":diamonds:","type":"button","value":"0"},
{"name":"clubs","text":":clubs:","type":"button","value":"1"},
{"name":"hearts","text":":hearts:","type":"button","value":"2"},
{"name":"spades","text":":spades:","type":"button","value":"3"}]
}]
slack.chat.post_message(
channel=player_id,
as_user=True,
attachments=attachments
)

交互式消息如下所示。单击此消息中的某个按钮的操作将通过HTTPPOST向web服务器发送有效负载。我在项目中的另一个脚本,我们将称之为app.py,是一个Flask脚本,当用户单击其中一个按钮时,它成功地接收到这个POST请求。脚本中接收POST请求的部分如下所示:

#app.py
...
# handles interactive button responses for mybot
@app.route('/actions', methods=['POST'])
def inbound():
payload = request.form.get('payload')
data = json.loads(payload)
token = data['token']
if token == SLACK_VERIFICATION_TOKEN:
print 'TOKEN is good!'
response_url = data['response_url']
channel_info = data['channel']
channel_id = channel_info['id']
user_info = data['user']
user_id = user_info['id']
user_name = user_info['name']
actions = data['actions'][0]
value = actions['value']
print 'User sending message: ',user_name
print "Value received: ",value
return Response(), 200

当点击按钮时,我得到了预期的输出:

TOKEN is good!
User sending message:  my_username
Value received:  3

所以到目前为止一切都是成功的。现在,我想做的是获取POST信息,并使用它来调用我的bot.py脚本中的一个函数,该函数处理王牌套装选择。问题是,如果我要调用该函数,让我们称之为handle_trump_suit_selection(),我首先必须在app.py文件中实例化一个Bot()对象,这当然不会按预期工作,因为该函数将使用新的Bot()实例调用,因此不会处于与当前游戏相同的状态。

那么,我该如何将POST信息返回到bot.py中所需的Bot()实例以进行进一步处理呢?我是Python中OOP的新手,尤其是Flask和Slack API的新手,所以对我来说很容易;)。

提前谢谢。

大获成功

tl;dr:基本上,解决方案是创建一个Celery任务,该任务使用Slack Events API从Flask应用程序实例化机器人程序实例。您将任务设置为在输入所需的输入后启动,立即将所需的响应(200)返回Slack,同时机器人程序脚本(启动RTM API网络套接字)并行启动。

实质:如上所述,事实证明,所需要的是某种排队服务。我最终选择了Celery,因为它在与Heroku(我托管Slack应用程序的地方)集成方面相对容易,而且它的文档易于遵循。

以这种方式开发Slack应用程序需要设置并使用Slack Events API从发布消息的Slack频道接收命令(本例中为"play my_game")。程序的Flask应用程序(app.py)部分会侦听此事件,当输入与您要查找的内容匹配时,它并行启动Celery任务(在tasks.py中,在本例中它实例化了Bot.py的Bot()实例)。:)现在机器人程序可以使用Slack RTM API和Slack Events API进行监听和响应。这允许您在Slack框架内构建丰富的应用程序/服务。

如果你想设置类似的东西,下面是我的项目布局和重要的代码细节。请随意将它们用作模板。

项目布局:

  • 项目名称文件夹
    • app_folder
      • 静态文件夹
      • 模板文件夹
      • __init__.py
      • my_app.py
      • bot.py
      • tasks.py
    • Procfile
    • requirements.txt

__init__.py:

from celery import Celery
app = Celery('tasks')
import os
app.conf.update(BROKER_URL=os.environ['RABBITMQ_BIGWIG_URL']) # Heroku Celery broker

my_app.py:

from flask import Flask, request, Response, render_template
import app
from app import tasks
app = Flask(__name__)
@app.route('/events', methods=['POST'])
def events():
"""
Handles the inbound event of a post to the main Slack channel
"""
data = json.loads(request.data)
try:
for k, v in data['event'].iteritems():
ts = data['event']['ts']
channel = data['event']['channel']
user_id = data['event']['user']
team_id = data['team_id']
if 'play my_game' in str(v):
tasks.launch_bot.delay(user_id, channel, ts, team_id) # launch the bot in parallel
return Response(), 200
except Exception as e:
raise

机器人.py:

from slackclient import SlackClient
class Bot():
def main():
# opening the Slack web-socket connection
READ_WEBSOCKET_DELAY = 1  # 1 second delay between reading from firehose
if self.slack_client.rtm_connect():
while True:
command, channel, user, ts = self.parse_slack_output()
if command and channel:
if channel not in self.channel_ids_to_name.keys():
# this (most likely) means that this channel is a PM with the bot
self.handle_private_message(command, user, ts)
else:
self.handle_command(command, channel, user, ts)
time.sleep(READ_WEBSOCKET_DELAY)

任务.py:

import bot
from bot import Bot
from app import app
@app.task
def launch_bot(user_id, channel, ts, team_id):
'''
Instantiates the necessary objects to play a game
Args:
[user_id] (str) The id of the user from which the command was sent
[channel] (str) The channel the command was posted in
[ts] (str) The timestamp of the command
'''
print "launch_bot(user_id,channel)"
app.control.purge()
bot = Bot()
bot.initialize(user_id, channel)
bot.main()

Procfile(如果使用Heroku):

web: gunicorn --pythonpath app my_app:app
worker: celery -A app.tasks worker -B --loglevel=DEBUG

如果您有任何问题,请告诉我。我花了一点时间才弄清楚,如果你在这个问题上撞头,我很乐意帮助你。

最新更新