Python 在slack中使用实时投票更新消息
我想让一个机器人在一个懒散的地方发送一张选票,并在同一条信息中实时显示谁投了票。我使用slack_bolt编写了这样一个应用程序,但现在它只显示最后一个投票人,覆盖了前一个投票人。如何做到这一点?这似乎很容易,但我就是不知道怎么做Python 在slack中使用实时投票更新消息,python,bots,slack,slack-api,Python,Bots,Slack,Slack Api,我想让一个机器人在一个懒散的地方发送一张选票,并在同一条信息中实时显示谁投了票。我使用slack_bolt编写了这样一个应用程序,但现在它只显示最后一个投票人,覆盖了前一个投票人。如何做到这一点?这似乎很容易,但我就是不知道怎么做 from slack_bolt.adapter.socket_mode import SocketModeHandler from src.settings import Settings import logging logging.basicConfig(lev
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()
chat_update中的attachment参数似乎没有添加新附件,而是重新定义了邮件的附件,因此您只能看到最近的投票 解决方法是首先检索现有附件,添加新投票,然后调用client.chat\u update 浏览您在正文*对象中收到的数据,您应该能够看到有关原始邮件的信息,并从中获取附件