【问题标题】:Update message with real-time voting in slack使用 Slack 中的实时投票更新消息
【发布时间】: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


    【解决方案1】:

    chat_update 中的附件参数似乎没有添加新附件,而是重新定义了消息的附件,因此您只能看到最近的投票。

    解决方法是先检索现有附件,添加新投票,然后调用 client.chat_update。

    探索您在 body* 对象中收到的数据,您应该能够看到有关原始邮件的信息,并从中获取附件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      相关资源
      最近更新 更多