node-red-contrib-chatbot
node-red-contrib-chatbot copied to clipboard
MS Teams. Bot cannot send a message to the user through the conversation node
Hi.
I know chatId and am trying to send the user a message myself. But I get the error. Using a conversation node.
I really hope for help. This is an important functionality for me. Thanks.
Error: "TypeError: message.originalMessage.getResponse is not a function or its return value is not iterable"
node-red version: 1.2.9 node-red-contrib-chatbot version: 0.19.5
Hello @guidone
I found a workaround. To do this, you need to use the storage of the context in a file. So after the first message we save it and then check it when sending it, if it was not there, then we take it from the file and send the message. In this way, you can use the conversation node and write messages first from the bot. I'm not sure if this is the best way. But my knowledge of NodeJS was enough to do it just like this) If you correct this for a more correct solution, I will be grateful. And thanks for the library!
Change code (in and out methods) in file: lib\platforms\microsoft-teams\index.js
line34-->
//set chatId instead userId otherwise sending from the conversation node will not work.
//since userId this is a bot and always the same
userIdKey: function(payload) {
const { activity } = payload;
//return activity != null && activity.recipient != null ? activity.recipient.id : null;
return activity != null && activity.recipient != null ? activity.from.id : null;
},
MicrosoftTeams.in(function (message) {
const { originalMessage: { activity } } = message;
const context = message.chat();
//Check if the context was previously, if not, then save
context.get('msactivity')
.then(msactivity => {
if (typeof (msactivity) === 'undefined') {
//Save ms teams context in file
Promise.resolve(context.set({
msactivity: activity
})).then(() => {
//console.log('setted context', activity)
}
);
}
})
.catch(error => {
console.log('Error in setting context', error);
})
const [req, res] = message.originalMessage.getResponse();
if (activity.type === 'message' && activity.text != null) {
message.payload.content = activity.text;
message.payload.type = 'message';
}
return message;
});
MicrosoftTeams.out('message', function (message) {
const context = message.chat();
//If use start conversation node
if (typeof (message.originalMessage.activity) === 'undefined') {
context.get('msactivity')
.then(msactivity => {
const reference = TurnContext.getConversationReference(msactivity);
// Start a new conversation with the user
return this.connector.createConversation(reference, async (ctx) => {
await ctx.sendActivity(message.payload.content);
});
});
}
else {
//Else using usual conversation flow
return this.sendActivity(message, message.payload.content)
.then(result => context.set('messageId', result.id));
}
});