示例#1
0
def initialize():
    global bot
    global youtube

    try:
        f = open('token.txt', 'r')
        token = f.readline().strip()
        f.close()
    except FileNotFoundError:
        token = input("Please paste the bot Token here: ")
        f = open('token.txt', 'w')
        f.write(token)
        f.close()

    try:
        f = open('apikey.txt', 'r')
        apikey = f.readline().strip()
        f.close()
    except FileNotFoundError:
        apikey = input("Please paste the YouTube v3 API Key here: ")
        f = open('apikey.txt', 'w')
        f.write(apikey)
        f.close()

    bot = Bot(token)
    youtube = YouTube(apikey)

    if not Data.exists(lambda d: d.id == 0):
        fetch = youtube.fetchData()
        Data(id=0, pewdiepie=fetch[0], tseries=fetch[1], difference=fetch[2])
示例#2
0
def fetchData():
    if not Data.exists(lambda d: d.id == 0):
        Data(id=0)
    data = Data.get(id=0)
    res = get('https://www.teamtrees.org')
    soup = BeautifulSoup(res.content, "html.parser")
    trees = int(soup.find(id="totalTrees").get_attribute_list("data-count")[0])
    data.trees = trees
示例#3
0
def updateUserdata(chatId, data):
    userdata = Data.get(chatId=chatId)
    if data['didattica']: userdata.didattica = data['didattica']
    if data['note']: userdata.note = data['note']
    if data['voti']: userdata.voti = data['voti']
    if data['agenda']: userdata.agenda = data['agenda']
    if data['circolari']: userdata.circolari = data['circolari']
示例#4
0
def fetchAndStore(api, chatId):
    data = Data.get(chatId=chatId)
    data.credito = api.credito()
    data.dataRinnovo = api.dataRinnovo().strftime("%d/%m alle %H:%M")
    data.nome = api.nome()
    data.accountId = api.id()
    data.numero = api.numero()

    data.totChiamate = api.totChiamate()
    data.costoChiamate = api.costoChiamate()
    data.totSms = api.totSms()
    data.costoSms = api.costoSms()
    data.totGiga = api.totGiga()
    data.costoGiga = api.costoGiga()
    data.pianoGiga = api.pianoGiga()
    data.totMms = api.totMms()
    data.costoMms = api.costoMms()

    data.ext_totChiamate = api.totChiamate(estero=True)
    data.ext_costoChiamate = api.costoChiamate(estero=True)
    data.ext_totSms = api.totSms(estero=True)
    data.ext_costoSms = api.costoSms(estero=True)
    data.ext_totGiga = api.totGiga(estero=True)
    data.ext_costoGiga = api.costoGiga(estero=True)
    data.ext_pianoGiga = api.pianoGiga(estero=True)
    data.ext_totMms = api.totMms(estero=True)
    data.ext_costoMms = api.costoMms(estero=True)
示例#5
0
def initialize():
    if not Data.exists(lambda d: d.general == True):
        Data(general=True)
    fetchRaces()

    try:
        f = open('token.txt', 'r')
        token = f.readline().strip()
        f.close()
    except FileNotFoundError:
        token = input("Paste the bot API Token: ")
        f = open('token.txt', 'w')
        f.write(token)
        f.close()

    bot = Bot(token)
示例#6
0
def inlinebutton(msg):
    chatId, query_data = glance(msg, flavor="callback_query")[1:3]
    user = User.get(chatId=chatId)
    data = Data.get(general=True)
    query_split = query_data.split("#")
    message_id = int(query_split[1])
    button = query_split[0]

    if button == "latest":
        results = [0]
        n = data.disputedRaces[-1]
        pilots = get_html(get_results_url(get_state(n), n), 'tr')
        for num in range(1, 21):
            results.append(get_results_number(n, num, pilots))
        message = ""
        for res in results:
            message += "- {}: {}".format(res['abbr'], res['time'])
        
        bot.editMessageText((chatId, message_id), "🏎 {} Grand Prix\n"
                                                  "{}".format(get_state(n), message), reply_markup=keyboards.back(message_id))

    elif button == "start":
        bot.editMessageText((chatId, message_id), "What would you like to see?", reply_markup=keyboards.start(message_id))

    else:
        bot.editMessageText((chatId, message_id), "Sorry, this function is currently not available.", reply_markup=None)
示例#7
0
def clearUserData(chatId):
    user = User.get(chatId=chatId)
    user.delete()

    userdata = Data.get(chatId=chatId)
    userdata.delete()

    stored = ParsedData.get(chatId=chatId)
    stored.delete()
示例#8
0
def fetchRaces():
    data = Data.get(general=True)
    now = datetime.now()
    disputed = []
    length = len(get_html("https://www.formula1.com/en/racing/2019.html", "article"))
    for i in range(length):
        date = get_date(i)
        if (now.month > date[1]) or ((now.month == date[1]) and (now.day > date[0])):
            disputed.append(i)
    data.disputedRaces = disputed
示例#9
0
def reply(msg):
    chatId = msg['chat']['id']
    fromId = msg['from']['id']
    name = msg['from']['first_name']
    text = msg['text'].replace("@" + bot.getMe()['username'], "")

    if not Chat.exists(lambda c: c.chatId == str(chatId)):
        if chatId > 0:
            Chat(chatId=str(chatId), isGroup=False)
        else:
            Chat(chatId=str(chatId), isGroup=True)
    chat = Chat.get(chatId=str(chatId))
    data = Data.get(id=0)

    if chat.isGroup:
        # Allow only group admins
        member = bot.getChatMember(chatId, fromId)
        if member['status'] not in ['creator', 'administrator']:
            return

    if text == "/start":
        bot.sendMessage(chatId, "<b>Welcome, {}!</b>\n"
                                "I'm the BitchLasagna Bot. I can monitor the current situation of PewDiePie vs. T-Series "
                                "subcount, send notifications on critical situations and more.\n\n"
                                "<b>PewDiePie:</b> {:,} subs\n"
                                "<b>T-Series:</b> {:,} subs\n"
                                "<b>Difference:</b> {:,} subs".format(name, data.pewdiepie, data.tseries, data.difference),
                        parse_mode="HTML")

    elif text == "/help":
        bot.sendMessage(chatId, "Hi, <b>{}</b>! I'm the <b>BitchLasagna Bot</b>.\n"
                                "Here's a brief list of what I can do:\n\n"
                                "/start - Welcome message\n"
                                "/help - Show this list\n"
                                "/show - Send current subscribers count\n"
                                "/alert - Toggle on or off trigger notifications.\n"
                                "<b>NB</b>: In a group, only admins can use this bot.\n\n"
                                "<i>Reminder: due to limited API availability, subscribers data is updated about every 2 minutes.</i>"
                                "".format(name), parse_mode="HTML")

    elif text == "/show":
        bot.sendMessage(chatId, "<b>Current subscribers status</b>\n"
                                "<b>PewDiePie:</b> {:,} subs\n"
                                "<b>T-Series:</b> {:,} subs\n"
                                "<b>Difference:</b> {:,} subs".format(data.pewdiepie, data.tseries, data.difference),
                        parse_mode="HTML")

    elif text == "/alert":
        chat.wantsAlert = not chat.wantsAlert
        if chat.wantsAlert:
            bot.sendMessage(chatId, "✅ <i>Alerts have been successfully activated for this chat!</i>\n"
                                    "Use /alert again to toggle them off.", parse_mode="HTML")
        else:
            bot.sendMessage(chatId, "❌ <i>Alerts have been successfully deactivated for this chat.</i>\n"
                                    "Use /alert again to toggle them on.", parse_mode="HTML")
示例#10
0
def sendUpdates():
    data = Data.get(id=0)
    goalInterval = 1000000
    currentGoal = data.trees - data.trees % goalInterval
    if currentGoal > data.lastGoal:
        for chat in select(c for c in Chat)[:]:
            try:
                chatId = chat.chatId if not chat.isGroup else int(
                    "-100" + str(chat.chatId))
                bot.sendMessage(
                    chatId,
                    "🌲 #TeamTrees just reached <b>{:,} trees!</b>".format(
                        currentGoal),
                    parse_mode="HTML")
            except (BotWasBlockedError, BotWasKickedError):
                chat.delete()
            except TelegramError:
                pass
    data.lastGoal = currentGoal
示例#11
0
def createMessage():
    if not Message.exists(lambda m: m.id == 0):
        Message(id=0)
    message = Message.get(id=0)
    data = Data.get(id=0)
    trees = data.trees
    totalTrees = 20000000
    remainingTrees = totalTrees - trees
    days = (datetime.now() - datetime(2019, 10, 25)).days
    totalDays = 68
    remainingDays = totalDays - days
    remainingTrees = max(0, remainingTrees)
    remainingDays = max(0, remainingDays)
    message.trees = "<b>#TeamTrees Status</b>\n\n" \
              "🌱 Trees Planted: <b>{:,} ({}%)</b>\n" \
              "🌿 Remaining: <b>{:,} ({}%)</b>\n" \
              "🌳 Final Goal: <b>{:,} trees</b>\n\n" \
              "📆 Days Passed: <b>{} days ({}%)</b>\n" \
              "🕙 Remaining: <b>{} days ({}%)</b>\n\n" \
              "🌲 teamtrees.org".format(trees, round(trees * 100 / totalTrees, 1), remainingTrees,
                                        round(remainingTrees * 100 / totalTrees, 1), totalTrees, days,
                                        round(days * 100 / totalDays, 1), remainingDays, round(remainingDays * 100 / totalDays, 1))
示例#12
0
def button_press(msg):
    chatId, query_data = glance(msg, flavor="callback_query")[1:3]
    query_split = query_data.split("#")
    message_id = int(query_split[1])
    button = query_split[0]
    data = Data.get(chatId=chatId)

    if button == "logout_yes":
        helpers.clearUserData(chatId)
        bot.editMessageText((chatId, message_id), "😯 Fatto, sei stato disconnesso!\n"
                                                  "Premi /login per entrare di nuovo.\n\n"
                                                  "Premi /help se serve aiuto.", reply_markup=None)

    elif button == "logout_no":
        bot.editMessageText((chatId, message_id), "<i>Logout annullato.</i>", parse_mode="HTML", reply_markup=None)

    elif button == "overview_ext":
        costo = data.ext_costoChiamate + data.ext_costoGiga + data.ext_costoSms + data.ext_costoMms
        bot.editMessageText((chatId, message_id), f"ℹ️ <b>Riepilogo piano estero</b>\n\n"
                                           f"📞 Chiamate: <b>{data.ext_totChiamate}</b>\n"
                                           f"🌐 Dati consumati: <b>{data.ext_totGiga['count']}{data.ext_totGiga['unit']}</b> su <b>"
                                           f"{data.ext_pianoGiga['count']}{data.ext_pianoGiga['unit']}</b>\n"
                                           f"✉️ SMS Inviati: <b>{data.ext_totSms}</b>\n"
                                           f"📧 MMS Inviati: <b>{data.ext_totMms}</b>\n\n"
                                           f"💸 Costi extra: {costo:.2f}€",
                            parse_mode="HTML", reply_markup=keyboards.overviewIta(message_id))

    elif button == "overview_ita":
        costo = data.costoChiamate + data.costoGiga + data.costoSms + data.costoMms
        bot.editMessageText((chatId, message_id), f"ℹ️ <b>Riepilogo piano</b>\n\n"
                                           f"📞 Chiamate: <b>{data.totChiamate}</b>\n"
                                           f"🌐 Dati consumati: <b>{data.totGiga['count']}{data.totGiga['unit']}</b> su <b>"
                                           f"{data.pianoGiga['count']}{data.pianoGiga['unit']}</b>\n"
                                           f"✉️ SMS Inviati: <b>{data.totSms}</b>\n"
                                           f"📧 MMS Inviati: <b>{data.totMms}</b>\n\n"
                                           f"💸 Costi extra: {costo:.2f}€",
                            parse_mode="HTML", reply_markup=keyboards.overviewExt(message_id))
示例#13
0
def clearUserData(chatId):
    user = User.get(chatId=chatId)
    user.delete()

    data = Data.get(chatId=chatId)
    data.delete()
