forked from valgilbert/pacCoinTipBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoxdCoinTelegramTipBot.py
More file actions
executable file
·283 lines (242 loc) · 8.59 KB
/
foxdCoinTelegramTipBot.py
File metadata and controls
executable file
·283 lines (242 loc) · 8.59 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import json
import subprocess
import logging
from typing import Any
import pickledb
from telegram import ParseMode
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
''' begin configuration section '''
BOTNAME = '____CHANGEME____'
TOKEN = '____CHANGEME____'
LOG_FILE = '____CHANGEME____'
DB_FILE = '____CHANGEME____'
CLI_EXE = '____CHANGEME____'
RPC_USER = '____CHANGEME____'
RPC_PASS = '____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('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(MessageHandler(Filters.all, events))
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(f"@{user} [#{uuid}]: {msg}")
return True
def send_message(bot, cid, msg, mode):
if "commands" not in msg and "cryptopia" not in msg:
logger.info(f"{BOTNAME}: {msg}")
mode = ParseMode.MARKDOWN or False
bot.send_message(chat_id=cid, text=msg, parse_mode=mode)
return True
def info(bot, update):
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 #FoxdCoin address
[user] = telegram username
[amount] = amount of #foxdCoin to utilise
`
*NOTE*:
- don't deposit a significant amount of #FoxdCoin through this #BOT
- make sure that you enter a valid #FoxdCoin 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
```
"""
chat_uuid = update.message.chat_id
send_message(bot, chat_uuid, msg, True)
return True
def help(bot, update):
msg = \
"""
The following commands are at your disposal:
/info, /balance, /deposit, /withdraw, /tip and /price
"""
chat_uuid = update.message.chat_id
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 FoxdCoin)!"
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 = f'@{target} has no activity in this chat!'
send_message(bot, chat_uuid, msg, False)
return False
balance = rpc_call('getbalance', [str(user_uuid)])
if balance < amount:
msg = f'@{user_name} you have insufficent funds.'
send_message(bot, chat_uuid, msg, False)
return False
rpc_call('move', [str(user_uuid), str(target_uuid), str(amount)])
msg = f'@{user_name} tipped @{target} of {amount} FoxdCoin'
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 = rpc_call('getbalance', [str(user_uuid)])
balance = str(round(float(result), 8))
msg = f'@{user_name} your current balance is: {balance} FoxdCoin'
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
p2pkh = rpc_call('getaccountaddress', [str(user_uuid)])
msg = f'@{user_name} your depositing address is: {p2pkh}'
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
is_valid = rpc_call('validateaddress', [message[1]])['isvalid']
if not is_valid:
msg = 'Please input a valid FoxdCoin 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]
balance = float(rpc_call('getbalance', [str(user_uuid)]))
if balance < amount:
msg = f'@{user_name} you have insufficent funds.'
send_message(bot, chat_uuid, msg, False)
return False
rpc_call('sendfrom', [str(user_uuid), str(address), str(amount)])
msg = f'@{user_name} has successfully withdrew {amount} FoxdCoin to address: {address}'
send_message(bot, chat_uuid, msg, False)
return True
def rpc_call(method: str, params: list[Any]) -> dict[str, Any] | None:
try: result = subprocess.run((
lambda m, p: [CLI_EXE,
f'-rpcuser={RPC_USER}',
f'-rpcpassword={RPC_PASS}',
m, *p])(method, params),
stdout = subprocess.PIPE)
except: return
ret = result.stdout.strip().decode('utf-8')
if len(ret) == 0: return
else: return json.loads(ret)
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()