微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何以编程方式停止自定义聊天频道上的 Twilio Autopilot 对话?

如何解决如何以编程方式停止自定义聊天频道上的 Twilio Autopilot 对话?

我正在为 Twilio Autopilot 机器人实施端到端测试。我使用自定义聊天通道开始与机器人的对话,方法是向此 URL 发送 HTTP POST: https://channels.autopilot.twilio.com/v2/AC8xxxxxxxxxxxxxx/UA6xxxxxxxxxxxxx/custom/chat

我假设在下面,这会创建一个名为 chat 的新聊天频道并将用户添加到该频道,然后开始一个新的对话。

方法在此处的 Twilio 文档中有所描述:https://www.twilio.com/docs/autopilot/channels/custom-channel

我得到的响应包含一个 dialogue SID:

 "dialogue": {
    "sid": "UK32f8xxxxxxxxxxxxxxxxxx",...
 }

由于我希望可以重复测试并获得相同的结果,因此我希望能够以编程方式停止在每次测试结束时启动的对话。

给定对话 SID,有没有办法以编程方式停止对话?如果对话无法停止,有没有办法以编程方式删除自定义频道?其他解决此问题的方法也很受欢迎。谢谢。

解决方法

虽然这没有回答“如何停止对话”或“如何删除自定义频道”,但这是我用来解决“可重复测试”要求的解决方案。

我使用的是 Node 和 axios,虽然下面的代码主要是一个简单的 HTTP 请求,所以它可以用其他语言复制。

helper 类是不言自明的 - 它只是在创建时生成一个新的用户 ID 以避免对话冲突,然后调用我的问题中提到的自定义频道 URL:

export class AutopilotUtils {
    private authHeaderValue: string;
    private userIdentity: string;
    private targetUrl: string;
    
    constructor(
        twilioAccountSid: string,twilioAuthToken: string,twilioAssistandSid: string) {

        const autopilotUrl = `https://channels.autopilot.twilio.com/v1/${twilioAccountSid}/${twilioAssistantSid}/custom/testchat`;

        // the Inbound Context becomes the value of the Memory parameter
        // on the target URL
        const inboundContext = {
            someProp: "somePropValue"
        };
        this.userIdentity = `testUser_${this.generateQuickGuid()}`;
        this.authHeaderValue = `Basic ${Buffer.from(twilioAccountSid + ":" + twilioAuthToken).toString("base64")}`;
        this.targetUrl = `${autopilotUrl}?Memory=${JSON.stringify(inboundContext)}`;
    }

    public async sendMessage(userInput: string): Promise<AxiosResponse<any>> {
        const result = await axios({
            method: "post",url: this.targetUrl,data: `user_id=${this.userIdentity}&text=${userInput}`,headers: {
                "Content-Type": "application/x-www-form-urlencoded","Accept": "application/json",Authorization: this.authHeaderValue
            }
        });
        return result;
    }

    private generateQuickGuid() {
        return Math.random().toString(36).substring(2,15) +
            Math.random().toString(36).substring(2,15);
    }
}

然后就可以在这样的测试中使用(使用 jest 和 chai):

// make a new utils instance (which will use the same user identity
// for the duration of its lifetime)
const utils = new AutopilotUtils(acctSid,authToken,botSid);

// send "hello" to the bot
const result = await utils.sendMessage("hello");

// result will contain the actions that were returned. for v1,the
// shape of the returned object is:
//
// { says: [{speech: "blah"},{speech: "blah again"}],listen: {}}
//
// the action names,order,and content will match what the bot
// returns,so every action might have its own shape and would
// need to be checked accordingly. we are checking the "data"
// property only because that's how axios data comes back,it's
// not part of the object shape returned from Twilio.
expect(result).to.not.be.undefined;
expect(result).to.have.property("data").that.has.property("says");
const responseArray = result.data["says"];
expect(responseArray).to
    .satisfy((s: {speech: string}[]) => s.some(i => i.speech && i.speech.includes("hi there")));

代码中的注释解释了它是如何工作的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。