示例#14
0
def reply(msg):
    chatId = msg['chat']['id']
    name = msg['from']['first_name']
    if "text" in msg:
        text = msg['text']
    else:
        bot.sendMessage(chatId, "🤨 Formato file non supportato. /help")
        return

    if not User.exists(lambda u: u.chatId == chatId):
        User(chatId=chatId)
    if not Data.exists(lambda d: d.chatId == chatId):
        Data(chatId=chatId)

    user = User.get(chatId=chatId)
    data = Data.get(chatId=chatId)

    if text == "/about":
        bot.sendMessage(chatId, "ℹ️ <b>Informazioni sul bot</b>\n"
                                "IliadInfoBot è un bot creato e sviluppato da Filippo Pesavento, che ti permette "
                                "di visualizzare tutte le info sul tuo account Iliad in mancanza dell'app.\n"
                                "Prova ad usarlo per scoprire quanto è comodo!\n\n"
                                "<b>Sviluppo:</b> Filippo Pesavento\n"
                                "<b>Hosting:</b> Filippo Pesavento\n"
                                "<b>Info sicurezza:</b> /aboutprivacy\n\n"
                                "<i>IliadInfoBot non è in alcun modo affiliato con Iliad Italia S.p.A ed è una creazione "
                                "esclusiva di Filippo Pesavento.</i>", parse_mode="HTML")

    elif text == "/aboutprivacy":
        bot.sendMessage(chatId, "ℹ️ <b>Informazioni sulla privacy</b>\n"
                                "La mia password è al sicuro? 🤔\n\n"
                                "🔐 <b>Sì: la tua password viene criptata.</b>\n"
                                "Il bot conserva la tua password in maniera sicura, salvandola in un formato non leggibile da "
                                "persone estranee. Sei al sicuro: i tuoi dati non verranno visti nè rubati da nessuno!\n\n"
                                "🔐 <b>Spiegazione dettagliata:</b>\n"
                                "Tecnicamente potrei decriptare a mano le password e vederle, ma sostanzialmente è complicato, "
                                "perchè il bot genera una chiave per l'algoritmo (visto che il cripting deve essere reversibile, "
                                "per poter mandare le notifiche automatiche) prendendo come dati una chiave comune (che salvo nella RAM "
                                "e inserisco ad ogni avvio, per evitare che qualcuno che non sia io possa leggere il database e i dati degli utenti) "
                                "e anche l'username dell'utente. Quindi ogni utente ha la propria password criptata con una chiave diversa da tutti "
                                "gli altri, e sarebbe difficile anche per me risalire alla password, dovendo sapere di chi è l'username collegato a "
                                "quella password specifica.\n"
                                "Questo non vuol dire che non possa farlo: con un po' di lavoro ci riuscirei. Quindi alla fine devi decidere tu: "
                                "io ti posso assicurare che non leggerò mai nè proverò mai a decriptare le password, sia per un discorso di etica "
                                "che per scelta personale, ma non sono tuo amico nè tuo conoscente: quindi se decidi di non fidarti di uno sconosciuto "
                                "che ti scrive su Telegram (ti posso capire benissimo) sei libero di non usare il bot 🙂\n\n"
                                "<a href=\"https://t.me/pesaventofilippo\">Contattami</a>\n\n"
                                "<i>Se sei venuto qui prima di digitare la password per il login, scrivila adesso!</i>",
                        parse_mode="HTML", disable_web_page_preview=True)


    elif user.status != "normal":
        if text == "/annulla":
            user.status = "normal"
            bot.sendMessage(chatId, "Comando annullato!")

        elif user.status == "login_0":
            if len(text) != 8 or not text.isdigit():
                bot.sendMessage(chatId, "⚠️ Errore: l'username deve essere un numero 8 cifre. Riprova!")
                return
            user.username = text
            user.status = "login_1"
            bot.sendMessage(chatId, "👍 Ottimo. Adesso inviami la password.\n"
                                    "Ricorda che la password viene salvata solo per te e viene criptata, nessuno potrà leggerla.\n\n"
                                    "Sei preoccupato per la sicurezza della password? /aboutprivacy")

        elif user.status == "login_1":
            user.password = crypt_password(text, chatId)
            user.status = "normal"
            commit()
            api = IliadApi(user.username, decrypt_password(chatId))

            try:
                api.load()
            except AuthenticationFailedError:
                helpers.clearUserData(chatId)
                try:
                    bot.sendMessage(chatId, "😯 Le tue credenziali di accesso sono errate.\n"
                                            "Controlla i dati inseriti e rieffettua il /login.")
                except (TelegramError, BotWasBlockedError):
                    pass
                return

            bot.sendMessage(chatId, "Fatto 😊\n"
                                    "Premi /help per vedere la lista dei comandi disponibili.\n\n"
                                    "<i>Se vuoi, puoi eliminare il messaggio che mi hai mandato contenente la password: "******"non mi serve più!</i>", parse_mode="HTML")
            sent = bot.sendMessage(chatId, "🔍 Aggiorno il profilo...")
            helpers.fetchAndStore(api, chatId)
            bot.editMessageText((chatId, sent['message_id']), "✅ Profilo aggiornato!")

        elif user.status == "calling_support":
            user.status = "normal"
            for a in helpers.isAdmin():
                bot.sendMessage(a, "🆘 <b>Richiesta di aiuto</b>\n"
                                   "Da: <a href=\"tg://user?id={0}\">{1}</a>\n\n"
                                   "<i>Rispondi al messaggio per parlare con l'utente.</i>".format(chatId, name),
                                parse_mode="HTML")
                if "reply_to_message" in msg:
                    bot.forwardMessage(a, chatId, msg["reply_to_message"]["message_id"])
                bot.forwardMessage(a, chatId, msg['message_id'], disable_notification=True)
            bot.sendMessage(chatId, "<i>Richiesta inviata.</i>\n"
                                    "Un admin ti risponderà il prima possibile.", parse_mode="HTML")


    elif text == "/help":
        bot.sendMessage(chatId, "Ciao, sono il bot di <b>Iliad</b>! 👋🏻\n"
                                "Posso aiutarti a <b>controllare</b> il tuo piano dati e posso mandarti <b>notifiche</b> (in futuro).\n\n"
                                "<b>Lista dei comandi</b>:\n"
                                "- /start - Avvia bot\n"
                                "- /login - Effettua il login\n"
                                "- /profilo - Informazioni sul profilo Iliad\n"
                                "- /overview - Riepilogo generale dei consumi\n"
                                "- /credito - Credito residuo\n"
                                "- /internet - Visualizza piano dati\n"
                                "- /chiamate - Visualizza piano chiamate\n"
                                "- /sms - Visualizza piano SMS\n"
                                "- /mms - Visualizza piano MMS\n"
                                "- /logout - Disconnettiti\n"
                                "- /aggiorna - Aggiorna tutti i dati. <b>Nota</b>: lo faccio già in automatico ogni mezz'ora per te!\n"
                                "- /help - Mostra questa lista\n"
                                "- /about - Informazioni sul bot\n"
                                "- /aboutprivacy - Più informazioni sulla privacy\n"
                                "- /support - Contatta lo staff (emergenze)\n\n"
                                "<i>IliadInfoBot non è in alcun modo affiliato con Iliad Italia S.p.A ed è una creazione "
                                "esclusiva di Filippo Pesavento.</i>", parse_mode="HTML")

    elif text == "/users" and helpers.isAdmin(chatId):
        totalUsers = len(select(u for u in User)[:])
        loggedUsers = len(select(u for u in User if u.password != "")[:])
        bot.sendMessage(chatId, "👤 Utenti totali: <b>{}</b>\n"
                                "👤 Utenti loggati: <b>{}</b>".format(totalUsers, loggedUsers), parse_mode="HTML")

    elif text == "/globalupdate" and helpers.isAdmin(chatId):
        bot.sendMessage(chatId, "🕙 Inizio aggiornamento globale...")
        runUpdates()
        bot.sendMessage(chatId, "✅ Aggiornamento globale completato!")

    elif text.startswith("/broadcast ") and helpers.isAdmin(chatId):
        bdText = text.split(" ", 1)[1]
        pendingUsers = select(u.chatId for u in User)[:]
        userCount = len(pendingUsers)
        for u in pendingUsers:
            try:
                bot.sendMessage(u, bdText, parse_mode="HTML", disable_web_page_preview=True)
            except (TelegramError, BotWasBlockedError):
                userCount -= 1
        bot.sendMessage(chatId, "📢 Messaggio inviato correttamente a {0} utenti!".format(userCount))

    elif text.startswith("/sendmsg ") and helpers.isAdmin(chatId):
        selId = int(text.split(" ", 2)[1])
        selText = str(text.split(" ", 2)[2])
        bot.sendMessage(selId, selText, parse_mode="HTML")
        bot.sendMessage(chatId, selText + "\n\n- Messaggio inviato!", parse_mode="HTML")

    elif "reply_to_message" in msg:
        if helpers.isAdmin(chatId):
            try:
                userId = msg['reply_to_message']['forward_from']['id']
                bot.sendMessage(userId, "💬 <b>Risposta dello staff</b>\n"
                                        "{0}".format(text), parse_mode="HTML")
                bot.sendMessage(chatId, "Risposta inviata!")
            except Exception:
                bot.sendMessage(chatId, "Errore nell'invio.")
        else:
            bot.sendMessage(chatId, "Scrivi /support per parlare con lo staff.")

    elif text == "/annulla":
        bot.sendMessage(chatId, "😴 Nessun comando da annullare!")


    elif helpers.hasStoredCredentials(chatId):
        if text == "/start":
            bot.sendMessage(chatId, "Bentornato, <b>{0}</b>!\n"
                                    "Cosa posso fare per te? 😊".format(name), parse_mode="HTML")

        elif text == "/login":
            bot.sendMessage(chatId, "Sei già loggato.\n"
                                    "Premi /logout per uscire.")

        elif text == "/logout":
            sent = bot.sendMessage(chatId, "Tutti i tuoi dati relativi all'account e le credenziali verranno eliminate dal bot.\n"
                                            "Sei <b>veramente sicuro</b> di voler uscire?", parse_mode="HTML")
            bot.editMessageReplyMarkup((chatId, sent['message_id']), keyboards.logout(sent['message_id']))

        elif text == "/profilo":
            bot.sendMessage(chatId, f"👤 <b>Info profilo</b>\n\n"
                                    f"ℹ️ Nome: <b>{data.nome}</b>\n"
                                    f"📞 Numero: <b>{data.numero}</b>\n"
                                    f"🆔 ID Account: <b>{data.accountId}</b>\n\n"
                                    f"💶 Credito residuo: <b>{data.credito:.2f}€</b>\n"
                                    f"📅 Data rinnovo: <b>{data.dataRinnovo}</b>", parse_mode="HTML")

        elif text == "/overview":
            costo = data.costoChiamate + data.costoGiga + data.costoSms + data.costoMms
            sent = bot.sendMessage(chatId, f"ℹ️ <b>Riepilogo piano</b>\n\n"
                                           f"📞 Chiamate: <b>{data.totChiamate}</b>\n"
                                           f"🌐 Dati consumati: <b>{data.totGiga['count']}{data.totGiga['unit']}</b> su <b>"
                                           f"{data.pianoGiga['count']}{data.pianoGiga['unit']}</b>\n"
                                           f"✉️ SMS Inviati: <b>{data.totSms}</b>\n"
                                           f"📧 MMS Inviati: <b>{data.totMms}</b>\n\n"
                                           f"💸 Costi extra: {costo:.2f}€", parse_mode="HTML")
            bot.editMessageReplyMarkup((chatId, sent['message_id']), keyboards.overviewExt(sent['message_id']))

        elif text == "/credito":
            bot.sendMessage(chatId, f"Il tuo credito residuo è di <b>{data.credito:.2f} euro</b>.", parse_mode="HTML")

        elif text == "/chiamate":
            bot.sendMessage(chatId, f"🇮🇹 <b>Chiamate in Italia</b>\n"
                                    f"🕙 Tempo: <b>{data.totChiamate}</b>\n"
                                    f"💸 Costi extra: <b>{data.costoChiamate:.2f}€</b>\n\n"
                                    f"🇪🇺 <b>Chiamate in Europa</b>\n"
                                    f"🕙 Tempo: <b>{data.ext_totChiamate}</b>\n"
                                    f"💸 Costi extra: <b>{data.ext_costoChiamate:.2f}€</b>", parse_mode="HTML")

        elif text == "/sms":
            bot.sendMessage(chatId, f"🇮🇹 <b>SMS in Italia</b>\n"
                                    f"✉️ Inviati: <b>{data.totSms} SMS</b>\n"
                                    f"💸 Costi extra: <b>{data.costoSms:.2f}€</b>\n\n"
                                    f"🇪🇺 <b>SMS in Europa</b>\n"
                                    f"✉️ Inviati: <b>{data.ext_totSms} SMS</b>\n"
                                    f"💸 Costi extra: <b>{data.ext_costoSms:.2f}€</b>", parse_mode="HTML")

        elif text == "/mms":
            bot.sendMessage(chatId, f"🇮🇹 <b>MMS in Italia</b>\n"
                                    f"📧 Inviati: <b>{data.totMms} MMS</b>\n"
                                    f"💸 Costi extra: <b>{data.costoSms:.2f}€</b>\n\n"
                                    f"🇪🇺 <b>MMS in Europa</b>\n"
                                    f"📧 Inviati: <b>{data.ext_totMms} MMS</b>\n"
                                    f"💸 Costi extra: <b>{data.ext_costoMms:.2f}€</b>", parse_mode="HTML")

        elif text == "/internet":
            bot.sendMessage(chatId, f"🇮🇹 <b>Piano dati in Italia</b>\n"
                                    f"📶 Consumati: <b>{data.totGiga['count']}{data.totGiga['unit']}</b> su <b>"
                                           f"{data.pianoGiga['count']}{data.pianoGiga['unit']}</b>\n"
                                    f"💸 Costi extra: <b>{data.costoGiga:.2f}€</b>\n\n"
                                    f"🇪🇺 <b>Piano dati in Europa</b>\n"
                                    f"📶 Consumati: <b>{data.ext_totGiga['count']}{data.ext_totGiga['unit']}</b> su <b>"
                                           f"{data.ext_pianoGiga['count']}{data.ext_pianoGiga['unit']}</b>\n"
                                    f"💸 Costi extra: <b>{data.ext_costoGiga:.2f}€</b>", parse_mode="HTML")

        elif text == "/support":
            user.status = "calling_support"
            bot.sendMessage(chatId, "🆘 <b>Richiesta di supporto</b>\n"
                                    "Se hai qualche problema che non riesci a risolvere, scrivi qui un messaggio, e un admin "
                                    "ti contatterà il prima possibile.\n\n"
                                    "<i>Per annullare, premi</i> /annulla.", parse_mode="HTML")

        elif text == "/aggiorna":
            if user.remainingCalls > 0:
                user.remainingCalls -= 1
                commit()
                sent = bot.sendMessage(chatId, "📙📙📙 Aggiorno il profilo... 0%")
                api = IliadApi(user.username, decrypt_password(chatId))
                bot.editMessageText((chatId, sent['message_id']), "📗📙📙 Cerco aggiornamenti... 10%")

                try:
                    api.load()
                except AuthenticationFailedError:
                    helpers.clearUserData(chatId)
                    bot.editMessageText((chatId, sent['message_id']), "⚠️ Le tue credenziali non sono corrette.\n"
                                                                      "Rieffettua il /login!.")
                    return

                bot.editMessageText((chatId, sent['message_id']), "📗📙📙 Cerco aggiornamenti... 50%")
                helpers.fetchAndStore(api, chatId)
                bot.editMessageText((chatId, sent['message_id']), "📗📗📗  Cerco aggiornamenti... 100%")
                bot.editMessageText((chatId, sent['message_id']), "✅ Profilo aggiornato!")

            else:
                bot.sendMessage(chatId, "⛔️ Hai usato troppi /aggiorna recentemente. Aspetta un po'!")

        else:
            bot.sendMessage(chatId, "Non ho capito...\n"
                                    "Serve aiuto? Premi /help")

    else:
        if text == "/login":
            user.status = "login_0"
            bot.sendMessage(chatId, "Per favore, inviami il tuo <b>username</b> (il codice da 8 cifre che usi per accedere "
                                    "all'Area Personale).\n"
                                    "Usa /annulla se serve.", parse_mode="HTML")
        else:
            bot.sendMessage(chatId, "Benvenuto, <b>{0}</b>!\n"
                                    "Per favore, premi /login per utilizzarmi.\n\n"
                                    "Premi /help se serve aiuto.".format(name), parse_mode="HTML")
