2022-10-25 18:04:12 +00:00
import time
2023-02-13 12:41:06 +00:00
import traceback
2022-10-25 18:04:12 +00:00
from decouple import config
2022-10-20 20:53:51 +00:00
from django . core . management . base import BaseCommand
2023-02-13 12:41:06 +00:00
from django . db import transaction
2022-02-21 23:41:36 +00:00
2023-05-01 10:30:53 +00:00
from api . models import Robot
2024-10-01 19:35:57 +00:00
from api . notifications import Notifications
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
2022-02-21 23:41:36 +00:00
class Command ( BaseCommand ) :
help = " Polls telegram /getUpdates method "
2022-10-20 09:56:10 +00:00
rest = 3 # seconds between consecutive polls
2022-02-21 23:41:36 +00:00
2022-10-20 09:56:10 +00:00
bot_token = config ( " TELEGRAM_TOKEN " )
updates_url = f " https://api.telegram.org/bot { bot_token } /getUpdates "
2022-08-08 15:58:06 +00:00
session = get_session ( )
2024-10-01 19:35:57 +00:00
notifications = Notifications ( )
2022-10-20 09:56:10 +00:00
2022-02-21 23:41:36 +00:00
def handle ( self , * args , * * options ) :
offset = 0
while True :
time . sleep ( self . rest )
2022-10-20 09:56:10 +00:00
params = { " offset " : offset + 1 , " timeout " : 5 }
2023-02-13 12:41:06 +00:00
try :
response = self . session . get ( self . updates_url , params = params )
if response . status_code != 200 :
with open ( " error.log " , " a " ) as f :
f . write (
f " Error getting updates, status code: { response . status_code } \n "
)
continue
response = response . json ( )
response = self . session . get ( self . updates_url , params = params ) . json ( )
except Exception as e :
with open ( " error.log " , " a " ) as f :
f . write ( f " Error getting updates: { e } \n { traceback . format_exc ( ) } \n " )
2022-02-21 23:41:36 +00:00
continue
2022-03-01 13:56:37 +00:00
2023-02-13 12:41:06 +00:00
if not response [ " result " ] :
continue
for result in response [ " result " ] :
if not result . get ( " message " ) or not result . get ( " message " ) . get ( " text " ) :
continue
message = result [ " message " ] [ " text " ]
if not message or not message . startswith ( " /start " ) :
continue
parts = message . split ( " " )
if len ( parts ) < 2 :
2024-10-01 19:35:57 +00:00
self . notifications . send_telegram_message (
2024-07-17 23:34:34 +00:00
result [ " message " ] [ " from " ] [ " id " ] ,
' You must enable the notifications bot using the RoboSats client. Click on your " Robot robot " -> " Enable Telegram " and follow the link or scan the QR code. ' ,
2023-02-13 12:41:06 +00:00
)
continue
token = parts [ - 1 ]
2023-05-01 10:30:53 +00:00
robot = Robot . objects . filter ( telegram_token = token ) . first ( )
if not robot :
2024-10-01 19:35:57 +00:00
self . notifications . send_telegram_message (
2024-07-17 23:34:34 +00:00
result [ " message " ] [ " from " ] [ " id " ] ,
f ' Wops, invalid token! There is no Robot with telegram chat token " { token } " ' ,
2023-02-13 12:41:06 +00:00
)
2022-03-01 13:56:37 +00:00
continue
2022-10-20 09:56:10 +00:00
2023-02-13 12:41:06 +00:00
attempts = 5
while attempts > = 0 :
2022-10-20 09:56:10 +00:00
try :
2023-02-13 12:41:06 +00:00
with transaction . atomic ( ) :
2023-05-01 10:30:53 +00:00
robot . telegram_chat_id = result [ " message " ] [ " from " ] [ " id " ]
robot . telegram_lang_code = result [ " message " ] [ " from " ] [
2022-10-20 09:56:10 +00:00
" language_code "
]
2024-06-27 16:47:23 +00:00
self . notifications . welcome ( robot . user )
2023-05-01 10:30:53 +00:00
robot . telegram_enabled = True
2023-05-08 18:10:37 +00:00
robot . save (
update_fields = [
" telegram_lang_code " ,
" telegram_chat_id " ,
" telegram_enabled " ,
]
)
2022-03-26 15:23:34 +00:00
break
2023-02-13 12:41:06 +00:00
except Exception :
time . sleep ( 5 )
attempts - = 1
2022-10-20 09:56:10 +00:00
offset = response [ " result " ] [ - 1 ] [ " update_id " ]