上一章介绍了,只是实现了简单的回复,这一章我们将对接ERNIE-Bot接口,实现智能回复。
我们在上一章代码基础上进行调整,首先我们要加入access_token的请求逻辑。第一章我们说过,请求需要使用应用对应的API Key,Secret Key作为入参。eggjs规定了配置的写法,我们可以将其配置在config/config.default.js里面。

//配置ernie config.ernie = { client_id: '填入您的API Key', //API Key client_secret: '填入您的Secret Key',//Secret Key access_token: '',//先置空,后续由程序填充 expire_day: 30 //access_token过期时长(天) }
我们在app/service目录增加ernie.js。

ernie.js文件专门用来和ERNIE-Bot接口进行交互,代码如下:
const { Service } = require('egg'); class ErnieService extends Service &client_secret=${ctx.app.config.ernie.client_secret}`, { method: 'GET', rejectUnauthorized: false, data: {}, headers: {}, timeout: 30000, contentType: 'json', dataType: 'json', }) console.log(res) if (res.data.access_token) { ctx.app.config.ernie.access_token = res.data.access_token; console.log('access_token', ctx.app.config.ernie.access_token) } } catch (error) { console.log(error) } } } module.exports = ErnieService;
我们按eggjs的service的写法,定义了一个方法,用来请求access_token,但是这样写并不能执行,我们需要有个逻辑,在应用启动完毕后会调用这个方法。
我们在根目录增加app.js。

app.js代码如下:
module.exports = app => { app.beforeStart(async () => { // 应用会等待这个函数执行完成才启动 console.log("==app beforeStart=="); }); app.ready(async () => ) app.beforeClose(async () => { console.log("==app beforeClose=="); }) };
我们执行命令npm run dev启动项目,看到最终会打印出access_token,有了access_token我们继续开发接口请求函数sendMsg。
sendMsg函数用于调用ERNIE-Bot会话接口,传入对话内容并返回对话结果。在class ErnieService中加入以下代码:
async sendMsg(msg) {
console.log('===================ErnieService sendMsg====================='); let ctx = this.ctx; try { const res = await ctx.curl( `https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=${ctx.app.config.ernie.access_token}`, { method: 'POST', rejectUnauthorized: false, data: { "messages": msg }, timeout: 30000, contentType: 'json', dataType: 'json', }) console.log(res) if (res.data) { return res.data; } return null; } catch (error) { console.log(error) return null; } }
我们在wechat.js中,将原先的逻辑调整一下,主要是改两个地方,
1、我们在之前的代码中经常有看到ctx这个变量,可以通过他获取配置文件的参数值。但Service文件中的ctx的获取只能通过app.js调用Service中函数的方式来获得。所以我们将机器人初始化的操作定义在startBot()方法中,由app.js来执行这个方法,就可以接收到ctx的值。
2、原先回复是直接扔回用户发的信息的,现在将其对接到ernie.js中的sendMsg方法。
wechat.js全部代码如下:
const {
Service
} = require(‘egg’);
const {
WechatyBuilder, ScanStatus
} = require(“wechaty”); const qrcode = require(“qrcode-terminal”);
let ctx; let wechaty; let startStatus = false;
const onMessage = async (message) => {
console.log(`收到消息: ${message}`); if (message.type() === wechaty.Message.Type.Text) { const userMsg = await message.text(); try { let msgRecord = [{ "role": "user", "content": userMsg }]; let res = await ctx.service.ernie.sendMsg(msgRecord); if (res) `); } else `); } } } } catch (error) }
};
const onLogout = (user) => {
console.log(`用户 ${user} 退出成功`);
}; const onLogin = async (user) => {
console.log(`用户 ${user} 登录成功`);
}; const onError = console.error; const onScan = (code, status) => , console.log)
}
};
class WechatService extends Service
await wechaty.stop(); startStatus = false; wechaty = null; } wechaty = await WechatyBuilder.build(); wechaty .on("scan", onScan) .on("login", onLogin) .on("logout", onLogout) .on("error", onError) .on("message", onMessage); await wechaty.start(); startStatus = true; }
}
module.exports = WechatService;
在AccessToken请求后加入初始化BOT
app.ready(async () => )

至此,我们已经实现了一次会话的功能。多轮对话的功能等下一章再详述。
本章完整代码在下载。运行前请配置好config/config.default.js里面config.ernie下的client_id和client_secret配置项。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/276932.html