LoginSignup
5
3

More than 5 years have passed since last update.

Slackのstatus emojiを変更する(python3)

Posted at

Slackでは、自分のステータスを「自宅」「仕事」「リモートで作業中」「休暇」のように、任意に変更することが出来ます。

この記事では、Python3を使って、status emojiを変更する方法を記述します。

まずはAPI Documentationを確認

最初に、SlackAPIのAPI Documentationを確認してみましょう。
Slackにおける会話・チャンネル・データの抽出・更新等さまざまな操作が可能です。
例えば、今回使うのは、users.profile.setというメソッド(方法)です。

users.profile.set method | Slack

HTTPリクエストでPOSTする

https://slack.com/api/users.profile.set へ後述のパラメータを付与して POST します。

POSTについては、
HTTP(HyperText Transfer Protocol)とは - IT用語辞典
を参照してください。

pythonではこの処理を、requests.post()で

requests.post('http://api.hoge.com', data={'foo': 'bar'})

のように書きます。

パラメータ(抜粋•適宜和訳)

Argument Example Required Description
token xxxx-xxxxxxxxx-xxxx Required
(必須)
Authentication token bearing required scopes.
profile { first_name: "John", ... } Optional
(任意)
Collection of key:value pairs presented as a URL-encoded JSON hash.
(URLにエンコードされたjsonハッシュとして渡される値の集まり)
user W1234567890 Optional
(任意)
ID of user to change. This argument may only be specified by team admins on paid teams.
(プロフィールを変更する対象ユーザーのID。この要素は場合によっては課金しているチームの管理人のみ有効。自分のプロフィールをいじる分には無課金チームでも使えます。)

実装

Pythonでのrequestsの導入方法

Requests の使い方 (Python Library) - Qiita
等を参考にしました。

必用な値の取得

コード

change_status.py
import requests
import json

USER_TOKEN = "xoxp-********..."
USER_ID = "?????**" #@の後に続くユーザ名ではない

def change_status(text,emoji):
    data = {
    "token" : USER_TOKEN,
    "user": USER_ID,
    "profile":json.dumps({
        "status_text":text,
        "status_emoji":emoji
        })
    }
    res = requests.post('https://slack.com/api/users.profile.set',params = data)
    #print(res.text) POSTした結果を表示したい場合

if __name__ == '__main__':
    change_status("ラボ",":office:") #status_message,status_emojiの順

実行

python3 change_status.py

結果

無事変更されました。
変更されたステータス

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3