Last active
April 13, 2016 03:24
-
-
Save elpeo/70add27ecedf243802c9624fbcf97ac3 to your computer and use it in GitHub Desktop.
tiqavot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'json' | |
require 'rest-client' | |
require 'open-uri' | |
post '/callback' do | |
params = JSON.parse(request.body.read) rescue Hash.new([]) | |
params['result'].each do |msg| | |
img = nil | |
text = msg['content']['text'] | |
if text | |
ary = open('http://api.tiqav.com/search.json?q=' + URI.encode_www_form_component(text)) do |f| | |
JSON.parse(f.read) | |
end rescue [] | |
unless ary.empty? | |
img = 'http://tiqav.com/' + ary[0]['id'] + '.' + ary[0]['ext'] | |
end | |
end | |
content = img ? { | |
contentType: 2, | |
toType: 1, | |
originalContentUrl: img, | |
previewImageUrl: img | |
} : { | |
contentType: 1, | |
toType: 1, | |
text: text || '...' | |
} | |
request_content = { | |
to: [msg['content']['from']], | |
toChannel: 1383378250, # Fixed value | |
eventType: "138311608800106203", # Fixed value | |
content: content | |
} | |
endpoint_uri = 'https://trialbot-api.line.me/v1/events' | |
content_json = request_content.to_json | |
RestClient.proxy = ENV["FIXIE_URL"] | |
RestClient.post(endpoint_uri, content_json, { | |
'Content-Type' => 'application/json; charset=UTF-8', | |
'X-Line-ChannelID' => ENV["LINE_CHANNEL_ID"], | |
'X-Line-ChannelSecret' => ENV["LINE_CHANNEL_SECRET"], | |
'X-Line-Trusted-User-With-ACL' => ENV["LINE_CHANNEL_MID"], | |
}) | |
end | |
"OK" | |
end |
Author
elpeo
commented
Apr 12, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment