def done(bot, update, args):
    if Helper.not_a_group(update) or Helper.not_authorised(update):
        return

    for arg in args:
        try:
            OrderHandler.update_done_order(bot, update.message.chat.id, int(arg))
        except Exception as error:
            logging.warning('Unable to mark item "%s" as done, error: "%s"' % (arg, error))
def start(bot, update):
    if Helper.not_a_group(update) or Helper.not_authorised(update):
        return

    # message = update.message.reply_text('%s volunteered to be the kopi boy/girl, please order via the notification sent to you privately. Use /sub to subscribe to future notifications. Order now or else...' % update.message.from_user.first_name)
    message = update.message.reply_text("Stanly's Coffee is now open! Please order via the notification sent to you privately. Use /sub to subscribe to future notifications.")
    OrderHandler.create_orders_file(message.chat.id, message.message_id, message.text)

    NotificationHandler.send_notification_to_subscribers(bot, update, message)
    def remove_from_subscribers(chat_id, user_id):
        file = open(Helper.file_path('subscribers/%s.txt' % chat_id), 'r')
        lines = file.readlines()
        file.close()

        file = open(Helper.file_path('subscribers/%s.txt' % chat_id), 'w')
        for line in lines:
            if line.rstrip() != str(user_id):
                file.write(line)
        file.close()
    def add_to_subscribers(chat_id, user_id):
        if os.path.exists(Helper.file_path('subscribers/%s.txt' % chat_id)):
            with open(Helper.file_path('subscribers/%s.txt' % chat_id),
                      'r') as data_file:
                for line in data_file:
                    if str(user_id) in line.rstrip():
                        return

        with open(Helper.file_path('subscribers/%s.txt' % chat_id),
                  'a+') as data_file:
            data_file.write(str(user_id) + '\n')
Ejemplo n.º 5
0
    def write_to_orders_file(chat_id, message_id, user, drink):
        order_count = 0
        for line in open(
                Helper.file_path('orders/%s-%s.txt' % (chat_id, message_id)),
                'r'):
            if ' - ' in line:
                order_count += 1

        order_count += 1

        with open(Helper.file_path('orders/%s-%s.txt' % (chat_id, message_id)),
                  'a+') as data_file:
            data_file.write('%d. %s - %s\n' % (order_count, user, drink))
Ejemplo n.º 6
0
    def update_orders_in_group(bot, chat_id, message_id):
        with open(Helper.file_path('orders/%s-%s.txt' % (chat_id, message_id)),
                  'r') as data_file:
            orders = data_file.read()

        bot.edit_message_text(text=orders,
                              chat_id=chat_id,
                              message_id=message_id)
def unsub(bot, update):
    if Helper.not_a_group(update):
        return

    SubscriptionHandler.remove_from_subscribers(update.message.chat.id,
                            update.message.from_user.id)

    update.message.reply_text('%s has unsubscribed from coffee runs \U0001F612 One less drink to buy when kopi run starts in group: %s' % (update.message.from_user.first_name, update.message.chat.title))
def sub(bot, update):
    if Helper.not_a_group(update):
        return

    SubscriptionHandler.add_to_subscribers(update.message.chat.id,
                       update.message.from_user.id)

    update.message.reply_text('%s has subscribed to coffee runs. You will receive a notification from yours truly when a coffee run starts in group: %s.\n\nClick here @%s and press "Send Message" > "START" so that I can send you notifications. Delete that chat and you will never hear from me again \U0001F608' % (update.message.from_user.first_name, update.message.chat.title, bot.get_me().username))
    def send_notification_to_subscribers(bot, update, message):
        with open(Helper.file_path('subscribers/%s.txt' % update.message.chat.id), 'r') as data_file:
            for line in data_file:
                # bot.send_message(chat_id=line.rstrip(),
                #                  text='%s volunteered to be the kopi boy/girl, order now or else...'
                #                  % update.message.from_user.first_name,
                #                  reply_markup=KeyboardHandler().keyboard_reply_markup(message))

                try:
                    bot.send_message(chat_id=line.rstrip(),
                                     text="Stanly's Coffee is now open, please place your order:",
                                     reply_markup=KeyboardHandler().keyboard_reply_markup(message))
                except TelegramError as error:
                    logging.warning('Unable to notify subscriber "%s", error: "%s"' % (line.rstrip(), error))
    def generate_drink_combis(self):
        # Read drinks.json so that keyboard can be populated later
        with open(Helper.file_path('drinks.json')) as data_file:
            drinks_data = json.load(data_file)

        drinks = drinks_data['0']
        combinations = []

        for i in range(1, 5):
            iterables = [drinks]

            for j in range(1, i + 1):
                iterables.append(drinks_data[str(j)])

            for combination in itertools.product(*iterables):
                combinations.append('-'.join(combination))

        return combinations
Ejemplo n.º 11
0
    def update_done_order(bot, chat_id, item_num):
        list_of_orders_in_chat = glob.glob(
            Helper.file_path('orders/%s-*.txt' % (chat_id)))
        latest_order_file = max(list_of_orders_in_chat, key=os.path.getctime)

        row_to_find = str(item_num) + '.'

        final_order_message = ''
        with open(latest_order_file, 'r') as data_file:
            for line in data_file:
                if line.startswith(row_to_find):
                    line = line.rstrip() + ' [done]\n'

                final_order_message = final_order_message + line

        with open(latest_order_file, 'w') as data_file:
            data_file.write(final_order_message)

        message_id = re.search('(\d+)\.txt', latest_order_file)[1]

        bot.edit_message_text(text=final_order_message,
                              chat_id=chat_id,
                              message_id=message_id)
def read_config_file():
    with open(Helper.file_path('firebase_config.json')) as config_file:
        config = json.load(config_file)
    return config
Ejemplo n.º 13
0
 def create_orders_file(chat_id, message_id, opening_message):
     with open(Helper.file_path('orders/%s-%s.txt' % (chat_id, message_id)),
               'a+') as data_file:
         data_file.write('%s\n\n' % opening_message)