[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年他的博士论文中提出来 ...
随机推荐
- Android布局文件的载入过程分析:Activity.setContentView()源代码分析
大家都知道在Activity的onCreate()中调用Activity.setContent()方法能够载入布局文件以设置该Activity的显示界面.本文将从setContentView()的源代 ...
- BZOJ 2982 combination Lucas定理
题目大意:发上来就过不了审核了--总之大意就是求C(n,m) mod 10007 m,n∈[1,2*10^8] 卢卡斯定理:C(n,m)=C(n%p,m%p)*C(n/p,m/p) mod p 要求p ...
- swift具体解释之八---------------下标脚本
swift具体解释之八-----下标脚本 下标脚本 能够定义在类(Class).结构体(structure)和枚举(enumeration)这些目标中.能够觉得是訪问对象.集合或序列的快捷方式.不须要 ...
- 根据on,获取选中的元素
打钩的元素有个on的class. // 礼物发送 function send_gift() { var type,diamond,name,id; var list = $('.live .on'); ...
- Python3小白初体验
三层循环嵌套,以后优化 data = { "北京":{ "a":{ ", " }, "b":{ ", &quo ...
- Function的一些结论与eval函数.
1.1 函数的创建方式 1 函数声明 2 函数表达式 3 new Function // 1 function foo() {} // 2 var foo = function() {}; // 3 ...
- jFinal 关联数据库操作
1.建数据库(我用的是oracle数据库,其他的相对也差不多) -- Create table create table CLASSES ( classesid NUMBER not null, cl ...
- BZOJ 4236 set乱搞
思路: 取个差 在set里面找 更新 (这个用map更好吧 但是我不会--) //By SiriusRen #include <set> #include <cstdio> # ...
- Codefroces B. T-primes
http://codeforces.com/problemset/problem/230/B B. T-primes time limit per test 2 seconds memory limi ...
- 添加 validate 验证规则
上篇文章链接:http://blog.csdn.net/chenmoimg_/article/details/71191476 修改 msg.js $.extend($.validator.messa ...