[PYTHON] The format of the message obtained by Slack API is subtly difficult to use

I got the message by hitting the API, but I want to get at least

--User ID

Of these, I couldn't get the user ID, so I tried trial and error. The story described here is originally located on GitHub.

️ Requirements

Even if you do some kind of analysis material, it seems that you can not talk without at least the following three pieces of information.

The requirement this time is to extract these three pieces of information from all posts (of a specific channel).

Conclusion

As a result of trial and error, I concluded that the following feelings would be good.

For time and text,

data = json.load(open("posts.json"))
for msg in data:
	print msg.get("ts", "")
	print msg.get("text", "")

It's OK with a feeling like.

Regarding user acquisition, those that cannot be acquired under the following conditions are through.

def get_user_id(m):
    if m.has_key("user"): return m["user"]
    if m.has_key("bot_id"): return m["bot_id"]
    return None

for msg in data:
	uid = get_user_id(msg)
	if uid is None:
		continue
	# do something ...

Under the verification conditions, the number of posts corresponding to this was 0.2 [%] of the total, so we conclude that it can be ignored.

Verification conditions

Data comes from your company's random channel. I will refrain from disclosing the specific period and the number of posts, but I looked at the messages posted in a period of about one and a half years.

that's all.

Recommended Posts

The format of the message obtained by Slack API is subtly difficult to use
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
[RSpec] Make the response of the request to the Google API stubized by stub_request (: post) the same format as the actual one.
Introduction to Machine Translation Architecture by the University of Cambridge by Slack Translation App Kiara
Use twitter API to get the number of tweets related to a certain keyword
Use the MediaWiki API to get Wiki information
How to use the Google Cloud Translation API
How to use the NHK program guide API
[Python] Throw a message to the slack channel
Use Twitter API to reduce the time taken by Twitter (create a highlighting (like) timeline)
Add a function to tell the weather of today to slack bot (made by python)
Let's use the API of the official statistics counter (e-Stat)
Let's use the Python version of the Confluence API module.
[Python] Use the Face API of Microsoft Cognitive Services
Play music by hitting the unofficial API of Google Play Music
Let's use AWS Lambda to create a mechanism to notify slack when the value monitored by CloudWatch is exceeded on Python
Re: Python lambda is useless ^ H ^ H ^ H ^ H ^ H Difficult to use
The copy method of pandas.DataFrame is deep copy by default
How to test the attributes added by add_request_method of pyramid
[Django] Pass the user instance authenticated by API to ModelSerializer
I want to use only the normalization process of SudachiPy
Is there a secret to the frequency of pi numbers?
[Slack api + Python] I tried to summarize the methods such as status confirmation and message sending
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
A story that is a little addicted to the authority of the directory specified by expdp (for beginners)
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.