예제 #1
0
def assign_confirm(update, context):
    text = update.message.text
    print("assign confirm", text)
    if text == "Да":
        tmp_order = Order.select().where(
            Order.order_id == assign_json['order']).get()
        tmp_order.courier = assign_json['courier']
        tmp_order.status = 1
        tmp_order.save()
        print("Link done")
        context.bot.send_message(chat_id=assign_json['courier'],
                                 text="Вам пришел заказ:\n" +
                                 print_order(assign_json['order']),
                                 reply_markup=reg_couriers_markup)
        user_id = Order.get(Order.order_id == assign_json['order']).client_id
        context.bot.send_message(chat_id=user_id,
                                 text="Ваш заказ взят курьером {} \n".format(
                                     assign_json['courier']),
                                 reply_markup=standart_markup)
        context.bot.send_message(chat_id=user_id,
                                 text="Статус вашего заказа: В обработке \n",
                                 reply_markup=standart_markup)
        context.bot.send_message(
            chat_id=update.message.chat_id,
            text=
            "К заказу успешно прикреплен курьер. \nСообщение ему уже отправлено! \n",
            reply_markup=admin_functions_markup)

    else:
        assign_json.clear()
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text="Отменяем операцию \n",
                                 reply_markup=admin_functions_markup)
    return ConversationHandler.END
예제 #2
0
def admin_get_order_list(update, context):
    if update.message.chat_id not in managers:
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text=bot_messages.not_admin,
                                 reply_markup=standart_markup)
        return ConversationHandler.END
    result = ""
    for order in Order.select():
        # print(order.locationX, order.locationY)

        result = result + "Order ID: " + str(order.order_id) + "\n"
        result = result + "Status: "
        if order.status == 0:
            result = result + "Не обработан\n"
        if order.status == 1:
            result = result + "В обработке\n"
        if order.status == 2:
            result = result + "Доставлен\n"

        result = result + "Text: " + str(order.text) + "\n"
        result = result + "Priority: " + str(order.priority) + "\n"
        if order.courier != -1:
            result = result + "Courier: " + str(order.courier) + "\n"
        result = result + "\n"
    if len(result) == 0:
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text="Заказы еще не появились..",
                                 reply_markup=admin_functions_markup)
    else:
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text=result,
                                 reply_markup=admin_functions_markup)
    return ConversationHandler.END
예제 #3
0
def admin_print_orders_map(update, context):
    if update.message.chat_id not in managers:
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text=bot_messages.not_admin,
                                 reply_markup=standart_markup)
        return ConversationHandler.END
    print("create map")
    map = src.map.Map()
    for order in Order.select():
        print(order.locationX, order.locationY)
        map.add_placemark([order.locationX, order.locationY],
                          hint='Заказ: {}'.format(order.order_id),
                          icon_color="#ff0000")
    map.save_html('orders.html')
    print("save map")
    filename = 'orders.jpg'
    output_stream = open(filename, 'wb')
    client = pdfcrowd.HtmlToImageClient('ansa1_',
                                        'ca1a906dd7c3e773b52fefcf28aded2d')
    client.setScreenshotWidth(640)
    client.setScreenshotHeight(480)
    client.setOutputFormat('jpg')
    client.convertFileToStream('orders.html', output_stream)
    output_stream.close()
    print("send map image")
    context.bot.send_photo(chat_id=update.message.chat_id,
                           photo=open(filename, 'rb'),
                           reply_markup=admin_functions_markup)
    os.remove('orders.html')
    os.remove('orders.jpg')
    return ConversationHandler.END
예제 #4
0
def current_orders(update, context):
    try:
        print(update.message.chat_id)
        # orders = Order.select().where(Order.courier == update.message.chat_id).get()
        map = src.map.Map()
        for order in Order.select().where(
                Order.courier == update.message.chat_id):
            print(order.locationX, order.locationY)
            map.add_placemark([order.locationX, order.locationY],
                              hint='Заказ: {}'.format(order.order_id),
                              icon_color="#ff0000")
        map.save_html('current_orders.html')
        filename = 'current_orders.jpg'
        output_stream = open(filename, 'wb')
        client = pdfcrowd.HtmlToImageClient(
            'ansa1_', 'ca1a906dd7c3e773b52fefcf28aded2d')
        client.setScreenshotWidth(640)
        client.setScreenshotHeight(480)
        client.setOutputFormat('jpg')
        client.convertFileToStream('current_orders.html', output_stream)
        output_stream.close()

        context.bot.send_photo(chat_id=update.message.chat_id,
                               photo=open(filename, 'rb'),
                               reply_markup=back_only_markup)
        os.remove('current_orders.html')
        os.remove('current_orders.jpg')
        return ConversationHandler.END
    except Order.DoesNotExist:
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text=bot_messages.no_orders,
                                 reply_markup=back_only_markup)
        return ConversationHandler.END
예제 #5
0
def print_order(order_id):
    order_text = ""
    try:
        tmp_order = Order.select().where(Order.order_id == order_id).get()
        order_text = "Order ID: {}, \nText: {}, \nPriority: {} \n".format(
            tmp_order.order_id, tmp_order.text, tmp_order.priority)
        if tmp_order.courier != -1:
            order_text = order_text + "Courier: " + str(tmp_order.courier)
        return order_text
    except Order.DoesNotExist:
        return order_text
