2022-02-21 23:41:36 +00:00
from decouple import config
from secrets import token_urlsafe
from api . models import Order
2022-02-22 00:55:31 +00:00
from api . utils import get_tor_session
2022-03-01 13:38:04 +00:00
import time
2022-02-21 23:41:36 +00:00
class Telegram ( ) :
''' Simple telegram messages by requesting to API '''
2022-02-22 00:55:31 +00:00
session = get_tor_session ( )
2022-02-21 23:41:36 +00:00
def get_context ( user ) :
""" returns context needed to enable TG notifications """
context = { }
if user . profile . telegram_enabled :
context [ ' tg_enabled ' ] = True
else :
context [ ' tg_enabled ' ] = False
if user . profile . telegram_token == None :
user . profile . telegram_token = token_urlsafe ( 15 )
user . profile . save ( )
context [ ' tg_token ' ] = user . profile . telegram_token
context [ ' tg_bot_name ' ] = config ( " TELEGRAM_BOT_NAME " )
return context
2022-02-22 00:55:31 +00:00
def send_message ( self , user , text ) :
2022-02-21 23:41:36 +00:00
""" sends a message to a user with telegram notifications enabled """
bot_token = config ( ' TELEGRAM_TOKEN ' )
chat_id = user . profile . telegram_chat_id
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
except :
pass
2022-02-21 23:41:36 +00:00
2022-02-22 01:50:32 +00:00
def welcome ( self , user ) :
2022-02-21 23:41:36 +00:00
lang = user . profile . telegram_lang_code
2022-02-24 21:59:16 +00:00
# In weird cases the order cannot be found (e.g. it is cancelled)
queryset = Order . objects . filter ( maker = user )
order = queryset . last ( )
2022-02-21 23:41:36 +00:00
print ( str ( order . id ) )
if lang == ' es ' :
2022-02-22 17:50:01 +00:00
text = f ' Hola { user . username } , te enviaré un mensaje cuando tu orden con ID { str ( order . id ) } haya sido tomada. '
2022-02-21 23:41:36 +00:00
else :
2022-02-22 17:50:01 +00:00
text = f " Hey { user . username } , I will send you a message when someone takes your order with ID { str ( order . id ) } . "
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-02-22 01:50:32 +00:00
def order_taken ( self , order ) :
2022-02-21 23:41:36 +00:00
user = order . maker
2022-02-22 00:55:31 +00:00
if not user . profile . telegram_enabled :
return
2022-02-22 01:50:32 +00:00
2022-02-21 23:41:36 +00:00
lang = user . profile . telegram_lang_code
taker_nick = order . taker . username
site = config ( ' HOST_NAME ' )
if lang == ' es ' :
2022-02-22 17:50:01 +00:00
text = f ' ¡Tu orden con ID { order . id } ha sido tomada por { taker_nick } !🥳 Visita http:// { site } /order/ { order . id } para continuar. '
2022-02-21 23:41:36 +00:00
else :
2022-02-22 00:50:25 +00:00
text = f ' Your order with ID { order . id } was taken by { taker_nick } !🥳 Visit http:// { site } /order/ { order . id } to proceed with the trade. '
2022-02-21 23:41:36 +00:00
2022-02-22 17:50:01 +00:00
self . send_message ( user , text )
return
2022-03-11 15:24:39 +00:00
def order_taken_confirmed ( self , order ) :
user = order . maker
if not user . profile . telegram_enabled :
return
lang = user . profile . telegram_lang_code
taker_nick = order . taker . username
site = config ( ' HOST_NAME ' )
if lang == ' es ' :
text = f ' ¡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 :
2022-03-12 14:55:29 +00:00
text = f ' 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. '
2022-03-11 15:24:39 +00:00
self . send_message ( user , text )
return
2022-04-29 18:54:20 +00:00
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 ' 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 ' 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 )
return
2022-02-22 17:50:01 +00:00
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 ' :
2022-04-29 18:54:20 +00:00
text = f ' Tu orden con ID { order . id } ha expirado sin ser tomada por ningún robot. Visita http:// { site } /order/ { order . id } para renovarla. '
2022-02-22 17:50:01 +00:00
else :
2022-04-29 18:54:20 +00:00
text = f ' Your order with ID { order . id } has expired without a taker. Visit http:// { site } /order/ { order . id } to renew it. '
2022-02-22 17:50:01 +00:00
self . send_message ( user , 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 ' :
2022-06-06 00:09:06 +00:00
text = f ' ¡Tu orden con ID { order . id } ha finalizado exitosamente!⚡ Únete a nosotros en @robosats_es y ayúdanos a mejorar. '
2022-02-23 16:15:48 +00:00
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 ' Has cancelado tu orden pública con ID { order . id } . '
else :
text = f ' 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 ' :
2022-03-01 13:38:04 +00:00
text = f ' El tomador ha cancelado antes de bloquear su fianza. '
2022-02-23 16:15:48 +00:00
else :
2022-03-01 13:38:04 +00:00
text = f ' The taker has canceled before locking the bond. '
2022-02-23 16:15:48 +00:00
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 ' :
2022-03-01 13:38:04 +00:00
text = f ' El tomador no ha bloqueado la fianza a tiempo. '
2022-02-22 17:50:01 +00:00
else :
2022-03-01 13:38:04 +00:00
text = f ' The taker has not locked the bond in time. '
2022-02-22 17:50:01 +00:00
2022-02-22 02:12:42 +00:00
self . send_message ( user , text )
2022-03-01 13:38:04 +00:00
return
2022-06-02 22:32:01 +00:00
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 ' La orden con ID { str ( order . id ) } fue cancelada colaborativamente. '
else :
text = f ' 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 ' La orden con ID { str ( order . id ) } ha entrado en disputa. '
else :
text = f ' A dispute has been opened on your order with ID { str ( order . id ) } . '
self . send_message ( user , text )
return
2022-03-01 13:38:04 +00:00
def order_published ( self , order ) :
time . sleep ( 1 ) # Just so this message always arrives after the previous two
2022-03-01 21:34:25 +00:00
2022-03-01 13:38:04 +00:00
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 ' Tu orden con ID { str ( order . id ) } es pública en el libro de ordenes. '
else :
text = f " 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 ( )
2022-06-06 00:09:06 +00:00
return