Example #1
0
def handle(msg):
    global wakeup_interval
    chat_id = msg['chat']['id']
    command = msg['text']
    command = command.replace('@MontaltoBot', '')
    log_time = time.strftime('%d/%m/%y %H:%M:%S')

    from_string = ' da %s' % msg['from']['first_name']
    text_to_log = '[%s]' % log_time + ' ricevuto comando: %s' % \
    command + from_string

    print(text_to_log)
    with open('logs/actions', 'a') as actions:
        #actions.write(text_to_log + '\n')
        actions.write(text_to_log + '(D %s)\n' % wakeup_interval)
    actions.close()

    if not check_auth(str(chat_id)):
        message = 'Mi dispiace, non posso eseguire il comando.\n'
        sender(chat_id, message)
        print('chat_id non riconosciuto %s' % chat_id, '%s' % from_string)
        return
    elif not (valid_act_file and valid_avg_file):
        # aggiorniamo i dati in possesso per vedere se ora abbiamo dati validi
        get_last_update(1)
        get_last_update(2)

    if msg['from']['first_name'] != 'Fabrizio' and not random_answer.answer():
        message = random_answer.joke(msg['from']['first_name'])
        sender(chat_id, message)
    elif not (valid_act_file and valid_avg_file and result):
        # se non sono ancora validi, comunicarlo all'utente'
        if not result:
            message = 'Non sono presenti dati aggiornati per soddisfare' + \
            ' la richiesta!'
        else:
            message = 'E\' stato riscontrato un problema coi dati.\n'
            message += 'Riprova più tardi!.\n'
        sender(chat_id, message)
    elif not check_auth(str(chat_id)):
        message = 'Mi dispiace, non posso eseguire il comando.\n'
        sender(chat_id, message)
        print('chat_id non riconosciuto %s' % chat_id, '%s' % from_string)
    elif command == '/generali':
        get_last_update(1)
        get_last_update(2)
        message = strings.last_update_msg + strings.at_time_msg
        message += last_update_act[1]
        message += strings.separator
        message += dati_generali.production(last_update_act, last_update_avg)
        sender(chat_id, message)
    elif command.startswith("/imposta", 0, 14):
        length = len(command)
        cmd = command.split(' ')
        if length == 14 or len(cmd) < 3:
            message = "Sintassi comando: '/imposta (stringa) (valore)'"
            sender(chat_id, message)
        elif cmd[1] not in strings.stringList:
            message = "Le stringhe disponibili sono: B1, B2, A2, A3"
            sender(chat_id, message)
        else:
            try:
                float(cmd[2])
                set_production(cmd)
                message = "Produzione di %s" % cmd[1]
                message += " aggiornata a %.1f" % float(cmd[2])
                sender(chat_id, message)
            except ValueError:
                message = "Valore '%s' non valido!" % cmd[2]
                sender(chat_id, message)
    elif command == '/istantanee':
        get_last_update(1)
        message = strings.last_update_msg + strings.at_time_msg
        message += last_update_act[1]
        message += strings.separator
        message += istantanee.actual_production(last_update_act)
        sender(chat_id, message)
    elif command == '/medie':
        get_last_update(2)
        message = strings.last_update_msg + strings.at_time_msg
        message += last_update_avg[1]
        message += strings.separator
        message += medie.actual_averages(last_update_avg)
        sender(chat_id, message)
    elif command == '/completo':
        get_last_update(1)
        get_last_update(2)
        message = strings.last_update_msg + strings.at_time_msg
        message += last_update_act[1]
        message += strings.separator
        message += dati_generali.production(last_update_act, last_update_avg)
        message += strings.separator
        message += medie.actual_averages(last_update_avg)
        message += strings.separator
        message += istantanee.actual_production(last_update_act)
        sender(chat_id, message)
    elif command == '/produzione':
        message = "B1: %s" % dati_generali.prodB1 + ' | %s\n' % \
        dati_generali.meseB1
        message += "B2: %s" % dati_generali.prodB2 + ' | %s\n' % \
        dati_generali.meseB2
        message += "A2: %s" % dati_generali.prodA2 + ' | %s\n' % \
        dati_generali.meseA2
        message += "A3: %s" % dati_generali.prodA3 + ' | %s\n' % \
        dati_generali.meseA3
        sender(chat_id, message)
    elif command == '/statistiche':
        # prelevo i dati aggiornati
        get_last_update(1)
        get_last_update(2)
        # recupera statistiche riferite alle produzioni totali e odierne
        b_strings = dati_generali.get_updated_production(2, last_update_avg)
        a_strings = dati_generali.get_updated_production(1, last_update_act)
        message = dati_generali.get_statistics(a_strings, b_strings)
        # recupera statistiche riferite alle istantanee
        b1 = float(last_update_act[2])
        b2 = float(last_update_act[3])
        a2 = float(last_update_act[4])
        a3 = float(last_update_act[5])
        message += istantanee.get_statistics(b1, b2, a2, a3)
        # recupera statistiche riferite alle medie
        b1 = float(last_update_avg[2])
        b2 = float(last_update_avg[3])
        a2 = float(last_update_avg[4])
        a3 = float(last_update_avg[5])
        message += medie.get_statistics(b1, b2, a2, a3)
        sender(chat_id, message)
    elif command == '/aiuto':
        message = 'I comandi disponibili sono i seguenti:\n'
        message += '\n\\rileva - effettua rilevazione dei dati'
        message += '\n\imposta [STRINGA] [VALORE] - fissa il VALORE di produ'
        message += 'zione della STRINGA da cui valutare i dati'
        sender(chat_id, message)
    elif command == '/start':
        message = 'Grazie per aver iniziato ad usare MontaltoBot!\n'
        sender(chat_id, message)
    elif command == '/fine':
        get_last_update(1)
        get_last_update(2)
        cmd = [0, 'B1', last_update_avg[6]]
        set_production(cmd)
        message = 'Produzione B1 impostata a: ' + last_update_avg[6] + '\n'
        cmd = [0, 'B2', last_update_avg[7]]
        set_production(cmd)
        message += 'Produzione B2 impostata a: ' + last_update_avg[7] + '\n'
        cmd = [0, 'A2', last_update_act[6]]
        set_production(cmd)
        message += 'Produzione A2 impostata a: ' + last_update_act[6] + '\n'
        cmd = [0, 'A3', last_update_act[7]]
        set_production(cmd)
        message += 'Produzione A3 impostata a: ' + last_update_act[7] + '\n'
        message += 'Produzioni aggiornate!\n'
        sender(chat_id, message)
    elif command == '/richieste':
        message = 'CONTEGGIO RICHIESTE\n\n'
        # fabrizio
        cmd_to_exe = 'cat logs/actions | grep \'Fabrizio\' | wc -l'
        p = subprocess.Popen([cmd_to_exe, ''], stdout=subprocess.PIPE,
        stderr=subprocess.PIPE, shell=True)
        out, err = p.communicate()
        strout = str(out)
        strout = strout.replace('b\'', '')
        strout = strout.replace('\\n', '')
        fab = strout.replace('\'', '')
        message += 'Fabrizio: %s' % fab + '\n'
        # maurilio
        cmd_to_exe = cmd_to_exe.replace('Fabrizio', 'Maurilio')
        p = subprocess.Popen([cmd_to_exe, ''], stdout=subprocess.PIPE,
        stderr=subprocess.PIPE, shell=True)
        out, err = p.communicate()
        strout = str(out)
        strout = strout.replace('b\'', '')
        strout = strout.replace('\\n', '')
        mau = strout.replace('\'', '')
        cmd_jokes = 'cat logs/actions | grep \'per Maurilio\' | wc -l'
        p = subprocess.Popen([cmd_jokes, ''], stdout=subprocess.PIPE,
        stderr=subprocess.PIPE, shell=True)
        out, err = p.communicate()
        strout = str(out)
        strout = strout.replace('b\'', '')
        strout = strout.replace('\\n', '')
        mauj = strout.replace('\'', '')
        message += 'Maurilio: %s' % mau + ' (%s scherzi)\n' % mauj
        # andrea
        cmd_to_exe = cmd_to_exe.replace('Maurilio', 'Andrea')
        p = subprocess.Popen([cmd_to_exe, ''], stdout=subprocess.PIPE,
        stderr=subprocess.PIPE, shell=True)
        out, err = p.communicate()
        strout = str(out)
        strout = strout.replace('b\'', '')
        strout = strout.replace('\\n', '')
        andr = strout.replace('\'', '')
        cmd_jokes = 'cat logs/actions | grep \'per Andrea\' | wc -l'
        p = subprocess.Popen([cmd_jokes, ''], stdout=subprocess.PIPE,
        stderr=subprocess.PIPE, shell=True)
        out, err = p.communicate()
        strout = str(out)
        strout = strout.replace('b\'', '')
        strout = strout.replace('\\n', '')
        andrj = strout.replace('\'', '')
        message += 'Andrea: %s' % andr + ' (%s scherzi)\n' % andrj
        sender(chat_id, message)
    else:
        sender(chat_id, strings.command_not_found)
Example #2
0
     if ("00" in minuti) and not automatic_update_done and valid_files:
         log_time = time.strftime('%d/%m/%y %H:%M:%S')
         text_to_log = '[%s]' % log_time + \
         ' aggiornamento automatico delle ore %s' % orario
         print(text_to_log)
         with open('logs/actions', 'a') as actions:
             #actions.write(text_to_log + '\n')
             actions.write(text_to_log + '(D %s)\n' % wakeup_interval)
         actions.close()
         get_last_update(1)
         get_last_update(2)
         message = 'Aggiornamento orario delle %s\n' % orario
         message += strings.last_update_msg + strings.at_time_msg
         message += last_update_act[1]
         message += strings.separator
         dati = dati_generali.production(last_update_act, last_update_avg)
         message += dati
         sender('-108582578', message)
         automatic_update_done = True
     elif ("01" in minuti) and automatic_update_done:
         automatic_update_done = False
     end_of_day = False
 # TODO verificare quando istantanee tutte a 0 (su 2/3 check)
 # e chiudere la giornata (aumentare intervallo di wakeup?)
 #print(last_update_act[3])
 #if last_update_act[3] == '0.0':
     #print("asdasdasd")
 if int(ora) > 19 and not end_of_day:  # giornata è finita aggiorna prod
     get_last_update(1)
     get_last_update(2)
     cmd = [0, 'B1', last_update_avg[6]]