Skip to content

Instantly share code, notes, and snippets.

@kskomori
Last active November 1, 2016 11:18
Show Gist options
  • Save kskomori/ce48348699fc56ddf22eabd61f99d4d2 to your computer and use it in GitHub Desktop.
Save kskomori/ce48348699fc56ddf22eabd61f99d4d2 to your computer and use it in GitHub Desktop.
今更ながらLINE BOTのEchoサーバをRails4で簡易的に書いてみる ref: http://qiita.com/kskomori/items/6718babf55a283fe267e
require 'faraday'
class Linebots::BotController < ApplicationController
protect_from_forgery except: :callback
def callback
if request.method != 'POST'
render text: 'NG', status: 500
end
post_text(params[:result][0][:content][:from], params[:result][0][:content][:text])
render text: ''
end
private
def post_text(to, text)
content = {
contentType: 1,
toType: 1,
text: text
}
post_event(to, content)
end
def post_event(to, content)
msg = {
to: [ to ],
toChannel: '1383378250', # 固定値
eventType: 138311608800106203, # 固定値
content: content
}
conn = Faraday::Connection.new(url: 'https://trialbot-api.line.me') do |builder|
builder.request :url_encoded
builder.response :logger
builder.adapter Faraday.default_adapter
end
res = conn.post '/v1/events' do |req|
req.headers['Content-type'] = 'application/json; charset=UTF-8'
req.headers['X-Line-ChannelID'] = 'xxxxxxxxxxxxx'
req.headers['X-Line-ChannelSecret'] = 'yyyyyyyyyyyyyyyyyyy'
req.headers['X-Line-Trusted-User-With-ACL'] = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzz'
req.body = msg.to_json
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment