[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年他的博士论文中提出来 ...
随机推荐
- GIMP类似于PhotoShop的开源免费软件
首先我们先看看他的界面如何,都有哪些功能!而且它支持多种平台,可以在MacOS.Windows.Linux操作系统上使用.非常值得推荐! 1.官方地址下载地址: https://www.gimp. ...
- 紫书 例题 10-14 UVa 12034(组合数+递推)
这道题有点类似动态规划,设答案为f(n) 第一个人有i个人,就有c(n,i)种可能 然后后面有f(n-i)种可能,所以相乘,然后枚举所有可能加起来就ok了. #include<cstdio> ...
- 05-数据类型转换(bool类型)
- jquery中$.get()提交和$.post()提交有区别
jquery中$.get()提交和$.post()提交有区别吗? 相同点:都是异步请求的方式来获取服务端的数据: 异同点: 1.请求方式不同:$.get() 方法使用GET方法来进行异步请求的.$.p ...
- 【UVA 437】The Tower of Babylon(拓扑排序+DP,做法)
[Solution] 接上一篇,在处理有向无环图的最长链问题的时候,可以在做拓扑排序的同时,一边做DP; 设f[i]表示第i个方块作为最上面的最高值; f[y]=max(f[y],f[x]+h[y]) ...
- BZOJ 2242 [SDOI2011]计算器 BSGS+高速幂+EXGCD
题意:id=2242">链接 方法: BSGS+高速幂+EXGCD 解析: BSGS- 题解同上.. 代码: #include <cmath> #include <c ...
- Zookeeper简单概念介绍
过去,每个应用都是一个CPU.一个主机上的单一系统.然而今天,随着大数据和云计算时代的到来,不论什么相互独立的程序都可以运行在多个计算机上.然而面临的问题是,协调这些集群的系统比在单一主机上要复杂的多 ...
- UI_UISegmentedControl 控件
创建控件 - (void)createSegmentControl { UISegmentedControl *segmentedControl = [[UISegmentedControl allo ...
- Simditor用法
一不小心接触到Simditor,瞬间被它优美极简的界面所吸引.Simditor是Tower开源的所见即所得的在线富文本编辑器. Simditor的理念是保持简单,避免过度的功能,每个特性都追求极致的用 ...
- DMA在FPGA的应用之我见
首先,来做一个简单的实验,利用DMA来实现on-chip-memory和SRAM之间的传输,同时也在做一个关于SRAM不同地址之间的传输. 一.硬件设计 1.首先设计自己的SOPC结构,包括CPU.j ...