This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from broidHQ/int/google-assistant
Welcome Google Assistant to Broid family
- Loading branch information
Showing
23 changed files
with
5,295 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
[![npm][npm]][npm-url] | ||
[![node][node]][node-url] | ||
[![deps][deps]][deps-url] | ||
[![tests][tests]][tests-url] | ||
[![bithound][bithound]][bithound-url] | ||
[![bithoundscore][bithoundscore]][bithoundscore-url] | ||
[![nsp-checked][nsp-checked]][nsp-checked-url] | ||
|
||
# Broid Google Assistant Integration | ||
|
||
Broid Integrations is an open source project providing a suite of Activity Streams 2 libraries for unified communications among a vast number of communication platforms. | ||
|
||
> Connect your App to Multiple Messaging Channels with One OpenSource Language. | ||
[![gitter](https://badges.gitter.im/broidHQ/broid.svg)](https://t.broid.ai/c/Blwjlw?utm_source=github&utm_medium=readme&utm_campaign=top&link=gitter) | ||
|
||
## Getting started | ||
|
||
### Install | ||
|
||
```bash | ||
npm install --save broid-google-assistant | ||
``` | ||
|
||
### Connect to Google Assistant | ||
|
||
```javascript | ||
import broidGoogleAssistant from 'broid-google-assistant' | ||
|
||
const googleAssistant = new broidGoogleAssistant({ | ||
username: '<your_action_name_here>', | ||
http: { | ||
port: 8080, | ||
host: "0.0.0.0" | ||
} | ||
}) | ||
|
||
googleAssistant.connect() | ||
.subscribe({ | ||
next: data => console.log(data), | ||
error: err => console.error(`Something went wrong: ${err.message}`), | ||
complete: () => console.log('complete'), | ||
}) | ||
``` | ||
|
||
**Options availables** | ||
|
||
| name | Type | default | Description | | ||
| --------------- |:--------:| :--------: | --------------------------| | ||
| serviceID | string | random | Arbitrary identifier of the running instance | | ||
| logLevel | string | `info` | Can be : `fatal`, `error`, `warn`, `info`, `debug`, `trace` | | ||
| username | string | | Your action name here | | ||
| http | object | `{ "port": 8080, "http": "0.0.0.0" }` | WebServer options (`host`, `port`) | | ||
|
||
### Receive a message | ||
|
||
```javascript | ||
googleAssistant.listen() | ||
.subscribe({ | ||
next: data => console.log(`Received message: ${data}`), | ||
error: err => console.error(`Something went wrong: ${err.message}`), | ||
complete: () => console.log('complete'), | ||
}) | ||
``` | ||
|
||
### Post a message | ||
|
||
To send a message, the format should use the [broid-schemas](https://github.com/broidhq/broid-schemas). | ||
|
||
```javascript | ||
const message_formated = '...' | ||
|
||
googleAssistant.send(message_formated) | ||
.then(() => console.log("ok")) | ||
.catch(err => console.error(err)) | ||
``` | ||
|
||
## Examples of messages | ||
|
||
### Message received | ||
|
||
- A message received from Sally | ||
|
||
```json | ||
{ | ||
"@context": "https://www.w3.org/ns/activitystreams", | ||
"published": 1483677146, | ||
"type": "Create", | ||
"generator": { | ||
"id": "67c9cb10-8a74-42c8-ba55-294d0447cdf9", | ||
"type": "Service", | ||
"name": "google-assistant" | ||
}, | ||
"actor": { | ||
"id": "IL12J7nWa/2zothSEg46DsY0q7o/H9FUis/YGdp64te=", | ||
"type": "Person" | ||
}, | ||
"target": { | ||
"id": "my_action_name", | ||
"type": "Person", | ||
"name": "my_action_name" | ||
}, | ||
"object": { | ||
"type": "Note", | ||
"id": "1484625833669", | ||
"content": "Hello world" | ||
} | ||
} | ||
``` | ||
|
||
- A message received from Sally with Arguments | ||
|
||
```json | ||
{ | ||
"@context": "https://www.w3.org/ns/activitystreams", | ||
"published": 1483677146, | ||
"type": "Create", | ||
"generator": { | ||
"id": "67c9cb10-8a74-42c8-ba55-294d0447cdf9", | ||
"type": "Service", | ||
"name": "google-assistant" | ||
}, | ||
"actor": { | ||
"id": "IL12J7nWa/2zothSEg46DsY0q7o/H9FUis/YGdp64te=", | ||
"type": "Person" | ||
}, | ||
"target": { | ||
"id": "my_action_name", | ||
"type": "Person", | ||
"name": "my_action_name" | ||
}, | ||
"object": { | ||
"type": "Note", | ||
"id": "1484625833669", | ||
"content": "Hello world", | ||
"context": [ | ||
{ | ||
"content": "argValue", | ||
"name": "argName", | ||
"type": "Object" | ||
}, | ||
{ | ||
"content": "argValue1", | ||
"name": "argName1", | ||
"type": "Object" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
### Send a message | ||
|
||
- Send a simple message | ||
|
||
```json | ||
{ | ||
"@context": "https://www.w3.org/ns/activitystreams", | ||
"type": "Create", | ||
"generator": { | ||
"id": "f6e92eb6-f69e-4eae-8158-06613461cf3a", | ||
"type": "Service", | ||
"name": "google-assistant" | ||
}, | ||
"object": { | ||
"type": "Note", | ||
"content": "hello world" | ||
}, | ||
"to": { | ||
"id": "IL12J7nWa/2zothSEg46DsY0q7o/H9FUis/YGdp64te=", | ||
"type": "Person" | ||
} | ||
} | ||
``` | ||
|
||
# Contributing to Broid | ||
|
||
Broid is an open source project. Broid wouldn't be where it is now without contributions by the community. Please consider forking Broid to improve, enhance or fix issues. If you feel like the community will benefit from your fork, please open a pull request. | ||
|
||
And because we want to do the better for you. Help us improving Broid by | ||
sharing your feedback on our [Integrations GitHub Repo](https://github.com/broidhq/integrations) and let's build Broid together! | ||
|
||
## Code of Conduct | ||
|
||
Make sure that you're read and understand the [Code of Conduct](http://contributor-covenant.org/version/1/2/0/). | ||
|
||
## Copyright & License | ||
|
||
Copyright (c) 2016-2017 Broid.ai | ||
|
||
This project is licensed under the AGPL 3, which can be | ||
[found here](https://www.gnu.org/licenses/agpl-3.0.en.html). | ||
|
||
[npm]: https://img.shields.io/badge/npm-broid-green.svg?style=flat | ||
[npm-url]: https://www.npmjs.com/~broid | ||
|
||
[node]: https://img.shields.io/node/v/broid-slack.svg | ||
[node-url]: https://nodejs.org | ||
|
||
[deps]: https://img.shields.io/badge/dependencies-checked-green.svg?style=flat | ||
[deps-url]: #integrations | ||
|
||
[tests]: https://img.shields.io/travis/broidHQ/integrations/master.svg | ||
[tests-url]: https://travis-ci.org/broidHQ/integrations | ||
|
||
[bithound]: https://img.shields.io/bithound/code/github/broidHQ/integrations.svg | ||
[bithound-url]: https://www.bithound.io/github/broidHQ/integrations | ||
|
||
[bithoundscore]: https://www.bithound.io/github/broidHQ/integrations/badges/score.svg | ||
[bithoundscore-url]: https://www.bithound.io/github/broidHQ/integrations | ||
|
||
[nsp-checked]: https://img.shields.io/badge/nsp-checked-green.svg?style=flat | ||
[nsp-checked-url]: https://nodesecurity.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"use strict"; | ||
const Promise = require("bluebird"); | ||
const broid_schemas_1 = require("broid-schemas"); | ||
const broid_utils_1 = require("broid-utils"); | ||
const uuid = require("node-uuid"); | ||
const R = require("ramda"); | ||
const Rx_1 = require("rxjs/Rx"); | ||
const parser_1 = require("./parser"); | ||
const webHookServer_1 = require("./webHookServer"); | ||
const events = [ | ||
"assistant.intent.action.MAIN", | ||
"assistant.intent.action.TEXT", | ||
"assistant.intent.action.PERMISSION", | ||
]; | ||
class Adapter { | ||
constructor(obj) { | ||
this.serviceID = obj && obj.serviceID || uuid.v4(); | ||
this.logLevel = obj && obj.logLevel || "info"; | ||
this.token = obj && obj.token || null; | ||
this.tokenSecret = obj && obj.tokenSecret || null; | ||
this.username = obj && obj.username || ""; | ||
const HTTPOptions = { | ||
host: "127.0.0.1", | ||
port: 8080, | ||
}; | ||
this.HTTPOptions = obj && obj.http || HTTPOptions; | ||
this.HTTPOptions.host = this.HTTPOptions.host || HTTPOptions.host; | ||
this.HTTPOptions.port = this.HTTPOptions.port || HTTPOptions.port; | ||
this.parser = new parser_1.default(this.serviceID, this.username, this.logLevel); | ||
this.logger = new broid_utils_1.Logger("adapter", this.logLevel); | ||
} | ||
users() { | ||
return Promise.reject(new Error("Not supported")); | ||
} | ||
channels() { | ||
return Promise.reject(new Error("Not supported")); | ||
} | ||
serviceId() { | ||
return this.serviceID; | ||
} | ||
connect() { | ||
if (this.connected) { | ||
return Rx_1.Observable.of({ type: "connected", serviceID: this.serviceId() }); | ||
} | ||
this.connected = true; | ||
if (!this.username || this.username === "") { | ||
return Rx_1.Observable.throw(new Error("Username should exist.")); | ||
} | ||
this.webhookServer = new webHookServer_1.default(this.HTTPOptions, this.logLevel); | ||
R.forEach((event) => this.webhookServer.addIntent(event), events); | ||
this.webhookServer.listen(); | ||
return Rx_1.Observable.of(({ type: "connected", serviceID: this.serviceId() })); | ||
} | ||
disconnect() { | ||
return Promise.reject(new Error("Not supported")); | ||
} | ||
listen() { | ||
if (!this.webhookServer) { | ||
return Rx_1.Observable.throw(new Error("No webhookServer found.")); | ||
} | ||
const fromEvents = R.map((event) => Rx_1.Observable.fromEvent(this.webhookServer, event), events); | ||
return Rx_1.Observable.merge(...fromEvents) | ||
.mergeMap((normalized) => this.parser.parse(normalized)) | ||
.mergeMap((parsed) => this.parser.validate(parsed)) | ||
.mergeMap((validated) => { | ||
if (!validated) { | ||
return Rx_1.Observable.empty(); | ||
} | ||
return Promise.resolve(validated); | ||
}); | ||
} | ||
send(data) { | ||
this.logger.debug("sending", { message: data }); | ||
return broid_schemas_1.default(data, "send") | ||
.then(() => { | ||
let ssml = false; | ||
const content = R.path(["object", "content"], data) | ||
|| R.path(["object", "name"], data); | ||
const type = R.path(["object", "type"], data); | ||
if (content.startsWith("<speak>") && content.endsWith("</speak>")) { | ||
ssml = true; | ||
} | ||
const noInputs = []; | ||
if (type === "Note") { | ||
return this.webhookServer.send(ssml, content, noInputs) | ||
.then(() => ({ type: "sent", serviceID: this.serviceId() })); | ||
} | ||
return Promise.reject(new Error("Note is only supported.")); | ||
}); | ||
} | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Adapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
const adapter_1 = require("./adapter"); | ||
module.exports = adapter_1.default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"use strict"; |
Oops, something went wrong.