예제 #1
0
def check_holiday():
    holidays = JsonDB("holidays.json").dictionary
    current_local_time = time.localtime()
    result = "Today there is no holiday("
    for date in holidays.keys():
        if date.split(".") == [
                str(current_local_time[1]),
                str(current_local_time[2])
        ]:
            result = holidays[date]
            break
    return result
예제 #2
0
def message_handler(incoming_message):
    global shadow_db

    # default / and ? commands from commands.json
    commands = JsonDB("commands.json")["commands"]
    # quotes for bot's random quote feature
    quotes = JsonDB("data/quotes.json")["quotes"]

    result = {
        "method": "send_message",
        "chat_id": incoming_message["chat_id"],
        "text": "?"
    }
    for command_name in commands.keys():
        if incoming_message["text"] == command_name:
            result["text"] = commands[command_name]
    # random quote feature
    if incoming_message["text"] == "/quote":
        result["text"] = random.choice(quotes)
    # lootboxes feature
    if incoming_message["text"] == "/lootbox":
        result["text"] = usual_lootbox()
    if incoming_message["text"] == "/weapon_lootbox":
        result["text"] = weapon_lootbox()
    # throwing dice feature
    if incoming_message["text"].startswith("/dice"):
        result["text"] = throw_dice(incoming_message["text"])
    # random choice feature
    if incoming_message["text"].startswith("/random"):
        try:
            random_result = random.choice(
                incoming_message["text"].split(" ")[1:])
        except:
            random_result = "Type command correctly"
        finally:
            result["text"] = random_result
    # days until newyear or summer feature
    if incoming_message["text"] == "/newyear":
        result["text"] = days_until_newyear()
    if incoming_message["text"] == "/summer":
        result["text"] = days_until_summer()
    # locations feature
    if incoming_message["text"].startswith("/where"):
        try:
            location = incoming_message["text"].split(" ")[1]
            result = {
                "method": "send_location",
                "coordinates": get_coordinates(location),
                "chat_id": incoming_message["chat_id"]
            }
        except:
            result = {
                "method": "send_message",
                "chat_id": incoming_message["chat_id"],
                "text": "Type command correctly"
            }
    if incoming_message["text"].startswith(
            "/location") and incoming_message["text"] != "/locations":
        try:
            location = incoming_message["text"].split(" ")[1:]
            result = {
                "method": "send_location",
                "coordinates": {
                    "latitude": float(location[0]),
                    "longitude": float(location[1])
                },
                "chat_id": incoming_message["chat_id"]
            }
        except:
            result = {
                "method": "send_message",
                "chat_id": incoming_message["chat_id"],
                "text": "Type command correctly"
            }
    # chat id getter
    if incoming_message["text"] == "/chat_id":
        result["text"] = incoming_message["chat_id"]
    # unix time feature
    if incoming_message["text"] == "/unix_time":
        result["text"] = "{} seconds since 00:00:00 1 January 1970".format(
            str(round(time.time())))
    # holiday feature
    if incoming_message["text"] == "/holiday":
        result["text"] = check_holiday()
    # rgb feature
    if incoming_message["text"].startswith("/rgb"):
        try:
            rgb = [
                int(color) for color in incoming_message["text"].split(" ")[1:]
            ]
            rgb = tuple(rgb)
            if len(rgb) != 3:
                raise ValueError
        except:
            rgb = (255, 255, 255)
        finally:
            result = {
                "method":
                "send_photo",
                "photo":
                open(show_color_rgb(rgb), "rb"),
                "caption":
                "Red - {}, Green - {}, Blue - {}".format(
                    rgb[0], rgb[1], rgb[2]),
                "chat_id":
                incoming_message["chat_id"]
            }
    # drop log file feature
    if incoming_message["text"] == "/droplog":
        result = {
            "method": "send_document",
            "caption": "Log",
            "chat_id": incoming_message["chat_id"]
        }
    if "text" in result.keys():
        if result["text"] == "?":
            result = None
    return result