Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- #JunoBot.py Versione Telegram Bot di JunoV2.py
- import telepot , random , time , techcore , re , os , sys
- memory = {}
- name = "Juno"
- UName = ""
- operatorRegex = re.compile(r"""(
- ((.*) #Testo +
- (\s+))? #Spazi iniziali
- (quanto\s{1}?fa|qual'è\s{1}?il\s{1}?risultato di|calcolami)? #Domanda
- (\s*) #Spazio
- (\d+) #Primo Numero
- (\s*) #Spazio
- (\+|\*|-|/|\*\*) #Operatore
- (\s*) #Spazio
- (\d+) #Secondo Numero
- )""" , re.VERBOSE)
- info = """
- Io sono %s, un Intelligenza artificiale molto basilare
- creata in Python grazie alla libreria Telepot.
- """ % (name)
- qd = False
- sd = ""
- def response(msg) :
- global qd
- global sd
- global info
- global memory
- chat_id = msg["chat"]["id"]
- d = msg["text"]
- print(str(chat_id) + " scrive: " + d)
- if d == "/start" :
- print("Risposta: " + "Ciao, sono Juno, cosa mi vuoi dire?")
- bot.sendMessage(chat_id , "Ciao, sono Juno, cosa mi vuoi dire?")
- return
- if d == "/info" :
- bot.sendMessage(chat_id , info)
- print("Risposta : " + info)
- return
- if d == "/reset" :
- print("Memory resetting...")
- bot.sendMessage(chat_id , "Resetto la memoria...")
- memory = {"come ti chiami" : "Mi chiamo " + name , "quanti anni hai" : "2 anni" , "come stai" : "Bene" , "ciao " + name.lower() : "Ciao anche a te" , "bravo " + name.lower() : "Grazie!" , "sai chi sono io?" : "Tu sei " + UName}
- return
- if qd == True :
- if d != "niente" :
- memory[sd] = d
- bot.sendMessage(chat_id , "Risposta Salvata!")
- print("Risposta: " + "Risposta Salvata!")
- else :
- bot.sendMessage(chat_id , "Non ho salvato!")
- print("Risposta: " + "Non ho salvato!")
- qd = False
- sd = ""
- return
- d = techcore.semplifyText(d)
- if operatorRegex.search(d) != None :
- Match = operatorRegex.search(d)
- Num1 = int(Match.group(7))
- Num2 = int(Match.group(11))
- Operator = Match.group(9)
- ris = 0
- if Operator == "+" :
- ris = Num1 + Num2
- elif Operator == "-" :
- ris = Num1 - Num2
- elif Operator == "*" :
- if (Num2 > 999 or Num1 > 999) :
- bot.sendMessage(chat_id , "Numero troppo grande!")
- print("Risposta: " + "Numero troppo grande!")
- return
- ris = Num1 * Num2
- elif Operator == "/" :
- if Num2 == 0 :
- bot.sendMessage(chat_id , "Non puoi dividere per zero!")
- return
- ris = Num1 / Num2
- elif Operator == "**" :
- if (Num2 > 999 or Num1 > 999) :
- bot.sendMessage(chat_id , "Numero troppo grande!")
- print("Risposta: " + "Numero troppo grande!")
- return
- ris = Num1 ** Num2
- bot.sendMessage(chat_id , "Il risultato è " + str(ris))
- print("Risposta: " + "Il risultato è " + str(ris))
- return
- if d in memory :
- bot.sendMessage(chat_id , memory[d])
- print("Risposta: " + memory[d])
- else :
- bot.sendMessage(chat_id , "Non Ho capito, cosa devo rispondere?")
- print("Risposta: " + "Non Ho capito, cosa devo rispondere?")
- sd = d
- qd = True
- if not os.path.isfile("memory.m") :
- memory = {"come ti chiami" : "Mi chiamo " + name , "quanti anni hai" : "2 anni" , "come stai" : "Bene" , "ciao " + name.lower() : "Ciao anche a te" , "bravo " + name.lower() : "Grazie!" , "sai chi sono io?" : "Tu sei " + UName}
- else :
- memory = techcore.loadFromFile("memory.m")
- try :
- bot = telepot.Bot("censured")
- bot.message_loop(response)
- while 1:
- time.sleep(10)
- except KeyboardInterrupt :
- sys.exit()
- finally :
- techcore.saveInFile(memory , "memory.m")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement