Last active
April 19, 2017 11:20
-
-
Save ScienJus/26a341fda25d009acea1 to your computer and use it in GitHub Desktop.
qqbot 的 demo:通过 Tuling123 的 API 自动回复消息
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 '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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line24:
kind_of => kind_of?