-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpacCoinTelegramTipBot.py
More file actions
executable file
·482 lines (355 loc) · 12.1 KB
/
pacCoinTelegramTipBot.py
File metadata and controls
executable file
·482 lines (355 loc) · 12.1 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import subprocess
import requests
import logging
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler
from telegram.ext import Filters
from telegram import ParseMode
from telegram import TelegramError
from telegram import Update
from telegram.ext.dispatcher import run_async
import pickledb
import re
import time
''' begin configuration section '''
BOTNAME = '____CHANGEME____'
TOKEN = '____CHANGEME____'
LOG_FILE = '____CHANGEME____'
DB_FILE = '____CHANGEME____'
APP_EXE = '____CHANGEME____'
''' end configuration section '''
''' DO NOT EDIT BELLOW THS LINE '''
fh = logging.FileHandler(LOG_FILE)
fh.setLevel(logging.INFO)
ft = logging.Formatter('%(asctime)-15s - %(message)s')
fh.setFormatter(ft)
logger = logging.getLogger(BOTNAME)
logger.setLevel(logging.INFO)
logger.addHandler(fh)
db = pickledb.load(DB_FILE, True)
def main():
updater = Updater(TOKEN, workers=10)
dp = updater.dispatcher
dp.add_handler(CommandHandler('hi', hi))
dp.add_handler(CommandHandler('moon', moon))
dp.add_handler(CommandHandler('info', info))
dp.add_handler(CommandHandler('help', help))
dp.add_handler(CommandHandler('balance', balance))
dp.add_handler(CommandHandler('withdraw', withdraw))
dp.add_handler(CommandHandler('deposit', deposit))
dp.add_handler(CommandHandler('tip', tip))
dp.add_handler(CommandHandler('mcap', mcap))
dp.add_handler(CommandHandler('price', price))
dp.add_handler(MessageHandler(Filters.all, events))
update_queue = updater.start_polling()
updater.idle()
def getUserID(bot, update, username):
cid = update.message.chat_id
value = None
users = db.getall()
for uid in users:
user = db.get(uid)
if user != username:
continue
user_data = bot.getChatMember(cid, uid)
user_name = user_data.user.username
db.set(str(uid), user_name)
if username == user_name:
value = uid
return value
def events(bot, update):
from_user = update.message.from_user
user_name = from_user.username
user_uuid = from_user.id
db.set(str(user_uuid), user_name)
return True
def receive_message(message):
msg = message.text
user = message.from_user.username
uuid = message.from_user.id
logger.info("@{} [#{}]: {}".format(user, uuid, msg))
return True
def send_message(bot, cid, msg, mode):
if "commands" not in msg and "cryptopia" not in msg:
logger.info("{}: {}".format(BOTNAME, msg))
mode = ParseMode.MARKDOWN or False
bot.send_message(chat_id=cid, text=msg, parse_mode=mode)
return True
def info(bot, update):
chat_uuid = update.message.chat_id
user_name = update.message.from_user.username
user_uuid = update.message.from_user.id
msg = \
"""
```
INFO SECTION
```
commands like */tip* & */withdraw* have a specfic format,\
use them like so:
Tipping format:
`/tip @[user] [amount] (without brackets)`
Withdrawing format:
`/withdraw [address] [amount] (without brackets)`
WHERE:
`
[address] = withdraw #PacCoin address
[user] = telegram username
[amount] = amount of #pacCoin to utilise
`
*NOTE*:
- don't deposit a significant amount of #PacCoin through this #BOT
- make sure that you enter a valid #PacCoin address when you perform a withdraw
- we are not responsible of your funds if something bad happen to this #BOT
```
USE THIS #BOT AT YOUR OWN RISK
```
"""
send_message(bot, chat_uuid, msg, True)
return True
def help(bot, update):
chat_uuid = update.message.chat_id
user_name = update.message.from_user.username
user_uuid = update.message.from_user.id
msg = \
"""
The following commands are at your disposal:
/info, /balance, /deposit, /withdraw, /tip and /price
"""
send_message(bot, chat_uuid, msg, True)
return True
def tip(bot, update):
chat_uuid = update.message.chat_id
receive_message(update.message)
user_name = update.message.from_user.username
user_uuid = update.message.from_user.id
message = update.message.text.split(' ')
if len(message) != 3:
msg = "Please use /tip <username> <amount>!"
send_message(bot, chat_uuid, msg, False)
return False
if user_name is None:
msg = "Please set a #telegram username!"
send_message(bot, chat_uuid, msg, False)
return False
if not isValidUsername(message[1]):
msg = "Please input a valid username (ex: @JonDoe)!"
send_message(bot, chat_uuid, msg, False)
return False
if not isValidAmount(message[2]):
msg = "Please input a valid amount (ex: 1000)!"
send_message(bot, chat_uuid, msg, False)
return False
amount = float(message[2])
target = (message[1])[1:]
if amount > 100000:
msg = "Please send a lower amount (max: 100,000 PacCoin)!"
send_message(bot, chat_uuid, msg, False)
return False
if target == user_name:
msg = "You can't tip yourself silly.!"
send_message(bot, chat_uuid, msg, False)
return False
if target == BOTNAME:
bot.send_message(chat_id=chat_uuid, text='HODL.')
return False
target_uuid = getUserID(bot, update, target)
if not target_uuid:
msg = '@{} has no activity in this chat!'.format(target)
send_message(bot, chat_uuid, msg, False)
return False
result = subprocess.run([
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'getbalance',
str(user_uuid)
],
stdout=subprocess.PIPE)
balance = float(result.stdout.strip().decode('utf-8'))
if balance < amount:
msg = '@{0} you have insufficent funds.'.format(user_name)
send_message(bot, chat_uuid, msg, False)
return False
tx = subprocess.run([
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'move',
str(user_uuid),
str(target_uuid),
str(amount),
],
stdout=subprocess.PIPE)
msg = '@{0} tipped @{1} of {2} PacCoin'.format(user_name, target, amount)
send_message(bot, chat_uuid, msg, False)
return True
def balance(bot, update):
chat_uuid = update.message.chat_id
user_name = update.message.from_user.username
user_uuid = update.message.from_user.id
if user_name is None:
msg = "Please set a telegram username in your profile settings!"
send_message(bot, chat_uuid, msg, False)
return False
result = subprocess.run([
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'getbalance',
str(user_uuid)
],
stdout=subprocess.PIPE)
clean = result.stdout.strip().decode('utf-8')
balance = float(clean)
balance = str('{:,.8f}'.format(balance))
msg = '@{0} your current balance is: {1} PacCoin'.format(user_name, balance)
send_message(bot, chat_uuid, msg, False)
return True
def deposit(bot, update):
chat_uuid = update.message.chat_id
user_name = update.message.from_user.username
user_uuid = update.message.from_user.id
if user_name is None:
msg = 'Please set a telegram username in your profile settings!'
send_message(bot, chat_uuid, msg, False)
return False
result = subprocess.run([
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'getaccountaddress',
str(user_uuid)
],
stdout=subprocess.PIPE)
clean = result.stdout.strip().decode('utf-8')
msg = '@{} your depositing address is: {}'.format(user_name, clean)
send_message(bot, chat_uuid, msg, False)
return True
def withdraw(bot, update):
chat_uuid = update.message.chat_id
receive_message(update.message)
user_name = update.message.from_user.username
user_uuid = update.message.from_user.id
if user_name is None:
msg = 'Please set a telegram username!'
send_message(bot, chat_uuid, msg, False)
return False
message = update.message.text.split(' ')
if len(message) != 3:
msg = 'Please use /withdraw <address> <amount>!'
send_message(bot, chat_uuid, msg, False)
return False
if not isValidAddress(message[1]):
msg = 'Please input a valid PacCoin address!'
send_message(bot, chat_uuid, msg, False)
return False
if not isValidAmount(message[2]):
msg = 'Please input a valid amount (ex: 1000)!'
send_message(bot, chat_uuid, msg, False)
return False
amount = float(message[2])
address = message[1]
result = subprocess.run([
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'getbalance',
str(user_uuid)
],
stdout=subprocess.PIPE)
clean = result.stdout.strip().decode('utf-8')
balance = float(clean)
if balance < amount:
msg = '@{0} you have insufficent funds.'.format(user_name)
send_message(bot, chat_uuid, msg, False)
return False
tx = subprocess.run([
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'sendfrom',
str(user_uuid),
str(address),
str(amount),
],
stdout=subprocess.PIPE)
msg = '@{0} has successfully withdrew {2} PacCoin to address: {1}'\
.format(user_name, address, amount)
send_message(bot, chat_uuid, msg, False)
return True
def price(bot, update):
chat_uuid = update.message.chat_id
getmarket = 'https://chart.meanxtrade.com/info.php?market=PacCoin_LTC'
response = requests.get(getmarket)
json_data = response.json()
if not json_data:
msg = 'Failed to get data from cryptopia exchange!'
send_message(bot, chat_uuid, msg, False)
return False
message = {}
message['*LastPrice*'] = '{:,.8f}'.format(float(json_data['last']))
message['*AskPrice*'] = '{:,.8f}'.format(float(json_data['lowestAsk']))
message['*BidPrice*'] = '{:,.8f}'.format(float(json_data['highestBid']))
message['*Volume*'] = '{:,.8f}'.format(float(json_data['baseVolume']))
message['*High24Hr*'] = '{:,.8f}'.format(float(json_data['high24hr']))
message['*Low24Hr*'] = '{:,.8f}'.format(float(json_data['low24hr']))
message['*p%rate*'] = '{:,.8f}'.format(float(json_data['percentChange']))
pretty_object = json.dumps(message, indent=4, sort_keys=True)
label_market = json_data['Data']['Label']
msg = '*cryptopia* \[{0}]:\n{1}'.format(label_market, pretty_object)
send_message(bot, chat_uuid, msg, True)
return True
def mcap(bot, update):
chat_uuid = update.message.chat_id
user_name = update.message.from_user.usernamei
user_uuid = update.message.from_user.id
bot.send_message(chat_id=chat_uuid, text='work in progress!')
return True
def moon(bot, update):
chat_uuid = update.message.chat_id
user_name = update.message.from_user.username
user_uuid = update.message.from_user.id
bot.send_message(chat_id=chat_uuid, text='Moon mission inbound!')
return True
def hi(bot, update):
chat_uuid = update.message.chat_id
user_name = update.message.from_user.username
user_uuid = update.message.from_user.id
msg = 'Hello @{0}, how are you doing today?'.format(user_name)
send_message(bot, chat_uuid, msg, False)
return True
def isValidAddress(param):
if len(param) < 30:
return False
elif len(param) > 35:
return False
elif not param.isalnum():
return False
elif not param[0] == 'K':
return False
else:
return True
def isValidUsername(user):
if len(user) < 7:
return False
elif len(user) > 35:
return False
elif not re.match('^@[0-9A-Za-z_]+$', user):
return False
else:
return True
def isValidAmount(amount):
try:
float(amount)
return True
except ValueError:
pass
if amount.isnumeric():
return True
return False
if __name__ == '__main__':
main()