AsyncTelegram2 icon indicating copy to clipboard operation
AsyncTelegram2 copied to clipboard

Get Message ID

Open jordigrau83 opened this issue 1 year ago • 1 comments

I want to edit a message that I'm sending to a user via:

Sent = myBot.sendTo(userid[0], msgtxt);

BUT to edit the message I need the messageID of that message, how do I get it?? the SendTo function only returns a boolean and I also tried with this method:

TBMessage msg; msg.chatId =userid[0]; myBot.sendMessage(msg, "test"); Serial.println(" messageID: " + String(msg.messageID));

but msg.messageID is not the real number :( I only get the real messageID from received messages, but I don't want to edit a received message, I want to edit the message that I send. any help?

jordigrau83 avatar May 06 '24 22:05 jordigrau83

To receive MessageID, I added a separate method of sending a message on my knee.

AsyncTelegram2.h

int32_t sendNewMessage(const TBMessage &msg, const char *message, bool wait = true);

AsyncTelegram2.cpp

int32_t AsyncTelegram2::sendNewMessage(const TBMessage &msg, const char *message, bool wait)
{

    if (!strlen(message))
        return 0;
    m_waitSent = true;
    m_lastSentTime = millis();

    // DynamicJsonDocument root(m_JsonBufferSize);
    JSON_DOC(m_JsonBufferSize);
    root["chat_id"] = msg.chatId;
    root["text"] = message;

    switch (m_formatType)
    {
    case FormatStyle::NONE:
        break;
    case FormatStyle::HTML:
        root["parse_mode"] = "HTML";
        break;
    case FormatStyle::MARKDOWN:
        root["parse_mode"] = "MarkdownV2";
        break;
    }

    if (msg.disable_notification)
        root["disable_notification"] = true;

    root.shrinkToFit();
    String payload;
    serializeJson(root, payload);

    // sendCommand
    if (checkConnection())
    {
        String httpBuffer((char *)0);
        httpBuffer.reserve(BUFFER_BIG);
        httpBuffer = "POST https://" TELEGRAM_HOST "/bot";
        httpBuffer += m_token;
        httpBuffer += "/sendMessage";
        // Let's use 1.0 protocol in order to avoid chunked transfer encoding
        httpBuffer += " HTTP/1.0"
                      "\nHost: " TELEGRAM_HOST
                      "\nConnection: keep-alive"
                      "\nContent-Type: application/json";
        httpBuffer += "\nContent-Length: ";
        httpBuffer += strlen(payload.c_str());
        httpBuffer += "\n\n";
        httpBuffer += payload;

        #if DEBUG_ENABLE
        if (strcmp(command, "getUpdates") != 0) {
            log_debug("Command %s, payload: %s\n", command, payload);
        }
        #endif

        // Send the whole request in one go is much faster
        telegramClient->print(httpBuffer);

        m_waitingReply = true;
        // Blocking mode
        if (wait)
        {
            // Wait data (with timeout)
            uint32_t timeout = millis() + 1000;
            while (!telegramClient->available() && millis() < timeout) ;

            // Skip headers
            if (!telegramClient->find((char *)HEADERS_END))
            {
                log_error("Invalid HTTP response");
                telegramClient->stop();
                return 0;
            }

            // If there are incoming bytes available from the server, read them and print them:
            m_rxbuffer = "";
            while (telegramClient->available())
            {
                yield();
                m_rxbuffer += (char)telegramClient->read();
            }

            m_waitingReply = false;
            JsonDocument doc;
            deserializeJson(doc, m_rxbuffer);

            if (doc["ok"] == true) {
                return doc["result"]["message_id"];
            }
        }
    }
    return 0;
}

Work

  TBMessage msg;
  msg.chatId = tgUserid;
  keepaliveMessageId = myBot.sendNewMessage(msg, message);
  Serial.print("Sended message messageID: ");
  Serial.println(keepaliveMessageId);

marlenus avatar Oct 16 '25 14:10 marlenus