Skip to content

Instantly share code, notes, and snippets.

@supermomonga
Last active December 18, 2015 04:58
Show Gist options
  • Save supermomonga/5728806 to your computer and use it in GitHub Desktop.
Save supermomonga/5728806 to your computer and use it in GitHub Desktop.
class Psgr
def Psgr.query room, text
{events: [{message: {room: room,text: text,}}]}.to_json.to_s
end
def Psgr.post query
url = URI.parse 'http://postgres-lingrbot.herokuapp.com/'
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url.path)
request.body = query
request['Content-Type'] = 'application/xml'
http.request request
# response.code # => 200 Ok
# response.body # => The body (HTML, XML, blob, whatever)
end
end
# -*- encoding: utf-8 -*-
require 'rubygems'
require 'bundler'
require 'sinatra'
require 'json'
require 'net/http'
Bundler.require
post '/bot/sugoi/cb' do
content_type :text
data = JSON.parse(request.env['rack.input'].read())
data['events'].map do |e|
if e['message']
m = e['message']['text']
if /^#help$/ =~ m
query = Psgr.query e['message']['room'], 'Select name from help;'
res = Psgr.post query
if res.code == '200'
body = res.body
body = JSON.parse(body.gsub('" "','","')) if body[0] == '['
[*body].join "\n"
else
"Oops, something wrong with calling PostgreSQL bot. Returned http code is #{res.code}"
end
elsif /^#help\s+([^\s]*)\s*/m =~ m
query = Psgr.query e['message']['room'], "Select description from help where name = '#{$1}';"
res = Psgr.post query
if res.code == '200'
res.body
else
"Oops, something wrong with calling PostgreSQL bot. Returned http code is #{res.code}"
end
elsif /^#img\s+([^\s]*)\s*(.*)/m =~ m
query = Psgr.query e['message']['room'], "Select url from 画像 where name = '#{$1}' order by random() limit #{$2 == '' ? 1 : $2};"
res = Psgr.post query
if res.code == '200'
body = res.body
body = JSON.parse(body.gsub('" "','","')) if body[0] == '['
img = [*body].join "\n"
img == '(empty)' ? 'https://dl.dropbox.com/u/330501/resource/lingr.com/naiyo.jpg' : img
else
"Oops, something wrong with calling PostgreSQL bot. Returned http code is #{res.code}"
end
elsif /^#i\s+([^\s]*)\s*(.*)/m =~ m
query = Psgr.query e['message']['room'], "Insert into #{$1} values(#{$2});"
res = Psgr.post query
if res.code == '200'
res.body
else
"Oops, something wrong with calling PostgreSQL bot. Returned http code is #{res.code}"
end
elsif /^#s\s+([^\s]*)\.([^\s]*)\s*([^\s]*)/m =~ m
query = Psgr.query e['message']['room'], "Select #{$2} from #{$1} #{ "where name = '#{$3}'" if $3 != '' };"
res = Psgr.post query
if res.code == '200'
res.body
else
"Oops, something wrong with calling PostgreSQL bot. Returned http code is #{res.code}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment