forked from oisin/trellobo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trellobo.rb
executable file
·129 lines (114 loc) · 4.86 KB
/
trellobo.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
$LOAD_PATH.unshift(*Dir[File.dirname(__FILE__) + "/vendor/gems/**/lib"])
require 'cinch'
require 'trello'
require 'json'
# You will need an access token to use ruby-trello 0.3.0 or higher, which trellobo depends on. To
# get it, you'll need to go to this URL:
#
# https://trello.com/1/connect?key=DEVELOPER_PUBLIC_KEY&name=trellobo&response_type=token&scope=read,write&expiration=never
#
# Substitute the DEVELOPER_PUBLIC_KEY with the value you'll supply in TRELLO_API_KEY below. At the end of this process,
# You'll be told to give some key to the app, this is what you want to put in the TRELLO_API_ACCESS_TOKEN_KEY below.
#
# there are 5 environment variables that must be set for the trellobot to behave
# the way he is supposed to -
#
# TRELLO_API_KEY : your Trello API developer key
# TRELLO_API_SECRET : your Trello API developer secret
# TRELLO_API_ACCESS_TOKEN_KEY : your Trello API access token key. See above how to generate it.
# TRELLO_BOARD_ID : the trellobot looks at only one board and the lists on it, put its id here
# TRELLO_BOT_QUIT_CODE : passcode to cause trellobot to quit - defaults to none
# TRELLO_BOT_CHANNEL : the name of the channel you want trellobot to live on
# TRELLO_BOT_NAME : the name for the bot, defaults to 'trellobot'
# TRELLO_BOT_SERVER : the server to connect to, defaults to 'irc.freenode.net'
$board = nil
include Trello
include Trello::Authorization
Trello::Authorization.const_set :AuthPolicy, OAuthPolicy
OAuthPolicy.consumer_credential = OAuthCredential.new ENV['TRELLO_API_KEY'], ENV['TRELLO_API_SECRET']
OAuthPolicy.token = OAuthCredential.new ENV['TRELLO_API_ACCESS_TOKEN_KEY'], nil
def sync_board
return $board.refresh! if $board
$board = Trello::Board.find(ENV['TRELLO_BOARD_ID'])
end
def say_help(msg)
msg.reply "I can tell you the open cards on the lists on your Trello board. Just address me with the name of the list (it's not case sensitive)."
msg.reply "For example - trellobot: ideas"
msg.reply "I also understand the these commands : "
msg.reply " -> 1. help - shows this!"
msg.reply " -> 2. sync - resyncs my cache with the board."
msg.reply " -> 3. lists - show me all the board list names"
end
bot = Cinch::Bot.new do
configure do |c|
# Initialize defaults for optional envs
ENV['TRELLO_BOT_QUIT_CODE'] ||= ""
ENV['TRELLO_BOT_NAME'] ||= "trellobot"
ENV['TRELLO_BOT_SERVER'] ||= "irc.freenode.net"
c.server = ENV['TRELLO_BOT_SERVER']
c.nick = ENV['TRELLO_BOT_NAME']
c.channels = [ENV['TRELLO_BOT_CHANNEL']]
sync_board
end
# trellobot is polite, and will only reply when addressed
on :message, /^rcb_trellobot[_]*:/ do |m|
# if trellobot can't get thru to the board, then send the human to the human url
sync_board unless $board
unless $board
m.reply "I can't seem to get the list of ideas from Trello, sorry. Try here: https://trello.com/board/#{ENV['TRELLO_BOARD_ID']}"
bot.halt
end
# trellobot: what up? <- The bit we are interested in is past the ':'
parts = m.message.split(':',2)
searchfor = parts[1].strip.downcase
case searchfor
when /debug/
debugger
when /lists/
$board.lists.each { |l|
m.reply " -> #{l.name}"
}
when /help/
when /\?/
say_help(m)
when /sync/
sync_board
m.reply "Ok, synced the board, #{m.user.nick}."
else
if searchfor.length > 0
# trellobot presumes you know what you are doing and will attempt
# to retrieve cards using the text you put in the message to him
# at least the comparison is not case sensitive
list = $board.lists.detect { |l| l.name.casecmp(searchfor) == 0 }
if list.nil?
m.reply "There's no list called <#{searchfor}> on the board, #{m.user.nick}. Sorry."
else
cards = list.cards
if cards.count == 0
m.reply "Nothing doing on that right now, #{m.user.nick}."
else
ess = (cards.count == 1) ? "" : "s"
m.reply "I have #{cards.count} card#{ess} today"
cards.each { |c|
m.reply " -> #{c.short_id}. #{c.name} - #{c.member_ids.collect {|m| x=Member.find(m).username }.join("/").presence || "UNASSIGNED"}"
}
end
end
else
say_help(m)
end
end
end
# if trellobot loses his marbles, it's easy to disconnect him from the server
# note that if you are doing a PaaS deploy, he may respawn depending on what
# the particular hosting env is (e.g. Heroku will start him up again)
on :private, /^quit(\s*)(\w*)/ do |m, blank, code|
bot.quit if ENV['TRELLO_BOT_QUIT_CODE'].eql?(code)
if code.empty?
m.reply "There is a quit code required for this bot, sorry."
else
m.reply "That is not the correct quit code required for this bot, sorry."
end
end
end
bot.start