Skip to content

Instantly share code, notes, and snippets.

@Azzeccagarbugli
Created February 15, 2017 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Azzeccagarbugli/9f94429c31f25b936d143ae567f09791 to your computer and use it in GitHub Desktop.
Save Azzeccagarbugli/9f94429c31f25b936d143ae567f09791 to your computer and use it in GitHub Desktop.
FrazionetorBOT
import telepot
import string
import time
import sys
from fractions import Fraction
machine_state = -1
numeratore = 1
denominatore = 1
class Frazione:
numeratore_sempl = 1
denominatore_sempl = 1
def __init__(self, n, d):
numeratore_sempl = self.n
denominatore_sempl = self.d
# Funzione usata per semplificare le frazioni inserite dall'utente
def Semplifica():
#numeratore_sempl = 1
#denominatore_sempl = 1
#long i
if int(numeratore_sempl) > int(denominatore_sempl):
i = int(numeratore_sempl)
else:
i = int(denominatore_sempl)
while i > 0:
if int(numeratore_sempl % i == 0) and int(denominatore_sempl % i == 0):
numeratore_sempl = int(numeratore_sempl / i)
denominatore_sempl = int(denominatore_sempl / i)
if (numeratore_sempl != numeratore_sempl / i) and (denominatore_sempl != denominatore_sempl / i):
#Console.WriteLine ("\nLa frazione semplificata è: {0}/{1}\n", numeratore, denominatore);
frazione_sw = ("La frazione semplificata è: {0}/{1}".format(numeratore_sempl, denominatore_sempl))
#frazione_sw = ("La frazione semplificata è: *%s*/*%s*" % numeratore, denominatore), parse_mode = "Markdown"
return frazione_sw
print(frazione_sw)
else:
#Console.WriteLine ("\nLa frazione non può essere semplificata\n");
frazione_sw = ("La frazione non può essere ridotta ai minimi termini: {0}/{1}".format(numeratore_sempl, denominatore_sempl))
return frazione_sw
print(frazione_sw)
break
i = i - 1
# Funzione che viene eseguita all'arrivo di ogni nuovo messaggio
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
global machine_state
global numeratore
global denominatore
chat_id = msg['chat']['id']
command_input = msg['text']
print(content_type, chat_type, chat_id)
if machine_state == -1 and content_type == 'text':
if command_input == '/start' or command_input == '/start@FrazionetorBot':
start_text = '''Benvenuto nel futuro! Inzia a digitare un comando per cominciare un'esperienza metafisica'''
bot.sendMessage(chat_id, start_text)
machine_state = 0
elif machine_state == 0 and content_type == 'text':
if command_input == '/help' or command_input == '/help@FrazionetorBot':
help_text = "Salve, puoi utilizzare il comando /frazionifica per iniziare a semplificare"
help_text += "le frazioni mentre puoi usare il comando /cronologia per controllare la cronologia"
help_text += "dei tuoi calcoli.\nPuoi contattare lo sviluppatore su github.com/Azzeccagarbugli"
bot.sendMessage(chat_id, help_text)
machine_state = 1
elif machine_state == 1 and content_type == 'text':
if command_input == '/frazionifica' or command_input == '/start@FrazionetorBot':
numerator_text = 'Inserisci il numeratore della frazione che vuoi semplificare'
bot.sendMessage(chat_id, numerator_text)
machine_state = 2
else:
problem_text = '''Cosa cavolo sono queste cose?! Mi stai prendendo per un deficente forse?'''
bot.sendMessage(chat_id, problem_text)
machine_state = 0
elif machine_state == 2 and content_type == 'text':
if command_input.isdigit() == True:
numeratore = int(float(command_input))
denominatore_text = "Inserisci il denominatore della frazione che vuoi semplificare"
bot.sendMessage(chat_id, denominatore_text)
machine_state = 6
elif command_input.isdigit() == False:
numeratore_problem_text = "Quello che hai inserito non è un valore numerico! Inserisci un"
numeratore_problem_text += "numero o altrimenti premi la lettera 'q' per uscire dal comando /frazionifica"
bot.sendMessage(chat_id, numeratore_problem_text)
machine_state = 5
else:
problem_text = '''Cosa cavolo sono queste cose?! Mi stai prendendo per un deficente forse?'''
bot.sendMessage(chat_id, problem_text)
machine_state = 0
elif machine_state == 6 and content_type == 'text':
if command_input.isdigit() == True:
denominatore = int(float(command_input))
Fraz = Frazione(numeratore, denominatore)
t = Fraz.Semplifica()
print(t)
# frazione_sempl = semplifica(numeratore, denominatore)
# print(frazione_sempl)
#bot.sendMessage(chat_id, ("La frazione semplificata è: *%s*" % frazione_sempl), parse_mode = "Markdown")
#frazione_sempl = semplifica(numeratore, denominatore)
#bot.sendMessage(chat_id, ("La frazione semplificata è: *%s*" % frazione_sempl), parse_mode = "Markdown")
machine_state = 1
elif command_input.isdigit() == False:
numeratore_problem_text = "Quello che hai inserito non è un valore numerico! Inserisci un"
numeratore_problem_text += "numero o altrimenti premi la lettera 'q' per uscire dal comando /frazionifica"
bot.sendMessage(chat_id, numeratore_problem_text)
machine_state = 8
else:
problem_text = '''Cosa cavolo sono queste cose?! Mi stai prendendo per un deficente forse?'''
bot.sendMessage(chat_id, problem_text)
machine_state = 0
bot = telepot.Bot('XXXXXXX:AAGj0xH5zWNXXXXX94DDV8P865H2PiHyQCHwXrdw')
bot.message_loop(handle)
print('Vediamo quello che succede ...')
while 1:
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment