-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.rb
executable file
·74 lines (67 loc) · 2.86 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
## Keyword Bot
#
## Summary and Configuration
# This bot responds to up to ten keywords with a simple response. For instance,
# you can set up this bot to respond to 'hours', 'location' and 'specials'.
# To configure the bot, fill in a setting with the keyword and the response
# separated by a colon. As an example, to setup a keyword for hours,
# fill this in on one of the keyword prompts:
#
# KEYWORD_1: 'hours: We are open every day from 9 to five'
#
### Things to keep in mind:
# * Use only one colon (:) for each keyword, or you'll confuse the bot
# * You can put any keyword in any of the ten settings. Only the order
# * in which we report the choices matters to what you use.
#
## Installation
# This bot can be installed on any GreenBot server through the web UI, or
# by through the command line at the greenbot-core root with a
# a 'npm install keyword-bot'
#
# This bot requires a ruby installation, 2.0 or older
## Annoated Bot Code
### Full source in git repo
# Convenience functions for separating the setting into the keyword and the
# response. For instance, KEYWORD_1 may be set to 'hours:We are open every day'
# The keyword function takes whatever is before ':' and defines that as a
# keyword, in this case 'hours'. The response function would take that same
# keyword string and define that as the response to the keyword (hours, in
# this case) as 'We are open every day'
def keyword(str)
str.split(':').first.strip.downcase
end
def response(str)
str.split(':').last
end
# The first message we receive in from the user is also the keyword that
# the user wanted. It is held in the environment variable INITIAL_MSG.
# In order for this bot to work properly, it should either be set as the
# default bot (so it will get every keyword), or every keyword that is
# listed here should also be configured in the network handles to point
# to this bot.
initial_msg = ENV['INITIAL_MSG'].strip.downcase
# Iterate over all of the settings given to the script, and seperate them
# out into the keyword and the response. Not all settings will be
# defined.
keywords = {}
%w( KEYWORD_1 KEYWORD_2 KEYWORD_3 KEYWORD_4 KEYWORD_5 KEYWORD_6 KEYWORD_7
KEYWORD_8 KEYWORD_9).each do |p|
# For each of the settings, check to see that it isn't empty,
# And that it includes the prompt separator (:)
setting = ENV[p]
next if setting.nil? || setting.empty?
next unless setting.include?(':')
# Fill in the hash with the keyword as the key, and the rest of the string
# as the response to that keyword.
keywords[keyword(setting)] = response(setting)
end
if keywords.keys.include? initial_msg
# the message that somebody texted in matches one of our keywords.
# Send back the matching response.
puts keywords[initial_msg]
else
# the message that somebody texted does not match any of our keywords.
# Send back the valid choices.
puts "Please choose from: #{keywords.keys.join(',')}"
end