示例#15
0
def runUserUpdate(chatId, long_fetch, runDatetime, sendMessages=True):
    api = ClasseVivaAPI()
    if helpers.userLogin(chatId, api, _quiet=True):
        userdata = Data.get(chatId=chatId)
        settings = Settings.get(chatId=chatId)
        try:
            data = helpers.fetchStrict(api)
        except ApiServerError:
            return

        if settings.wantsNotifications:
            if (not settings.doNotDisturb) or (runDatetime.hour in range(
                    7, 21)):
                dataDidattica = parsers.parseNewDidattica(
                    userdata.didattica, data['didattica'])
                dataNote = parsers.parseNewNote(userdata.note, data['note'])
                dataVoti = parsers.parseNewVoti(userdata.voti, data['voti'],
                                                chatId)
                dataAgenda = parsers.parseNewAgenda(userdata.agenda,
                                                    data['agenda'], chatId)
                dataCircolari = parsers.parseNewCircolari(
                    userdata.circolari, data['circolari'])
                if sendMessages:
                    try:
                        if dataDidattica and ("didattica"
                                              in settings.activeNews):
                            bot.sendMessage(chatId,
                                            "🔔 <b>Nuovi file caricati!</b>"
                                            "{0}".format(dataDidattica),
                                            parse_mode="HTML",
                                            disable_web_page_preview=True)
                        if dataNote and ("note" in settings.activeNews):
                            bot.sendMessage(chatId,
                                            "🔔 <b>Hai nuove note!</b>"
                                            "{0}".format(dataNote),
                                            parse_mode="HTML",
                                            disable_web_page_preview=True)
                        if dataVoti and ("voti" in settings.activeNews):
                            bot.sendMessage(chatId,
                                            "🔔 <b>Hai nuovi voti!</b>"
                                            "{0}".format(dataVoti),
                                            parse_mode="HTML",
                                            disable_web_page_preview=True)
                        if dataAgenda and ("agenda" in settings.activeNews):
                            bot.sendMessage(chatId,
                                            "🔔 <b>Hai nuovi impegni!</b>\n"
                                            "{0}".format(dataAgenda),
                                            parse_mode="HTML",
                                            disable_web_page_preview=True)
                        if dataCircolari and ("circolari"
                                              in settings.activeNews):
                            bot.sendMessage(chatId,
                                            "🔔 <b>Hai nuove circolari!</b>"
                                            "{0}".format(dataCircolari),
                                            parse_mode="HTML",
                                            disable_web_page_preview=True)
                    except BotWasBlockedError:
                        helpers.clearUserData(chatId)
                        return
                    except TelegramError:
                        pass
                helpers.updateUserdata(chatId, data)
                helpers.fetchAndStore(chatId, api, data, long_fetch)
        user = User.get(chatId=chatId)
        user.remainingCalls = 3
