Skip to content

Commit

Permalink
Parse quote msg (#308)
Browse files Browse the repository at this point in the history
* fix: adapt to receive quote message, #306

* 0.7.40

* style: for eslint
  • Loading branch information
su-chang committed Sep 2, 2020
1 parent 560c440 commit 4fdaa45
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-padplus",
"version": "0.7.39",
"version": "0.7.40",
"description": "Puppet Padplus for Wechaty",
"directories": {
"test": "tests"
Expand Down
63 changes: 63 additions & 0 deletions src/pure-function-helpers/message-quote-payload-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { PadplusMessagePayload, REFER_MSG_TYPE } from '../schemas'
import { xmlToJson } from './xml-to-json'
import { log } from 'brolog'

function referMsgDic (type: string) {
switch (type) {
case REFER_MSG_TYPE.TEXT:
return '文本消息'
case REFER_MSG_TYPE.IMAGE:
return '图片消息'
case REFER_MSG_TYPE.VIDEO:
return '视频消息'
case REFER_MSG_TYPE.LOCATION:
return '定位消息'
case REFER_MSG_TYPE.LINK:
return '链接/文件消息'
default:
return '未知类型消息'
}
}

export async function quotePayloadParser (rawPayload: PadplusMessagePayload): Promise<string> {
const { content } = rawPayload

interface XmlSchema {
msg: {
appmsg: {
title: string,
refermsg: {
type: string,
svrid: string,
fromusr: string,
chatusr: string,
displayname: string,
content: string,
msgsource: string,
}
}
}
}

const tryXmlText = content.replace(/^[^\n]+\n/, '')

try {
const jsonPayload: XmlSchema = await xmlToJson(tryXmlText)

if (!jsonPayload || !jsonPayload.msg || !jsonPayload.msg.appmsg || !jsonPayload.msg.appmsg.refermsg) {
log.error(`can not get detail info about quote message, ${JSON.stringify(jsonPayload)}`)
return referMsgDic('')
}

const { title, refermsg } = jsonPayload.msg.appmsg
const { type, displayname, content } = refermsg
const splitLine = '\n- - - - - - - - - - - - - - -\n'
const refContent = type === REFER_MSG_TYPE.TEXT ? content : referMsgDic(type)

const preText = `"${displayname}:\n${refContent}"${splitLine}`

return preText + title
} catch (e) {
throw new Error(`can not parse xml to json: ${JSON.stringify(tryXmlText)}`)
}
}
18 changes: 8 additions & 10 deletions src/pure-function-helpers/message-raw-payload-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ import { recalledPayloadParser } from './message-recalled-payload-parser'
import { messageSourceParser } from './message-source-parser'
import { messageType } from './message-type'
import { log } from '../config'
// import { xmlToJson } from './xml-to-json'
import { quotePayloadParser } from './message-quote-payload-parser'

const PRE = 'messageRawPayloadParser'

export async function messageRawPayloadParser (
rawPayload: PadplusMessagePayload,
): Promise<MessagePayload> {

// console.log('messageRawPayloadParser:', rawPayload)

/**
* 0. Set Message Type
*/
Expand Down Expand Up @@ -187,14 +185,11 @@ export async function messageRawPayloadParser (
}

/**
* 6. Set Contact for ShareCard
* 7. Set text for quote message
*/
/* if (type === MessageType.Contact) {
const xml = await xmlToJson(rawPayload.content.split('\n')[1])
log.silly(PRE, `xml : ${JSON.stringify(xml)}`)
const shareCardData = xml.msg.$
text = JSON.stringify(shareCardData)
} */
if (rawPayload.appMsgType === WechatAppMessageType.QuoteMessage) {
text = await quotePayloadParser(rawPayload)
}

let payload: MessagePayload

Expand Down Expand Up @@ -261,6 +256,9 @@ export async function messageRawPayloadParser (
payload.type = MessageType.GroupNote
payload.text = appPayload.title
break
case WechatAppMessageType.QuoteMessage:
payload.type = MessageType.Text
break
default:
payload.type = MessageType.Unknown
break
Expand Down
9 changes: 9 additions & 0 deletions src/schemas/padplus-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export enum WechatAppMessageType {
MiniProgram = 33,
MiniProgramApp = 36, // this is forwardable mini program
GroupNote = 53,
QuoteMessage = 57,
Transfers = 2000,
RedEnvelopes = 2001,
ReaderType = 100001,
Expand Down Expand Up @@ -272,3 +273,11 @@ export enum GrpcMQType {
SYNC = 51,
LOGOUT = 1100,
}

export enum REFER_MSG_TYPE {
TEXT = '1',
IMAGE = '3',
VIDEO = '43',
LOCATION = '48',
LINK = '49',
}

0 comments on commit 4fdaa45

Please sign in to comment.