Skip to content

Instantly share code, notes, and snippets.

@ScienJus
Last active April 19, 2017 11:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ScienJus/26a341fda25d009acea1 to your computer and use it in GitHub Desktop.
Save ScienJus/26a341fda25d009acea1 to your computer and use it in GitHub Desktop.
qqbot 的 demo:通过 Tuling123 的 API 自动回复消息
require 'qqbot'
require 'net/http'
require 'json'
def tuling(msg)
key = 'your tuling123 key'
uri = URI('http://www.tuling123.com/openapi/api')
uri.query =
URI.encode_www_form(
key: key,
info: msg
)
Net::HTTP.start(uri.host, uri.port) do |http|
req = Net::HTTP::Get.new(uri)
res = http.request(req)
JSON.parse(res.body)['text']
end
end
qqbot = QQBot.new
qqbot.poll do |message|
msg = message.content
if msg.kind_of?(String) && msg.start_with?('AI')
reply = tuling(msg[2...msg.length])
if message.type == 0
qqbot.send_to_friend(message.from_id, reply)
elsif message.type == 1
qqbot.send_to_group(message.from_id, reply)
elsif message.type == 2
qqbot.send_to_discuss(message.from_id, reply)
end
end
end
@depctg
Copy link

depctg commented Feb 11, 2016

line24:
kind_of => kind_of?

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