首页 > 解决方案 > 松弛机器人 | 检查消息反应

问题描述

我目前正在开发一个松弛机器人。一旦请求的任务完成,这将要求用户添加对消息的反应。但是,我不知道如何执行此任务,并且我的 api 研究没有成功。我确实发现了resions.get 和reactions.list,但它似乎不是正确的解决方案。

这是我的代码:

import os 
from slack import WebClient
from slack.errors import SlackApiError
import time
import datetime
from datetime import date
from dateutil.relativedelta import relativedelta, MO


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

attachments = [{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Hello ! Il est temps de remplir le formulaire d'absences.\n\n*Merci de suivre les instructions ci-dessous.*"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": ":round_pushpin: *<www.youtube.com|Clique ici pour te rendre sur la fiche d'absences.>* \nSurtout n'oublie pas de réagir avec :heavy_check_mark: afin d'indiquer que c'est bien fait."
            }
        }
    ]
}]


today = date.today()

def last_monday_of_month(any_day):
    next_month = any_day.replace(day=28) + datetime.timedelta(days=4)  # this will never fail
    last_day = next_month - datetime.timedelta(days=next_month.day)
    return last_day + relativedelta(weekday=MO(-2))

last_monday_of_month(date.today())

if today == last_monday_of_month(date.today()):
    client.chat_postMessage(channel='starterbot', text="", attachments=attachments)
    print("Success.")
else:
    print("Error.")

如果有人可以帮助我。

标签: pythonslackslack-api

解决方案


你会想要使用reactions.get方法。您只需提供timestamp您想要获得反应的消息。

您的代码看起来就像您只是在发布一条消息。如果您想在用户对消息做出反应时做某事,您需要订阅消息事件。这意味着拥有一个正在运行的服务(使用事件 API 将其发布给您,或者使用 RTM API 订阅)


推荐阅读