无法通过websocket与amazon connect进行通信



我集成了amazon connect平台,在我们自己的聊天机器人中保持双向流通信,客户将是我们平台的用户,代理将在amazon connect平台上进行通信。我现在使用websockets进行通信,但它给了我禁止的错误。详情如下

最初,我使用了aws-sdk@aws-sdk/client-connectparticipant库与aws建立连接,然后按顺序使用多个sdk进一步发送消息。

  1. startChatContact→使用AWS库与AWS建立连接,然后使用它检索参与令牌
  2. createParticipantConnection→使用参与令牌从此sdk检索连接令牌使用类型:['CONNECTION_CREDENTIALS']
  3. sendEvent→使用连接令牌和内容类型:'application/vnd.amazonaws.connect.event.connection。已确认'发送事件
  4. sendMessage→事件发送后,发送带有连接令牌和内容类型:'text/plain'的消息
import * as AWS from 'aws-sdk';
import * as AWSConnectParticipant from "@aws-sdk/client-connectparticipant";
private messageText = "";
private connectionToken = "";
private connectParticipant = new AWSConnectParticipant.ConnectParticipant({
credentials: {
accessKeyId: '...',
secretAccessKey: '...'
},
region: '...'
});
// It will get called when user sends a message on the chat window
public sendMessage(text: string): void {
this.messageText = text || "";
if (this.connectionToken) {
this.sendEventOnAWSConnect();
} else {
this.startChatContact();
}
}

startChatContact() {
const connect = new AWS.Connect({
accessKeyId: '...',
secretAccessKey: '...',
region: '...'
});
const params = {
ContactFlowId: '...',
InstanceId: '...',
ParticipantDetails: {
DisplayName: 'Customer'
}
};
connect.startChatContact(params, (err: any, data: any) => {
if (data) {
this.createParticipantConnection(data);
}
});
}
createParticipantConnection(startChatContactRes: any) {
const params = {
ParticipantToken: startChatContactRes.ParticipantToken,
Type: [ 'CONNECTION_CREDENTIALS' ]
};
this.connectParticipant.createParticipantConnection(params, (err: any, data: any) => {
if (data) {
this.connectionToken = data.ConnectionCredentials.ConnectionToken;
this.sendEventOnAWSConnect();
this.checkAgentMessage(data.Websocket.Url);
}
});
}

sendEventOnAWSConnect() {
const params = {
ConnectionToken: this.connectionToken,
ContentType: 'application/vnd.amazonaws.connect.event.connection.acknowledged'
};
this.connectParticipant.sendEvent(params, (err: any, data: any) => {
if (data) {
this.sendMessageOnAWSConnect();
}
});
}
sendMessageOnAWSConnect() {
const params = {
ConnectionToken: this.connectionToken,
Content: this.messageText,
ContentType: 'text/plain'
};
this.connectParticipant.sendMessage(params, (err: any, data: any) => {
if (data) {
console.log("Agent connected");
}
});
}

正常工作。我能够通过以下代码在亚马逊连接上发送消息。但是我在接收代理信息时遇到了一些问题。我已经搜索了任何事件,我可以触发我的结束或任何webhook,但无法找到任何相同的。

关于第一个方法的问题:不是一个好方法。寻找更好的解决方案
我最初使用了轮询技术在这里我使用了getTranscript来自@aws-sdk/client-connectparticipant的SDK,并每2秒调用api以检查任何新的代理消息,但我现在正在寻找更好的方法。

第二个方法的问题:get connect.core.getWebSocketManager()为未定义经过探索,我还发现有一个onMessage事件,我可以使用amazon-connect-streams触发该事件和amazon-connect-chatjs创建代理会话后的库connect.core.getWebSocketManager ()是未定义的。此外,在连接后进行编码。联系没有得到执行,所以我也评论了。我也创建了类似的客户会话,但也有**onMessage **事件没有被触发。我正在调用它的方法,即checkAgentMessage后,我从createParticipantConnection方法成功获得响应,因为我在checkAgentMessage方法中使用联系人id,参与者id和参与者令牌,我从createParticipantConnection方法获得。代码如下:

