forked from BeepBoopHQ/starter-ruby-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.rb
106 lines (88 loc) · 3.32 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'slack-ruby-client'
require 'logging'
logger = Logging.logger(STDOUT)
logger.level = :debug
Slack.configure do |config|
config.token = ENV['SLACK_TOKEN']
if not config.token
logger.fatal('Missing ENV[SLACK_TOKEN]! Exiting program')
exit
end
end
client = Slack::RealTime::Client.new
# listen for hello (connection) event - https://api.slack.com/events/hello
client.on :hello do
logger.debug("Connected '#{client.self['name']}' to '#{client.team['name']}' team at https://#{client.team['domain']}.slack.com.")
end
# listen for channel_joined event - https://api.slack.com/events/channel_joined
client.on :channel_joined do |data|
if joiner_is_bot?(client, data)
client.message channel: data['channel']['id'], text: "Thanks for the invite! I don\'t do much yet, but #{help}"
logger.debug("#{client.self['name']} joined channel #{data['channel']['id']}")
else
logger.debug("Someone far less important than #{client.self['name']} joined #{data['channel']['id']}")
end
end
# listen for message event - https://api.slack.com/events/message
client.on :message do |data|
case data['text']
when 'hi', 'bot hi' then
client.typing channel: data['channel']
client.message channel: data['channel'], text: "Hello <@#{data['user']}>."
logger.debug("<@#{data['user']}> said hi")
if direct_message?(data)
client.message channel: data['channel'], text: "It\'s nice to talk to you directly."
logger.debug("And it was a direct message")
end
when 'attachment', 'bot attachment' then
# attachment messages require using web_client
client.web_client.chat_postMessage(post_message_payload(data))
logger.debug("Attachment message posted")
when bot_mentioned(client)
client.message channel: data['channel'], text: 'You really do care about me. :heart:'
logger.debug("Bot mentioned in channel #{data['channel']}")
when 'bot help', 'help' then
client.message channel: data['channel'], text: help
logger.debug("A call for help")
when /^bot/ then
client.message channel: data['channel'], text: "Sorry <@#{data['user']}>, I don\'t understand. \n#{help}"
logger.debug("Unknown command")
end
end
def direct_message?(data)
# direct message channles start with a 'D'
data['channel'][0] == 'D'
end
def bot_mentioned(client)
# match on any instances of `<@bot_id>` in the message
/\<\@#{client.self['id']}\>+/
end
def joiner_is_bot?(client, data)
/^\<\@#{client.self['id']}\>/.match data['channel']['latest']['text']
end
def help
%Q(I will respond to the following messages: \n
`bot hi` for a simple message.\n
`bot attachment` to see a Slack attachment message.\n
`@<your bot\'s name>` to demonstrate detecting a mention.\n
`bot help` to see this again.)
end
def post_message_payload(data)
main_msg = 'Beep Beep Boop is a ridiculously simple hosting platform for your Slackbots.'
{
channel: data['channel'],
as_user: true,
attachments: [
{
fallback: main_msg,
pretext: 'We bring bots to life. :sunglasses: :thumbsup:',
title: 'Host, deploy and share your bot in seconds.',
image_url: 'https://storage.googleapis.com/beepboophq/_assets/bot-1.22f6fb.png',
title_link: 'https://beepboophq.com/',
text: main_msg,
color: '#7CD197'
}
]
}
end
client.start!