def plants_list_cmd(bot, update, args=None): chat_id, chat_type, user_id, text, message = support.extract_update_info( update) support.delete_message(chat_id, message.message_id, bot) if are_banned(chat_id, user_id): return user = get_real_user(user_id) if user is None: bot.sendMessage(chat_id=chat_id, text="❌ Debes registrarte para usar este comando.", parse_mode=telegram.ParseMode.MARKDOWN) return text = "Lista de plantaciones en *{}*:\n".format(message.chat.title) plants_list = get_plant_list(chat_id) if not list(plants_list): bot.sendMessage(chat_id=user_id, text="❌ No hay plantaciones registradas.", parse_mode=telegram.ParseMode.MARKDOWN, disable_web_page_preview=True) return count = 0 for plant in plants_list: plantName = support.replace_plants(plant.plant_type) poi = get_poi(plant.portal) if plant.alerted: text = text + "\n- #{0} *{1}* - ⚠️ {2} ⚠️ - [{3}](https://maps.google.com/maps?q={4},{5})".format( plant.id, plantName, f"{plant.grow_end.hour:02}:{plant.grow_end.minute:02}", poi.name, poi.latitude, poi.longitude) else: text = text + "\n- #{0} *{1}* - {2} - [{3}](https://maps.google.com/maps?q={4},{5})".format( plant.id, plantName, f"{plant.grow_end.hour:02}:{plant.grow_end.minute:02}", poi.name, poi.latitude, poi.longitude) count += 1 if count == 100: pass bot.sendMessage(chat_id=user_id, text=text, parse_mode=telegram.ParseMode.MARKDOWN, disable_web_page_preview=True)
def callback_AlertFortress(bot, job): try: poi_id = job.context.poi_id poi = get_poi(poi_id) lat = poi.latitude lon = poi.longitude button_list = [ [(InlineKeyboardButton("🙋♀️ Voy", callback_data=f'fort_yes_{poi.id}')), (InlineKeyboardButton("🕒 Tardo", callback_data=f'fort_late_{poi.id}')), (InlineKeyboardButton("❌ No voy", callback_data=f'fort_no_{poi.id}'))], [(InlineKeyboardButton("✅ Estoy", callback_data=f'fort_here_{poi.id}')), (InlineKeyboardButton("📍 Ubicación", callback_data=f'fort_ubi_{poi.id}')), (InlineKeyboardButton("⚠️ Aviso", callback_data=f'fort_alert_{poi.id}'))] ] fort_message = bot.edit_message_reply_markup( chat_id=job.context.chat_id, message_id=job.context.message_id, reply_markup=InlineKeyboardMarkup(button_list)) ent = fort_message.parse_entities(["mention"]) for mention in ent: username = fort_message.parse_entity(mention) string = r'\n(🙋♀️|✅|🕒|❌) 🧙(\d|\d\d|\?\?) (🍮|⚔|🐾|📚) {}'.format( username) search = re.search(string, fort_message.text) if search.group(1) == "❌": continue user = get_user_by_name(username[1:]) bot.sendMessage(chat_id=user.id, text=job.context.message_text, parse_mode=telegram.ParseMode.MARKDOWN, disable_web_page_preview=True) return except: return
def gh_btn(bot, update, job_queue): query = update.callback_query data = query.data user = update.effective_user user_id = query.from_user.id text = query.message.text chat_id = query.message.chat.id message_id = query.message.message_id name = query.message.venue.title lat = query.message.location.latitude lon = query.message.location.longitude coords = str(lat) + ", " + str(lon) if are_banned(user_id, chat_id): return queryData = data.split("_") userBtn = queryData[4] if userBtn == str(user_id) or support.is_admin(chat_id, user_id, bot): if queryData[1] == "addplant": bot.delete_message(chat_id=chat_id, message_id=message_id) poi_list = get_poi_list(chat_id, PortalType.GREENHOUSE.value) poi_sorted = sort_list(poi_list, coords) button_list = [] if len(poi_sorted) >= 1: button_list.append([ InlineKeyboardButton( poi_sorted[0].name, callback_data='gh_addubi_{0}_{1}_{2}_{3}'.format( poi_sorted[0].id, queryData[2], user_id, queryData[3])) ]) if len(poi_sorted) >= 2: button_list.append([ InlineKeyboardButton( poi_sorted[1].name, callback_data='gh_addubi_{0}_{1}_{2}_{3}'.format( poi_sorted[1].id, queryData[2], user_id, queryData[3])) ]) if len(poi_sorted) >= 3: button_list.append([ InlineKeyboardButton( poi_sorted[2].name, callback_data='gh_addubi_{0}_{1}_{2}_{3}'.format( poi_sorted[2].id, queryData[2], user_id, queryData[3])) ]) if len(poi_sorted) >= 4: button_list.append([ InlineKeyboardButton( poi_sorted[3].name, callback_data='gh_addubi_{0}_{1}_{2}_{3}'.format( poi_sorted[3].id, queryData[2], user_id, queryData[3])) ]) if len(poi_sorted) >= 5: button_list.append([ InlineKeyboardButton( poi_sorted[4].name, callback_data='gh_addubi_{0}_{1}_{2}_{3}'.format( poi_sorted[4].id, queryData[2], user_id, queryData[3])) ]) button_list.append([ InlineKeyboardButton( "❌ Cancelar", callback_data='gh_cancel_._._{}'.format(user_id)) ]) plant = support.replace_plants(int(queryData[2])) bot.send_venue(chat_id=chat_id, title=plant + " " + queryData[3], address="¿En qué invernadero está plantado?", latitude=lat, longitude=lon, reply_markup=InlineKeyboardMarkup(button_list)) return elif queryData[1] == "addubi": userTime = datetime.strptime(queryData[5], '%H:%M') group = get_group(chat_id) group_tz = group.timezone tz = pytz.timezone(group_tz) userDatetime = datetime.now().replace(hour=userTime.hour, minute=userTime.minute, second=0) userAsLocal = tz.localize(userDatetime) userAsLocal = userAsLocal.astimezone(pytz.utc) ''' if datetime.now(pytz.utc) > userAsLocal: userAsLocal = userAsLocal + timedelta(days=1) ''' userAsLocal15 = userAsLocal - timedelta(minutes=15) userAsLocalDeletePlant = userAsLocal + timedelta(minutes=30) userAsLocal15 = userAsLocal15.time() userAsLocalTime = userAsLocal.time() userAsLocalDPTime = userAsLocalDeletePlant.time() plant = support.replace_plants(int(queryData[3])) setPlant = set_plant(queryData[2], queryData[3], chat_id, userDatetime, userAsLocalDeletePlant) thePlant = get_plant(setPlant) poi = get_poi(queryData[2]) ap_object = support.AlertPlantContext( chat_id, "¡Magos de *{0}*, en 15 minutos se podrá recoger *{1}* en [{2}](https://maps.google.com/maps?q={3},{4})!" .format(query.message.chat.title, plant, poi.name, poi.latitude, poi.longitude), False, thePlant.id) job_queue.run_once(support.callback_AlertPlant, userAsLocal15, context=ap_object, name="{}_plantJob15".format(thePlant.id)) ap_object = support.AlertPlantContext( chat_id, "¡Magos de *{0}*, ya se puede recoger *{1}* en [{2}](https://maps.google.com/maps?q={3},{4})!" .format(query.message.chat.title, plant, poi.name, poi.latitude, poi.longitude), True, thePlant.id) job_queue.run_once(support.callback_AlertPlant, userAsLocalTime, context=ap_object, name="{}_plantJob".format(thePlant.id)) dp_object = support.DeletePlantContext(thePlant.id) job_queue.run_once(support.callback_DeletePlant, userAsLocalDPTime, context=dp_object, name="{}_plantJobDelete".format(thePlant.id)) bot.delete_message(chat_id=chat_id, message_id=message_id) success_message = bot.sendMessage( chat_id=chat_id, text="🌱 Plantación añadida correctamente.", parse_mode=telegram.ParseMode.MARKDOWN) delete_object = support.DeleteContext(chat_id, success_message.message_id) job_queue.run_once(support.callback_delete, 10, context=delete_object) support.save_jobs(job_queue) return elif queryData[1] == "cancel": bot.delete_message(chat_id=chat_id, message_id=message_id) return else: bot.answer_callback_query( callback_query_id=query.id, text= "Sólo un administrador o el usuario que ha creado el aviso puede pulsar ese botón.", show_alert=True)
def fort_btn(bot, update, job_queue): query = update.callback_query data = query.data user = update.effective_user username = query.from_user.username user_id = query.from_user.id text = query.message.text chat_id = query.message.chat.id message_id = query.message.message_id message = query.message markdown_text = query.message.text_markdown_urled if are_banned(user_id, chat_id): return user = get_user(user_id) if user is None: bot.answer_callback_query( query.id, "❌ Debes registrarte para usar esta función.", show_alert=True) return queryData = data.split("_") if len(queryData) == 5: if queryData[3] == str(user_id) or support.is_admin( chat_id, user_id, bot): if queryData[1] == "addubi": group = get_group(chat_id) group_tz = group.timezone tz = pytz.timezone(group_tz) try: userTime = datetime.strptime(queryData[4], '%d/%H:%M') userDatetime = datetime.now().replace( day=userTime.day, hour=userTime.hour, minute=userTime.minute, second=0) dateText = f"el *{userDatetime.day}/{userDatetime.month}* a las *{userDatetime.hour:02}:{userDatetime.minute:02}*" except: userTime = datetime.strptime(queryData[4], '%H:%M') userDatetime = datetime.now().replace( hour=userTime.hour, minute=userTime.minute, second=0) dateText = f"a las *{userDatetime.hour:02}:{userDatetime.minute:02}*" userAsLocal = tz.localize(userDatetime) userAsLocal = userAsLocal.astimezone(pytz.utc) if datetime.now(pytz.utc) > userAsLocal: userAsLocal = userAsLocal + timedelta(days=1) userDatetime = userDatetime + timedelta(days=1) dateText = f"el *{userDatetime.day}/{userDatetime.month}* a las *{userDatetime.hour:02}:{userDatetime.minute:02}*" userAsLocal30 = userAsLocal - timedelta(minutes=30) #userAsLocal30 = userAsLocal30.time() #userAsLocalTime = userAsLocal.time() userAsLocal = userAsLocal.replace(tzinfo=None) poi = get_poi(queryData[2]) lat = poi.latitude lon = poi.longitude button_list = [[ (InlineKeyboardButton("🙋♀️ Voy", callback_data=f'fort_yes_{poi.id}')), (InlineKeyboardButton( "🕒 Tardo", callback_data=f'fort_late_{poi.id}')), (InlineKeyboardButton("❌ No voy", callback_data=f'fort_no_{poi.id}')) ], [(InlineKeyboardButton( "✅ Estoy", callback_data=f'fort_here_{poi.id}')), (InlineKeyboardButton( "📍 Ubicación", callback_data=f'fort_ubi_{poi.id}')), (InlineKeyboardButton( "⚠️ Aviso", callback_data=f'fort_alert_{poi.id}'))]] text = "Fortaleza en [{0}](https://maps.google.com/maps?q={1},{2}) {3}\n\nLista:".format( poi.name, lat, lon, dateText) fort_msg = bot.sendMessage( chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup(button_list)) chat_url = support.message_url(message, fort_msg.message_id, "desafío") f_object = support.AlertFortressContext( chat_id, f"¡Mago de *{message.chat.title}*, en 30 minutos tendrá lugar un {chat_url} que pondrá a prueba tus habilidades como mago en [{poi.name}](https://maps.google.com/maps?q={lat},{lon})!", fort_msg.message_id, poi.id) job_queue.run_once(support.callback_AlertFortress, userAsLocal, context=f_object) support.save_jobs(job_queue) bot.delete_message(chat_id=chat_id, message_id=message_id) return else: bot.answer_callback_query( callback_query_id=query.id, text= "Sólo un administrador o el usuario que ha creado el aviso puede pulsar ese botón.", show_alert=True) return if queryData[1] == "cancel": if queryData[2] == str(user_id) or support.is_admin( chat_id, user_id, bot): bot.delete_message(chat_id=chat_id, message_id=message_id) return else: bot.answer_callback_query( callback_query_id=query.id, text= "Sólo un administrador o el usuario que ha creado el aviso puede pulsar ese botón.", show_alert=True) return poi_id = queryData[2] poi = get_poi(poi_id) lat = poi.latitude lon = poi.longitude button_list = [ [(InlineKeyboardButton("🙋♀️ Voy", callback_data=f'fort_yes_{poi.id}')), (InlineKeyboardButton("🕒 Tardo", callback_data=f'fort_late_{poi.id}')), (InlineKeyboardButton("❌ No voy", callback_data=f'fort_no_{poi.id}'))], [(InlineKeyboardButton("✅ Estoy", callback_data=f'fort_here_{poi.id}')), (InlineKeyboardButton("📍 Ubicación", callback_data=f'fort_ubi_{poi.id}')), (InlineKeyboardButton("⚠️ Aviso", callback_data=f'fort_alert_{poi.id}'))] ] string = r'\n(🙋♀️|✅|🕒|❌) 🧙(\d|\d\d|\?\?) (🍮|⚔|🐾|📚)(\d|\d\d|\?\?) @{}'.format( username) if queryData[1] == "ubi": bot.send_venue( chat_id=user_id, title=poi.name, address=" ", latitude=lat, longitude=lon, reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton( text="📍 Google Maps", url='https://maps.google.com/maps?q={0},{1}'.format( lat, lon)) ]])) return elif queryData[1] == "alert": if last_run( str(user_id) + str(chat_id) + str(message_id), 'fort_alert'): bot.answer_callback_query( callback_query_id=query.id, text= "Ya has enviado un ⚠️ Aviso, espera un rato para enviar otro.", show_alert=True) return if re.search(string, markdown_text): ent = message.parse_entities(["mention"]) chat_url = support.message_url(message, message_id, "fortaleza") for mention in ent: username = message.parse_entity(mention) string = r'\n(🙋♀️|✅|🕒|❌) 🧙(\d|\d\d|\?\?) (🍮|⚔|🐾|📚)(\d|\d\d|\?\?) {}'.format( username) search = re.search(string, markdown_text) if search.group(1) == "❌": continue user = get_user_by_name(username[1:]) btn_user = get_user(user_id) bot.sendMessage( chat_id=user.id, text= f"Alerta para la {chat_url} en [{poi.name}](https://maps.google.com/maps?q={lat},{lon}) enviada por @{btn_user.alias}", parse_mode=telegram.ParseMode.MARKDOWN, disable_web_page_preview=True) bot.answer_callback_query( callback_query_id=query.id, text= "⚠️ Aviso enviado a todos los magos apuntados en la lista.", show_alert=True) else: bot.answer_callback_query( query.id, "❌ Debes apuntarte para poder enviar una alerta.", show_alert=True) return markdown_text = re.sub(string, "", markdown_text) if user is None or user.profession is Professions.NONE.value: text_prof = "🍮" elif user.profession is Professions.AUROR.value: text_prof = "⚔" elif user.profession is Professions.MAGIZOOLOGIST.value: text_prof = "🐾" elif user.profession is Professions.PROFESSOR.value: text_prof = "📚" text_level = ("{}".format(user.level) if user and user.level else "??") text_profession_level = ("{}".format(user.profession_level) if user and user.profession_level else "??") if queryData[1] == "yes": text = markdown_text + f"\n🙋♀️ 🧙{text_level} {text_prof}{text_profession_level} @{username}" elif queryData[1] == "here": text = markdown_text + f"\n✅ 🧙{text_level} {text_prof}{text_profession_level} @{username}" elif queryData[1] == "late": text = markdown_text + f"\n🕒 🧙{text_level} {text_prof}{text_profession_level} @{username}" elif queryData[1] == "no": text = markdown_text + f"\n❌ 🧙{text_level} {text_prof}{text_profession_level} @{username}" bot.edit_message_text(text=text, chat_id=chat_id, message_id=message_id, parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(button_list), disable_web_page_preview=True)