Chatbot using default question and choosing option
Loading
Chatbot using default question and choosing option
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 28, 2023, 6:50 AM EDT
npm install botframework-webchatimport { BotFrameworkWebChatModule } from 'botframework-webchat'; @NgModule({ imports: [BotFrameworkWebChatModule], // ... }) export class AppModule { }import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` `, }) export class AppComponent { directLineToken = 'YOUR_DIRECT_LINE_TOKEN'; userID = 'YOUR_USER_ID'; username = 'YOUR_USERNAME'; }Here is an example of how to create a simple bot with a default question and options:
const { ActivityHandler } = require('botbuilder'); class MyBot extends ActivityHandler { async onMessage(context) { const reply = { type: 'message' }; switch (context.activity.text.toLowerCase()) { case 'hi': reply.text = 'Hello there! How can I assist you today?'; reply.suggestedActions = { actions: [ { type: 'imBack', title: 'Check account balance', value: 'Check account balance' }, { type: 'imBack', title: 'Make a transfer', value: 'Make a transfer' }, { type: 'imBack', title: 'Cancel transaction', value: 'Cancel transaction' } ] }; break; default: reply.text = 'I didn\'t understand. Could you please rephrase your request?'; break; } await context.sendActivity(reply); } } module.exports.MyBot = MyBot;In this example, the bot responds with a default question "Hello there! How can I assist you today?" and offers the user three options: "Check account balance", "Make a transfer", and "Cancel transaction". If the user types anything else, the bot responds with "I didn't understand. Could you please rephrase your request?".
You can then deploy your bot to a hosting service or integrate it with your own web application using the BotFrameworkWebChat component.
Note: This is just a simple example. Machine learning can be used to create more advanced chatbots that can learn from user interactions and provide more personalized responses.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 29, 2023, 8:08 PM EDT
Part1:
To create a chatbot with default questions and options using Angular:
1. Create a new Angular project using the Angular CLI command: `ng new my-chatbot-project`.
2. Install the necessary dependencies, such as the Angular Material library, which provides UI components for building a chatbot interface: `ng add @angular/material`.
3. Define the chatbot interface using HTML and CSS, which can include a chat window, input box, and buttons for selecting options.
4. Create a component to handle the chatbot logic, which can include default questions and options. The component can use the Angular HttpClient module to make HTTP requests to a backend API or a third-party chatbot service.
5. Implement the chatbot logic using the component, which can include:
- Displaying the default question when the chatbot is opened.
- Detecting the user's input and sending it to the backend API or third-party service.
- Receiving the response from the API or service and displaying it in the chat window.
- Providing buttons or other UI elements for the user to select options, and sending those selections to the backend API or service.
- Handling errors and displaying error messages if there are issues with the API or service.
6. Deploy the Angular application to a hosting service, such as Firebase or Heroku, so that users can access the chatbot through a URL.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 29, 2023, 8:09 PM EDT
Part 2:
Sample code snippet :
import { Component } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Component({ selector: 'app-chatbot', templateUrl: './chatbot.component.html', styleUrls: ['./chatbot.component.css'] }) export class ChatbotComponent { message: string; options = ['Option 1', 'Option 2', 'Option 3']; selectedOption: string; constructor(private http: HttpClient) {} ngOnInit() { this.message = 'What can I help you with today?'; } sendMessage() { if (this.selectedOption) { // User selected an option this.http.post('http://my-api.com/chatbot', { option: this.selectedOption }).subscribe(response => { // Display response in chat window this.message = response.message; }); } else { // User entered a message this.http.post('http://my-api.com/chatbot', { message: this.message }).subscribe(response => { // Display response in chat window and provide options this.message = response.message; this.options = response.options; }); } } selectOption(option: string) { this.selectedOption = option; } }This component uses the Angular HttpClient module to make HTTP requests to a backend API, which can handle the chatbot logic and return responses based on the user's input. The `options` array provides a list of options for the user to choose from, which can be displayed as buttons or other UI elements in the chat window.