Skip to content

Instantly share code, notes, and snippets.

@elpeo
Last active April 13, 2016 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpeo/70add27ecedf243802c9624fbcf97ac3 to your computer and use it in GitHub Desktop.
Save elpeo/70add27ecedf243802c9624fbcf97ac3 to your computer and use it in GitHub Desktop.
tiqavot
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
@elpeo
Copy link
Author

elpeo commented Apr 12, 2016

12980751_10208649625859467_783459986_o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment