Skip to content

Message.forward() does not support returning a message #2073

@windmemory

Description

@windmemory

Here is the implementation of Message.forward()

  public async forward (to: Room | Contact): Promise<void> {
    log.verbose('Message', 'forward(%s)', to)

    // let roomId
    // let contactId

    try {
      await this.wechaty.puppet.messageForward(
        to.id,
        this.id,
      )
    } catch (e) {
      log.error('Message', 'forward(%s) exception: %s', to, e)
      throw e
    }
  }

And here is the implementation of Message.say()

public async say (
    something :  string
                                    | number
                                    | Message
                                    | Contact
                                    | FileBox
                                    | UrlLink
                                    | MiniProgram,
  ): Promise<void | Message> {
    log.verbose('Message', 'say(%s)', something)

    // const user = this.wechaty.puppet.userSelf()
    const from = this.from()
    // const to   = this.to()
    const room = this.room()

    let conversationId: string
    let conversation

    if (room) {
      conversation = room
      conversationId = room.id
    } else if (from) {
      conversation = from
      conversationId = from.id
    } else {
      throw new Error('neither room nor from?')
    }

    /**
     * Support say a existing message: just forward it.
     */
    if (something instanceof Message) {
      return something.forward(conversation)
    }

    // Convert number to string
    if (typeof something === 'number') {
      something = String(something)
    }

    let msgId: void | string
    if (typeof something === 'string') {
      /**
       * Text Message
       */
      let mentionIdList
      if (from && await this.mentionSelf()) {
        mentionIdList = [from.id]
      }

      msgId = await this.wechaty.puppet.messageSendText(
        conversationId,
        something,
        mentionIdList,
      )
    } else if (something instanceof Contact) {
      /**
       * Contact Card
       */
      msgId = await this.wechaty.puppet.messageSendContact(
        conversationId,
        something.id,
      )
    } else if (something instanceof FileBox) {
      /**
       * File Message
       */
      msgId = await this.wechaty.puppet.messageSendFile(
        conversationId,
        something,
      )
    } else if (something instanceof UrlLink) {
      /**
       * Link Message
       */
      msgId = await this.wechaty.puppet.messageSendUrl(
        conversationId,
        something.payload,
      )
    } else if (something instanceof MiniProgram) {
      /**
       * MiniProgram
       */
      msgId = await this.wechaty.puppet.messageSendMiniProgram(
        conversationId,
        something.payload,
      )
    } else {
      throw new Error('Message.say() received unknown msg: ' + something)
    }
    if (msgId) {
      const msg = this.wechaty.Message.load(msgId)
      await msg.ready()
      return msg
    }
  }

Comparing these two methods, we can see that the Message.forward() does not store the msgId returns back from the under layer puppet. We need to add a support for that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions