Пример #1
0
def list_owed_button(bot, update, user_data):
    query = update.callback_query
    chat_id = str(query.message.chat_id)

    message_here = strings.errButtonMsg
    reply_markup = None

    if query.data.startswith("owers"):
        ower = query.data[5:]
        user_data["oweower"] = ower
        owed = helper.loadjson(loc_owedjson)
        keyboard = helper.make_keyboard(owed[chat_id][ower].keys(), "owees")

        message_here = msgListMoneyOwedIndiv.format(ower)
        reply_markup = InlineKeyboardMarkup(keyboard)

    elif query.data.startswith("owees"):
        owed = helper.loadjson(loc_owedjson)
        ower = user_data["oweower"]
        owee = query.data[5:]

        message_here = ower + " owes " + owee + " " + Bot.CURRENCY \
                       + str(owed[chat_id][ower][owee])
        user_data.pop("oweower")  # cleanup
    else:
        message_here = strings.errUnknownCallback

    bot.editMessageText(text=message_here,
                        chat_id=query.message.chat_id,
                        message_id=query.message.message_id,
                        reply_markup=reply_markup)
Пример #2
0
def list_owed(bot, update, args):
    owed = helper.loadjson(loc_owedjson)
    chat_id = str(update.message.chat_id)
    reply_markup = None

    try:
        owed[chat_id]
    except KeyError:
        owed[chat_id] = {}

    if len(args) == 0:
        keyboard = helper.make_keyboard(owed[chat_id].keys(), "owers")
        reply_markup = InlineKeyboardMarkup(keyboard)
        res = msgListMoneyOwed

    elif len(args) == 1:
        if args[0] == "all":  # if arg1 is "all", print all debts
            if len(owed[chat_id]) == 0:
                res = msgNoDebts
            else:
                res = msgListMoneyOwed
                for ower in owed[chat_id]:
                    res += helper.print_owed(owed, chat_id, ower)
        else:  # else, print debts of name
            try:
                res = msgListMoneyOwedIndiv.format(args[0])
                res += helper.print_owed(owed, chat_id, args[0])
            except KeyError:
                res = args[0] + " has no debts!"

    else:
        res = strings.errBadFormat

    update.message.reply_text(res, reply_markup=reply_markup)
Пример #3
0
def owes_helper(chat_id, ower, owee, amount):
    owed = helper.loadjson(loc_owedjson)

    try:
        owed[chat_id]
    except KeyError:
        owed[chat_id] = {}

    try:
        owed[chat_id][ower]
    except KeyError:
        owed[chat_id][ower] = {}
        print("Added new ower: " + ower + ".")

    try:
        owed[chat_id][ower][owee]
    except KeyError:
        owed[chat_id][ower][owee] = 0
        print("Added new owee for ower " + ower + ".")

    owed[chat_id][ower][owee] += float(amount)
    result = owed[chat_id][ower][owee]

    # check whether owed sum is now 0 and removes if necessary
    if owed[chat_id][ower][owee] == 0:
        owed[chat_id][ower].pop(owee)
        if owed[chat_id][ower] == {}:
            owed[chat_id].pop(ower)
            if owed[chat_id] == {}:
                owed.pop(chat_id)

    helper.dumpjson(loc_owedjson, owed)
    return result
Пример #4
0
def inline_owes(bot, update, args, user_data):
    owed = helper.loadjson(loc_owedjson)
    chat_id = str(update.message.chat_id)
    res = ""

    if len(args) == 0:
        try:
            keyboard = helper.make_keyboard(owed[chat_id].keys(), "")
            res = msgCurrentOwers
        except KeyError:
            res = msgNoDebts
            keyboard = []

        reply_markup = InlineKeyboardMarkup(keyboard)
        update.message.reply_text(res, reply_markup=reply_markup)
        return OWER

    elif len(args) == 1:
        if args[0] == "all":
            update.message.reply_text(strings.errAllName)
            return ConversationHandler.END

        try:
            res = msgWhoOwedTo.format(args[0])
            keyboard = helper.make_keyboard(owed[chat_id][args[0]].keys(), "")
        except KeyError:
            keyboard = []

        user_data["ower"] = args[0]
        reply_markup = InlineKeyboardMarkup(keyboard)
        update.message.reply_text(res, reply_markup=reply_markup)
        return OWEE

    elif len(args) == 2:
        if args[0] == "all" or args[1] == "all":
            update.message.reply_text(strings.errAllName)
            return ConversationHandler.END
        user_data["ower"] = args[0]
        user_data["owee"] = args[1]
        update.message.reply_text(msgHowMuch.format(args[0], args[1]))
        return AMOUNT

    elif len(args) == 3:
        if args[0] == "all" or args[1] == "all":
            update.message.reply_text(strings.errAllName)
        else:
            try:
                amount = owes_helper(chat_id, args[0], args[1], args[2])
                update.message.reply_text(args[0] + " now owes " + args[1] + " " + Bot.CURRENCY
                                          + str(amount) + ".")
            except ValueError:
                update.message.reply_text(strings.errNotInt)

    else:
        update.message.reply_text(strings.errBadFormat)

    return ConversationHandler.END
Пример #5
0
def rm_command(bot, update, args):
    cmds = helper.loadjson(loc_cmdsjson)
    chat_id = str(update.message.chat_id)

    if len(args) == 1:
        # remove command from command repo
        cmd_name = "/" + args[0]
        try:
            del cmds[chat_id][cmd_name]
            helper.dumpjson(loc_cmdsjson, cmds)
            print("removed command \"" + cmd_name + "\".")
        except KeyError:
            return
Пример #6
0
def all_notes(bot, update, args):
    notes = helper.loadjson(loc_notesjson)
    chat_id = str(update.message.chat_id)

    try:
        notes[chat_id]
    except KeyError:
        notes[chat_id] = {}

    msg = "No notes in this chat."
    if len(notes[chat_id]) > 0:
        msg = msgNotesForChat
        for note in notes[chat_id]:
            msg += "\n" + note

    update.message.reply_text(msg)
Пример #7
0
def clear(bot, update, args):
    owed = helper.loadjson(loc_owedjson)
    chat_id = str(update.message.chat_id)
    sender = update.message.from_user

    try:
        owed[chat_id]
    except KeyError:
        owed[chat_id] = {}

    if sender.id != Bot.OWNER_ID:
        update.message.reply_text(strings.errNotAdmin)
        print(strings.errUnauthCommand.format(sender.username))
        return

    if len(args) == 1 and args[0] == "all":
        helper.dumpjson(loc_bckpjson, owed)
        owed.pop(chat_id)
        update.message.reply_text(msgAllDebtsCleared)
        print(msgAllDebtsClearedTerm)

    elif len(args) == 2:
        try:
            owed[chat_id][args[0]]
        except KeyError:
            update.message.reply_text(strings.errNoOwer + args[0])
            return

        if args[1] == "all":
            owed[chat_id].pop(args[0])
            print(msgDebtsOfCleared.format(args[0]))

        else:
            owed[chat_id][args[0]].pop(args[1])
            update.message.reply_text(
                msgDebtsOfToCleared.format(args[0], args[1]))

            # remove from database if no debts
            if owed[chat_id] == {}:
                owed.pop(chat_id)
            elif owed[chat_id][args[0]] == {}:
                owed[chat_id].pop(args[0])

    else:
        update.message.reply_text(strings.errBadFormat)

    helper.dumpjson(loc_owedjson, owed)
Пример #8
0
def get_note(bot, update, args):
    notes = helper.loadjson(loc_notesjson)
    chat_id = str(update.message.chat_id)

    try:
        notes[chat_id]
    except KeyError:
        notes[chat_id] = {}

    if len(args) == 1:
        msg = ""
        try:
            msg = notes[chat_id][args[0]]
        except KeyError:
            msg = errNoNoteFound + args[0]

        update.message.reply_text(msg)
    else:
        update.message.reply_text(strings.errBadFormat)
Пример #9
0
def handle_user_command(bot, update):
    cmds = helper.loadjson(loc_cmdsjson)
    chat_id = str(update.message.chat_id)
    command_text = update.message.text
    command_list = command_text.split()

    if len(command_list) >= 1:
        try:
            cmd_name = command_list[0]
            del command_list[0]
            no_first_elem = " ".join(command_list)
            msg = cmds[chat_id][cmd_name] + " " + no_first_elem
            update.message.reply_text(msg)
        except KeyError:
            # default to unknown commands handler
            Bot.unknown(bot, update)

    else:
        update.message.reply_text(errBadFormat)
Пример #10
0
def ower_button(bot, update, user_data):
    owed = helper.loadjson(loc_owedjson)
    query = update.callback_query
    chat_id = str(query.message.chat_id)
    ower = query.data  # this is the name pressed
    user_data["ower"] = ower

    try:
        keyboard = helper.make_keyboard(owed[chat_id][ower].keys(), "")
    except KeyError:
        keyboard = []

    reply = InlineKeyboardMarkup(keyboard)
    bot.editMessageText(text=msgWhoOwedTo.format(ower),
                        chat_id=query.message.chat_id,
                        message_id=query.message.message_id,
                        reply_markup=reply)

    return OWEE
Пример #11
0
def save_note(bot, update, args):
    notes = helper.loadjson(loc_notesjson)
    chat_id = str(update.message.chat_id)

    try:
        notes[chat_id]
    except KeyError:
        notes[chat_id] = {}

    if len(args) >= 2:
        # add note to note repo
        notename = args[0]
        del args[0]
        note_data = " ".join(args)
        notes[chat_id][notename] = note_data
        print("Added new note \"" + notename + "\" with content \"" +
              note_data + "\".")
    else:
        update.message.reply_text(strings.errBadFormat)

    helper.dumpjson(loc_notesjson, notes)
Пример #12
0
def add_command(bot, update, args):
    cmds = helper.loadjson(loc_cmdsjson)
    chat_id = str(update.message.chat_id)

    try:
        cmds[chat_id]
    except KeyError:
        cmds[chat_id] = {}

    if len(args) == 1:
        update.message.reply_text(no_cmd_text_given)
    elif len(args) >= 2:
        # add command to command repo
        cmd_name = "/" + args[0]
        del args[0]
        cmd_data = " ".join(args)
        cmds[chat_id][cmd_name] = cmd_data
        print("Added new cmd \"" + cmd_name + "\" with content \"" + cmd_data +
              "\".")
    else:
        update.message.reply_text(errBadFormat)

    helper.dumpjson(loc_cmdsjson, cmds)