Search code examples
pythonslackslack-api

Get a json object | Slack bot


i'm actually doing a slack bot. But here, i need your help. I need to get a json object.

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."
            }
        }
    ]
}]


channel='C016D8D4XEG'

response = client.chat_postMessage(channel='starterbot', text="", attachments=attachments)

timestamp = response['ts']

add_reacts = client.reactions_add(channel="C01665VJ2JH",  name="heavy_check_mark", timestamp=timestamp)

jsonObject = client.conversations_list(channel="C01665VJ2JH", timestamp=timestamp)
reactions = jsonObject["reactions"][-1]["name"]


print(reactions)

As you can see, i did :

jsonObject = client.conversations_list(channel="C01665VJ2JH", timestamp=timestamp)
reactions = jsonObject["reactions"][-1]["name"]

This code is supposed to get the "name" inside "reactions" but it still doesn't works.

This is my json file

Please guys give me an answer.


Solution

  • The issue with your code lies here

    jsonObject = client.conversations_list(channel="C01665VJ2JH", timestamp=timestamp)
    

    conversations.listmethod does not take the channel or the timestamp parameter. If you want to get a history of the reactions added to the message you can do one of the following:

    • Use the conversations.history method to retrieve the messages in that particular channel then cross reference them with timestamp.
    • Use the events API to listen for new reactions added

    With both methods you should be able to capture the message block and parse the reactions.