簡體   English   中英

使用 Slack 中的實時投票更新消息

[英]Update message with real-time voting in slack

我想制作一個發送投票的機器人,並在同一條消息中實時顯示誰投票如何。 我使用 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()

chat_update 中的附件參數似乎沒有添加新附件,而是重新定義了消息的附件,因此您只能看到最近的投票。

一種解決方法是首先檢索現有附件,添加新投票,然后調用 client.chat_update。

探索您在正文中收到的數據 * object,您應該能夠看到有關原始消息的信息,並從那里獲取附件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM