import type { FileBoxInterface } from 'file-box' import * as PUPPET from '@juzi/wechaty-puppet' import { log } from '@juzi/wechaty-puppet' import PuppetMini from '../puppet-mini.js' import { saveMessage } from '../server/mongo/service.js' import dayjs from 'dayjs' export function sendTextMessage ({ msgId, quoteId, contactId, msg, mqtt, token }:{ msgId: string, quoteId?: string, contactId: string, msg: string, token: string, mqtt: any }) { void sendMessage(contactId, { msgId, quoteId, text: msg, type: 'text', }, token, mqtt) } export async function sendFileMessage ({ msgId, quoteId, contactId, file, mqtt, token } : {msgId: string, quoteId?: string, contactId: string, file: FileBoxInterface, token: string, mqtt: any}) { void sendMessage(contactId, { fileUrl: `/${msgId}/${msgId}_${file.name}`, mediaType: file.mediaType, msgId, quoteId, type: 'file', }, token, mqtt) } export async function sendLinkMessage ({ msgId, quoteId, contactId, file, linkPayload, mqtt, token }:{msgId: string, quoteId?: string, contactId: string, linkPayload: PUPPET.payloads.UrlLink, file: FileBoxInterface | undefined, token: string, mqtt: any}) { void sendMessage(contactId, { description: linkPayload.description, msgId, quoteId, thumbnailUrl: file?.name ? `/${msgId}/${msgId}_${file.name}` : '', title: linkPayload.title, type: 'urlLink', url: linkPayload.url, }, token, mqtt) } export async function sendPostMessage ({ msgId, quoteId, contactId, postPayload, mqtt, token }:{msgId: string, quoteId?: string, contactId: string, postPayload: PUPPET.payloads.Post, token: string, mqtt: any}) { log.info('Post消息类型[5, 6, 3]:', postPayload.type) if (postPayload.type === PUPPET.types.Post.Message) { const msgInfo = postPayload.sayableList[0] as PUPPET.payloads.Sayable log.info('Sayable 消息类型', msgInfo.type) if (msgInfo.type === 'Text') { void sendMessage(contactId, { buttons: [], content: '', msgId, quoteId, text: msgInfo.payload.text || '', title: '', type: 'text', }, token, mqtt) } else if (msgInfo.type === 'Image' || msgInfo.type === 'Attachment') { if (msgInfo.payload.filebox !== 'string') { const file = msgInfo.payload.filebox as FileBoxInterface await PuppetMini.cacheManager.setFile(msgId, file) void sendMessage(contactId, { fileUrl: `/${msgId}/${msgId}_${file.name}`, mediaType: file.mediaType, msgId, quoteId, type: 'file', }, token, mqtt) } } return } const title = postPayload.sayableList[0] as PUPPET.payloads.Sayable const content = postPayload.sayableList[1] as PUPPET.payloads.Sayable log.info('发送消息', JSON.stringify(postPayload)) if (title.type !== 'Text' || content.type !== 'Text') { throw new Error('Wrong Post!!! please check your Post payload to make sure it right') } if (postPayload.type === PUPPET.types.Post.TextButton) { const buttons: { text: string | undefined, url: string | undefined, appId?: string, username?: string, isMini?: boolean, description?: string, thumbUrl?: string }[] = [] postPayload.sayableList.forEach((item, index) => { if (index > 1) { const buttonInfo = item as PUPPET.payloads.Sayable if (buttonInfo.type === 'Url') { const button = { isMini: false, text: buttonInfo.payload.title, url: buttonInfo.payload.url } buttons.push(button) } else if (buttonInfo.type === 'MiniProgram') { const button = { appId: buttonInfo.payload.appid, description: buttonInfo.payload.description, isMini: true, text: buttonInfo.payload.title, thumbUrl: buttonInfo.payload.thumbUrl, url: buttonInfo.payload.pagePath, username: buttonInfo.payload.username } buttons.push(button) } } }) void sendMessage(contactId, { buttons, content: content.payload.text || '', msgId, quoteId, title: title.payload.text || '', type: 'textCard', }, token, mqtt) } else if (postPayload.type === PUPPET.types.Post.GoodsCard) { const goods: any[] = [] postPayload.sayableList.forEach((item, index) => { if (index > 1) { const buttonInfo = item as PUPPET.payloads.Sayable if (buttonInfo.type === 'Url') { const button = { ...buttonInfo.payload, isMini: false } goods.push(button) } else if (buttonInfo.type === 'MiniProgram') { const button = { appId: buttonInfo.payload.appid, description: buttonInfo.payload.description, isMini: true, text: buttonInfo.payload.title, thumbUrl: buttonInfo.payload.thumbUrl, url: buttonInfo.payload.pagePath, username: buttonInfo.payload.username } goods.push(button) } } }) void sendMessage(contactId, { content: content.payload.text || '', goods, msgId, quoteId, title: title.payload.text || '', type: 'goodsCard', }, token, mqtt) } } export async function sendMessage (contactId: string, payLoad: any, token: string, mqtt: any) { const content = JSON.stringify(payLoad) const tokenId = token.split('_').pop() const userInfo = await PuppetMini.cacheManager.getContact(contactId) log.info('最终发送消息,获取用户信息', JSON.stringify(userInfo)) if (userInfo) { void saveMessage({ botId: PuppetMini.chatbotId, buttons: payLoad.buttons || [], content: payLoad.content || '', conversionId: userInfo.id, fileUrl: payLoad.fileUrl, goods: payLoad.goods || [], isBot: true, mediaType: payLoad.mediaType, msgId: payLoad.msgId, position: 'left', quoteId: payLoad.quoteId, text: payLoad.text, timestamp: dayjs().unix(), title: payLoad.title, type: payLoad.type, }) log.verbose('PuppetMini', '消息发送,id:(%s),mqtt状态: (%s)', payLoad.msgId, PuppetMini.mqttStatus) mqtt.publish(`/chat/${tokenId}/${userInfo.hashId}/message`, content, { qos: 0, retain: false }, (error: any) => { if (error) { log.error('PuppetMini mqtt error', error) } }) } }