37
36

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.

30秒でChatwork APIを使ってマイチャットに投稿する方法

Last updated at Posted at 2013-12-10

1行概要

rubyとHTTP通信ライブラリfaradayを使ってChatworkのマイチャットに投稿するサンプル。

犯行動機

ChatはGETできて半人前、POSTできて一人前。
Skypeとか遠距離恋愛で使ってください。エンジニアの使うものではありません。

方法

1. faradayのインストール (5秒)

$ gem install faraday
Successfully installed faraday-0.8.8
Parsing documentation for faraday-0.8.8
Installing ri documentation for faraday-0.8.8
1 gem installed

2. ChatworkのマイチャットのグループIDを調べる (5秒)

Screen Shot 2013-12-10 at 11.03.00 PM.png

3. Chatwork APIのトークンを調べる (5秒)

Screen Shot 2013-12-10 at 11.06.08 PM.png

4. 投稿! (15秒)

main.rb
require 'faraday'

ROOM_ID = '<ここに2で取得したマイチャットIDを入れる>' # my chat
CHATWORK_TOKEN = '<ここに3で取得したトークンを入れる>'

conn = Faraday::Connection.new(url: 'https://api.chatwork.com') do |builder|
  builder.use Faraday::Request::UrlEncoded
  builder.use Faraday::Response::Logger
  builder.use Faraday::Adapter::NetHttp
end

response = conn.post do |request|
  request.url "/v1/rooms/#{ROOM_ID}/messages"
  request.headers = {
    'X-ChatWorkToken' => CHATWORK_TOKEN
  }
  request.params[:body] = "Hello World!" # => ここに入れる文字が投稿される
end

そして実行!

$ ruby main.rb
37
36
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
37
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?