-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
398 lines (291 loc) · 10.7 KB
/
main.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from telegram.ext import Updater, CommandHandler
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from decimal import Decimal
import requests
import logging
import pymongo
import json
config = json.loads(open("config.json").read())
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
def get_mongo():
client = pymongo.MongoClient(config['mongo']['host'],
config['mongo']['port'])
return client[config['mongo']['db']]
def get_user_id(user):
db = get_mongo()
u = db.users.find_one({'username': user})
return u
def get_user(user):
db = get_mongo()
u = db.users.find_one({'userid': user})
if u is None:
u = {'userid': user}
db.users.insert(u)
return u
def add_to_chat(user, chat):
db = get_mongo()
db.users.update({'userid': user['userid']}, {'$addToSet': {'chats': chat}})
def is_registered(user):
db = get_mongo()
return db.users.count({'username': user}) != 0
def is_registered_id(user):
db = get_mongo()
return db.users.count({'userid': user}) != 0
def give_balance(user, amount):
db = get_mongo()
db.users.update({'userid': user['userid']},
{'$inc': {'redeemed': float(-amount)}})
return db.users.find_one({'userid': user['userid']})
def get_balance(user):
db = get_mongo()
rpc = AuthServiceProxy("http://%s:%s@%s:%d" %
(config['rpc']['user'], config['rpc']['password'],
config['rpc']['host'], config['rpc']['port']))
address = get_address(user)
received = rpc.getreceivedbyaddress(address)
return received - Decimal(db.users.find_one(
{'userid': user['userid']}).get('redeemed', 0))
def get_unconfirmed(user):
rpc = AuthServiceProxy("http://%s:%s@%s:%d" %
(config['rpc']['user'], config['rpc']['password'],
config['rpc']['host'], config['rpc']['port']))
address = get_address(user)
received = rpc.getreceivedbyaddress(address)
received_unconfirmed = rpc.getreceivedbyaddress(address, 0)
return received_unconfirmed - received
def validate_address(address):
rpc = AuthServiceProxy("http://%s:%s@%s:%d" %
(config['rpc']['user'], config['rpc']['password'],
config['rpc']['host'], config['rpc']['port']))
return rpc.validateaddress(address)['isvalid']
def get_address(user):
db = get_mongo()
address = db.users.find_one({'userid': user['userid']}) \
.get('address', None)
if address is None:
rpc = AuthServiceProxy("http://%s:%s@%s:%d" %
(config['rpc']['user'],
config['rpc']['password'],
config['rpc']['host'], config['rpc']['port']))
address = rpc.getnewaddress()
db.users.update({'userid': user['userid']},
{'$set': {'address': address}})
return address
def start(bot, update):
update.message.reply_text('Hello! I\'m a tipbot for the Hush crypto. ' +
'Add me to a group and start tipping!')
add_to_chat(get_user(update.message.from_user.id), update.message.chat_id)
def tip(bot, update):
args = update.message.text.split()[1:]
if len(args) == 2:
# Unfortunatelly the only way i can currently think of for getting the
# user ID for the username is if I get the user to register first.
# Sucks, but I guess I need it to be done
user = get_user_id(args[0])
from_user = get_user(update.message.from_user.id)
try:
amount = Decimal(args[1])
except decimal.InvalidOperation:
update.message.reply_text("Usage: /tip <user> <amount>")
return
if user is not None:
if amount > 0:
if get_balance(from_user) - amount >= 0:
from_user = give_balance(from_user, -amount)
user = give_balance(user, amount)
bot.sendMessage(chat_id=update.message.chat_id,
text="%s tipped %s %f Hush" % (
from_user['username'],
args[0],
amount
))
else:
update.message.reply_text("Not enough money!")
else:
update.message.reply_text("Invalid amount!")
else:
update.message.reply_text("%s is not registered!" % (args[0]))
else:
update.message.reply_text("Usage: /tip <user> <amount>")
add_to_chat(get_user(update.message.from_user.id), update.message.chat_id)
def soak(bot, update):
args = update.message.text.split()[1:]
if len(args) == 1:
from_user = get_user(update.message.from_user.id)
try:
amount = Decimal(args[0])
except decimal.InvalidOperation:
update.message.reply_text("Usage: /soak <amount>")
return
if amount > 0:
if get_balance(from_user) - amount >= 0:
db = get_mongo()
users = db.users.find({'chats': update.message.chat_id,
'userid': {'$ne': from_user['userid']},
'username': {'$ne': None}})
if users.count() > 0:
tip = amount/users.count()
from_user = give_balance(from_user, -amount)
usernames = []
for user in users:
print(user)
give_balance(user, tip)
usernames.append(user['username'])
users_str = ", ".join(usernames)
print(users.count())
for user in users:
print(user)
give_balance(user, tip)
bot.sendMessage(chat_id=update.message.chat_id,
text="%s soaked %f Hush to %s!" % (
from_user['username'],
tip,
users_str
))
else:
update.message.reply_text("No users on this channel have"
" interacted with the bot.")
else:
update.message.reply_text("Not enough money!")
else:
update.message.reply_text("Invalid amount")
else:
update.message.reply_text("Usage: /soak <amount>")
add_to_chat(get_user(update.message.from_user.id), update.message.chat_id)
def balance(bot, update):
bal = get_balance(get_user(update.message.from_user.id))
r = requests.get('https://api.cryptonator.com/api/ticker/%s-%s' %
('ok', 'usd'))
usd = Decimal(bal)*Decimal(r.json()['ticker']['price'])
unconfirmed = ""
if get_unconfirmed(get_user(update.message.from_user.id)) > 0:
unconfirmed = "(+ %s unconfirmed)" % \
get_unconfirmed(get_user(update.message.from_user.id))
update.message.reply_text("You have %s Hush (%f USD) %s" %
(bal, usd, unconfirmed))
add_to_chat(get_user(update.message.from_user.id), update.message.chat_id)
def register(bot, update):
args = update.message.text.split()[1:]
if len(args) == 1:
username = args[0]
if not is_registered_id(update.message.from_user.id):
if not is_registered(username):
db = get_mongo()
db.users.insert_one({'username': username,
'userid': update.message.from_user.id})
update.message.reply_text("You're now registered as %s!" %
args[0])
else:
update.message.reply_text("Username is already in use!")
else:
db = get_mongo()
db.users.update({'userid': update.message.from_user.id},
{'$set': {'username': username}})
update.message.reply_text("Your nick was updated to %s!" %
args[0])
else:
update.message.reply_text("Usage: /register <username>")
add_to_chat(get_user(update.message.from_user.id), update.message.chat_id)
def deposit(bot, update):
update.message.reply_text(
"Your deposit address is %s" %
get_address(get_user(update.message.from_user.id)))
add_to_chat(get_user(update.message.from_user.id), update.message.chat_id)
def withdraw(bot, update):
bal = get_balance(get_user(update.message.from_user.id))
args = update.message.text.split()[1:]
if len(args) == 2:
try:
amount = Decimal(args[1])
except decimal.InvalidOperation:
update.message.reply_text("Usage: /withdraw <address> <amount>")
return
if bal - amount >= 0 and amount > 1:
if validate_address(args[0]):
rpc = AuthServiceProxy("http://%s:%s@%s:%d" %
(config['rpc']['user'],
config['rpc']['password'],
config['rpc']['host'],
config['rpc']['port']))
rpc.settxfee(0.5)
txid = rpc.sendtoaddress(args[0], amount-1)
give_balance(get_user(update.message.from_user.id), -amount)
update.message.reply_text(
"Withdrew %f Hush! TX: %s" %
(amount-1, "https://www.zpool.ca/explorer/HUSH?txid=" + txid))
else:
update.message.reply_text("Invalid address")
else:
update.message.reply_text("amount has to be more than 1, and " +
"you need to have enough money")
else:
update.message.reply_text("Usage: /withdraw <address> <amount>")
add_to_chat(get_user(update.message.from_user.id), update.message.chat_id)
def convert(bot, update):
args = update.message.text.split()[1:]
if len(args) == 3:
try:
amount = Decimal(args[0])
except decimal.InvalidOperation:
update.message.reply_text("Usage: /convert <amount> <from> <to>")
return
request = requests.get('https://api.cryptonator.com/api/ticker/%s-%s' %
(args[1], args[2]))
ticker = request.json()
if ticker['success']:
res = Decimal(ticker['ticker']['price']) * amount
base = ticker['ticker']['base']
target = ticker['ticker']['target']
update.message.reply_text("%f %s = %f %s" % (amount, base, res,
target))
else:
update.message.reply_text("Error: %s " % ticker['error'])
else:
update.message.reply_text("Usage: /convert <amount> <from> <to>")
add_to_chat(get_user(update.message.from_user.id), update.message.chat_id)
def market(bot, update):
args = update.message.text.split()[1:]
if len(args) > 0:
base = args[0]
if len(args) > 1:
target = args[1]
else:
target = 'usd'
else:
base = 'ok'
target = 'btc'
request = requests.get('https://api.cryptonator.com/api/full/%s-%s' %
(base, target))
ticker = request.json()
if ticker['success']:
price = Decimal(ticker['ticker']['price'])
volume = ticker['ticker']['volume']
change = ticker['ticker']['change']
markets = ticker['ticker']['markets']
message = "Price: %s Volume: %s Change: %s\n" % (price, volume, change)
for market in markets:
name = market['market']
price = market['price']
volume = market['volume']
message += "\n%s - Price: %s Volume: %s" % (name, price, volume)
update.message.reply_text(message)
else:
update.message.reply_text("Error: %s " % ticker['error'])
add_to_chat(get_user(update.message.from_user.id), update.message.chat_id)
if __name__ == "__main__":
updater = Updater(config['token'])
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('tip', tip))
updater.dispatcher.add_handler(CommandHandler('register', register))
updater.dispatcher.add_handler(CommandHandler('balance', balance))
updater.dispatcher.add_handler(CommandHandler('bal', balance))
updater.dispatcher.add_handler(CommandHandler('deposit', deposit))
updater.dispatcher.add_handler(CommandHandler('withdraw', withdraw))
updater.dispatcher.add_handler(CommandHandler('convert', convert))
updater.dispatcher.add_handler(CommandHandler('con', convert))
updater.dispatcher.add_handler(CommandHandler('market', market))
updater.dispatcher.add_handler(CommandHandler('soak', soak))
updater.start_polling()
updater.idle()