【发布时间】:2021-07-04 09:29:08
【问题描述】:
我想制作一个发送投票的机器人,并在同一条消息中实时显示谁投票如何。我使用 slack_bolt 编写了这样一个应用程序,但现在它只显示最后一个投票者,覆盖了前一个投票者。如何才能做到这一点?看起来这很容易,但我就是不知道怎么做。
from slack_bolt.adapter.socket_mode import SocketModeHandler
from src.settings import Settings
import logging
logging.basicConfig(level=logging.DEBUG)
settings = Settings()
app = App(token=settings.slack_bot_token)
static_select = {
"blocks": [
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Pick an item from the dropdown list"
},
"accessory": {
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": "Select an item",
"emoji": True
},
"options": [
{
"text": {
"type": "plain_text",
"text": "0",
"emoji": True
},
"value": "value-0"
},
{
"text": {
"type": "plain_text",
"text": "0,5",
"emoji": True
},
"value": "value-1"
},
{
"text": {
"type": "plain_text",
"text": "1",
"emoji": True
},
"value": "value-2"
},
{
"text": {
"type": "plain_text",
"text": "2",
"emoji": True
},
"value": "value-3"
}
],
"action_id": "static_select-action"
}
}
]
}
@app.command("/go_poll")
def hello_command(ack, client):
ack()
client.chat_postMessage(channel="#random", blocks=static_select['blocks'])
@app.action("static_select-action")
def option_chosen(ack, body, client, logger):
ack()
selected_option = body['actions'][0]['selected_option']['text']['text']
res = client.chat_update(
channel=body["channel"]["id"],
ts=body["message"]["ts"],
as_user=True,
attachments=[{"pretext": f'@{body["user"]["username"]}:{selected_option} '}]
)
logger.info(res)
if __name__ == "__main__":
SocketModeHandler(app, settings.slack_app_token).start()
【问题讨论】:
标签: python bots slack slack-api