UNPKG

wechat-bot

Version:

middleware framework for creating wechat bot

48 lines (44 loc) 1.72 kB
var querystring = require('querystring'); var tpl = [ '<xml>', '<ToUserName><![CDATA[<%=sp%>]]></ToUserName>', '<FromUserName><![CDATA[<%=user%>]]></FromUserName>', '<CreateTime><%=(new Date().getTime())%></CreateTime>', '<MsgType><![CDATA[<%=type%>]]></MsgType>', '<% if (type === "text") { %>', '<Content><![CDATA[<%=text%>]]></Content>', '<% } else if (type === "location") { %>', '<Location_X><%=xPos%></Location_X>', '<Location_Y><%=yPos%></Location_Y>', '<Scale><%=scale%></Scale>', '<Label><![CDATA[<%=label%>]]></Label>', '<% } else if (type === "image") { %>', '<PicUrl><![CDATA[<%=pic%>]]></PicUrl>', '<% } else if (type === "voice") { %>', '<MediaId><%=mediaId%></MediaId>', '<Format><%=format%></Format>', '<% } else if (type === "link") { %>', '<Title><![CDATA[<%=title%>]]></Title>', '<Description><![CDATA[<%=description%>]]></Description>', '<Url><![CDATA[<%=url%>]]></Url>', '<% } else if (type === "event") { %>', '<Event><![CDATA[<%=event%>]]></Event>', '<% if (event === "LOCATION") { %>', '<Latitude><%=latitude%></Latitude>', '<Longitude><%=longitude%></Longitude>', '<Precision><%=precision%></Precision>', '<% } %>', '<% } %>', '</xml>' ].join(''); exports.tail = function () { var q = { timestamp: new Date().getTime(), nonce: parseInt((Math.random() * 100000000000), 10) }; var s = ['some token', q.timestamp, q.nonce].sort().join(''); q.signature = require('crypto').createHash('sha1').update(s).digest('hex'); q.echostr = 'hehe'; return '?' + querystring.stringify(q); }; exports.template = require('ejs').compile(tpl);