3
5

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 5 years have passed since last update.

ChatWork に画像をアップロード in Python

Last updated at Posted at 2018-11-10

ChatWork にファイルをアップロードする API がリリースされていたので使ってみました。

Requirement

  • anaconda3-5.2.0(Python 3.6.5)

Program

import requests

END_POINT = 'https://api.chatwork.com/v2/'
API_TOKEN = 'your api token'


def post_jpeg(room_id, file_path, message=''):
    url = '{}rooms/{}/files'.format(END_POINT, room_id)
    jpeg_bin = open(file_path, 'rb')
    headers = {'X-ChatWorkToken': API_TOKEN}
    files = {
        'file': ('kawaii.jpg', jpeg_bin, 'image/jpeg'),
        'message': message,
    }
    request = requests.post(url, headers=headers, files=files)

room_id = 'your room id'
file_path = './hoge.jpg'
message = '正義'
post_jpeg(room_id, file_path, message)

kawaii.jpg

Hint

  • message は必須ではないです。
  • 'image/jpeg' の部分は Content-Type での記述になります、他のファイル形式でも試してみてください。
post_jpeg
files = {'file': ('kawaii.jpg', jpeg_bin, 'image/jpeg')}

Reference

3
5
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
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?