Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat at all #2418

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 67 additions & 4 deletions src/user-modules/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,8 @@ class RoomMixin extends MixinBase implements SayableSayer {
}

async announce () : Promise<string>
async announce (text: string) : Promise<void>
async announce (text: string, isAnnounce?: boolean) : Promise<void | MessageInterface>
async announce (textList: TemplateStringsArray, ...varList: any[]) : Promise<MessageInterface>

/**
* SET/GET announce from the room
Expand Down Expand Up @@ -852,21 +853,83 @@ class RoomMixin extends MixinBase implements SayableSayer {
* await room.announce('change announce to wechaty!')
* console.log(`room announce change from ${oldAnnounce} to ${room.announce()}`)
*/
async announce (text?: string): Promise<void | string> {
async announce (text?: string | TemplateStringsArray, ...varList: unknown[]): Promise<void | string> {
log.verbose('Room', 'announce(%s)',
typeof text === 'undefined'
? ''
: `"${text || ''}"`,
: `"${text || ''}", ${varList.join(', ')},`,
)

if (typeof text === 'undefined') {
const announcement = await this.wechaty.puppet.roomAnnounce(this.id)
return announcement
} else {
await this.wechaty.puppet.roomAnnounce(this.id, text)
if (isTemplateStringArray(text)) {
const msgId = this.announceTemplateStringsArray(
text as TemplateStringsArray,
...varList
)
if (msgId) {
const msg = await this.wechaty.Message.find({ id: msgId })
return msg
}
} else {
// should be announce when not passed to be compatible with old versions
let isAnnounce = !!varList[0]
const msgId = await this.wechaty.puppet.roomAnnounce(this.id, text, isAnnounce)
if (!isAnnounce && msgId) {
const msg = await this.wechaty.Message.find({ id: msgId })
return msg
}
}
}
}

private async announceTemplateStringsArray(
textList: TemplateStringsArray,
...varList: unknown[]
) {
const mentionList = varList.filter(c => ContactImpl.valid(c)) as ContactInterface[]

if (varList.length === 0) {
return this.wechaty.puppet.roomAnnounce(this.id, textList[0]!, false)
} else {
const finalText = this.generateTextFromTemplateStringsArray(textList, varList)
return this.wechaty.puppet.roomAnnounce(
this.id,
finalText,
false,
mentionList.map(c => c.id),
)
}
}

private async generateTextFromTemplateStringsArray(
textList: TemplateStringsArray,
...varList: unknown[]
) {
const textListLength = textList.length
const varListLength = varList.length
if (textListLength - varListLength !== 1) {
throw new Error('Can not say message, invalid Template String Array.')
}
let finalText = ''

let i = 0
for (; i < varListLength; i++) {
if (ContactImpl.valid(varList[i])) {
const mentionContact: ContactInterface = varList[i] as any
const mentionName = await this.alias(mentionContact) || mentionContact.name()
finalText += textList[i] + '@' + mentionName
} else {
finalText += textList[i]! + varList[i]!
}
}
finalText += textList[i]

return finalText
}

/**
* Get QR Code Value of the Room from the room, which can be used as scan and join the room.
* > Tips:
Expand Down