qqbot 的 demo:通过 Tuling123 的 API 自动回复消息

February 16, 2017 · View on GitHub

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