7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

chatwork API の使い方 (python3)

Last updated at Posted at 2018-11-21

chatwork へのメッセージの書き込みと、読み取りです。

API の仕様はこちら
エンドポイント: /rooms

書き込み
APIトークン と room_id は書き換えて下さい。

send_message.py
# ! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	send_message.py
#
#					Nov/21/2018
#
# ------------------------------------------------------------------
import	sys
import requests

sys.stderr.write("*** 開始 ***\n")
# 
APIKEY = 'e12321ef233d8a92deb1cc15bc09b79e'
ROOMID = '11111178'
#
URL = 'https://api.chatwork.com/v2'

str_out = "これはテストです。\n"
str_out += "\tこのメッセージはpython で送信されました。\n"
str_out += "2018年11月21日\n"
str_out += "PM 18:39\n"
 
url = URL + '/rooms/' + ROOMID + '/messages'
headers = { 'X-ChatWorkToken': APIKEY }
params = { 'body': str_out }
 
resp = requests.post(url,
                     headers=headers,
                     params=params)
 
print(resp.content)

sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

読み込み
APIトークン と room_id は書き換えて下さい。

get_message.py
# ! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	get_message.py
#
#					Nov/21/2018
#
# ------------------------------------------------------------------
import	sys
import	json
import	requests

sys.stderr.write("*** 開始 ***\n")
# 
APIKEY = 'e12321ef233d8a92deb1cc15bc09b79e'
ROOMID = '11111178'
#
URL = 'https://api.chatwork.com/v2'

 
url = URL + '/rooms/' + ROOMID + '/messages'
headers = { 'X-ChatWorkToken': APIKEY }
 
resp = requests.get(url,headers=headers)
 

dict_data = json.loads(resp.content)
print(dict_data)
print(dict_data[0]['body'])
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

メッセージの削除

delete_message.py
# ! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	delete_message.py
#
#					Nov/22/2018
#
# ------------------------------------------------------------------
import	sys
import	requests

sys.stderr.write("*** 開始 ***\n")
# 
APIKEY = 'e12321ef233d8a92deb1cc15bc09b79e'
ROOMID = '11111178'
#
URL_V2 = 'https://api.chatwork.com/v2'

message_id = '1117008309421867008'
url = URL_V2 + '/rooms/' + ROOMID + '/messages/' + message_id
headers = { 'X-ChatWorkToken': APIKEY }
 
resp = requests.delete(url,headers=headers)

print(resp) 
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

API Token は次の画面で表示ができます。
chatwrok_api_token.png

ルームIDはグループチャットURL末尾の数字と同様の番号です。
 例:以下の「********* 」の箇所
 https://www.chatwork.com/#!rid*********

7
11
1

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
7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?