def def_knowledge(bot, update): global Fibot chat_id = update.message.chat_id message = update.message.text Fibot.nlg.give_feedback(chat_id, correct=False, correct_statement=message) Fibot.send_preset_message(chat_id, "corrected_message") Fibot.send_preset_message(chat_id, "send_me_message") return TRAINING
def training_off(bot, update): global Fibot chat_id = update.message.chat_id if Fibot.chats.get_chat(chat_id)['training']: Fibot.chats.update_info(chat_id, 'training', False, overwrite=True) Fibot.send_preset_message(chat_id, "training_inactive") else: Fibot.send_preset_message(chat_id, "training_already_inactive") return MESSAGE_INCOME
def ask(bot, update): global Fibot chat_id = update.message.chat_id text = update.message.text message_id = update.message.message_id if Fibot.chats.get_chat(chat_id)['logged'] & Fibot.chats.token_has_expired( chat_id): Fibot.chats.load() Fibot.process_income_message(chat_id, text, message_id=message_id) return MESSAGE_INCOME
def ask(bot, update): global Fibot chat_id = update.message.chat_id text = update.message.text message_id = update.message.message_id if Fibot.chats.get_chat(chat_id)['logged'] & Fibot.chats.token_has_expired( chat_id): print("This token has expired!!") r_t = Fibot.chats.get_chat(chat_id)['refresh_token'] print("This is the rt {}".format(r_t)) callback = Fibot.oauth.refresh_token(r_t) Fibot.chats.update_chat(chat_id, callback, full_data=False) Fibot.process_income_message(chat_id, text) return MESSAGE_INCOME
def feedback_info(bot, update): global Fibot chat_id = update.message.chat_id message = update.message.text if message == "Sí": Fibot.nlg.give_feedback(chat_id, correct=True) return TRAINING elif message == "No": Fibot.send_preset_message(chat_id, "request_good_answer") return GET_CORRECT else: update.message.reply_text("¿Fué coherente la última respuesta?", reply_markup=markup) return CORR_INCORR
def main(mode): fibot = Fibot() if mode == "train": fibot.qa.load(train=True) if mode == "manual": fibot.qa.load(train=False) fibot.qa.train_manual() return
def main(): parser = argparse.ArgumentParser(description='') parser.add_argument('--nlu', nargs=1, required=False, choices=['es', 'ca', 'en', 'n'], action='append', help='Language for the interpretation') parser.add_argument('--dialog', nargs=1, required=False, choices=['y', 'n'], default=[], help='File for the interpreter to use') args = parser.parse_args() print(args) trainNLU = False trainNLG = False languages = [] if args.nlu: languages = [i[0] for i in args.nlu] trainNLU = True if args.dialog: trainNLG = bool(args.dialog[0] == 'y') config = tf.ConfigProto() config.gpu_options.allow_growth = True tf.Session(config=config) fibot = Fibot() fibot.qa.load(trainNLG=trainNLG, trainNLU=trainNLU, train_list=languages) return
def main(): global Fibot parser = argparse.ArgumentParser(description='') parser.add_argument('--thread_log', action='store_true', help='Whether to log the threads info') args = parser.parse_args() if args.thread_log: print(colored("LOG: Thread logging activo", 'cyan')) else: print(colored("LOG: Thread logging inactivo", 'cyan')) Fibot.load_components(thread_logging=bool(args.thread_log)) print(colored("LOG: Todo inicializado", 'cyan')) # Create the Updater and pass it your bot's token. updater = Updater(Fibot.bot_token) dp = updater.dispatcher conv_handler = ConversationHandler( entry_points=[ CommandHandler('start', start, pass_args=True), CommandHandler('login', start_authentication), CommandHandler('logout', logout), CommandHandler('updates_on', updates_on), CommandHandler('updates_off', updates_off), CommandHandler('set_lang', set_lang), MessageHandler(filters=Filters.text, callback=state_machine) ], states={ MESSAGE_INCOME: [MessageHandler(filters=Filters.text, callback=state_machine)], }, fallbacks=[RegexHandler('^Done$', done)], allow_reentry=True #So users can use /login ) dp.add_handler(conv_handler) # Start the Bot updater.start_polling() # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()
def logout(bot, update): global Fibot chat_id = update.message.chat_id user_name = Fibot.chats.get_chat(chat_id)['name'] if Fibot.chats.get_chat(chat_id)['logged']: data = { 'name': user_name, 'language': Fibot.chats.get_chat(chat_id)['language'], 'access_token': None, 'refresh_token': None, 'current_state': Fibot.state_machine['MessageHandler'], 'expire_time_end': None, 'logged': False, 'notifications': False } Fibot.chats.update_chat(chat_id, data) Fibot.send_preset_message(chat_id, "logout_done", user_name) else: Fibot.send_preset_message(chat_id, "logout_failed", user_name)
def main(): global Fibot #Fibot = Fibot() Fibot.load_components() print("Everything initialisated") # Create the Updater and pass it your bot's token. updater = Updater(Fibot.bot_token) dp = updater.dispatcher conv_handler = ConversationHandler( entry_points=[ CommandHandler('start', start), CommandHandler('login', start_authentication), CommandHandler('logout', logout), CommandHandler('updates_on', updates_on), CommandHandler('updates_off', updates_off), CommandHandler('train_on', training_on), CommandHandler('train_off', training_off) ], states={ MESSAGE_INCOME: [MessageHandler(filters=Filters.text, callback=state_machine)], TRAINING: [MessageHandler(filters=Filters.text, callback=train_machine)], CORR_INCORR: [RegexHandler('^(Sí|No)$', callback=feedback_info)], GET_CORRECT: [MessageHandler(filters=Filters.text, callback=def_knowledge)], }, fallbacks=[RegexHandler('^Done$', done)], allow_reentry=True #So users can use /login ) dp.add_handler(conv_handler) # Start the Bot updater.start_polling() # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()
def authenticate(bot, update): global Fibot chat_id = update.message.chat_id user_name = Fibot.chats.get_chat(chat_id)['name'] url = update.message.text urls = re.findall( 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', url) if not urls: Fibot.send_preset_message(chat_id, "request_oauth_url") Fibot.chats.update_info(chat_id, 'current_state', Fibot.state_machine['Wait_authorisation'], overwrite=True) return MESSAGE_INCOME auth_code = url.split('=')[1] callback = Fibot.oauth.authenticate(auth_code) if isinstance(callback, dict): Fibot.chats.update_chat(chat_id, callback, full_data=False) Fibot.send_preset_message(chat_id, "login_done", user_name) Fibot.chats.update_info(chat_id, 'current_state', Fibot.state_machine['MessageHandler'], overwrite=True) else: Fibot.send_preset_message(chat_id, "url_error") Fibot.chats.update_info( chat_id, 'current_state', Fibot.state_machine_nodes['Wait_authorisation'], overwrite=True) return MESSAGE_INCOME
def training_on(bot, update): global Fibot chat_id = update.message.chat_id if not Fibot.chats.get_chat(chat_id)['training']: Fibot.chats.update_info(chat_id, 'training', True, overwrite=True) Fibot.send_preset_message(chat_id, "training_active") Fibot.send_preset_message(chat_id, "send_me_message") else: Fibot.send_preset_message(chat_id, "training_already_active") return TRAINING
def updates_on(bot, update): global Fibot chat_id = update.message.chat_id if Fibot.chats.get_chat(chat_id)['logged'] and not Fibot.chats.get_chat( chat_id)['notifications']: Fibot.chats.update_info(chat_id, 'notifications', True, overwrite=True) Fibot.send_preset_message(chat_id, "notif_active") elif Fibot.chats.get_chat(chat_id)['logged'] and Fibot.chats.get_chat( chat_id)['notifications']: Fibot.send_preset_message(chat_id, "notif_already_active") else: Fibot.send_preset_message(chat_id, "notif_active_failed")
def set_lang(bot, update): global Fibot languages = ['ca', 'es', 'en'] chat_id = update.message.chat_id text = update.message.text if len(text.split(' ')) > 1: lang = text.split(' ')[1] if lang in languages: Fibot.chats.update_info(chat_id, 'language', lang, overwrite=True) Fibot.send_preset_message(chat_id, "language_change_ok") else: Fibot.send_preset_message(chat_id, "select_language") else: Fibot.send_preset_message(chat_id, "wrong_lang_format")
def start(bot, update, args): global Fibot chat_id = update.message.chat_id if Fibot.chats.user_has_data(chat_id): Fibot.send_preset_message(chat_id, "start_known", Fibot.chats.get_chat(chat_id)['name']) else: user_name = update.message.from_user.first_name data = { 'name': user_name, 'language': 'ca', 'access_token': None, 'refresh_token': None, 'current_state': Fibot.state_machine['MessageHandler'], 'expire_time_end': None, 'logged': False, 'notifications': False } Fibot.chats.update_chat(chat_id, data, compulsory=True) Fibot.send_preset_message(chat_id, "start_unknown_1", user_name) Fibot.send_preset_message(chat_id, "start_unknown_2") Fibot.send_preset_message(chat_id, "start_unknown_3") return MESSAGE_INCOME
def start_authentication(bot, update): global Fibot print(colored("LOG: Empezando autenticación", 'cyan')) chat_id = update.message.chat_id user_name = Fibot.chats.get_chat(chat_id)['name'] logged = Fibot.chats.get_chat(chat_id)['logged'] if (not logged): Fibot.send_preset_message(chat_id, "send_oauth_url", Fibot.oauth.get_autho_full_page()) Fibot.send_preset_message(chat_id, "inform_oauth_procedure") Fibot.chats.update_info(chat_id, 'current_state', Fibot.state_machine['Wait_authorisation'], overwrite=True) else: Fibot.send_preset_message(chat_id, "already_login", user_name) return MESSAGE_INCOME
def main(): CHAT_ID = None parser = argparse.ArgumentParser(description='') parser.add_argument('--thread_log', action='store_true', help='Whether to log the threads info') parser.add_argument('--chat_id', required=False, type=int, default=469557458, help="Chat_id for the conversation") parser.add_argument('--no_debug', action='store_true', required=False, help="Prints debug information about queries") args = parser.parse_args() if args.chat_id: CHAT_ID = args.chat_id debug = not args.no_debug fibot = Fibot(local=True, debug=debug) if args.thread_log: print(colored("LOG: Thread logging activo", 'cyan')) else: print(colored("LOG: Thread logging inactivo", 'cyan')) fibot.load_components(thread_logging=bool(args.thread_log)) print(colored("LOG: Todo inicializado", 'cyan')) print( colored( "INFO: Simulando conversación como usuario con chat_id {}".format( CHAT_ID), 'red')) print(colored("INFO: Escribe 'quit' para terminar conversación", 'red')) message = input('> ') while not message == 'quit': fibot.process_income_message(CHAT_ID, message) message = input('> ')
from telegram import ReplyKeyboardMarkup, ChatAction from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler, ConversationHandler) #-- Local imports --# from Fibot.fibot import Fibot # States of the ConversationHandler MESSAGE_INCOME, TRAINING, CORR_INCORR, GET_CORRECT = range(4) #Custom Keyboard for training models reply_keyboard = [['Sí', 'No']] markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True) #The main object of the bot, see Fibot/fibot.py to understand the implementation Fibot = Fibot() """ Function that responds to the /start command """ def start(bot, update): global Fibot chat_id = update.message.chat_id if Fibot.chats.user_has_data(chat_id): Fibot.send_preset_message(chat_id, "start_known", Fibot.chats.get_chat(chat_id)['name']) else: user_name = update.message.from_user.first_name data = { 'name': user_name,