forked from NarubyRiverlione/slack_tipbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.rb
161 lines (140 loc) · 4.99 KB
/
command.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'bitcoin-client'
Dir['./coin_config/*.rb'].each {|file| require file }
require './bitcoin_client_extensions.rb'
class Command
attr_accessor :result, :action, :user_name, :icon_emoji
ACTIONS = %w(balance deposit tip withdraw networkinfo commands checkChannel)
def initialize(slack_params)
@coin_config_module = Kernel.const_get ENV['COIN'].capitalize
text = slack_params['text']
@params = text.split(/\s+/)
raise "WACK" unless @params.shift == slack_params['trigger_word']
@user_name = slack_params['user_name']
@user_id = slack_params['user_id']
@channel_name = slack_params['channel_name']
@action = @params.shift
@result = {}
end
def perform
if ACTIONS.include?(@action)
self.send("#{@action}".to_sym)
else
raise @coin_config_module::PERFORM_ERROR
end
end
def client
@client ||= Bitcoin::Client.local
end
### Commands ###
def checkChannel
@result[:text] = "Talking in #{@channel_name} \n"
if allCommandAllowed
@result[:text] += "All tipper commands are allowed."
else
@result[:text] += @coin_config_module::RESTRICED_CMDS_MESSAGE
end
end
def balance
if allCommandAllowed
balance = client.getbalance(@user_id)
@result[:text] = "@#{@user_name} #{@coin_config_module::BALANCE_REPLY_PRETEXT} #{balance}#{@coin_config_module::CURRENCY_ICON}"
if balance >= @coin_config_module::WEALTHY_UPPER_BOUND
@result[:text] += @coin_config_module::WEALTHY_UPPER_BOUND_POSTTEXT
@result[:icon_emoji] = @coin_config_module::WEALTHY_UPPER_BOUND_EMOJI
elsif balance > 0 && balance < @coin_config_module::WEALTHY_UPPER_BOUND
@result[:text] += @coin_config_module::BALANCE_REPLY_POSTTEXT
end
else
@result[:text] = @coin_config_module::RESTRICED_CMDS_MESSAGE
end
end
def deposit
if allCommandAllowed
@result[:text] = "#{@coin_config_module::DEPOSIT_PRETEXT} #{user_address(@user_id)} #{@coin_config_module::DEPOSIT_POSTTEXT}"
else
@result[:text] = @coin_config_module::RESTRICED_CMDS_MESSAGE
end
end
def tip
user = @params.shift
raise @coin_config_module::TIP_ERROR_TEXT unless user =~ /<@(U.+)>/
target_user = $1
set_amount
tx = client.sendfrom @user_id, user_address(target_user), @amount
@result[:text] = "<@#{@user_id}> sent <@#{target_user}> #{@amount}#{@coin_config_module::CURRENCY_ICON}"
# @result[:text] = "#{@coin_config_module::TIP_PRETEXT} <@#{@user_id}> => <@#{target_user}> #{@amount}#{@coin_config_module::CURRENCY_ICON}"
# @result[:attachments] = [{
# fallback:"<@#{@user_id}> => <@#{target_user}> #{@amount}Ð",
# color: "good",
# fields: [
# {
# title: "such tipping #{@amount}Ð wow!",
# value: "#{@coin_config_module::TIP_POSTTEXT1}#{tx}",
# short: true
# },
# {
# title: "#{@coin_config_module::TITLE_TIPPER}",
# value: "<@#{@user_id}>",
# short: true
# },{
# title: "#{@coin_config_module::TITLE_TIP_RECIEVER}",
# value: "<@#{target_user}>",
# short: true
# }]
# }]
@result[:text] += " (<#{@coin_config_module::TIP_POSTTEXT1}#{tx}.htm#{@coin_config_module::TIP_POSTTEXT2}>)"
end
def withdraw
if allCommandAllowed
address = @params.shift
set_amount
tx = client.sendfrom @user_id, address, @amount
@result[:text] = "#{@coin_config_module::WITHDRAW_TEXT} <@#{@user_id}> => #{address} #{@amount}#{@coin_config_module::CURRENCY_ICON} "
@result[:text] += " (<#{@coin_config_module::TIP_POSTTEXT1}#{tx}#{@coin_config_module::TIP_POSTTEXT2}>)"
@result[:icon_emoji] = @coin_config_module::WITHDRAW_ICON
else
@result[:text] = @coin_config_module::RESTRICED_CMDS_MESSAGE
end
end
### no command, internal functions ###
def networkinfo
info = client.getinfo
@result[:text] = info.to_s
@result[:icon_emoji] = @coin_config_module::NETWORKINFO_ICON
end
private
def allCommandAllowed
if @channel_name != @coin_config_module::ALLOW_ALL_CMDS_CHANNEL
return false
else
return true
end
end
def set_amount
amount = @params.shift
@amount = amount.to_f
randomize_amount if (@amount == "random")
raise @coin_config_module::TOO_POOR_TEXT unless available_balance >= @amount + 0.0001 # transaction cost need also be availibe
raise @coin_config_module::NO_PURPOSE_LOWER_BOUND_TEXT if @amount < @coin_config_module::NO_PURPOSE_LOWER_BOUND
end
def randomize_amount
lower = [1, @params.shift.to_i].min
upper = [@params.shift.to_i, available_balance].max
@amount = rand(lower..upper)
@result[:icon_emoji] = @coin_config_module::RANDOMIZED_EMOJI
end
def available_balance
client.getbalance(@user_id)
end
def user_address(user_id)
existing = client.getaddressesbyaccount(user_id)
if (existing.size > 0)
@address = existing.first
else
@address = client.getnewaddress(user_id)
end
end
def commands
@result[:text] = "#{ACTIONS.join(', ' )}"
end
end