-
Notifications
You must be signed in to change notification settings - Fork 7
/
__main__.py
278 lines (211 loc) · 8.73 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
from random import randint
import logging
from telegram.ext.dispatcher import run_async
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import ParseMode
from config import TELEGRAM_TOKEN, RANDOM_SALT, DEFINE
from redchaucha import *
from contrib.setexredis import *
from contrib.precios import precio
logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
@run_async
def mensajes(bot, update, args):
try:
user = update.message.from_user
addr = getaddress(user.id)[0]
max_read = int(args[0])
if max_read > 0:
msg = getTx(addr, max_read)
if len(msg) == 0:
msg = 'No tienes mensajes en el blockchain'
except:
msg = "Error >:C\nIntenta más tarde...\n\n"
msg += "Modo de uso: /mensajes cantidad"
logger.info("mensajes(%i) => %s" % (user.id, msg))
update.message.reply_text("%s" % msg, parse_mode=ParseMode.MARKDOWN)
@run_async
def op_return(bot, update, args):
try:
user = update.message.from_user
info = getaddress(user.id)
op_return = ' '.join(args)
if len(op_return) > 0:
msg = sendTx(info, 0.001, info[0], op_return)
else:
msg = "No hay mensaje, no puedo hacer nada :C"
except:
msg = "Error >:C\nIntenta más tarde..."
logger.info("op_return(%i) => %s" % (user.id, msg))
update.message.reply_text("%s" % msg)
@run_async
def start(bot, update):
msg = "*Holi !*"
logger.info("start() => %s" % (msg))
update.message.reply_text("%s" % msg, parse_mode=ParseMode.MARKDOWN)
@run_async
def qr(bot, update):
user = update.message.from_user
info = getaddress(user.id)
logger.info("qr(%i) => %s" % (user.id, info[0]))
update.message.reply_photo(
photo='https://api.qrserver.com/v1/create-qr-code/?size=300x300&margin=2&data=' + info[0])
@run_async
def send(bot, update, args):
try:
user = update.message.from_user
info = getaddress(user.id)
amount = float(args[0])
receptor = args[1]
msg = sendTx(info, amount, receptor, 'Quirquincho')
except:
msg = "Error >:C\nIntenta más tarde...\n\n"
msg += "Modo de uso: /send monto address"
logger.info("send(%i) => %s" % (user.id, msg))
update.message.reply_text("%s" % msg)
@run_async
def balance(bot, update):
try:
# Descubrimiento de address
user = update.message.from_user
addr = getaddress(user.id)[0]
confirmed, inputs, unconfirmed = getbalance(addr)
total = confirmed + unconfirmed
# MSG
msg = "Tienes %.8f CHA en tu dirección\n"
msg += "%.8f CHA para utilizar y %.8f CHA sin confirmar.\n\n"
msg += "Tu address es %s"
msg = msg % (total, confirmed, unconfirmed, addr)
except:
msg = "No se pudo ejecutar la lectura de balance :C"
logger.info("balance(%i) => %s" % (user.id, msg.replace('\n', ' - ')))
update.message.reply_text("%s" % msg)
@run_async
def dice(bot, update, args):
try:
user = update.message.from_user
usrInfo = getaddress(user.id)
botBalance = getbalance(quirquincho[0])[0]
usrBalance = getbalance(usrInfo[0])[0]
if args[0] == 'house':
msg = 'Aún puedes ganar %.8f chauchas!' % float(botBalance)
else:
bet = float(args[0])
betFee = round(bet*0.025, 8) # Fee del 2.5%
if usrBalance >= bet and botBalance > 0 and bet > 0.001:
num = randint(0, 50)
msg = '%.8f CHA (fee: %.8f)\n' % (bet, betFee)
if num >= 25:
msg = 'Perdiste ' + msg
msg += sendTx(usrInfo, bet, quirquincho[0], '/dice')
else:
msg = 'Ganaste ' + msg
msg += sendTx(quirquincho, bet, usrInfo[0], '/dice')
sendTx(usrInfo, betFee, quirquincho[0], 'dice tax')
else:
msg = 'No tienes chauchas suficientes'
except:
msg = "Error >:C\nIntenta más tarde...\n\n"
msg += 'Modo de uso: \n - /dice cantidad\n - /dice house'
logger.info("dice(%i) => %s" % (user.id, msg.replace('\n', ' - ')))
update.message.reply_text("%s" % msg)
@run_async
def define(bot, update, args):
try:
word = args[0] # Si no viene el argumento se cae
originalword = args[0]
if len(args) > 1:
word = " ".join(args)
word = word.lower()
if args[0] == 'url':
msg = DEFINE
user = update.message.from_user
usrInfo = getaddress(user.id)
botBalance = getbalance(quirquincho[0])[0]
botBalanceDefine = getbalance(quirquinchoDefine[0])[0]
# print(botBalanceDefine)
usrBalance = getbalance(usrInfo[0])[0]
info = get(DEFINE)
# Se setea el encoding a ISO-8859-1
info.encoding = info.apparent_encoding
info = info.json()
val = 0
for definitions in info:
if definitions['title'].lower() == word:
retorno = redisWeekValidation(word, user)
msg = retorno + \
"*%s*: %s" % (originalword.capitalize(),
definitions['definition'])
if retorno == "" and user.id not in set(getWinners()) and redisDayValidation(word, user):
textoTransaccion = sendTx(
quirquinchoDefine, 1, usrInfo[0], '/define')
if not textoTransaccion.startswith("https://explorer"):
logger.info("/define sendTx (%i) => %s" %
(user.id, textoTransaccion))
else:
texto = "!!*Has ganado una chaucha*!!\n\nQuirquincho te la enviará a tu direccion: %s\n\n" % getaddress(user.id)[
0]
msg = texto+msg+"\n\n"
msg += textoTransaccion
# Se incluye en lista de ganadores. Son 14. Al 15avo se comienza a quitar de la lista
arrayWinners(user)
elif retorno == "":
msg = "!!*Busca una nueva definición y obtén tu chaucha diaria*!!.\n\n" + \
msg # Este tiene un pto para identificar donde cae
val = 1
break
if val == 0:
msg = "No existe la definicion :'( \n\n"
msg += "Revisar el link %s para más definiciones" % DEFINE
#msg += "Si quieres proponer esta definicion utiliza el comando adddefinition"
except Exception as e:
print(e)
msg = "Error >:C\nIntenta más tarde...\n\n"
msg += 'Modo de uso: \n - /define concepto'
#logger.info("define(%s) => %s" % (user.username, msg.replace('\n', ' - ')))
update.message.reply_text("%s" % msg, parse_mode=ParseMode.MARKDOWN)
@run_async
def azar(bot, update, args):
try:
max_number = int(args[0])
rand_number = randint(0, max_number)
msg = "Número al azar (0, %s): %s\n" % (max_number, rand_number)
except:
msg = "Error >:C\n\n"
msg += "Modo de uso: /azar [N]"
logger.info("random(%i) => %s" % (update.message.from_user.id, msg))
update.message.reply_text("%s" % msg)
def error(bot, update, error):
logger.warning('Update: "%s" - Error: "%s"' % (update, error))
# Main loop
def main():
global quirquincho
global quirquinchoDefine
# Configuración
updater = Updater(TELEGRAM_TOKEN)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# Listado de comandos
dp.add_handler(CommandHandler("qr", qr))
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("balance", balance))
dp.add_handler(CommandHandler("dice", dice, pass_args=True))
dp.add_handler(CommandHandler("define", define, pass_args=True))
dp.add_handler(CommandHandler("mensajes", mensajes, pass_args=True))
dp.add_handler(CommandHandler("op_return", op_return, pass_args=True))
dp.add_handler(CommandHandler("send", send, pass_args=True))
dp.add_handler(CommandHandler("azar", azar, pass_args=True))
dp.add_handler(CommandHandler("precio", precio, pass_args=True))
# log all errors
dp.add_error_handler(error)
quirquincho = getaddress('Quirquincho' + str(RANDOM_SALT))
# Se hace una wallet nueva para el traspaso de coins
quirquinchoDefine = getaddress(str(RANDOM_SALT))
# Inicio de bot
logger.info("Quirquincho V 2.0 - %s" % quirquincho[0])
logger.info("QuirquinchoDefine V 2.0 - %s" % quirquinchoDefine[0])
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()