def clearalertscmd(bot, update): logging.debug("detectivepikachubot:clearalertscmd: %s %s" % (bot, update)) (chat_id, chat_type, user_id, text, message) = extract_update_info(update) user_username = message.from_user.username if isBanned(chat_id): return if user_id is not None and isBanned(user_id): return if not edit_check_private(chat_id, chat_type, user_username, "clearalerts", bot): delete_message(chat_id, message.message_id, bot) return user = getUser(user_id) _ = set_language(user["language"]) if clearAlerts(user_id): bot.sendMessage( chat_id=chat_id, text= _("👌 Se han eliminado las alertas de todos los gimnasios.\n\nA partir de ahora, ya no recibirás mensajes privados cada vez que alguien cree una incursión." ), parse_mode=telegram.ParseMode.MARKDOWN) else: bot.sendMessage(chat_id=chat_id, text=_("❌ No se ha eliminado ninguna alerta."), parse_mode=telegram.ParseMode.MARKDOWN)
def pikavalidated(user_id): logging.info("admintools:Pikavalidated: %s" % user_id) user = getUser(user_id) if user is None or user["validation"] == "none": return False else: return True
def delalertcmd(bot, update, args=None): logging.debug("detectivepikachubot:delalertcmd: %s %s %s" % (bot, update, args)) (chat_id, chat_type, user_id, text, message) = extract_update_info(update) user_username = message.from_user.username if isBanned(chat_id): return if user_id is not None and isBanned(user_id): return if not edit_check_private(chat_id, chat_type, user_username, "delalert", bot): delete_message(chat_id, message.message_id, bot) return user = getUser(user_id) _ = set_language(user["language"]) if len(args) < 1 or not str(args[0]).isnumeric(): bot.sendMessage( chat_id=chat_id, text= _("❌ ¡Tienes que pasarme un identificador numérico como parámetro!" ), parse_mode=telegram.ParseMode.MARKDOWN) return place = getPlace(args[0]) if place is None: bot.sendMessage( chat_id=chat_id, text= _("❌ ¡No he reconocido ese gimnasio! ¿Seguro que has puesto bien el identificador?" ), parse_mode=telegram.ParseMode.MARKDOWN) return if delAlert(user_id, place["id"]): bot.sendMessage( chat_id=chat_id, text= _("👌 Se ha eliminado la alerta del gimnasio *{0}*.\n\nA partir de ahora, ya no recibirás mensajes privados cada vez que alguien cree una incursión allí." ).format(ensure_escaped(place["desc"])), parse_mode=telegram.ParseMode.MARKDOWN) else: bot.sendMessage( chat_id=chat_id, text=_("❌ No se ha podido eliminar la alerta para ese gimnasio."), parse_mode=telegram.ParseMode.MARKDOWN)
def GET(self, chat_id): # Check auth if not checkauth(): return "error_noauthdata" # Handle cache clientcachedir = cachedir + "/" + session.client_id if not os.path.isdir(clientcachedir): os.makedirs(clientcachedir) teams_shelve = shelve.open(clientcachedir + "/teams_" + chat_id + ".shelve") teams_shelve.sync() shelve_ctime = time.gmtime( os.path.getctime(clientcachedir + "/teams_" + chat_id + ".shelve")) web.header('X-last-fetched-data', time.strftime("%d %b %Y %H:%M:%S %z", shelve_ctime)) # Only get data if not cached already if not "data" in teams_shelve: # Get users for this group - assumes permission granted from getparticipants users_shelve = shelve.open(clientcachedir + "/chat_" + chat_id + ".shelve") if not "data" in users_shelve: return "error_notusers" participants = users_shelve["data"] # Gets list of not validated users teams = {"Rojo": 0, "Azul": 0, "Amarillo": 0, "Desconocido": 0} for participant in participants: user = getUser(participant["id"]) if user is not None and user["validation"] != "none": if user["team"] == "Rojo": teams["Rojo"] += 1 elif user["team"] == "Azul": teams["Azul"] += 1 elif user["team"] == "Amarillo": teams["Amarillo"] += 1 else: teams["Desconocido"] += 1 else: teams["Desconocido"] += 1 time.sleep(0.05) teams_shelve["data"] = teams teams = teams_shelve["data"] teams_shelve.close() return json.dumps(teams)
def alertscmd(bot, update, args=None): logging.debug("detectivepikachubot:alertscmd: %s %s %s" % (bot, update, args)) (chat_id, chat_type, user_id, text, message) = extract_update_info(update) user_username = message.from_user.username if isBanned(chat_id): return if user_id is not None and isBanned(user_id): return if not edit_check_private(chat_id, chat_type, user_username, "alerts", bot): delete_message(chat_id, message.message_id, bot) return alerts = getAlerts(user_id) user = getUser(user_id) _ = set_language(user["language"]) if not len(alerts): text_message = _("🔔 No tienes ninguna alerta de incursión definida.") else: text_message = _( "🔔 Tienes definidas {0} alertas para los siguientes gimnasios:" ).format(len(alerts)) + "\n" for alert in alerts: place = getPlace(alert["place_id"]) group = getGroup(place["group_id"]) text_message = text_message + "\n" + _( "✅ `{0}` {1} - Grupo {2}").format( place["id"], ensure_escaped(place["desc"]), ensure_escaped(group["title"])) text_message = text_message + "\n\n" + _( "Para borrar una alerta, envíame `/delalert` seguido del identificador numérico, o `/clearalerts` para borrarlas todas." ) text_message = text_message + "\n\n" + _( "Para añadir alertas de incursión nuevas, *envíame una ubicación* con gimnasios cercanos (_usando la función de Telegram de enviar ubicaciones_) y te explico." ) bot.send_message(chat_id=user_id, text=text_message, parse_mode=telegram.ParseMode.MARKDOWN)
def addalertcmd(bot, update, args=None): logging.debug("detectivepikachubot:addalertcmd: %s %s %s" % (bot, update, args)) (chat_id, chat_type, user_id, text, message) = extract_update_info(update) user_username = message.from_user.username if isBanned(chat_id): return if user_id is not None and isBanned(user_id): return if edit_check_private(chat_id, chat_type, user_username, "addalert", bot) == False: delete_message(chat_id, message.message_id, bot) return user = getUser(user_id) _ = set_language(user["language"]) if len(args) < 1 or not str(args[0]).isnumeric(): bot.sendMessage( chat_id=chat_id, text= _("❌ ¡Tienes que pasarme un identificador numérico como parámetro!" ), parse_mode=telegram.ParseMode.MARKDOWN) return alerts = getAlerts(user_id) if len(alerts) >= 25: bot.sendMessage( chat_id=chat_id, text=_("❌ ¡Solo se pueden configurar un máximo de {0} alertas!" ).format(25), parse_mode=telegram.ParseMode.MARKDOWN) return place = getPlace(args[0]) if place is None: bot.sendMessage( chat_id=chat_id, text= _("❌ ¡No he reconocido ese gimnasio! ¿Seguro que has puesto bien el identificador?" ), parse_mode=telegram.ParseMode.MARKDOWN) return for alert in alerts: if alert["place_id"] == place["id"]: bot.sendMessage( chat_id=chat_id, text=_("❌ ¡Ya has configurado una alerta para ese gimnasio!"), parse_mode=telegram.ParseMode.MARKDOWN) return if addAlert(user_id, place["id"]): bot.sendMessage( chat_id=chat_id, text= _("👌 Se ha añadido una alerta para el gimnasio *{0}*.\n\nA partir de ahora, recibirás un mensaje privado cada vez que alguien cree una incursión en ese gimnasio." ).format(ensure_escaped(place["desc"])), parse_mode=telegram.ParseMode.MARKDOWN) else: bot.sendMessage( chat_id=chat_id, text=_("❌ No se ha podido añadir una alerta para ese gimnasio."), parse_mode=telegram.ParseMode.MARKDOWN)
def processLocation(bot, update): logging.debug("detectivepikachubot:processLocation: %s %s" % (bot, update)) (chat_id, chat_type, user_id, text, message) = extract_update_info(update) location = message.location if isBanned(chat_id): return if user_id is not None and isBanned(user_id): return if chat_type == "private": user = getUser(user_id) _ = set_language(user["language"]) places = getPlacesByLocation(location.latitude, location.longitude, 200) logging.debug(places) filtered_places = [] for place in places: group = getGroup(place["grupo_id"]) if group["testgroup"] == 1 or group["alerts"] == 0: continue ingroup = False groups = getGroupsByUser(user_id) for g in groups: if group["id"] == g["id"]: ingroup = True if ingroup == False: continue filtered_places.append(place) if len(filtered_places) == 0: bot.sendMessage( chat_id=chat_id, text= _("❌ No se han encontrado gimnasios cerca de esta zona en grupos en los que hayas participado en una incursión recientemente. Ten en cuenta que el radio de búsqueda es de aproximadamente 180 metros." ), parse_mode=telegram.ParseMode.MARKDOWN) else: text_message = _( "🗺 Se han encontrado los siguientes gimnasios:") + "\n" example_id = None alerts = getAlerts(user_id) alert_ids = [] for alert in alerts: alert_ids.append(alert["place_id"]) for place in filtered_places: group = getGroup(place["grupo_id"]) if example_id is None: example_id = place["id"] if place["id"] in alert_ids: icon = "✅" else: icon = "▪️" text_message = text_message + "\n%s `%s` %s - Grupo %s" % ( icon, place["id"], ensure_escaped( place["name"]), ensure_escaped(group["title"])) text_message = text_message + "\n\n" + _( "Para añadir una alerta para alguno de estos gimnasios, envíame el comando `/addalert` seguido del identificador numérico.\n\nPor ejemplo:\n`/addalert {0}`" ).format(example_id) bot.sendMessage(chat_id=chat_id, text=text_message, parse_mode=telegram.ParseMode.MARKDOWN)