A
A
AirronBark2021-11-06 15:23:01
Python
AirronBark, 2021-11-06 15:23:01

Is it possible to get message id from bot (telegram)?

I am writing a bot based on the aiogram library.
You need to get the id of a specific message from the bot.
The situation is this: the bot issues orders from the database (in a cycle) and each order can be assigned a status (accepted or not), but in order to assign it, I need to get the order id, and so it is stored in a specific message of the bot, you need to find exactly this message and more read it once to find the order id.
I know how to find the order id in the message, but I can’t get the bot’s message id.
Can you suggest something or is there any alternative solutions to this issue?

@dp.callback_query_handler(text='check_new_ords')
async def process_order_callback_2(query: types.CallbackQuery):
        if answer_ord_date == 'check_new_ords':
            if len(records):
                await bot.send_message(query.from_user.id, text='Хорошо! Вывожу новые заказы:')
                for r in records:
                    if r[6] == 1:
                        client_id_1= int(re.search('\d+',r[1]).group(0))
                        cl_name = db.get_client_name(client_id_1)
                        await bot.send_message(query.from_user.id,
                                                 text=f"Заказ № {r[0]}\nСтатус: НА РАССМОТРЕНИИ.\nНазвание работы: {r[2]}\nОписание: {r[3]}\nКлиент: {cl_name}\nДата создания заказа:\n{r[5]}\n\n",
                                                 reply_markup=nav.order_list_admin_1)

                await bot.send_message(query.from_user.id, text='Выберите пункт меню:',reply_markup=nav.back_admin_menu_order)

@dp.callback_query_handler(text="accept_ord")
@dp.callback_query_handler(text="refusal_ord")
async def process_order_callback_3(query: types.CallbackQuery):
    answ_date_2=query.data
    if answ_date_2 == "accept_ord":
        status = 2
## Вот здесь я застрял
        message_id=

##
        db.set_new_status(id, status)
        await bot.send_message(query.from_user.id, text=f'ЗАКАЗ №{id} ПРИНЯТ', reply_markup=nav.back_admin_menu_order)


Of course, there is an option to manually switch the status to the database, but this will be an extreme case.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-11-06
@AirronBark

Can't find message by ID. If you mean receiving a message as in your code, then
message_id=query.message.message_id

I
InternetMaster, 2021-11-06
@InternetMaster

If you have a message id, then you can easily find the sent message completely!
The bottom line is that you are using forwardMessage

await bot.forward_message(chat_id=<чат id КУДА ПЕРЕСЛАТЬ СООБЩЕНИЕ>, from_chat_id=<чат id ГДЕ БЫЛО ОТПРАВЛЕНО СООБЩЕНИЕ>, message_id<id сообщения>)

You can use any account as chat_id. In general, anyone, it doesn’t matter, the main thing is that the bot can send a message to this
PS account, of course, you will need to know the message_id of the message where the status of the order is, but the message_id can be recorded while sending the message. If successful (that is, if the bot can send a message), it will return a Message. To get the text - message.text
PSS This method can also be used for human-readable format. That is, if someone will physically stand, for example, at the point of issuing orders, then you can transfer the chat_id of the hard worker who issues orders to chat_id, and he will already receive the same message as the user.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question