-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable Telegram notifications for order takers #244
Changes from all commits
ae1fede
46ee155
3e443a2
b760b6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ class Telegram(): | |
''' Simple telegram messages by requesting to API''' | ||
|
||
session = get_session() | ||
site = config('HOST_NAME') | ||
|
||
def get_context(user): | ||
"""returns context needed to enable TG notifications""" | ||
|
@@ -41,188 +42,131 @@ def send_message(self, user, text): | |
return | ||
except: | ||
pass | ||
|
||
def welcome(self, user): | ||
''' User enabled Telegram Notifications''' | ||
lang = user.profile.telegram_lang_code | ||
|
||
# In weird cases the order cannot be found (e.g. it is cancelled) | ||
queryset = Order.objects.filter(maker=user) | ||
order = queryset.last() | ||
|
||
print(str(order.id)) | ||
if lang == 'es': | ||
text = f'Hola {user.username}, te enviaré un mensaje cuando tu orden con ID {str(order.id)} haya sido tomada.' | ||
text = f'Hola {user.username}, te enviaré notificaciones sobre tus órdenes en RoboSats.' | ||
else: | ||
text = f"Hey {user.username}, I will send you a message when someone takes your order with ID {str(order.id)}." | ||
text = f"Hey {user.username}, I will send you notifications about your RoboSats orders." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe not for this PR but probably it's worth it to include also i18n on the backend https://github.com/danhper/python-i18n There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, agree. There is no internationalization implemented on the backend yet, but would be very useful for the Telegram messages. |
||
self.send_message(user, text) | ||
user.profile.telegram_welcomed = True | ||
user.profile.save() | ||
return | ||
|
||
def order_taken(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
# def welcome(self, user): | ||
# lang = user.profile.telegram_lang_code | ||
|
||
# # In weird cases the order cannot be found (e.g. it is cancelled) | ||
# queryset = Order.objects.filter(maker=user) | ||
# order = queryset.last() | ||
|
||
# print(str(order.id)) | ||
# if lang == 'es': | ||
# text = f'Hola {user.username}, te enviaré un mensaje cuando tu orden con ID {str(order.id)} haya sido tomada.' | ||
# else: | ||
# text = f"Hey {user.username}, I will send you a message when someone takes your order with ID {str(order.id)}." | ||
# self.send_message(user, text) | ||
# user.profile.telegram_welcomed = True | ||
# user.profile.save() | ||
# return | ||
|
||
lang = user.profile.telegram_lang_code | ||
taker_nick = order.taker.username | ||
site = config('HOST_NAME') | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username} ¡Tu orden con ID {order.id} ha sido tomada por {taker_nick}!🥳 Visita http://{site}/order/{order.id} para continuar.' | ||
else: | ||
text = f'Hey {order.maker.username}, your order was taken by {taker_nick}!🥳 Visit http://{site}/order/{order.id} to proceed with the trade.' | ||
|
||
self.send_message(user, text) | ||
return | ||
|
||
def order_taken_confirmed(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
if order.maker.profile.telegram_enabled: | ||
lang = order.maker.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username} ¡Tu orden con ID {order.id} ha sido tomada por {order.taker.username}!🥳 Visita http://{self.site}/order/{order.id} para continuar.' | ||
else: | ||
text = f'Hey {order.maker.username}, your order was taken by {order.taker.username}!🥳 Visit http://{self.site}/order/{order.id} to proceed with the trade.' | ||
self.send_message(order.maker, text) | ||
|
||
if order.taker.profile.telegram_enabled: | ||
lang = order.taker.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {order.taker.username}, acabas de tomar la orden con ID {order.id}.' | ||
else: | ||
text = f'Hey {order.taker.username}, you just took the order with ID {order.id}.' | ||
self.send_message(order.taker, text) | ||
|
||
lang = user.profile.telegram_lang_code | ||
taker_nick = order.taker.username | ||
site = config('HOST_NAME') | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username} ¡Tu orden con ID {order.id} ha sido tomada por {taker_nick}!🥳 El tomador ya ha bloqueado su fianza. Visita http://{site}/order/{order.id} para continuar.' | ||
else: | ||
text = f'Hey {order.maker.username}, your order with ID {order.id} was taken by {taker_nick}!🥳 The taker bond has already been locked. Visit http://{site}/order/{order.id} to proceed with the trade.' | ||
|
||
self.send_message(user, text) | ||
return | ||
|
||
def fiat_exchange_starts(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
|
||
lang = user.profile.telegram_lang_code | ||
site = config('HOST_NAME') | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, el depósito de garantía y el recibo del comprador han sido recibidos. Es hora de enviar el dinero fiat. Visita http://{site}/order/{order.id} para hablar con tu contraparte.' | ||
else: | ||
text = f'Hey {order.maker.username}, the escrow and invoice have been submitted. The fiat exchange starts now via the platform chat. Visit http://{site}/order/{order.id} to talk with your counterpart.' | ||
|
||
self.send_message(user, text) | ||
for user in [order.maker, order.taker]: | ||
if user.profile.telegram_enabled: | ||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {user.username}, el depósito de garantía y el recibo del comprador han sido recibidos. Es hora de enviar el dinero fiat. Visita http://{self.site}/order/{order.id} para hablar con tu contraparte.' | ||
else: | ||
text = f'Hey {user.username}, the escrow and invoice have been submitted. The fiat exchange starts now via the platform chat. Visit http://{self.site}/order/{order.id} to talk with your counterpart.' | ||
self.send_message(user, text) | ||
return | ||
|
||
def order_expired_untaken(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
|
||
lang = user.profile.telegram_lang_code | ||
site = config('HOST_NAME') | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, tu orden con ID {order.id} ha expirado sin ser tomada por ningún robot. Visita http://{site}/order/{order.id} para renovarla.' | ||
else: | ||
text = f'Hey {order.maker.username}, your order with ID {order.id} has expired without a taker. Visit http://{site}/order/{order.id} to renew it.' | ||
|
||
self.send_message(user, text) | ||
if order.maker.profile.telegram_enabled: | ||
lang = order.maker.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, tu orden con ID {order.id} ha expirado sin ser tomada por ningún robot. Visita http://{self.site}/order/{order.id} para renovarla.' | ||
else: | ||
text = f'Hey {order.maker.username}, your order with ID {order.id} has expired without a taker. Visit http://{self.site}/order/{order.id} to renew it.' | ||
self.send_message(order.maker, text) | ||
return | ||
|
||
def trade_successful(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
|
||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'¡Tu orden con ID {order.id} ha finalizado exitosamente!⚡ Únete a nosotros en @robosats_es y ayúdanos a mejorar.' | ||
else: | ||
text = f'Your order with ID {order.id} has finished successfully!⚡ Join us @robosats and help us improve.' | ||
|
||
self.send_message(user, text) | ||
for user in [order.maker, order.taker]: | ||
if user.profile.telegram_enabled: | ||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'¡Tu orden con ID {order.id} ha finalizado exitosamente!⚡ Únete a nosotros en @robosats_es y ayúdanos a mejorar.' | ||
else: | ||
text = f'Your order with ID {order.id} has finished successfully!⚡ Join us @robosats and help us improve.' | ||
self.send_message(user, text) | ||
return | ||
|
||
def public_order_cancelled(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
|
||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, has cancelado tu orden pública con ID {order.id}.' | ||
else: | ||
text = f'Hey {order.maker.username}, you have cancelled your public order with ID {order.id}.' | ||
|
||
self.send_message(user, text) | ||
return | ||
|
||
def taker_canceled_b4bond(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
|
||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, el tomador ha cancelado antes de bloquear su fianza.' | ||
else: | ||
text = f'Hey {order.maker.username}, the taker has canceled before locking the bond.' | ||
|
||
self.send_message(user, text) | ||
return | ||
|
||
def taker_expired_b4bond(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
|
||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, el tomador no ha bloqueado la fianza a tiempo.' | ||
else: | ||
text = f'Hey {order.maker.username}, the taker has not locked the bond in time.' | ||
|
||
self.send_message(user, text) | ||
if order.maker.profile.telegram_enabled: | ||
lang = order.maker.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, has cancelado tu orden pública con ID {order.id}.' | ||
else: | ||
text = f'Hey {order.maker.username}, you have cancelled your public order with ID {order.id}.' | ||
self.send_message(order.maker, text) | ||
return | ||
|
||
def collaborative_cancelled(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
|
||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, tu orden con ID {str(order.id)} fue cancelada colaborativamente.' | ||
else: | ||
text = f'Hey {order.maker.username}, your order with ID {str(order.id)} has been collaboratively cancelled.' | ||
|
||
self.send_message(user, text) | ||
for user in [order.maker, order.taker]: | ||
if user.profile.telegram_enabled: | ||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {user.username}, tu orden con ID {str(order.id)} fue cancelada colaborativamente.' | ||
else: | ||
text = f'Hey {user.username}, your order with ID {str(order.id)} has been collaboratively cancelled.' | ||
self.send_message(user, text) | ||
return | ||
|
||
def dispute_opened(self, order): | ||
user = order.maker | ||
if not user.profile.telegram_enabled: | ||
return | ||
|
||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, la orden con ID {str(order.id)} ha entrado en disputa.' | ||
else: | ||
text = f'Hey {order.maker.username}, a dispute has been opened on your order with ID {str(order.id)}.' | ||
|
||
self.send_message(user, text) | ||
for user in [order.maker, order.taker]: | ||
if user.profile.telegram_enabled: | ||
lang = user.profile.telegram_lang_code | ||
if lang == 'es': | ||
text = f'Hey {user.username}, la orden con ID {str(order.id)} ha entrado en disputa.' | ||
else: | ||
text = f'Hey {user.username}, a dispute has been opened on your order with ID {str(order.id)}.' | ||
self.send_message(user, text) | ||
return | ||
|
||
def order_published(self, order): | ||
|
||
time.sleep(1) # Just so this message always arrives after the previous two | ||
|
||
user = order.maker | ||
lang = user.profile.telegram_lang_code | ||
|
||
# In weird cases the order cannot be found (e.g. it is cancelled) | ||
|
||
queryset = Order.objects.filter(maker=user) | ||
order = queryset.last() | ||
|
||
print(str(order.id)) | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, tu orden con ID {str(order.id)} es pública en el libro de ordenes.' | ||
else: | ||
text = f"Hey {order.maker.username}, your order with ID {str(order.id)} is public in the order book." | ||
self.send_message(user, text) | ||
user.profile.telegram_welcomed = True | ||
user.profile.save() | ||
if order.maker.profile.telegram_enabled: | ||
lang = order.maker.profile.telegram_lang_code | ||
# In weird cases the order cannot be found (e.g. it is cancelled) | ||
queryset = Order.objects.filter(maker=order.maker) | ||
if len(queryset) == 0: | ||
return | ||
order = queryset.last() | ||
if lang == 'es': | ||
text = f'Hey {order.maker.username}, tu orden con ID {str(order.id)} es pública en el libro de ordenes.' | ||
else: | ||
text = f"Hey {order.maker.username}, your order with ID {str(order.id)} is public in the order book." | ||
self.send_message(order.maker, text) | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of leaving commented code, do you want to just check how it goes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree,
I had commented out this line long time ago. I was not sure the solution we found was good enough. But now that I revisited this file, I think it's clear the solution worked out and it is time to delete this commented code, forever (actually, this PR also deletes the function this line is calling to) :D