2022-02-21 23:41:36 +00:00
from secrets import token_urlsafe
2022-10-25 18:04:12 +00:00
from decouple import config
2022-02-21 23:41:36 +00:00
from api . models import Order
2022-08-08 15:58:06 +00:00
from api . utils import get_session
2022-02-21 23:41:36 +00:00
2022-10-20 09:56:10 +00:00
class Telegram :
""" Simple telegram messages by requesting to API """
2022-02-21 23:41:36 +00:00
2022-08-08 15:58:06 +00:00
session = get_session ( )
2022-10-20 09:56:10 +00:00
site = config ( " HOST_NAME " )
2022-02-22 00:55:31 +00:00
2022-02-21 23:41:36 +00:00
def get_context ( user ) :
""" returns context needed to enable TG notifications """
context = { }
2022-10-20 09:56:10 +00:00
if user . profile . telegram_enabled :
context [ " tg_enabled " ] = True
2022-02-21 23:41:36 +00:00
else :
2022-10-20 09:56:10 +00:00
context [ " tg_enabled " ] = False
2022-10-20 20:53:51 +00:00
if user . profile . telegram_token is None :
2022-02-21 23:41:36 +00:00
user . profile . telegram_token = token_urlsafe ( 15 )
user . profile . save ( )
2022-10-20 09:56:10 +00:00
context [ " tg_token " ] = user . profile . telegram_token
context [ " tg_bot_name " ] = config ( " TELEGRAM_BOT_NAME " )
2022-02-21 23:41:36 +00:00
return context
2022-02-22 00:55:31 +00:00
def send_message ( self , user , text ) :
2022-10-20 09:56:10 +00:00
""" sends a message to a user with telegram notifications enabled """
2022-02-21 23:41:36 +00:00
2022-10-20 09:56:10 +00:00
bot_token = config ( " TELEGRAM_TOKEN " )
2022-02-21 23:41:36 +00:00
chat_id = user . profile . telegram_chat_id
2022-10-20 09:56:10 +00:00
message_url = f " https://api.telegram.org/bot { bot_token } /sendMessage?chat_id= { chat_id } &text= { text } "
2022-03-01 21:34:25 +00:00
# if it fails, it should keep trying
while True :
try :
self . session . get ( message_url ) . json ( )
return
2022-10-20 20:53:51 +00:00
except Exception :
2022-03-01 21:34:25 +00:00
pass
2022-10-20 09:56:10 +00:00
2022-02-22 01:50:32 +00:00
def welcome ( self , user ) :
2022-10-20 09:56:10 +00:00
""" User enabled Telegram Notifications """
2022-02-21 23:41:36 +00:00
lang = user . profile . telegram_lang_code
2022-02-24 21:59:16 +00:00
2022-10-20 09:56:10 +00:00
if lang == " es " :
text = f " Hola { user . username } , te enviaré notificaciones sobre tus órdenes en RoboSats. "
2022-02-21 23:41:36 +00:00
else :
2022-09-15 15:42:33 +00:00
text = f " Hey { user . username } , I will send you notifications about your RoboSats orders. "
2022-02-22 02:12:42 +00:00
self . send_message ( user , text )
2022-02-24 13:01:17 +00:00
user . profile . telegram_welcomed = True
user . profile . save ( )
2022-02-21 23:41:36 +00:00
return
2022-03-11 15:24:39 +00:00
def order_taken_confirmed ( self , order ) :
2022-09-15 15:42:33 +00:00
if order . maker . profile . telegram_enabled :
lang = order . maker . profile . telegram_lang_code
2022-10-20 09:56:10 +00:00
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. "
2022-09-15 15:42:33 +00:00
else :
2022-10-20 09:56:10 +00:00
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. "
2022-09-15 15:42:33 +00:00
self . send_message ( order . maker , text )
if order . taker . profile . telegram_enabled :
lang = order . taker . profile . telegram_lang_code
2022-10-20 09:56:10 +00:00
if lang == " es " :
text = f " Hey { order . taker . username } , acabas de tomar la orden con ID { order . id } . "
2022-09-15 15:42:33 +00:00
else :
2022-10-20 09:56:10 +00:00
text = f " Hey { order . taker . username } , you just took the order with ID { order . id } . "
2022-09-15 15:42:33 +00:00
self . send_message ( order . taker , text )
2022-03-11 15:24:39 +00:00
return
2022-04-29 18:54:20 +00:00
def fiat_exchange_starts ( self , order ) :
2022-09-15 15:42:33 +00:00
for user in [ order . maker , order . taker ] :
if user . profile . telegram_enabled :
lang = user . profile . telegram_lang_code
2022-10-20 09:56:10 +00:00
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. "
2022-09-15 15:42:33 +00:00
else :
2022-10-20 09:56:10 +00:00
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. "
2022-09-15 15:42:33 +00:00
self . send_message ( user , text )
2022-04-29 18:54:20 +00:00
return
2022-02-22 17:50:01 +00:00
def order_expired_untaken ( self , order ) :
2022-09-15 15:42:33 +00:00
if order . maker . profile . telegram_enabled :
lang = order . maker . profile . telegram_lang_code
2022-10-20 09:56:10 +00:00
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. "
2022-09-15 15:42:33 +00:00
else :
2022-10-20 09:56:10 +00:00
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. "
2022-09-15 15:42:33 +00:00
self . send_message ( order . maker , text )
2022-02-22 17:50:01 +00:00
return
def trade_successful ( self , order ) :
2022-09-15 15:42:33 +00:00
for user in [ order . maker , order . taker ] :
if user . profile . telegram_enabled :
lang = user . profile . telegram_lang_code
2022-10-20 09:56:10 +00:00
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. "
2022-09-15 15:42:33 +00:00
else :
2022-10-20 09:56:10 +00:00
text = f " Your order with ID { order . id } has finished successfully!⚡ Join us @robosats and help us improve. "
2022-09-15 15:42:33 +00:00
self . send_message ( user , text )
2022-02-23 16:15:48 +00:00
return
def public_order_cancelled ( self , order ) :
2022-09-15 15:42:33 +00:00
if order . maker . profile . telegram_enabled :
lang = order . maker . profile . telegram_lang_code
2022-10-20 09:56:10 +00:00
if lang == " es " :
text = f " Hey { order . maker . username } , has cancelado tu orden pública con ID { order . id } . "
2022-09-15 15:42:33 +00:00
else :
2022-10-20 09:56:10 +00:00
text = f " Hey { order . maker . username } , you have cancelled your public order with ID { order . id } . "
2022-09-15 15:42:33 +00:00
self . send_message ( order . maker , text )
2022-03-01 13:38:04 +00:00
return
2022-10-20 09:56:10 +00:00
2022-06-02 22:32:01 +00:00
def collaborative_cancelled ( self , order ) :
2022-09-15 15:42:33 +00:00
for user in [ order . maker , order . taker ] :
if user . profile . telegram_enabled :
lang = user . profile . telegram_lang_code
2022-10-20 09:56:10 +00:00
if lang == " es " :
text = f " Hey { user . username } , tu orden con ID { str ( order . id ) } fue cancelada colaborativamente. "
2022-09-15 15:42:33 +00:00
else :
2022-10-20 09:56:10 +00:00
text = f " Hey { user . username } , your order with ID { str ( order . id ) } has been collaboratively cancelled. "
2022-09-15 15:42:33 +00:00
self . send_message ( user , text )
2022-06-02 22:32:01 +00:00
return
2022-10-20 09:56:10 +00:00
2022-06-02 22:32:01 +00:00
def dispute_opened ( self , order ) :
2022-09-15 15:42:33 +00:00
for user in [ order . maker , order . taker ] :
if user . profile . telegram_enabled :
lang = user . profile . telegram_lang_code
2022-10-20 09:56:10 +00:00
if lang == " es " :
text = f " Hey { user . username } , la orden con ID { str ( order . id ) } ha entrado en disputa. "
2022-09-15 15:42:33 +00:00
else :
2022-10-20 09:56:10 +00:00
text = f " Hey { user . username } , a dispute has been opened on your order with ID { str ( order . id ) } . "
2022-09-15 15:42:33 +00:00
self . send_message ( user , text )
2022-06-02 22:32:01 +00:00
return
2022-03-01 13:38:04 +00:00
def order_published ( self , order ) :
2022-09-15 15:42:33 +00:00
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 ( )
2022-10-20 09:56:10 +00:00
if lang == " es " :
text = f " Hey { order . maker . username } , tu orden con ID { str ( order . id ) } es pública en el libro de ordenes. "
2022-09-15 15:42:33 +00:00
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 )
2022-06-22 13:09:41 +00:00
return