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的更多相关文章

  1. Chatbot:

    讲清了一些最最基本的概念 - Intents(意图)和Entities(关键字) - 基于意图(Intent-based)的对话 和 基于流程(Flow-based)的对话  聊天机器人教学:使用Di ...

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

  3. Angular杂谈系列1-如何在Angular2中使用jQuery及其插件

    jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...

  4. Angular企业级开发(5)-项目框架搭建

    1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...

  5. TypeScript: Angular 2 的秘密武器(译)

    本文整理自Dan Wahlin在ng-conf上的talk.原视频地址: https://www.youtube.com/watch?v=e3djIqAGqZo 开场白 开场白主要分为三部分: 感谢了 ...

  6. angular实现统一的消息服务

    后台API返回的消息怎么显示更优雅,怎么处理才更简洁?看看这个效果怎么样? 自定义指令和服务实现 自定义指令和服务实现消息自动显示在页面的顶部,3秒之后消失 1. 显示消息 这种显示消息的方式是不是有 ...

  7. div实现自适应高度的textarea,实现angular双向绑定

    相信不少同学模拟过腾讯的QQ做一个聊天应用,至少我是其中一个. 过程中我遇到的一个问题就是QQ输入框,自适应高度,最高高度为3row. 如果你也像我一样打算使用textarea,那么很抱歉,你一开始就 ...

  8. Angular企业级开发-AngularJS1.x学习路径

    博客目录 有链接的表明已经完成了,其他的正在建设中. 1.AngularJS简介 2.搭建Angular开发环境 3.Angular MVC实现 4.[Angular项目目录结构] 5.[SPA介绍] ...

  9. Angular企业级开发(4)-ngResource和REST介绍

    一.RESTful介绍 RESTful维基百科 REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来 ...

随机推荐

  1. ZOJ-3261 Connections in Galaxy War 并查集 离线操作

    题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...

  2. 数据库Flashback学习

    最近更新时间:2018/12/18 适用场景 数据库升级.快速构建测试环境.DG中重建主库 前置条件 1. ARCHIVELOG 模式 数据库为 mount 状态下开启,最好指定archive log ...

  3. 紫书 习题 10-10 UVa 1645(递推)

    除了根节点以外,有n-1个节点,然后就看n-1的因数有那些,所有因数加起来(递推)就好了. #include<cstdio> #define REP(i, a, b) for(int i ...

  4. Object-C,遍历目录

    最近武汉连续下雨很多天,降温了2次,温度一下子由28度到14度,再到8度,手太冷了. 加上最近发生了一些比较棘手的家庭琐事,最近没心情继续学习Object-C. 后来,我想明白了,心情不好的时候,还是 ...

  5. FZU 1202 信与信封问题 二分图匹配

    找匹配中的关键边. 做法: 拆掉一条匹配边,然后对边两边的点做一次增广,如果可以增广,那么此边不是关键边,否则是关键边. 详情可以参见:http://www.docin.com/p-109868135 ...

  6. Maven 工程 POM.XML文件最全详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. 高性能网络编程 - select系统调用

         IO复用使得程序可以同一时候监听多个文件描写叙述符,比方client须要同一时候处理用户输入和网络连接,server端须要同一时候处理监听套接字和连接套接字,select系统调用可以使得我们 ...

  8. 如何在IDEA中创建web项目并且部署到Tomcat中

    步骤1:File->New Project, 步骤2:选择Project SDK为1.7 -> Next -> Finish(JDK)我自己的是1.7(这里的project,跟ecl ...

  9. thinkphp路由的作用

    thinkphp路由的作用 问题 请问一下什么是thinkPHP路由,路由有什么作用?谢谢 解答 网络访问地址从来都是映射访问的,最初是这样,主机名(电脑名称)=>ip地址(如局域网192.16 ...

  10. 2.cocos设置背景图片

    在bool HelloWorld::init()中加入如下代码 auto bg = Sprite::create("1.jpg"); if (bg) { bg->setPos ...