import "amazon-connect-streams";
import "amazon-connect-chatjs";
createParticipantConnection(startChatContactRes: any) {
const params = {
ParticipantToken: startChatContactRes.ParticipantToken,
Type: [ 'CONNECTION_CREDENTIALS' ]
};
this.connectParticipant.createParticipantConnection(params, (err: any, data: any) => {
if (data) {
this.connectionToken = data.ConnectionCredentials.ConnectionToken;
this.sendEventOnAWSConnect();
this.checkAgentMessage(data);
}
});
}
checkAgentMessage(startChatContactRes: any): void {
// for type customer
// const customerChatSession = connect.ChatSession.create({
//   chatDetails: {
//     contactId: startChatContactRes.ContactId,
//     participantId: startChatContactRes.ParticipantId,
//     participantToken: startChatContactRes.ParticipantToken,
//   },
//   type: connect.ChatSession.SessionTypes.CUSTOMER
// });
// for type agent
// connect.contact(contact => {
// if (contact.getType() !== connect.ContactType.CHAT) {
//   // applies only to CHAT contacts
//   return;
// }

// alternative: if you want control over the args of `connect.ChatSession.setGlobalConfig()` and `connect.ChatSession.create()`
// contact.onAccepted(() => {    
const agentChatSession = connect.ChatSession.create({
chatDetails: {
contactId: startChatContactRes.ContactId,
participantId: startChatContactRes.ParticipantId,
participantToken: startChatContactRes.ParticipantToken,
},
options: { // REQUIRED
region: "...", // REQUIRED, must match the value provided to `connect.core.initCCP()`
},
type: connect.ChatSession.SessionTypes.AGENT, // REQUIRED
websocketManager: connect.core.getWebSocketManager() // REQUIRED
})
agentChatSession.onMessage(event => {
console.log("event", event);
});
// });
// });
}

我检查了是否可以从某个地方设置connect.core.getWebSocketManager(),但同样没有得到任何帮助。

第三种方法的问题:得到Forbidden作为错误或消息
我也遇到了另一个解决方案,那是从web套接字。所以,我实现的是一样的但是这里我得到了错误Forbidden

我已经改变了我的createParticipantConnection使用如下代码:

createParticipantConnection(startChatContactRes: any) {
const params = {
ParticipantToken: startChatContactRes.ParticipantToken,
Type: [ 'WEBSOCKET' ]
};
this.connectParticipant.createParticipantConnection(params, (err: any, data: any) => {
if (data) {
let socket = new WebSocket(data.Websocket.Url);
socket.onopen = function(e) {
console.log("[open] Connection established");
console.log("Sending to server");
socket.send("My name is John");
};
socket.onmessage = function(event) {
console.log("event", event);
console.log(`[message] Data received from server: ${event.data}`);
};
socket.onclose = function(event) {
if (event.wasClean) {
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
console.log('[close] Connection died');
}
};
socket.onerror = function(error) {
console.log(`[error]`);
};
// this.connectionToken = data.ConnectionCredentials.ConnectionToken;
// this.sendEventOnAWSConnect();
// this.checkAgentMessage(data);
}
});
}

CONNECTION_CREDENTIALSWEBSOCKET获取websocket url。得到与

相同的输出[open]连接已建立发送到服务器event MessageEvent{…}[message]从服务器接收的数据:{"message": "Forbidden"; "connectionId"; "…"; " requesttid "; "…"}

作为错误或消息抛出Forbidden。请让我知道,如果有什么我已经离开,需要也实施或我做错了什么在这里。也请让我知道,如果有人有第二种方法的问题的解决方案,或者是否有任何其他方法来检索代理消息。

呼叫CreateParticipantConnection后,需要发送订阅消息。然后,您将开始接收消息&websocket的事件。

From https://docs.aws.amazon.com/connect-participant/latest/APIReference/API_CreateParticipantConnection.html:

对于聊天,您需要在建立的websocket连接上发布以下内容:

{"topic"aws/subscribe","content":{"topics"["aws/chat"}}

相关内容

  • 没有找到相关文章

最新更新