利用音箱TTS功能和企业微信配合实现语音留言板(2)

好久以前搞过一个留言板,利用音箱TTS功能和企业微信配合实现语音留言板 ,那个是基于自定义存储的,最近发现一个很棒的节点,在挖掘它潜力的时候突然发现用到留言板上面也是个不错的选择。

直接切入正题,以下仅抛砖,并不代表适合每个人,需要根据自己的情况优化完善。

先看截图

其实很简单的一个流,讲解一下核心构造,
监听企业微信来的消息,构造消息内容后传递到my_message缓存起来,当我们触发读取消息,my_message将会释放最开始收到的消息,然后我们构造TTS消息让音箱读出来,接着告诉my_message删除并释放下一条消息,直到消息被全部读取。

那么这里就用到了核心的node-red-contrib-queue-gate节点,关于这个节点更多的玩法可以参考说明和导入节点demo学习,

好了,贴上上面流的代码,自己去试试吧

[{"id":"25c84bc1.476b04","type":"q-gate","z":"eb7dad34.a81d7","name":"my_message","controlTopic":"control","defaultState":"queueing","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"peek","dropCmd":"drop","statusCmd":"status","maxQueueLength":"100","keepNewest":false,"qToggle":false,"persist":true,"storeName":"store","x":550,"y":1020,"wires":[["4d4166ee.cb1ab8","e8c0a534.e98568"]]},{"id":"24695790.573b28","type":"inject","z":"eb7dad34.a81d7","name":"读取消息","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"peek","payloadType":"str","x":340,"y":1020,"wires":[["25c84bc1.476b04"]]},{"id":"224d0f97.34535","type":"link out","z":"eb7dad34.a81d7","name":"","links":["ad8e5485.eb11c8"],"x":1235,"y":1020,"wires":[]},{"id":"ad8e5485.eb11c8","type":"link in","z":"eb7dad34.a81d7","name":"","links":["224d0f97.34535"],"x":495,"y":980,"wires":[["25c84bc1.476b04"]]},{"id":"9efb6022.d1fe9","type":"function","z":"eb7dad34.a81d7","name":"播放下一条","func":"//先发一条消息销毁已读的留言\nnode.send({topic: \"control\", payload: \"drop\"})\n//依赖前面的节点,设置延迟等待时间\nmsg.delay = flow.get('message_wait');\nmsg.topic = \"control\";\nmsg.payload = \"peek\"; //播放下一条\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1010,"y":1020,"wires":[["d4d0d1ec.63827"]]},{"id":"825be362.db40d","type":"function","z":"eb7dad34.a81d7","name":"保存留言","func":"/**\n 时间戳\n 对Date的扩展,将 Date 转化为指定格式的String\n 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,\n 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)\n*/\nDate.prototype.Format = function(fmt){ //author: meizz\n    var o = {\n        \"M+\" : this.getMonth()+1,                 //月份\n        \"d+\" : this.getDate(),                    //日\n        \"h+\" : this.getHours(),                   //小时\n        \"m+\" : this.getMinutes(),                 //分\n        \"s+\" : this.getSeconds(),                 //秒\n        \"q+\" : Math.floor((this.getMonth()+3)/3), //季度\n        \"S\"  : this.getMilliseconds()             //毫秒\n    };\n    if(/(y+)/.test(fmt))\n    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+\"\").substr(4 - RegExp.$1.length));\n    for(var k in o)\n    if(new RegExp(\"(\"+ k +\")\").test(fmt))\n    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : ((\"00\"+ o[k]).substr((\"\"+ o[k]).length)));\n    return fmt;\n};\n\nvar UserName = {\n 'samuel':'三木'\n};\n\n//判断是打开企业微信后并发送了消息\nif(msg.message.MsgType != \"event\"){\n    //判断是有内容的消息\n    if(msg.message.AsrContent || msg.message.Content){\n        var message = \"\";\n        //如果是语音消息\n        if(msg.message.AsrContent) message = msg.message.AsrContent[0];\n        //如果是文本消息\n        if(msg.message.Content) message = msg.message.Content;\n        //匹配用户\n        var FromUserName = UserName[msg.message.FromUserName];\n        //增加新消息\n        msg = {};\n        msg.message = {};\n        msg.message.FromUserName = FromUserName;\n        msg.message.CreateTime = new Date().Format(\"M月d日h点m分\");\n        msg.message.Content = message;\n        return msg;\n    }\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":980,"wires":[["25c84bc1.476b04"]]},{"id":"fcbc0fc0.3c49d","type":"inject","z":"eb7dad34.a81d7","name":"清空消息","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"reset","payloadType":"str","x":340,"y":1060,"wires":[["25c84bc1.476b04"]]},{"id":"f5d42516.7219c8","type":"link in","z":"eb7dad34.a81d7","name":"企业微信消息","links":["5187e252.77509c"],"x":170,"y":980,"wires":[["825be362.db40d"]],"l":true},{"id":"e8c0a534.e98568","type":"function","z":"eb7dad34.a81d7","name":"主卧TTS","func":"msg.device = \"3218fab6-2dd59f\";\nmsg.payload = msg.message.FromUserName + \"在\" + msg.message.CreateTime + \"留言说\" + msg.message.Content;\n//设置延迟时间\nflow.set('message_wait', msg.payload.length*280);//数字根据实际情况调整\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":1020,"wires":[["16abb787.4323b8"]]},{"id":"16abb787.4323b8","type":"link out","z":"eb7dad34.a81d7","name":"","links":["ad9e50aa.21965"],"x":835,"y":1020,"wires":[]},{"id":"bad613d9.c837b","type":"link in","z":"eb7dad34.a81d7","name":"","links":["9523e395.d7623"],"x":895,"y":1020,"wires":[["9efb6022.d1fe9"]]},{"id":"4d4166ee.cb1ab8","type":"debug","z":"eb7dad34.a81d7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":980,"wires":[]},{"id":"d4d0d1ec.63827","type":"delay","z":"eb7dad34.a81d7","name":"延迟","pauseType":"delayv","timeout":"1","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1150,"y":1020,"wires":[["224d0f97.34535"]]},{"id":"469b6a8d.e32f24","type":"comment","z":"eb7dad34.a81d7","name":"这里为小爱TTS节点","info":"","x":890,"y":980,"wires":[]}]

你好! 小度可以用吗