松弛机器人获取日期选择器值

时间:2020-07-02 12:05:46

标签: python slack slack-api

我正在尝试用python创建我的第一个松弛机器人。 我需要您的帮助来解释如何获取datepicker的值。 这是我的代码:

import os 
from slack import WebClient
from slack.errors import SlackApiError
import time

client = WebClient(token=os.environ['SLACK_KEY'])

message = "Hey ! Pourrais-tu saisir la date de tes congés ce mois-ci ?"
attachments = [{
    "blocks": [
        {
            "type": "actions",
            "elements": [
                {
                    "type": "datepicker",
                    "initial_date": "1990-04-28",
                    "placeholder": {
                        "type": "plain_text",
                        "text": "Select a date",
                    }
                },
                {
                    "type": "datepicker",
                    "initial_date": "1990-04-28",
                    "placeholder": {
                        "type": "plain_text",
                        "text": "Select a date",
                    }
                }
            ]
        }
    ]
}]


def list_users():
    users_call = client.api_call("users.list")
    if users_call.get('ok'):
        return users_call['members']
    return None

def send_message(userid):
    response = client.chat_postMessage(channel=userid, text=message, username='groupadamin', attachments=attachments)
    

if __name__ == '__main__':
    users = list_users()
    if users:
        for u in users:
            send_message(u['id'])
        print("Success!")

我的机器人向私有用户发送了一条私人消息。我想得到他们的日期选择器的所有答案。

如果您需要更多详细信息,请问我。

1 个答案:

答案 0 :(得分:0)

通常,您需要听操作。 当用户点击datetimepicker时,就会将所谓的interaction-payload发送到您的python slackbot。 该有效负载记录在此处https://api.slack.com/reference/interaction-payloads/block-actions;您可以看到您可以通过访问actions.value获取选择日期的值。

特别是对于您的用例,给定您的代码示例,似乎您刚刚构建了一个可以发送消息的脚本。这将不允许您听动作。您需要一个可以发送和接收消息的python服务(API)。 为此,我建议看看bolt,这是一个由松弛维护的库,它将为您处理很多繁重的工作。具体来说,关于操作,您可以检查https://slack.dev/bolt-python/concepts#action-listening