-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: adapt to receive quote message, #306 * 0.7.40 * style: for eslint
- Loading branch information
Showing
4 changed files
with
81 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters