LoginSignup
1
1

More than 5 years have passed since last update.

チャットワークAPI(V2) python3でテスト投稿

Last updated at Posted at 2017-12-20

はじめに

“python3”でチャットワークAPIを使用して
メッセージを送る方法

API Tokenの取得

メニューから、API設定を選択
s.png
自分のAPIトークンを発行します。

プログラムの作成

“chatwork.py” を作成。

#!/usr/local/bin/python
#coding: UTF-8
print ("Content-Type: application/json")

import pycurl
import urllib.parse

c = pycurl.Curl()
option = {'body': 'Python ChatWork API Testだぜ'}
c.setopt(pycurl.SSL_VERIFYPEER,0)
c.setopt(pycurl.URL, 'http://api.chatwork.com/v2/rooms/(ルームID)/messages')
c.setopt(pycurl.HTTPHEADER, ['X-ChatWorkToken: (あなたのAPI Token)'])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, urllib.parse.urlencode(option))
c.perform()

Macで、“python3”のインストールが出来れば問題なく動作します。

参考
MacにPython3をインストールする方法 | ハジプロ!

チャットしたい文言は、body箇所に記載

option = {'body': 'Python ChatWork API Testだぜ'}

先ほど取得したあなたのAPI Tokenを記載します。

c.setopt(pycurl.HTTPHEADER, ['X-ChatWorkToken: (あなたのAPI Token)'])

(ルームID)には、チャットワークのルームIDを入力します。

c.setopt(pycurl.URL, 'http://api.chatwork.com/v2/rooms/(ルームID)/messages')

チャットワークから、ルームのURLを確認した際に以下の場合は

https://www.chatwork.com/#!rid00000000

00000000
が入ります。勿論、グループチャットではなく、個人やダイレクトチャットのページでも大丈夫です。

はまったポイント

リリース用に、Cent OS6 にて、 “python3”をインストール
実行をしたが、pycurlがインストールされていない。

そこで、

export PYCURL_SSL_LIBRARY=nss
pip install pycurl --compile pycurl --no-cache

pycurl のインストール
実行!しかし、

pycurl.error: (60, "Peer's certificate issuer has been marked as not trusted by the user.")

certificateで、エラーが出る!

そこで

c.setopt(pycurl.SSL_VERIFYPEER,0)

により今回は、SSLを無視
勿論、.crtを指定する方法もありますが今回は無視にて実行が出来ました。

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