You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import { DirectLine } from "botframework-directlinejs";
import { ConnectionStatus } from "botframework-directlinejs";
import readline from "node:readline";
import XMLHttpRequest from "xhr2";
import WebSocket from "ws";
// Create a readline interface for user input
var directLine = new DirectLine({
secret: "zzzzzzzzzzzzz",
conversationStartProperties: {
locale: "en-US",
},
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let initialBotResponseCaptured = false;
// Function to handle conversationUpdate events
function handleConversationUpdate(eventType, activity) {
console.log(handleConversationUpdate eventType=${eventType});
const membersAdded = activity.membersAdded;
if (membersAdded && membersAdded.length > 0) {
membersAdded.forEach((member) => {
if (member.id !== activity.recipient.id) {
console.log(Member added: ${member.name});
// Send a welcome message to the new member here if needed
}
});
}
}
// Function to handle message events
function handleMessage(eventType, activity) {
console.log(handleMessage eventType=${eventType});
console.log(Received message: ${JSON.stringify(activity)});
if (activity.inputHint) {
console.log("inside send post Activity");
rl.question("Caller Input: ", (userInput) => {
sendMessageToBot(userInput);
});
}
}
// Function to start the conversation
function startConversation() {
sendMessageToBot("Hello Bot");
directLine.activity$.subscribe({
next: (activity) => {
if (activity.type === "conversationUpdate") {
handleConversationUpdate("next", activity);
} else if (activity.type === "message") {
handleMessage("next", activity);
}
},
error: (err) => console.error(Error: ${err}),
complete: () => console.log("Observable completed"),
});
}
// Function to send a message to the bot
function sendMessageToBot(text) {
directLine
.postActivity({
from: { id: "user1" },
type: "message",
text: text,
})
.subscribe({
next: (id) => console.log(Posted activity, assigned ID ${id}),
error: (error) => console.log(Error posting activity: ${error}),
complete: () => console.log("Post activity completed"),
});
}
// Start the conversation
startConversation();
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000000","timestamp":"2024-08-07T07:06:57.1163245Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"locale":"en-US","text":"Welcome to the Prescription Refill Bot Assistant!","speak":"Welcome to the Prescription Refill Bot Assistant!"}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000001","timestamp":"2024-08-07T07:06:57.2545266Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"Welcome to the Prescription Refill Bot Assistant!","speak":"Welcome to the Prescription Refill Bot Assistant!"}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000002","timestamp":"2024-08-07T07:06:57.2931043Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"locale":"en-US","text":"To get started with the prescription refill process, we will need the medical record number of the person for whom the prescription needs to be refilled.","speak":"To get started with the prescription refill process, we will need the medical record number of the person for whom the prescription needs to be refilled."}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000003","timestamp":"2024-08-07T07:06:57.4638809Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"To get started with the prescription refill process, we will need the medical record number of the person for whom the prescription needs to be refilled.","speak":"To get started with the prescription refill process, we will need the medical record number of the person for whom the prescription needs to be refilled."}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000004","timestamp":"2024-08-07T07:06:57.5291907Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"locale":"en-US","text":"please enter your medical record number","speak":"please enter your medical record number","inputHint":"expectingInput"}
inside send post Activity
Caller Input: handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000005","timestamp":"2024-08-07T07:06:57.7230876Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"please enter your medical record number","speak":"please enter your medical record number","inputHint":"expectingInput"}
inside send post Activity
Caller Input: handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000006","timestamp":"2024-08-07T07:06:56.6976734Z","serviceUrl":"https://directline.botframework.com/","channelId":"directline","from":{"id":"user1"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"recipient":{"id":"testbot@m298axTo5bQ","name":"testbot"},"text":"Hello Bot"}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000007","timestamp":"2024-08-07T07:06:58.0691691Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"The collected MRN is HelloBot","speak":"The collected MRN is HelloBot","replyToId":"GWomcz8GjLjJQZu60J1amB-us|0000006"}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000008","timestamp":"2024-08-07T07:06:58.285815Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"Now, lets get going with prescription refill process.","speak":"Now, lets get going with prescription refill process.","replyToId":"GWomcz8GjLjJQZu60J1amB-us|0000006"}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000009","timestamp":"2024-08-07T07:06:58.4965762Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"Tell me the prescription you would like to refill.","speak":"Tell me the prescription you would like to refill.","inputHint":"expectingInput","replyToId":"GWomcz8GjLjJQZu60J1amB-us|0000006"}
inside send post Activity
Caller Input: Posted activity, assigned ID GWomcz8GjLjJQZu60J1amB-us|0000006
Post activity completed
How can I have client see only messages meant for the client? It seems at the conversationUpdate event is generated twice - once for client and once for the bot...but how can I suppress this generation within the bot composer application which is where this application is built? Please assist.
The content you are editing has changed. Please copy your edits and refresh the page.
import { DirectLine } from "botframework-directlinejs";
import { ConnectionStatus } from "botframework-directlinejs";
import readline from "node:readline";
import XMLHttpRequest from "xhr2";
import WebSocket from "ws";
global.XMLHttpRequest = XMLHttpRequest;
global.WebSocket = WebSocket;
// Create a readline interface for user input
var directLine = new DirectLine({
secret: "zzzzzzzzzzzzz",
conversationStartProperties: {
locale: "en-US",
},
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let initialBotResponseCaptured = false;
// Function to handle conversationUpdate events
function handleConversationUpdate(eventType, activity) {
console.log(
handleConversationUpdate eventType=${eventType}
);const membersAdded = activity.membersAdded;
if (membersAdded && membersAdded.length > 0) {
membersAdded.forEach((member) => {
if (member.id !== activity.recipient.id) {
console.log(
Member added: ${member.name}
);// Send a welcome message to the new member here if needed
}
});
}
}
// Function to handle message events
function handleMessage(eventType, activity) {
console.log(
handleMessage eventType=${eventType}
);console.log(
Received message: ${JSON.stringify(activity)}
);if (activity.inputHint) {
console.log("inside send post Activity");
rl.question("Caller Input: ", (userInput) => {
sendMessageToBot(userInput);
});
}
}
// Function to start the conversation
function startConversation() {
sendMessageToBot("Hello Bot");
directLine.activity$.subscribe({
next: (activity) => {
if (activity.type === "conversationUpdate") {
handleConversationUpdate("next", activity);
} else if (activity.type === "message") {
handleMessage("next", activity);
}
},
error: (err) => console.error(
Error: ${err}
),complete: () => console.log("Observable completed"),
});
}
// Function to send a message to the bot
function sendMessageToBot(text) {
directLine
.postActivity({
from: { id: "user1" },
type: "message",
text: text,
})
.subscribe({
next: (id) => console.log(
Posted activity, assigned ID ${id}
),error: (error) => console.log(
Error posting activity: ${error}
),complete: () => console.log("Post activity completed"),
});
}
// Start the conversation
startConversation();
Output
PS C:\development\NodeJSBotProject\botclient> node .\index.mjs
handleMessage eventType=next
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000002","timestamp":"2024-08-07T07:06:57.2931043Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"locale":"en-US","text":"To get started with the prescription refill process, we will need the medical record number of the person for whom the prescription needs to be refilled.","speak":"To get started with the prescription refill process, we will need the medical record number of the person for whom the prescription needs to be refilled."}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000003","timestamp":"2024-08-07T07:06:57.4638809Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"To get started with the prescription refill process, we will need the medical record number of the person for whom the prescription needs to be refilled.","speak":"To get started with the prescription refill process, we will need the medical record number of the person for whom the prescription needs to be refilled."}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000004","timestamp":"2024-08-07T07:06:57.5291907Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"locale":"en-US","text":"please enter your medical record number","speak":"please enter your medical record number","inputHint":"expectingInput"}
inside send post Activity
Caller Input: handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000005","timestamp":"2024-08-07T07:06:57.7230876Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"please enter your medical record number","speak":"please enter your medical record number","inputHint":"expectingInput"}
inside send post Activity
Caller Input: handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000006","timestamp":"2024-08-07T07:06:56.6976734Z","serviceUrl":"https://directline.botframework.com/","channelId":"directline","from":{"id":"user1"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"recipient":{"id":"testbot@m298axTo5bQ","name":"testbot"},"text":"Hello Bot"}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000007","timestamp":"2024-08-07T07:06:58.0691691Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"The collected MRN is HelloBot","speak":"The collected MRN is HelloBot","replyToId":"GWomcz8GjLjJQZu60J1amB-us|0000006"}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000008","timestamp":"2024-08-07T07:06:58.285815Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"Now, lets get going with prescription refill process.","speak":"Now, lets get going with prescription refill process.","replyToId":"GWomcz8GjLjJQZu60J1amB-us|0000006"}
handleMessage eventType=next
Received message: {"type":"message","id":"GWomcz8GjLjJQZu60J1amB-us|0000009","timestamp":"2024-08-07T07:06:58.4965762Z","channelId":"directline","from":{"id":"testbot","name":"testbot"},"conversation":{"id":"GWomcz8GjLjJQZu60J1amB-us"},"text":"Tell me the prescription you would like to refill.","speak":"Tell me the prescription you would like to refill.","inputHint":"expectingInput","replyToId":"GWomcz8GjLjJQZu60J1amB-us|0000006"}
inside send post Activity
Caller Input: Posted activity, assigned ID GWomcz8GjLjJQZu60J1amB-us|0000006
Post activity completed
How can I have client see only messages meant for the client? It seems at the conversationUpdate event is generated twice - once for client and once for the bot...but how can I suppress this generation within the bot composer application which is where this application is built? Please assist.
Tasks
The text was updated successfully, but these errors were encountered: