Advertisement
MRtecno98

Project Juno V2.0.0 Telegram Version

Aug 30th, 2017
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. #! python3
  2. #JunoBot.py Versione Telegram Bot di JunoV2.py
  3.  
  4. import telepot , random , time , techcore , re , os , sys
  5.  
  6. memory = {}
  7. name = "Juno"
  8. UName = ""
  9.  
  10. operatorRegex = re.compile(r"""(
  11.    ((.*)               #Testo +
  12.    (\s+))?             #Spazi iniziali
  13.    (quanto\s{1}?fa|qual'è\s{1}?il\s{1}?risultato di|calcolami)? #Domanda
  14.     (\s*)       #Spazio
  15.     (\d+)       #Primo Numero
  16.     (\s*)       #Spazio
  17.     (\+|\*|-|/|\*\*)    #Operatore
  18.     (\s*)       #Spazio
  19.     (\d+)       #Secondo Numero
  20.    )""" , re.VERBOSE)
  21.  
  22. info = """
  23. Io sono %s, un Intelligenza artificiale molto basilare
  24. creata in Python grazie alla libreria Telepot.
  25. """ % (name)
  26.  
  27. qd = False
  28. sd = ""
  29.  
  30. def response(msg) :
  31.    
  32.     global qd
  33.     global sd
  34.     global info
  35.     global memory
  36.     chat_id = msg["chat"]["id"]
  37.     d = msg["text"]
  38.  
  39.     print(str(chat_id) + " scrive: " + d)
  40.  
  41.     if d == "/start" :
  42.         print("Risposta: " + "Ciao, sono Juno, cosa mi vuoi dire?")
  43.         bot.sendMessage(chat_id , "Ciao, sono Juno, cosa mi vuoi dire?")
  44.         return
  45.  
  46.     if d == "/info" :
  47.         bot.sendMessage(chat_id , info)
  48.         print("Risposta : " + info)
  49.         return
  50.  
  51.     if d == "/reset" :
  52.         print("Memory resetting...")
  53.         bot.sendMessage(chat_id , "Resetto la memoria...")
  54.         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}
  55.         return
  56.    
  57.     if qd == True :
  58.         if d != "niente" :
  59.             memory[sd] = d
  60.             bot.sendMessage(chat_id , "Risposta Salvata!")
  61.             print("Risposta: " + "Risposta Salvata!")
  62.         else :
  63.             bot.sendMessage(chat_id , "Non ho salvato!")
  64.             print("Risposta: " + "Non ho salvato!")
  65.         qd = False
  66.         sd = ""
  67.         return
  68.  
  69.     d = techcore.semplifyText(d)
  70.  
  71.     if operatorRegex.search(d) != None :
  72.         Match = operatorRegex.search(d)
  73.         Num1 = int(Match.group(7))
  74.         Num2 = int(Match.group(11))
  75.         Operator = Match.group(9)
  76.         ris = 0
  77.         if Operator == "+" :
  78.             ris = Num1 + Num2
  79.         elif Operator == "-" :
  80.             ris = Num1 - Num2
  81.         elif Operator == "*" :
  82.             if (Num2 > 999 or Num1 > 999) :
  83.                 bot.sendMessage(chat_id , "Numero troppo grande!")
  84.                 print("Risposta: " + "Numero troppo grande!")
  85.                 return
  86.             ris = Num1 * Num2
  87.         elif Operator == "/" :
  88.             if Num2 == 0 :
  89.                 bot.sendMessage(chat_id , "Non puoi dividere per zero!")
  90.                 return
  91.             ris = Num1 / Num2
  92.         elif Operator == "**" :
  93.             if (Num2 > 999 or Num1 > 999) :
  94.                 bot.sendMessage(chat_id , "Numero troppo grande!")
  95.                 print("Risposta: " + "Numero troppo grande!")
  96.                 return
  97.             ris = Num1 ** Num2
  98.  
  99.         bot.sendMessage(chat_id  , "Il risultato è " + str(ris))
  100.         print("Risposta: " + "Il risultato è " + str(ris))
  101.         return
  102.    
  103.     if d in memory :
  104.         bot.sendMessage(chat_id , memory[d])
  105.         print("Risposta: " + memory[d])
  106.     else :
  107.         bot.sendMessage(chat_id , "Non Ho capito, cosa devo rispondere?")
  108.         print("Risposta: " + "Non Ho capito, cosa devo rispondere?")
  109.         sd = d
  110.         qd = True
  111.  
  112. if not os.path.isfile("memory.m") :
  113.     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}
  114. else :
  115.     memory = techcore.loadFromFile("memory.m")
  116.  
  117. try :
  118.     bot = telepot.Bot("censured")
  119.     bot.message_loop(response)
  120.  
  121.     while 1:
  122.         time.sleep(10)
  123.  
  124. except KeyboardInterrupt :
  125.     sys.exit()
  126.  
  127. finally :
  128.     techcore.saveInFile(memory , "memory.m")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement