tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

Someone please help me #2855

Closed solomode0001 closed 6 months ago

solomode0001 commented 6 months ago

I am trying to create a function that can reply to messages using a C# program.

private static void ReplyToMessage(long chatId, long messageIdToReply, string replyText)
{
    InputMessageContent messageContent = new TdApi.InputMessageText(new TdApi.FormattedText(replyText, null), null, true);
    InputMessageReplyTo replyTo = new InputMessageReplyToMessage(chatId, messageIdToReply, null);
    SendMessage sendMessageRequest = new TdApi.SendMessage(chatId, 0, replyTo, null, null, messageContent);
    _client.Send(sendMessageRequest, _defaultHandler);
}

In the updateNewMessage function, there is a difference between the messages being replied to in my program and manually.

The difference lies in the reply_to:

This is the manual reply log:

reply_to = messageReplyToMessage {
  chat_id = 5759209557
  message_id = 435159040
  quote = null
  origin = null
  origin_send_date = 0
  content = null
}

And this is from my code:

reply_to = messageReplyToMessage {
  chat_id = 0
  message_id = 0
  quote = textQuote {
    text = formattedText {
      text = "Wellcome!"
      entities = vector[0] {
      }
    }
    position = 0
    is_manual = false
  }
  origin = messageOriginUser {
    sender_user_id = 5759209557
  }
  origin_send_date = 1713824238
  content = null
}

Why are chat_id and message_id 0?

Full log manual:

updateNewMessage {
  message = message {
    id = 436743616
    sender_id = messageSenderUser {
      user_id = 5345209557
    }
    chat_id = 5759209557
    sending_state = null
    scheduling_state = null
    is_outgoing = false
    is_pinned = false
    is_from_offline = false
    can_be_edited = false
    can_be_forwarded = true
    can_be_replied_in_another_chat = true
    can_be_saved = true
    can_be_deleted_only_for_self = true
    can_be_deleted_for_all_users = true
    can_get_added_reactions = false
    can_get_statistics = false
    can_get_message_thread = false
    can_get_read_date = false
    can_get_viewers = false
    can_get_media_timestamp_links = false
    can_report_reactions = false
    has_timestamped_media = false
    is_channel_post = false
    is_topic_message = false
    contains_unread_mention = false
    date = 1713824275
    edit_date = 0
    forward_info = null
    import_info = null
    interaction_info = null
    unread_reactions = vector[0] {
    }
    reply_to = messageReplyToMessage {
      chat_id = 5759209557
      message_id = 435159040
      quote = null
      origin = null
      origin_send_date = 0
      content = null
    }
    message_thread_id = 0
    saved_messages_topic_id = 0
    self_destruct_type = null
    self_destruct_in = 0.000000
    auto_delete_in = 0.000000
    via_bot_user_id = 0
    sender_business_bot_user_id = 0
    sender_boost_count = 0
    author_signature = ""
    media_album_id = 0
    restriction_reason = ""
    content = messageText {
      text = formattedText {
        text = "Wellcome!"
        entities = vector[0] {
        }
      }
      web_page = null
      link_preview_options = null
    }
    reply_markup = null
  }
}

Full log program:

updateNewMessage {
  message = message {
    id = 434110465
    sender_id = messageSenderUser {
      user_id = 6977810414
    }
    chat_id = 5759209557
    sending_state = messageSendingStatePending {
      sending_id = 0
    }
    scheduling_state = null
    is_outgoing = true
    is_pinned = false
    is_from_offline = false
    can_be_edited = false
    can_be_forwarded = true
    can_be_replied_in_another_chat = false
    can_be_saved = true
    can_be_deleted_only_for_self = false
    can_be_deleted_for_all_users = true
    can_get_added_reactions = false
    can_get_statistics = false
    can_get_message_thread = false
    can_get_read_date = false
    can_get_viewers = false
    can_get_media_timestamp_links = false
    can_report_reactions = false
    has_timestamped_media = false
    is_channel_post = false
    is_topic_message = false
    contains_unread_mention = false
    date = 1713824238
    edit_date = 0
    forward_info = null
    import_info = null
    interaction_info = null
    unread_reactions = vector[0] {
    }
    reply_to = messageReplyToMessage {
      chat_id = 0
      message_id = 0
      quote = textQuote {
        text = formattedText {
          text = "Hi!"
          entities = vector[0] {
          }
        }
        position = 0
        is_manual = false
      }
      origin = messageOriginUser {
        sender_user_id = 5759209557
      }
      origin_send_date = 1713824238
      content = null
    }
    message_thread_id = 0
    saved_messages_topic_id = 0
    self_destruct_type = null
    self_destruct_in = 0.000000
    auto_delete_in = 0.000000
    via_bot_user_id = 0
    sender_business_bot_user_id = 0
    sender_boost_count = 0
    author_signature = ""
    media_album_id = 0
    restriction_reason = ""
    content = messageText {
      text = formattedText {
        text = "Wellcome!"
        entities = vector[0] {
        }
      }
      web_page = null
      link_preview_options = null
    }
    reply_markup = null
  }
}
levlam commented 6 months ago

Because you incorrectly use InputMessageReplyToMessage. See documentation of the chat_id field:

The identifier of the chat to which the message to be replied belongs; pass 0 if the message to be replied is in the same chat. Must always be 0 for replies in secret chats. A message can be replied in another chat or topic only if message.can_be_replied_in_another_chat

solomode0001 commented 6 months ago

Could you fix my code so it can properly reply to messages?

private static void ReplyToMessage(long chatId, long messageIdToReply, string replyText)
{
    InputMessageContent messageContent = new TdApi.InputMessageText(new TdApi.FormattedText(replyText, null), null, true);
    InputMessageReplyTo replyTo = new InputMessageReplyToMessage(chatId, messageIdToReply, null);
    SendMessage sendMessageRequest = new TdApi.SendMessage(chatId, 0, replyTo, null, null, messageContent);
    _client.Send(sendMessageRequest, _defaultHandler);
}

private class UpdateHandler : Td.ClientResultHandler
{
    void Td.ClientResultHandler.OnResult(TdApi.BaseObject @object)
    {
        if (@object is TdApi.UpdateAuthorizationState)
        {
            OnAuthorizationStateUpdated((@object as TdApi.UpdateAuthorizationState).AuthorizationState);
        }
        else
        {
            // Print("Unsupported update: " + @object);         
            if (@object is TdApi.UpdateNewMessage updateNewMessage)
            {
                Print(@object.ToString());
                TdApi.MessageContent content = updateNewMessage.Message.Content;
                if (content is TdApi.MessageText text)
                {
                    string message = text.Text.Text;
                    long chatId = updateNewMessage.Message.ChatId;
                    long messageId = updateNewMessage.Message.Id;
                    // Print($"message = {message} :: chat_id = {chatId} :: message_id = {messageId} :: message_length = {message.Length}");

                    if (message.ToLower().Contains("Hello"))
                    {
                        ReplyToMessage(chatId, messageId, "Wellcome!");
                    }                          
                }                      
            }

        }
    }
}
solomode0001 commented 6 months ago

I have found the solution.