小程序开发|小程序制作|小程序开发网

搜索

【微信小程序开发教程】智能对话开发

2017-8-31 23:25| 发布者: 北极圈的瓜| 查看: 119| 评论: 5

摘要: 界面: 功能:目前支持闲聊,问时间,问天气,算24点,单位换算,汇率查询,邮政编码,笑话,故事,算数功能。1.智能对话接口前端显示代码 {{item.text}} {{item.text}}{{item.url}} 语义解析技术由OLAMI提供js【2


这段时间开发了一个智能对话的微信小程序,下面就把这个介绍一下。0.介绍界面:对话:功能:目前支持闲聊,问时间,问天气,算24点,单位换算,汇率查询,邮政编码,笑话,故事,算数功能。1.智能对话接口首先是对话...
0.介绍界面:
对话:


功能:目前支持闲聊,问时间,问天气,算24点,单位换算,汇率查询,邮政编码,笑话,故事,算数功能。

1.智能对话接口首先是对话的接口,用的是OLAMI的接口,可以自己定义需要的对话,也有系统提供的对话模块。
对话模块定义好之后,查看API文档,将对话通过API发送之后就可以得到回答。

API调用代码

  1. NLIRequest:function(corpus,arg) { // corpus是要发送的对话;arg是回调方法
  2. var that = this;
  3. // appkey
  4. var appkey = that.globalData.NLPAppkey;
  5. // appsecret
  6. var appSecret = that.globalData.NLPAppSecret;
  7. var api = "nli";
  8. var timestamp = new Date().getTime();
  9. // MD5签名
  10. var sign = MD5.md5(appSecret + "api=" + api + "appkey=" + appkey + "timestamp=" + timestamp + appSecret);
  11. var rqJson = { "data": { "input_type": 1, "text": corpus }, "data_type": "stt" };
  12. var rq = JSON.stringify(rqJson);
  13. var nliUrl = that.globalData.NLPUrl;
  14. // cusid是用来实现上下文的,可以自己随意定义内容,要够长够随机
  15. var cusid = that.globalData.NLPCusid;
  16. console.log("[Console log]:NLIRequest(),URL:" + nliUrl);
  17. wx.request({
  18. url: nliUrl,
  19. data: {
  20. appkey: appkey,
  21. api: api,
  22. timestamp: timestamp,
  23. sign: sign,
  24. rq: rq,
  25. cusid: cusid,
  26. },
  27. header: { 'content-type': 'application/x-www-form-urlencoded' },
  28. method: 'POST',
  29. success: function (res) {
  30. var resData = res.data;
  31. console.log("[Console log]:NLIRequest() success...");
  32. console.log("[Console log]:Result:");
  33. console.log(resData);
  34. var nli = JSON.stringify(resData);
  35. //回调函数,解析数据
  36. typeof arg.success == "function" && arg.success(nli);
  37. },
  38. fail: function (res) {
  39. console.log("[Console log]:NLIRequest() failed...");
  40. console.error("[Console log]:Error Message:" + res.errMsg);
  41. typeof arg.fail == "function" && arg.fail();
  42. },
  43. complete: function () {
  44. console.log("[Console log]:NLIRequest() complete...");
  45. typeof arg.complete == "function" && arg.complete();
  46. }
  47. })
  48. }


2.对话内容显示前端显示代码
{{item.text}} {{item.text}}{{item.url}} 语义解析技术由OLAMI提供【1】scroll-into-view=”{{scrolltop}}”是将对话滚动到最新位置,在js中把最新的id赋给scrolltop,页面会自动滚动到指定位置。【2】chatList存储对话内容,循环渲染对话框。orientation是左右位置,左边是答案,右边是用户输入。
【3】userLogoUrl是用户头像的url,如果用户不授权使用用户公开信息,则使用默认的用户头像。

最后其他的内容就是一些判断以及解析数据。源代码地址:链接: https://pan.baidu.com/s/1kVeKjeZ 密码: avpb

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

鲜花

握手

雷人

路过

鸡蛋
发表评论

最新评论

引用 admin 2018-12-18 15:53
真的很牛皮,我喜欢,要滚去学习啦!
引用 admin 2018-4-8 12:50
感谢楼主的无私奉献
引用 风琴来 2018-4-5 18:58
支持支持顶顶顶
引用 admin 2018-4-5 14:26
lai chouchou
引用 快乐的鱼 2018-3-31 11:44
好 学习学习

查看全部评论(5)

返回顶部