def getPictosColor(bot, update, args): ''' Functions launched when /getPicsColor command is executed. Returns json python dictionary with all color pictograms that contains some on the words included in user_data ''' query = 'http://arasaac.org/api/index.php?callback=json' query += '&language=ES' query += '&word=' + args[0] query += '&catalog=colorpictos&nresults=500&thumbnailsize=100&TXTlocate=1' query += '&KEY=' + config.loadArasaacApiKey(".arasaacApiKey") logger.info("/getPicsColor QUERY: {}".format(query)) http = config.httpPool() req = http.request('GET', query) datos = json.loads(req.data.decode('utf-8')) pictos = datos["symbols"] logger.info("/getPicsColor PICTOS: {}".format(pictos)) for picto in pictos: bot.send_message(chat_id=update.message.chat_id, text='<a href="' + picto['imagePNGURL'] + '">' + picto['name'] + '</a>', parse_mode=telegram.ParseMode.HTML) if 'soundMP3URL' in picto.keys(): bot.send_audio(chat_id=update.message.chat_id, audio=picto['soundMP3URL'])
def getPictosBW(bot, update, args): ''' Functions launched when /getPicsBN command is executed. Returns a json python dictionary with all BN pictograms that contains some on the words included in user_data ''' query = 'http://arasaac.org/api/index.php?callback=json' query += '&language=ES' query += '&word=' + args[0] query += '&catalog=bwpictos&nresults=500&thumbnailsize=100&TXTlocate=1' query += '&KEY=' + config.loadArasaacApiKey(".arasaacApiKey") logger.info("/getPicsBW QUERY: {}".format(query)) http = config.httpPool() req = http.request('GET', query) datos = json.loads(req.data.decode('utf-8')) pictos = datos["symbols"] logger.info("/getPicsBW PICTOS: {}".format(pictos)) if len(pictos) > 0: for picto in pictos: html = '<a href="' + picto['imagePNGURL'] + '">' + picto[ 'name'] + '</a>\n' bot.send_message(chat_id=update.message.chat_id, text=html, parse_mode=telegram.ParseMode.HTML) if 'soundMP3URL' in picto.keys(): bot.send_audio(chat_id=update.message.chat_id, audio=picto['soundMP3URL']) else: bot.sendPhoto(chat_id=update.message.chat_id, photo=open('images/ArasaacBot_icon_100x100.png', 'rb')) bot.send_message( chat_id=update.message.chat_id, text="*No pictograms were founded!!* \n launch another query", parse_mode=telegram.ParseMode.MARKDOWN)
def getPictos(lang, word, force=False): ''' Function that return a list of pictos in a specific languages ''' word_ascii_utf8 = unicodedata.normalize('NFD', word).encode( 'ascii', 'ignore').decode('utf-8') pictos = [] if not (force): pictos = existsInCacheAndValid(lang, word) # if the return from cache is empty or force is true, send the request if len(pictos) < 1 or force: query = 'http://arasaac.org/api/index.php?callback=json' query += '&language=' + lang query += '&word=' + word_ascii_utf8 query += '&catalog=colorpictos&nresults=500&thumbnailsize=100&TXTlocate=4' query += '&KEY=' + config.loadArasaacApiKey(".arasaacApiKey") http = config.httpPool() req = http.request('GET', query) datos = json.loads(req.data.decode('utf-8')) pictos_temp = datos["symbols"] if len(pictos_temp) > 0: insertPictosDatabase(word, lang, pictos_temp) pictos = pictos_temp return pictos
def getPictosFromArasaac(query_url): ''' Function that get an url with the API web request and return an array of json objects (answer) ''' print("QUERYURL: {}".format(query_url)) http = config.httpPool() # GET request req = http.request('GET', query_url) # load the answer in JSON format data = json.loads(req.data.decode('utf-8')) # only return 'symbols' field (array of JSON objects) pictograms = data["symbols"] return pictograms
def getPictosFromArasaacAPI(language, word): ''' Functions that construct a http query for Arasaac API with a word and a language. TXTLocate=Exactly matching. ''' pictos = [] query = 'http://arasaac.org/api/index.php?callback=json' query += '&language='+language query += '&word='+word query += '&catalog=colorpictos&nresults=500&thumbnailsize=100&TXTlocate=4' query += '&KEY=' + config.loadArasaacApiKey(".arasaacApiKey") http = config.httpPool() req = http.request('GET', query) datos = json.loads(req.data.decode('utf-8')) pictos_temp = datos["symbols"] pictos = pictos_temp return pictos
def main(): #Configure logging module logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO) logger = logging.getLogger(__name__) logger.info('Starting logger') # Get ARASAAC API KEY and TELEGRAM_API_KEY global ARASAAC_API_KEY ARASAAC_API_KEY = config.loadArasaacApiKey(".arasaacApiKey") global TELEGRAM_API_KEY TELEGRAM_API_KEY = config.loadTelegramApiKey(".telegramApiKey") http = config.httpPool() logger.info(type(http)) # Create database (sqlite3) bot.db config.createBotDatabase("bot.sqlite3") # Creating an updater object of the Bot updater = telegram.ext.Updater(TELEGRAM_API_KEY) # Start command updater.dispatcher.add_handler(telegram.ext.CommandHandler('start', start)) # picsColor command - Get pictograms in color that contain the word passed updater.dispatcher.add_handler( telegram.ext.CommandHandler('picsColor', commands.pictos.getPictosColor, pass_args=True)) # picsBW command - Get pictograms in BW that contains the word passed updater.dispatcher.add_handler( telegram.ext.CommandHandler('picsBW', commands.pictos.getPictosBW, pass_args=True)) updater.dispatcher.add_handler( telegram.ext.CommandHandler('picsBN', commands.pictos.getPictosBW, pass_args=True)) # picto command - Command that init a wizard to make a search updater.dispatcher.add_handler( telegram.ext.CommandHandler('pics', commands.pictos.getPics, pass_args=True)) updater.dispatcher.add_handler( telegram.ext.CommandHandler('translate', commands.translate.translate, pass_args=True)) updater.dispatcher.add_handler( telegram.ext.CommandHandler('traducir', commands.translate.translate, pass_args=True)) # About command updater.dispatcher.add_handler(telegram.ext.CommandHandler('about', about)) # Restart commmand updater.dispatcher.add_handler( telegram.ext.CommandHandler('restart', commands.admin.restart)) updater.dispatcher.add_handler( telegram.ext.InlineQueryHandler(inline.pictoInline.pictoInline)) # CallbackQueryHandlers os /pics command 1º Choose color 2º Choose language # 3º search property (start with, contains, end with and exactly) updater.dispatcher.add_handler( telegram.ext.CallbackQueryHandler(commands.pictos.getPics_stage1_color, pattern="pics.color")) updater.dispatcher.add_handler( telegram.ext.CallbackQueryHandler( commands.pictos.getPics_stage2_language, pattern="pics.language")) updater.dispatcher.add_handler( telegram.ext.CallbackQueryHandler( commands.pictos.getPics_stage3_search, pattern="pics.search")) updater.dispatcher.add_handler( telegram.ext.CallbackQueryHandler( commands.translate.translate_stage1_language_callback, pattern="tr.lang")) updater.dispatcher.add_handler( telegram.ext.CallbackQueryHandler( commands.translate.translate_stage2_word_callback, pattern="tr.word")) updater.dispatcher.add_handler( telegram.ext.CallbackQueryHandler(commands.translate.agenda_callback, pattern="agenda")) # init Bot updater.start_polling() logger.info("Bot started") updater.idle()