Skip to content

Message payload parsers list

padlocal edited this page Jun 9, 2022 · 2 revisions

1. Get plain text from 笔记

  1. Test the message content with <type>24</type>
  2. If match, call the following function, to get plain text from 笔记:
function WxNoteMsgFilter(content) {
  return '[笔记]\n'+ he.decode(he.decode(content.replace(/.*?<type>24<\/type>.*?<recorditem>(?:&lt;recordinfo&gt;|<!\[CDATA\[<recordinfo>).*?(?:&gt;&lt;datadesc&gt;|<datadesc>)(.*?)(<|&lt;)\/datadesc(>|&gt;).*?<\/recorditem>.*/s,'$1')))
}

Thanks @suntong

2. Get plain text from 合并转发 messages

  1. Test the message content with <type>19</type>
  2. If match, call the following function, to get plain text from 合并转发 messages:
async function ChatHistoryMsgFilter(content) {
  if (!/<appmsg .*<type>19<\/type>/si.test(content))
    return content
  const summary = he.decode(content)
      .replace(/.*?<msg>.*?<title>(.*?)<\/title>.*?<des>(.*?)<\/des>.*/s,'\n= 合并转发$1 =\n\n== 简介 ==\n$2\n\n== 详细 ==\n\n')
      .replace(/^(.{1,30}?):/gm,'\n[$1]: ')

  let details
  const details0 = he.decode(content)
  try {
    const details1 = await xmlToJson(details0.replace(/.*?<msg>.*?<recorditem>(.*?)<\/recorditem>.*/s,'$1'))
    if (/<recorditem><recordinfo>.*<datalist .*<datadesc>/si.test(details0)) {
      details = details1.recordinfo.datalist.dataitem.map(
	(d) => {
	  if (d.ATTR.datatype == '1') 
	    return `[${d.sourcename}] @${d.sourcetime}\n${d.datadesc}\n\n`
	  else return `[${d.sourcename}] @${d.sourcetime}\n${d.datafmt}\n\n`
	}).reduce(
	  (acc, n) => {
	    return acc + n;
	  },
	  '' // accumulator variable value at first iteration step
	)
    }
  } catch (e) {
    console.error( "ChatHistoryMsgFilter", e )
  }
  return summary + details
}