Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The directLine JS client prints welcome messages twice. #9764

Open
3 tasks
dushyantg123 opened this issue Aug 7, 2024 · 0 comments
Open
3 tasks

The directLine JS client prints welcome messages twice. #9764

dushyantg123 opened this issue Aug 7, 2024 · 0 comments
Labels
Needs-triage A new issue that require triage Type: Bug Something isn't working

Comments

@dushyantg123
Copy link

dushyantg123 commented Aug 7, 2024

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

  • 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.

Tasks

No tasks being tracked yet.
@dushyantg123 dushyantg123 added Needs-triage A new issue that require triage Type: Bug Something isn't working labels Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-triage A new issue that require triage Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant