[PYTHON] 사내 Slack 의 인기 TOP30 이모 티 콘 조사 하기

s00.png TL;DR

사내 Slack의 전체 Public 채널에서 사용된 TOP30 인기 이모티콘을 조사하는 방법을 공유합니다. 이모티콘의 사용량과 자주 사용되는 이모티콘을 통해서 회사 분위기를 대략 파악해 볼까요?! :eyes:

※ 제악사항 : 채널 별 최신 1000개의 메시지 대상, 스레드에 포함된 이모티콘 제외

준비물

소스코드, 실행

실행방법


$ python ./get_emoji_count.py
=> result.txt 결과 파일이 생성 됩니다.

아래 코드의 token 값을 윗 단계에서 본인의 Slack OAuth Access Token으로 바꿔줍니다.

get_emoji_count.py


import requests
import json
import time
import sys

def get_channel_list(params):
    r = requests.get("https://slack.com/api/conversations.list", params=params)
    json = r.json()

    # Public Channel ID를 저장
    channels = []

    for channel in json["channels"]:
        channels.append(channel["id"])
        print(channel)

    print("공개 채널 수: ", len(channels))
    return channels


def count_emoji(channels, param):
    # 이모티콘 저장
    emojis = {}

    for channel in channels:

        params.update(count=1000, channel=channel, inclusive=1)
        r = requests.get("https://slack.com/api/conversations.history", params=params)
        json = r.json()

        # 메시지가 없을 때
        if "messages" in json == False:
            continue

        # 채널의 메시지 (최대 1000개 메시지까지만..)
        for message in json["messages"]:
            if "reactions" in message:
                # 종류
                for reaction in message["reactions"]:
                    # 집계 목록
                    if reaction["name"] in emojis:
                        emojis[reaction["name"]] += reaction["count"]
                    else:
                        emojis[reaction["name"]] = reaction["count"]

        # 1초 슬립
        time.sleep(1)

    print("이모티콘 수: ", len(emojis))
    return emojis

# ------------------------
# 사용 횟수가 많은 순서로 이모티콘 30개 정렬
# ------------------------
def sort_30(emojis):
    emojis_sorted = sorted(emojis.items(), key=lambda x: x[1], reverse=True)[:30]
    return emojis_sorted

if __name__ == '__main__':
    # 아래의 토큰값을 본인의 값으로 변경하시면 됩니다.
    token = "xoxp-394838520754-723291506419-1330403625172-123456789012345678901234567890"
    params = {"token": token}

    channel = get_channel_list(params)
    emojis = count_emoji(channel, params)
    emojis_sorted = sort_30(emojis)

    # 결과 파일로 출력
    f = open('result.txt', 'w')
    for data in emojis_sorted:
        f.write(str(data).replace("'",":") + "\n")
    f.close()

result.txt 파일을 열어보면 다음과 같습니다. (이 파일 내용을 그대로 Slack 메시지에 복붙하면 됩니다!)

(:+1:, 1172)
(:a:, 227)
(:heart:, 161)
(:b:, 137)
(:raised_hands:, 117)
(:three:, 110)
(:two:, 90)
(:heavy_check_mark:, 87)
(:100:, 57)
(:the_horns:, 50)
(:-1:, 42)
(:one:, 38)
(:tada:, 28)
(:+1::skin-tone-2:, 27)
(:clap:, 25)
(:heavy_plus_sign:, 18)
(:slightly_smiling_face:, 13)
(:ok_hand:, 13)
(:four:, 11)
(:fire:, 11)
(:blush:, 9)
(:white_check_mark:, 9)
(:zero:, 8)
(:wave:, 7)
(:+1::skin-tone-4:, 7)
(:hugging_face:, 7)
(:pray:, 7)
(:heart_eyes:, 7)
(:joy:, 5)
(:five:, 5)

s09.png

참고 링크

Recommended Posts

사내 Slack 의 인기 TOP30 이모 티 콘 조사 하기