-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.rb
50 lines (41 loc) · 1.61 KB
/
bot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'discordrb' #discordとのやり取りのため追加
require 'uri' #HTTP通信をするため追加
BOT_TOKEN = ENV["TALK_BOT_TOKEN"]
BOT_CLIENT_ID = ENV["TALK_BOT_CLIENT_ID"]
TALK_API_KEY = ENV["TALK_API_KEY"]
talkapi_url = "https://api.a3rt.recruit.co.jp/talk/v1/smalltalk"
#botのセットアップ
bot = Discordrb::Commands::CommandBot.new(
token: BOT_TOKEN,
client_id: BOT_CLIENT_ID,
prefix: "T"
)
bot.mention do |event|
#メッセージの取得とメンション部分の削除
message = event.message.content
message = message.delete("<@#{BOT_CLIENT_ID}> ")
p message
#API通信
uri = URI.parse(talkapi_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Post.new(uri.path)
req.set_form_data({'apikey' => TALK_API_KEY, 'query' => message})
res = http.request(req)
result = JSON.parse(res.body)
#レスポンスの取得
# message = result["status"] == 0 ? result["results"][0]["reply"] : "error#{result["status"]}\r\nmessege#{result["message"]}";
if result["status"] == 0 then
message = result["results"][0]["reply"]
elsif result["status"] == 2000 then
message = "すみません、よくわかりません"
else
message = "error#{result["status"]}\r\nmessege#{result["message"]}";
end
event.respond message
end
bot.command :help do |event|
bot.send_message("メンションの後に話したい内容を入れると、このボットが話してくれます。")
end
bot.run