示例#16
0
def reply(msg):
    global restrictedMode
    chatId = msg['chat']['id']
    name = msg['from']['first_name']
    text = msg.get("text", "")

    if chatId < 0:
        return

    if not User.exists(lambda u: u.chatId == chatId):
        User(chatId=chatId)
    if not Data.exists(lambda u: u.chatId == chatId):
        Data(chatId=chatId)
    if not ParsedData.exists(lambda u: u.chatId == chatId):
        ParsedData(chatId=chatId)
    if not Settings.exists(lambda u: u.chatId == chatId):
        Settings(chatId=chatId)

    user = User.get(chatId=chatId)
    userdata = Data.get(chatId=chatId)
    stored = ParsedData.get(chatId=chatId)

    if (not restrictedMode) or helpers.isAdmin(chatId):
        if text == "/about":
            bot.sendMessage(
                chatId, "ℹ️ <b>Informazioni sul bot</b>\n"
                "ClasseVivaBot è un bot creato e sviluppato da Filippo Pesavento, che ti può aiutare "
                "durante l'anno scolastico mandando notifiche per le novità del registro e molto altro.\n"
                "Prova ad usarlo per scoprire quanto è comodo!\n\n"
                "<b>Sviluppo:</b> <a href=\"https://t.me/pesaventofilippo\">Filippo Pesavento</a> e Francesco De Benedittis\n"
                "<b>Contributori:</b> Gianluca Parri e PolpOnline\n"
                "<b>Hosting:</b> Filippo Pesavento\n\n"
                "<b>Info sicurezza:</b> /aboutprivacy",
                parse_mode="HTML",
                disable_web_page_preview=True)

        elif text == "/aboutprivacy":
            bot.sendMessage(
                chatId, "ℹ️ <b>Informazioni sulla privacy</b>\n"
                "La mia password è al sicuro? 🤔\n\n"
                "🔐 <b>Sì: la tua password viene criptata.</b>\n"
                "Il bot conserva la tua password in maniera sicura, salvandola in un formato non leggibile da "
                "persone estranee. Sei al sicuro: i tuoi dati non verranno visti nè rubati da nessuno!\n\n"
                "🔐 <b>Spiegazione dettagliata:</b>\n"
                "Tecnicamente potrei decriptare a mano le password e vederle, ma sostanzialmente è complicato, "
                "perchè il bot genera una chiave per l'algoritmo (visto che il cripting deve essere reversibile, "
                "per poter mandare le notifiche automatiche) prendendo come dati una chiave comune (che salvo nella RAM, "
                "per evitare che qualcuno che non sia io possa leggere il database e i dati degli utenti) "
                "e anche l'username dell'utente. Quindi ogni utente ha la propria password criptata con una chiave diversa da tutti "
                "gli altri, e sarebbe difficile anche per me risalire alla password, dovendo sapere di chi è l'username collegato a "
                "quella password specifica.\n"
                "Questo non vuol dire che non possa farlo: con un po' di lavoro ci riuscirei. Quindi alla fine devi decidere tu: "
                "io ti posso assicurare che non leggerò mai nè proverò mai a decriptare le password, sia per un discorso di etica "
                "che per scelta personale, ma non sono tuo amico nè tuo conoscente: quindi se decidi di non fidarti di uno sconosciuto "
                "che ti scrive su Telegram (ti posso capire benissimo) sei libero di non usare il bot 🙂\n\n"
                "<a href=\"https://kutt.it/botinfo\">Altre info & Privacy Policy</a>\n"
                "<a href=\"https://t.me/pesaventofilippo\">Contattami</a>\n\n"
                "<i>Se sei venuto qui prima di digitare la password per il login, scrivila adesso!</i>",
                parse_mode="HTML",
                disable_web_page_preview=True)

        elif user.status != "normal":
            if text == "/annulla":
                user.status = "normal"
                bot.sendMessage(chatId, "Comando annullato!")

            elif user.status == "login_0":
                if len(text) < 5:
                    bot.sendMessage(
                        chatId,
                        "⚠️ Errore: l'username è troppo corto. Riprova!")
                    return
                user.username = text
                user.status = "login_1"
                bot.sendMessage(
                    chatId, "👍 Ottimo. Adesso inviami la password.\n"
                    "Ricorda che la password viene salvata solo per te e viene criptata, nessuno potrà leggerla.\n\n"
                    "Sei preoccupato per la sicurezza della password? /aboutprivacy"
                )

            elif user.status == "login_1":
                user.password = crypt_password(text, chatId)
                user.status = "normal"
                commit()
                api = ClasseVivaAPI()

                try:
                    api.login(user.username, decrypt_password(chatId))
                except ApiServerError:
                    try:
                        bot.sendMessage(
                            chatId,
                            "⚠️ I server di ClasseViva non sono raggiungibili.\n"
                            "Riprova tra qualche minuto.")
                    except (TelegramError, BotWasBlockedError):
                        pass
                    return
                except Exception:
                    helpers.clearUserData(chatId)
                    try:
                        bot.sendMessage(
                            chatId,
                            "😯 Le tue credenziali di accesso sono errate.\n"
                            "Effettua nuovamente il /login per favore.")
                    except (TelegramError, BotWasBlockedError):
                        pass
                    return

                bot.sendMessage(
                    chatId, "Fatto 😊\n"
                    "Premi /help per vedere la lista dei comandi disponibili.")
                sent = bot.sendMessage(chatId, "🔍 Aggiorno il profilo...")
                data = helpers.fetchStrict(api)
                helpers.updateUserdata(chatId, data)
                helpers.fetchAndStore(chatId, api, data, fetch_long=True)
                bot.editMessageText((chatId, sent['message_id']),
                                    "✅ Profilo aggiornato!")

            elif user.status == "calling_support":
                user.status = "normal"
                for a in helpers.isAdmin():
                    bot.sendMessage(
                        a,
                        "🆘 <b>Richiesta di aiuto</b>\n"
                        "Da: <a href=\"tg://user?id={0}\">{1}</a>\n\n"
                        "<i>Rispondi al messaggio per parlare con l'utente.</i>"
                        .format(chatId, name),
                        parse_mode="HTML")
                    if "reply_to_message" in msg:
                        bot.forwardMessage(
                            a, chatId, msg["reply_to_message"]["message_id"])
                    bot.forwardMessage(a,
                                       chatId,
                                       msg['message_id'],
                                       disable_notification=True)
                bot.sendMessage(chatId, "<i>Richiesta inviata.</i>\n"
                                "Un admin ti risponderà il prima possibile.",
                                parse_mode="HTML")

            elif user.status == "sending_orario":
                if msg.get("photo"):
                    fileId = msg.get("photo")[0]["file_id"]
                    ext = "photo"
                elif msg.get("document"):
                    fileId = msg.get("document")["file_id"]
                    ext = "document"
                else:
                    bot.sendMessage(
                        chatId,
                        "🤔 Documento non valido. Invia una foto o un file.\n"
                        "Premi /annulla per annullare.")
                    return

                if not Document.exists(
                        lambda d: (d.chatId == chatId and d.type == "orario")):
                    Document(chatId=chatId,
                             type="orario",
                             data={
                                 "fileId": fileId,
                                 "ext": ext
                             })
                else:
                    doc = Document.get(chatId=chatId, type="orario")
                    doc.data = {"fileId": fileId, "ext": ext}
                bot.sendMessage(
                    chatId, "✅ Orario impostato!\n"
                    "Richiamalo con /orario.")
                user.status = "normal"

            elif user.status == "memo_date":
                if not text:
                    bot.sendMessage(
                        chatId, "Inviami la data in formato GG/MM.\n"
                        "Premi /annulla per annullare.")
                    return

                try:
                    now = datetime.now()
                    when = datetime.strptime(text, "%d/%m")
                    when.replace(year=now.year)

                    if when < now:
                        when.replace(year=now.year + 1)
                    if when > now + timedelta(days=28):
                        bot.sendMessage(
                            chatId, "⚠️ Data non valida.\n"
                            "La data non può essere oggi, nel passato oppure fra più di 4 settimane.\n"
                            "Premi /annulla per annullare.")
                        return

                    memoDate = when.strftime("%d/%m/%Y")
                    user.status = f"memo_text#{memoDate}"

                    bot.sendMessage(
                        chatId, f"💡 <b>Data memo: {memoDate}</b>\n"
                        f"Inviami il testo del memo, o premi /annulla per annullare.",
                        parse_mode="HTML")
                except ValueError:
                    bot.sendMessage(
                        chatId, "⚠️ Data non valida.\n"
                        "Inviami la data in formato GG/MM.\n"
                        "Premi /annulla per annullare.")

            elif user.status.startswith("memo_text"):
                if not text or len(text) > 400:
                    bot.sendMessage(
                        chatId,
                        "⚠️ Inviami il testo della memo (max. 400 caratteri).\n"
                        "Premi /annulla per annullare.")
                    return

                memoDate = user.status.split("#")[1]
                memo = Document(chatId=chatId,
                                type="memo",
                                data={
                                    "date": memoDate,
                                    "text": text
                                })

                user.status = "normal"
                stored.agenda = parsers.parseAgenda(userdata.agenda, chatId)
                stored.domani = parsers.parseDomani(userdata.agenda, chatId)
                bot.sendMessage(
                    chatId,
                    f"✅ Ho creato la memo per il <b>{memo.data['date']}</b>:\n"
                    f"\"{memo.data['text']}\"\n\n"
                    f"Vedilo in /agenda!",
                    parse_mode="HTML")

        elif text == "/help":
            bot.sendMessage(
                chatId, "Ciao, sono il bot di <b>ClasseViva</b>! 👋🏻\n"
                "Posso aiutarti a <b>navigare</b> nel registro e posso mandarti <b>notifiche</b> quando hai nuovi avvisi.\n\n"
                "<b>Lista dei comandi</b>:\n"
                "- /login - Effettua il login\n"
                "- /aggiorna - Aggiorna manualmente tutti i dati, per controllare se ci sono nuovi avvisi.\n"
                "Oppure, puoi lasciarlo fare a me ogni mezz'ora :)\n"
                "- /agenda - Visualizza agenda (compiti e verifiche)\n"
                "- /domani - Vedi i compiti che hai per domani\n"
                "- /promemoria - Vedi un promemoria con i compiti da fare per domani e le lezioni svolte oggi.\n"
                "- /memo - Aggiungi un memo all'agenda\n"
                "- /voti - Visualizza la lista dei voti\n"
                "- /lezioni - Visualizza la lista delle lezioni\n"
                "- /didattica - Visualizza la lista dei file in didattica\n"
                "- /circolari - Visualizza le circolari da leggere\n"
                "- /orario - Imposta o visualizza l'orario delle lezioni\n"
                "- /settings - Modifica le impostazioni personali del bot\n"
                "- /assenze - Visualizza assenze, ritardi e uscite anticipate\n"
                "- /note - Visualizza la lista delle note\n"
                "- /info - Visualizza le tue info utente\n"
                "- /prof - Visualizza la lista delle materie e dei prof\n"
                "- /about - Informazioni sul bot\n"
                "- /aboutprivacy - Più informazioni sulla privacy\n"
                "- /logout - Disconnettiti\n"
                "- /support - Contatta lo staff (emergenze)\n\n"
                "<b>Notifiche</b>: ogni mezz'ora, se vuoi, ti invierò un messaggio se ti sono arrivati nuovi voti, note, compiti, materiali, avvisi o circolari.\n"
                "<b>Impostazioni</b>: con /settings puoi cambiare varie impostazioni, tra cui l'orario delle notifiche, quali notifiche ricevere e se riceverle di notte."
                "",
                parse_mode="HTML")

        elif text.startswith("/broadcast ") and helpers.isAdmin(chatId):
            bdText = text.split(" ", 1)[1]
            pendingUsers = select(u.chatId for u in User)[:]
            userCount = len(pendingUsers)
            for u in pendingUsers:
                try:
                    bot.sendMessage(u,
                                    bdText,
                                    parse_mode="HTML",
                                    disable_web_page_preview=True)
                except (TelegramError, BotWasBlockedError):
                    userCount -= 1
            bot.sendMessage(
                chatId,
                "📢 Messaggio inviato correttamente a {0} utenti!".format(
                    userCount))

        elif text.startswith("/sendmsg ") and helpers.isAdmin(chatId):
            selId = int(text.split(" ", 2)[1])
            selText = str(text.split(" ", 2)[2])
            bot.sendMessage(selId, selText, parse_mode="HTML")
            bot.sendMessage(chatId,
                            selText + "\n\n- Messaggio inviato!",
                            parse_mode="HTML")

        elif text == "/globalupdate" and helpers.isAdmin(chatId):
            bot.sendMessage(chatId, "🕙 Inizio aggiornamento globale...")
            runUpdates(long_fetch=True)
            bot.sendMessage(chatId, "✅ Aggiornamento globale completato!")

        elif text == "/silentupdate" and helpers.isAdmin(chatId):
            bot.sendMessage(chatId, "🕙 [BG] Inizio aggiornamento globale...")
            runUpdates(long_fetch=True, sendMessages=False)
            bot.sendMessage(chatId, "✅ [BG] Aggiornamento globale completato!")

        elif text == "/users" and helpers.isAdmin(chatId):
            totalUsers = len(select(u for u in User)[:])
            loggedUsers = len(select(u for u in User if u.password != "")[:])
            bot.sendMessage(chatId,
                            "👤 Utenti totali: <b>{}</b>\n"
                            "👤 Utenti loggati: <b>{}</b>".format(
                                totalUsers, loggedUsers),
                            parse_mode="HTML")

        elif text == "/restrict" and helpers.isAdmin(chatId):
            restrictedMode = True
            bot.sendMessage(chatId, "<i>Modalità ristretta attiva.\n"
                            "Solo gli admin possono usare il bot.</i>",
                            parse_mode="HTML")

        elif text == "/unrestrict" and helpers.isAdmin(chatId):
            restrictedMode = False
            bot.sendMessage(chatId, "<i>Modalità ristretta disattivata.\n"
                            "Tutti gli utenti potranno usare il bot.</i>",
                            parse_mode="HTML")

        elif "reply_to_message" in msg:
            if helpers.isAdmin(chatId):
                try:
                    userId = msg['reply_to_message']['forward_from']['id']
                    bot.sendMessage(userId,
                                    "💬 <b>Risposta dello staff</b>\n"
                                    "{0}".format(text),
                                    parse_mode="HTML")
                    bot.sendMessage(chatId, "Risposta inviata!")
                except Exception:
                    bot.sendMessage(chatId, "Errore nell'invio.")
            else:
                if text.lower() == "no":
                    bot.sendMessage(chatId,
                                    "<i>Ah ok, scusa.</i>",
                                    parse_mode="HTML")
                else:
                    bot.sendMessage(
                        chatId, "Scrivi /support per parlare con lo staff.")

        elif text == "/annulla":
            bot.sendMessage(chatId, "😴 Nessun comando da annullare!")

        elif helpers.hasStoredCredentials(chatId):
            if text == "/start":
                bot.sendMessage(chatId,
                                "Bentornato, <b>{0}</b>!\n"
                                "Cosa posso fare per te? 😊".format(name),
                                parse_mode="HTML")

            elif text == "/login":
                bot.sendMessage(
                    chatId, "Sei già loggato.\n"
                    "Premi /logout per uscire.")

            elif text == "/logout":
                bot.sendMessage(
                    chatId,
                    "Tutti i tuoi dati scolastici e le credenziali verranno eliminate dal bot.\n"
                    "Sei <b>veramente sicuro</b> di voler uscire?",
                    parse_mode="HTML",
                    reply_markup=keyboards.logout())

            elif text == "/didattica":
                helpers.sendLongMessage(
                    chatId,
                    "📚 <b>Files caricati in didattica</b>:\n\n"
                    "{0}".format(stored.didattica),
                    parse_mode="HTML",
                    disable_web_page_preview=True)

            elif text == "/info":
                bot.sendMessage(chatId,
                                "ℹ️ <b>Ecco le tue info</b>:\n\n"
                                "{0}".format(stored.info),
                                parse_mode="HTML")

            elif text == "/prof":
                helpers.sendLongMessage(chatId,
                                        "📚 <b>Lista materie e prof</b>:\n\n"
                                        "{0}".format(stored.prof),
                                        parse_mode="HTML")

            elif text == "/note":
                helpers.sendLongMessage(chatId,
                                        "❗️<b>Le tue note</b>:\n\n"
                                        "{0}".format(stored.note),
                                        parse_mode="HTML")

            elif text == "/voti":
                helpers.sendLongMessage(chatId,
                                        "📝 <b>I tuoi voti</b>:\n\n"
                                        "{0}".format(stored.voti),
                                        parse_mode="HTML")

            elif text == "/assenze":
                helpers.sendLongMessage(chatId,
                                        "{0}".format(stored.assenze),
                                        parse_mode="HTML")

            elif text == "/agenda":
                bot.sendMessage(
                    chatId,
                    "📆 <b>Agenda compiti per le prossime 2 settimane</b>:\n\n"
                    "{0}".format(stored.agenda),
                    parse_mode="HTML",
                    disable_web_page_preview=True)

            elif text == "/domani":
                isSaturday = datetime.now().isoweekday() == 6
                dayString = "lunedì" if isSaturday else "domani"
                bot.sendMessage(chatId,
                                "📆 <b>Compiti e verifiche per {0}</b>:\n\n"
                                "{1}".format(dayString, stored.domani),
                                parse_mode="HTML",
                                disable_web_page_preview=True)

            elif text == "/circolari":
                bot.sendMessage(chatId,
                                "📩 <b>Circolari da leggere</b>:\n\n"
                                "{0}".format(stored.circolari),
                                parse_mode="HTML",
                                disable_web_page_preview=True)

            elif text == "/lezioni":
                bot.sendMessage(chatId,
                                "📚 <b>Lezioni di oggi</b>:\n\n"
                                "{0}".format(stored.lezioni),
                                parse_mode="HTML",
                                disable_web_page_preview=True,
                                reply_markup=keyboards.lezioni())

            elif text == "/settings":
                bot.sendMessage(
                    chatId, "🛠 <b>Impostazioni</b>\n"
                    "Ecco le impostazioni del bot. Cosa vuoi modificare?",
                    parse_mode="HTML",
                    reply_markup=keyboards.settings_menu())

            elif text == "/promemoria":
                bot.sendMessage(chatId,
                                "🕙 <b>Promemoria!</b>\n\n"
                                "📆 <b>Cosa devi fare per domani</b>:\n\n"
                                "{0}\n\n\n"
                                "📚 <b>Le lezioni di oggi</b>:\n\n"
                                "{1}".format(stored.domani, stored.lezioni),
                                parse_mode="HTML",
                                disable_web_page_preview=True)

            elif text == "/aggiorna":
                if (user.remainingCalls > 0) or (helpers.isAdmin(chatId)):
                    if not updateLock.acquire(blocking=False):
                        bot.sendMessage(
                            chatId, "ℹ️ <b>Aspetta!</b>\n"
                            "Il bot sta già eseguendo degli aggiornamenti globali per tutti gli utenti.",
                            parse_mode="HTML")
                        return

                    sent = bot.sendMessage(chatId,
                                           "📙📙📙 Cerco aggiornamenti... 0%")
                    user.remainingCalls -= 1
                    commit()
                    updateLock.release()
                    bot.editMessageText((chatId, sent['message_id']),
                                        "📗📙📙 Cerco aggiornamenti... 10%")
                    api = ClasseVivaAPI()
                    bot.editMessageText((chatId, sent['message_id']),
                                        "📗📙📙 Cerco aggiornamenti... 20%")

                    if helpers.userLogin(chatId, api):
                        bot.editMessageText((chatId, sent['message_id']),
                                            "📗📙📙 Cerco aggiornamenti... 35%")
                        try:
                            data = helpers.fetchStrict(api)
                        except ApiServerError:
                            bot.editMessageText((
                                chatId, sent['message_id']
                            ), "⚠️ I server di ClasseViva non sono raggiungibili.\n"
                                                "Riprova tra qualche minuto.")
                            return
                        bot.editMessageText((chatId, sent['message_id']),
                                            "📗📗📙 Cerco aggiornamenti... 50%")
                        dataDidattica = parsers.parseNewDidattica(
                            userdata.didattica, data['didattica'])
                        bot.editMessageText((chatId, sent['message_id']),
                                            "📗📗📙  Cerco aggiornamenti... 60%")
                        dataNote = parsers.parseNewNote(
                            userdata.note, data['note'])
                        bot.editMessageText((chatId, sent['message_id']),
                                            "📗📗📙 Cerco aggiornamenti... 70%")
                        dataVoti = parsers.parseNewVoti(
                            userdata.voti, data['voti'], chatId)
                        bot.editMessageText((chatId, sent['message_id']),
                                            "📗📗📙 Cerco aggiornamenti... 80%")
                        dataAgenda = parsers.parseNewAgenda(
                            userdata.agenda, data['agenda'], chatId)
                        bot.editMessageText((chatId, sent['message_id']),
                                            "📗📗📙 Cerco aggiornamenti... 90%")
                        dataCircolari = parsers.parseNewCircolari(
                            userdata.circolari, data['circolari'])
                        bot.editMessageText((chatId, sent['message_id']),
                                            "📗📗📗  Cerco aggiornamenti... 100%")

                        if dataDidattica is not None:
                            bot.sendMessage(
                                chatId,
                                "🔔 <b>Nuovi file caricati!</b>{0}".format(
                                    dataDidattica),
                                parse_mode="HTML",
                                disable_web_page_preview=True)
                        if dataNote is not None:
                            bot.sendMessage(
                                chatId,
                                "🔔 <b>Hai nuove note!</b>{0}".format(dataNote),
                                parse_mode="HTML")
                        if dataVoti is not None:
                            bot.sendMessage(
                                chatId,
                                "🔔 <b>Hai nuovi voti!</b>{0}".format(dataVoti),
                                parse_mode="HTML")
                        if dataAgenda is not None:
                            bot.sendMessage(
                                chatId,
                                "🔔 <b>Hai nuovi impegni!</b>\n{0}".format(
                                    dataAgenda),
                                parse_mode="HTML",
                                disable_web_page_preview=True)
                        if dataCircolari is not None:
                            bot.sendMessage(
                                chatId,
                                "🔔 <b>Hai nuove circolari!</b>{0}".format(
                                    dataCircolari),
                                parse_mode="HTML",
                                disable_web_page_preview=True)

                        if not any([
                                dataDidattica, dataNote, dataVoti, dataAgenda,
                                dataCircolari
                        ]):
                            bot.editMessageText((chatId, sent['message_id']),
                                                "📗 Dati aggiornati!\n"
                                                "📗 Nessuna novità!")
                        else:
                            bot.deleteMessage((chatId, sent['message_id']))

                        helpers.updateUserdata(chatId, data)
                        helpers.fetchAndStore(chatId,
                                              api,
                                              data,
                                              fetch_long=True)

                else:
                    bot.sendMessage(
                        chatId,
                        "⛔️ Hai usato troppi /aggiorna recentemente. Aspetta un po'!"
                    )

            elif (text == "/support") or (text == "/start support"):
                user.status = "calling_support"
                bot.sendMessage(
                    chatId, "🆘 <b>Richiesta di supporto</b>\n"
                    "Se hai qualche problema che non riesci a risolvere, scrivi qui un messaggio, e un admin "
                    "ti contatterà il prima possibile.\n\n"
                    "<i>Per annullare, premi</i> /annulla.",
                    parse_mode="HTML")

            elif text == "/orario":
                if not Document.exists(
                        lambda d: (d.chatId == chatId and d.type == "orario")):
                    user.status = "sending_orario"
                    bot.sendMessage(
                        chatId, "🕑 <b>Impostazione orario</b>\n"
                        "Inviami un documento (PDF oppure foto) per salvarlo e richiamarlo "
                        "quando serve con /orario!",
                        parse_mode="HTML")
                else:
                    doc = Document.get(chatId=chatId, type="orario")
                    sendFunc = bot.sendPhoto if doc.data[
                        "ext"] == "photo" else bot.sendDocument
                    sendFunc(chatId,
                             doc.data["fileId"],
                             reply_markup=keyboards.mod_orario())

            elif text == "/memo":
                user.status = "memo_date"
                today = datetime.now().weekday()
                bot.sendMessage(
                    chatId, "💡 <b>Memo personale</b>\n"
                    "Crea un memo personale per aggiungere i compiti da fare all'agenda!\n"
                    "Inviami <b>la data</b> di consegna, nel formato GG/MM, oppure scegli un'opzione "
                    "da quelle qui sotto.\n\n"
                    "Premi /annulla per annullare.",
                    parse_mode="HTML",
                    reply_markup=keyboards.create_memo(today))

            elif text == "/resetmemo":
                memos = select(m for m in Document
                               if m.chatId == chatId and m.type == "memo")[:]
                for memo in memos:
                    memo.delete()
                bot.sendMessage(chatId, "✅ Ho eliminato tutte le memo!")

            # Custom Start Parameters
            elif text.startswith("/start "):
                param = text.split(' ')[1]
                if param.startswith("circ"):
                    evtCode = param.split("-")[0].replace("circ", "")
                    pubId = int(param.split("-")[1])
                    sendFile(chatId, "circolare", evtCode, pubId)

                elif param.startswith("file"):
                    intId = int(param[4:])
                    sendFile(chatId, "file", intId)

                elif param.startswith("link"):
                    intId = int(param[4:])
                    sendFile(chatId, "link", intId)

            elif text == "⬆️⬆️⬇️⬇️⬅️➡️⬅️➡️🅱️🅰️" or text == "⬆️⬆️⬇️⬇️⬅️➡️⬅️➡️🅱🅰":
                from random import choice
                today = datetime.today().strftime("%d/%m/%Y")
                subject = choice(
                    ["MATEMATICA", "ITALIANO", "INGLESE", "STORIA"])
                bot.sendMessage(chatId,
                                "🔔 <b>Hai nuovi voti!</b>\n\n"
                                "📚 <b>{}</b>\n\n"
                                "📗 <b>Voto 10</b> • Scritto • {}\n"
                                "<i>Start!</i>".format(subject, today),
                                parse_mode="HTML")

            # Text is not a keyword
            else:
                bot.sendMessage(
                    chatId, "Non ho capito...\n"
                    "Serve aiuto? Premi /help")

        # User not logged in
        else:
            if text == "/login":
                user.status = "login_0"
                bot.sendMessage(
                    chatId,
                    "Per favore, inviami il tuo <b>username</b> (quello che usi per accedere al registro).\n"
                    "Usa /annulla se serve.",
                    parse_mode="HTML")
            else:
                bot.sendMessage(chatId,
                                "Benvenuto, <b>{0}</b>!\n"
                                "Per utilizzarmi devi eseguire il /login.\n\n"
                                "Premi /help se serve aiuto.".format(name),
                                parse_mode="HTML")

    # Normal user with restricted mode
    else:
        if text == "/moreinfo":
            bot.sendMessage(
                chatId, "❓ <b>Che genere di problemi?</b>\n"
                "ClasseViva ha recentemente imposto un limite alle sue API, che questo bot utilizza, "
                "anche se è nascosto e non sempre uguale. Con l'aumentare degli utenti, il bot doveva fare "
                "sempre più richieste e una volta raggiunto il limite ha cominciato a non funzionare più.\n"
                "La soluzione sarebbe avere una lista di minimo 20 proxy da utilizzare per fare le richieste "
                "(si tratta di max. 200KB di traffico ogni 30 minuti, non è quello il problema), che "
                "però è molto difficile da trovare senza pagare qualche servizio, che al momento non posso permettermi.\n\n"
                "❔ <b>Posso contattarti?</b>\n"
                "Certo, puoi scrivermi per qualsiasi motivo <a href=\"https://t.me/pesaventofilippo\">qui</a> "
                "o mandarmi una mail a [email protected].\n"
                "Non sono un bot, quindi magari non rispondo subito 🙂",
                parse_mode="HTML",
                disable_web_page_preview=True)

        else:
            bot.sendMessage(
                chatId, "ℹ️ <b>Bot in manutenzione</b>\n"
                "Il bot è attualmente in manutenzione per problemi con ClasseViva, e tutte le sue "
                "funzioni sono temporaneamente disabilitate.\n"
                "Non eliminare questa chat: se vuoi puoi archiviarla su Telegram, così appena "
                "ci saranno notizie ti manderò un messaggio.\n\n"
                "/moreinfo",
                parse_mode="HTML")
示例#17
0
def updateData():
    data = Data.get(id=0)
    data.pewdiepie, data.tseries, data.difference = youtube.fetchData()