-
Notifications
You must be signed in to change notification settings - Fork 14
/
alice.rb
80 lines (70 loc) · 2.08 KB
/
alice.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require 'rubygems'
require 'bundler'
require 'bundler/setup'
require 'dotenv'
require 'require_all'
require 'slackbotsy'
require 'sinatra'
require 'open-uri'
Dotenv.load
Bundler.require
Mongoid.load!("config/mongoid.yml")
# Look into replacing this with https://github.com/slack-ruby/slack-ruby-bot
require_all 'alice'
config = {
'channel' => '#main,#mudroom',
'name' => ENV['BOT_NAME'],
'incoming_webhook' => ENV['INCOMING_WEBHOOK'],
'outgoing_token' => ENV['SLACK_API_TOKEN']
}
Yummly.configure do |config|
config.app_id = ENV['YUMMLY_APP_ID']
config.app_key = ENV['YUMMLY_APP_KEY']
end
I18n.enforce_available_locales = false
bot_interface_handler = Slackbotsy::Bot
# SLACK_API_TOKEN of 'xxx' is the .env.sample default
if ENV['SLACK_API_TOKEN'] == 'xxx' || ENV['USE_TESTBOTSY'] == 'true'
bot_interface_handler = Testbotsy
end
bot = bot_interface_handler.new(config) do
hear /(.+)/ do |mdata|
begin
name = User.ensure_user(user_name, user_id).primary_nick
if message = Pipeline::Listener.new(name, mdata[1]).route
{
content: message.response.content,
response_type: message.response_type
}.to_json
end
rescue Exception => e
Alice::Util::Logger.info "*** Unable to process \"#{mdata[1]}\": #{e}"
Alice::Util::Logger.info e.backtrace
end
end
end
post '/' do
if output = bot.handle_item(params)
response = JSON.parse(output)
if response["text"]
begin
parsed_response = JSON.parse(response["text"])
raw_text = parsed_response["content"]
if parsed_response["response_type"] == "emote"
processed_text = "_#{raw_text}_"
else
processed_text = raw_text
end
bot.say(processed_text, {channel: params['channel_name'], mrkdwn: 'true'})
rescue
Alice::Util::Logger.info "*** JSON parsing error! Output was #{output}"
Alice::Util::Logger.info "*** Params was #{params}"
end
end
end
end
get "/map" do
content_type = "image/svg+xml"
headers 'Content-Type' => "image/svg+xml"
Util::Mapper.map!
end