예제 #6
0
def read_user_priority(update, context):
    order_json['priority'] = update.message.text
    text = "Мы приняли ваш заказ!\n"
    glob = Global.select().where(Global.root == 'Global').get()
    tmp = glob.id
    glob.delete_instance()
    print(tmp)
    print(order_json)
    Order.create(text=order_json['text'],
                 locationX=order_json['latitude'],
                 locationY=order_json['longitude'],
                 order_id=tmp,
                 priority=order_json['priority'],
                 status=0,
                 client_id=update.message.chat_id,
                 courier=-1)
    Global.create(id=tmp + 1, root='Global')
    order_json.clear()
    context.bot.send_message(chat_id=update.message.chat_id,
                             text=text,
                             reply_markup=client_markup)
    return ConversationHandler.END
예제 #7
0
def assign_order(update, context):
    order = update.message.text
    try:
        tmp_order = Order.select().where(Order.order_id == order).get()
        context.bot.send_message(
            chat_id=update.message.chat_id,
            text="Вы хотите назначить курьера на заказ: \n" +
            print_order(tmp_order.order_id) + "Введите номер курьера:\n",
            reply_markup=back_only_markup)
        assign_json['order'] = order
        return bot_states.ASSIGN_COURIER
    except Order.DoesNotExist:
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text="Такого заказа не существует \n",
                                 reply_markup=admin_functions_markup)
        return ConversationHandler.END
예제 #8
0
def read_finish_order(update, context):
    try:
        order = Order.select().where(
            Order.order_id == update.message.text).get()
        context.bot.send_message(chat_id=order.client_id,
                                 text="Ваш заказ успкешно выполнен!\n",
                                 reply_markup=standart_markup)
        context.bot.send_message(
            chat_id=update.message.chat_id,
            text="Спасибо за успешно выполненный заказ!\n",
            reply_markup=reg_couriers_markup)
        order.delete_instance()
        return ConversationHandler.END
    except Order.DoesNotExist:
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text="Номер заказа не верен!\n",
                                 reply_markup=reg_couriers_markup)
        return ConversationHandler.END
예제 #9
0
def finish_order(update, context):
    try:
        result = ""
        courier = Courier.select().where(
            Courier.courier_id == update.message.chat_id).get()
        for order in Order.select().where(
                Order.courier == update.message.chat_id):
            result = result + "Номер заказа: " + str(order.order_id) + '\n'
            result = result + "Описание заказа: " + str(order.text) + '\n'
            result = result + str(order.locationX) + '\n'
            result = result + str(order.locationY) + '\n\n'
        result = result + "Какой заказ вы хотите завершить?\n"
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text=result,
                                 reply_markup=reg_couriers_markup)
        return bot_states.READ_FINISH_ORDER
    except Courier.DoesNotExist:
        context.bot.send_message(chat_id=update.message.chat_id,
                                 text="Вы не являетесь курьером!\n",
                                 reply_markup=courier_markup)
        return ConversationHandler.END
예제 #10
0
def main():
    Courier.create_table()
    Order.create_table()
    Global.create_table()
    # Global.create(id=0, root='Global')
    dp.add_handler(CommandHandler('start', start))
    dp.add_handler(CommandHandler('menu', menu))

    make_order_handler = ConversationHandler(
        entry_points=[CommandHandler('makeorder', make_order)],
        states={
            bot_states.READ_NEW_ORDER:
            [MessageHandler(Filters.text, read_new_order)],
            bot_states.READ_USER_LOCATION:
            [MessageHandler(Filters.location, read_user_location)],
            bot_states.READ_USED_PRIORITY:
            [MessageHandler(Filters.text, read_user_priority)]
        },
        fallbacks=[CommandHandler('cancel_order', cancel_order)])
    dp.add_handler(make_order_handler)

    make_courier_handler = ConversationHandler(
        entry_points=[CommandHandler('getjob', get_job)],
        states={
            bot_states.READ_COURIER_LOCATION:
            [MessageHandler(Filters.location, read_courier_location)]
        },
        fallbacks=[CommandHandler('cancel_registration', cancel_registration)])

    link_order_handler = ConversationHandler(
        entry_points=[CommandHandler('assign', assign)],
        states={
            bot_states.ASSIGN_ORDER:
            [MessageHandler(Filters.text, assign_order)],
            bot_states.ASSIGN_COURIER:
            [MessageHandler(Filters.text, assign_courier)],
            bot_states.ASSIGN_CONFIRM:
            [MessageHandler(Filters.text, assign_confirm)]
        },
        fallbacks=[CommandHandler('back', back)])
    dp.add_handler(link_order_handler)

    dp.add_handler(make_courier_handler)
    dp.add_handler(CommandHandler('current_orders', current_orders))
    dp.add_handler(CommandHandler('cancel_registration', cancel_registration))
    dp.add_handler(CommandHandler('admin_get_order_list',
                                  admin_get_order_list))
    dp.add_handler(
        CommandHandler('admin_get_courier_list', admin_get_courier_list))
    dp.add_handler(
        CommandHandler('admin_print_orders_map', admin_print_orders_map))
    dp.add_handler(
        CommandHandler('admin_print_couriers_map', admin_print_couriers_map))

    dp.add_handler(CommandHandler('client', client))
    dp.add_handler(CommandHandler('courier', courier))
    dp.add_handler(CommandHandler('back', back))
    dp.add_handler(CommandHandler('admin', admin))
    dp.add_handler(CommandHandler('admin_panel', admin_panel))

    finish_order_handler = ConversationHandler(
        entry_points=[CommandHandler('finish_order', finish_order)],
        states={
            bot_states.READ_FINISH_ORDER:
            [MessageHandler(Filters.text, read_finish_order)]
        },
        fallbacks=[CommandHandler('back', back)])

    dp.add_handler(finish_order_handler)

    dp.add_handler(CommandHandler('ping', ping))  # TODO

    updater.start_polling()
    updater.idle()