[Angular] Make a chatbot with DialogFlow
Register a account on https://console.dialogflow.com/api-client/
"Creat a intent" -- you can custom your message here.
"Small Talks" -- the default message from the DialogFlow.
Install:
yarn add api-ai-javascript
Put your API token to the app.
Create a servie:
import {Injectable} from '@angular/core';
import {environment} from '../../../environments/environment';
import {ApiAiClient} from 'api-ai-javascript';
import {Message} from '../models/bot-message';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
@Injectable()
export class ChatbotService {
readonly token = environment.dialogFlow.bot;
readonly client = new ApiAiClient({accessToken: this.token});
conversation = new BehaviorSubject<Message[]>([]);
constructor() {
}
// Adds message to the source
update(msg: Message) {
this.conversation.next([msg]);
}
// Send and receives message via DialogFlow
converse(msg: string) {
const userMessage = new Message(msg, 'user');
this.update(userMessage);
this.client.textRequest(msg)
.then((res) => {
const speech = res.result.fulfillment.speech;
const botMessage = new Message(speech, 'bot');
this.update(botMessage);
}).catch((err) => {
console.error(err);
});
}
}
Component:
import {Component, OnInit} from '@angular/core';
import {ChatbotService} from '../../services/chatbot.service';
import {Observable} from 'rxjs/Observable';
import {Message} from '../../models/bot-message';
import 'rxjs/add/operator/scan';
@Component({
selector: 'chat-dialog',
templateUrl: './chat-dialog.component.html',
styleUrls: ['./chat-dialog.component.scss']
})
export class ChatDialogComponent implements OnInit {
messages$: Observable<Message[]>;
formValue: string;
constructor(private chatService: ChatbotService) {
}
ngOnInit() {
this.messages$ = this.chatService.conversation
.asObservable()
.scan((acc, curr) => acc.concat(curr));
}
sendMessage() {
if (!this.formValue) {
return;
}
this.chatService.converse(this.formValue);
this.formValue = '';
}
}
Template:
<mat-card>
<mat-card-title class="title">
Umi
</mat-card-title>
<hr />
<mat-card-content class="content">
<div fxLayout="column"> <ng-container
*ngFor="let message of messages$ | async">
<div class="message" [ngClass]="{'from': message.sentBy === 'bot',
'to': message.sentBy === 'user'}">
{{message.content}}
</div>
</ng-container> </div>
</mat-card-content>
<mat-card-actions>
<div fxLayout="row" fxLayoutAlign="space-between">
<mat-form-field fxFlex="auto">
<textarea
matInput
type="textarea"
placeholder="What you want to say?..."
[(ngModel)]="formValue"
(keyup.enter)="sendMessage()"></textarea>
</mat-form-field>
<div fxLayout="column" fxLayoutAlign="center center">
<button
color="primary"
class="send-btn"
mat-mini-fab
[disabled]="!formValue"
(click)="sendMessage()">
<mat-icon>
send
</mat-icon>
</button>
</div>
</div>
</mat-card-actions>
</mat-card>
[Angular] Make a chatbot with DialogFlow的更多相关文章
- Chatbot:
讲清了一些最最基本的概念 - Intents(意图)和Entities(关键字) - 基于意图(Intent-based)的对话 和 基于流程(Flow-based)的对话 聊天机器人教学:使用Di ...
- How To Use Rocketbots As A Dialogflow CRM
Ever wished you had a CRM for Dialogflow? We did too, so we built one. This is a best practices arti ...
- Angular杂谈系列1-如何在Angular2中使用jQuery及其插件
jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...
- Angular企业级开发(5)-项目框架搭建
1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...
- TypeScript: Angular 2 的秘密武器(译)
本文整理自Dan Wahlin在ng-conf上的talk.原视频地址: https://www.youtube.com/watch?v=e3djIqAGqZo 开场白 开场白主要分为三部分: 感谢了 ...
- angular实现统一的消息服务
后台API返回的消息怎么显示更优雅,怎么处理才更简洁?看看这个效果怎么样? 自定义指令和服务实现 自定义指令和服务实现消息自动显示在页面的顶部,3秒之后消失 1. 显示消息 这种显示消息的方式是不是有 ...
- div实现自适应高度的textarea,实现angular双向绑定
相信不少同学模拟过腾讯的QQ做一个聊天应用,至少我是其中一个. 过程中我遇到的一个问题就是QQ输入框,自适应高度,最高高度为3row. 如果你也像我一样打算使用textarea,那么很抱歉,你一开始就 ...
- Angular企业级开发-AngularJS1.x学习路径
博客目录 有链接的表明已经完成了,其他的正在建设中. 1.AngularJS简介 2.搭建Angular开发环境 3.Angular MVC实现 4.[Angular项目目录结构] 5.[SPA介绍] ...
- Angular企业级开发(4)-ngResource和REST介绍
一.RESTful介绍 RESTful维基百科 REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来 ...
随机推荐
- 今日SGU 5.25
SGU 194 题意:无源汇有上下界的最大流 收获:https://wenku.baidu.com/view/0f3b691c59eef8c75fbfb35c.html #include<bit ...
- malloc()和free()的原理及实现
在C语言中只能通过malloc()和其派生的函数进行动态的申请内存,而实现的根本是通过系统调用实现的(在linux下是通过sbrk()系统调用实现). malloc()到底从哪里得到了内存空间?答案是 ...
- 【转】C# HttpWebRequest提交数据方式
[转]C# HttpWebRequest提交数据方式 HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性.这两个类位 于Sy ...
- ArcGIS api for javascript——地图配置-定制导航箭头
描述 本例展示了如何为平移地图包括自定义的按钮.地图被放置在一个宽3列和高3行的HTML表格的固定宽度中心单元.表格里剩余的单元放置标签为8个主要方向的缩写的按钮.每个按钮调用地图上不同的平移函数.例 ...
- java 自己定义异常,记录日志简单说明!留着以后真接复制
log4j 相关配制说明:http://blog.csdn.net/liangrui1988/article/details/17435139 自己定义异常 package org.rui.Excep ...
- Docker安装配置教程
Docker公开课 1 Docker介绍 1.1 Docker是什么 云计算\云服务 IAAS(基础设施即服务).PAAS(平台即服务).SAAS(软件即服务) Docker到底是什么呢? Docke ...
- AtCoder Beginner Contest 067 C - Splitting Pi
C - Splitting Pile Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Snu ...
- Hexo 搭建
前提 最近准备搭建一个博客平台,也看了很多开源的博客框架.比如Solo.wordpress等框架.自已曾经也在cnblog发布过几篇文章.东写写西写写.杂乱无章的.后续可以写一个自动同步各平台的程序~ ...
- 【Django】模板系统
目录 一.变量 二.过滤器 Filters 2. length 3. filesizeformat 4. slice 5. add 6. first.last 7. join 8. truncatec ...
- WinSocket 编程
套接字 套接字指通信双方在通信时所使用的通信点(Endpoint),通信的双方通过通信点来交换信息和数据.不同类型的通信会使用不同的类型通信点,比如对于电话通信而言,通信点就是电话号码和分机号码的组合 ...