Skip to content
This repository was archived by the owner on Dec 11, 2022. It is now read-only.

Commit a9e350b

Browse files
committed
Update README.md.
1 parent 61bac52 commit a9e350b

File tree

2 files changed

+89
-19
lines changed

2 files changed

+89
-19
lines changed

README.md

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Do you want to add more dynamic responses to your intents? Feel free to use this
2929

3030
### Clone this repository
3131

32-
- git clone TODO
32+
- git clone https://github.com/novalu/actions-on-google-typescript-template.git
3333

34-
### Install and configure Firebase CLI (https://developers.google.com/actions/tools/fulfillment-hosting)
34+
### Install and configure Firebase CLI
3535

3636
1. In your terminal run `npm i -g firebase-tools`
3737
2. Run command `firebase login`
@@ -47,6 +47,8 @@ Do you want to add more dynamic responses to your intents? Feel free to use this
4747
6. Choose (n) not to install dependencies
4848
7. You should see message "Firebase initialization complete!"
4949

50+
Note: more detailed instructions are available as this [Actions on Google guide](https://developers.google.com/actions/tools/fulfillment-hosting)
51+
5052
### Install project dependencies
5153

5254
1. Run command `npm i --only=dev`
@@ -107,36 +109,48 @@ You can test your manager by adding as a dependency to the `src/TestApp.ts` and
107109

108110
If you would to automatically reload and execute when source code is changed, run `gulp watch-changes` and in another terminal window run `gulp monitor-local`. If you make any changes in `.ts` files, project will be automatically recompiled using `tsc` and then executed with use of `nodemon`.
109111

110-
### Add a storage [WIP]
112+
### Add a storage
113+
114+
Sometimes you need a collection of items you want to use in your business logic. Creating a black-box storage is the best practice. You can use several storages which you can swap in dependency injection container, i.e. local storage with hardcoded collection of items and Firebase storage to fetch data from Firebase database.
111115

112116
#### Use Firebase database as a storage [WIP]
113117

118+
If you want to use your database in your Firebase project as a storage, create
119+
114120
* Add service-account.json
115121
* Fill database url to FirebaseUtils
116122

117-
### Misc
123+
### Add class to Dependency injection framework
118124

119-
* If you want to use external APIs, you must upgrade your Firebase account to Blaze plan
125+
Imagine you have an interface `DataStorage` and implementations `LocalDataStorage` and `FirebaseDataStorage`. Then you can these dependency as follows:
120126

121-
### Add class to Dependency injection framework
127+
* Add interface definition to `functions/src/di/types.ts` as shown here:
128+
129+
```typescript
130+
DataStorage: Symbol("DataStorage")
131+
```
122132

123-
* Add class to DI functions/src/di/types.ts as
133+
* Add binding to symbol from `types.ts` in `functions/src/di/baseContainer.ts`
124134

125135
```typescript
126-
CustomClass: Symbol("CustomClass")
136+
baseContainer.bind<DataStorage>(TYPES.DataStorage)
137+
.to(LocalDataStorage)
138+
.inSingletonScope();
127139
```
128140

129-
* Add binding to symbol from types.ts in functions/src/di/baseContainer.ts
141+
Above we've defined that `DataStorage` dependency will be resolved to `LocalDataStorage`. If you want to change resolve to different implementation, then you can change it here without the touching your business logic.
142+
143+
If you want to resolve class to instance of the same class, you can safely use this:
130144

131145
```typescript
132146
baseContainer.bind<CustomClass>(TYPES.CustomClass)
133147
.to(CustomClass)
134148
.inSingletonScope();
135149
```
136150

137-
### Use a dependency from Dependency injection framework
151+
### Use a dependency from dependency injection framework
138152

139-
If you want to use class from Dependency injection, then define it in a constructor:
153+
If you want to use class from dependency injection, then define it in a constructor with `@inject` annotation:
140154

141155
```typescript
142156
constructor(
@@ -146,20 +160,76 @@ constructor(
146160

147161
Then you can use member property `customClassInstance` in this class.
148162

149-
### Get something from network [WIP]
163+
### Use third-party API
164+
165+
If you want to communicate with third-party API to retrieve or post data, you can use class `src/utils/network/Request`. This class use well known `superagent` library to make network requests. Define the `Request` dependency in you manager and then use it e.g. as follows:
166+
167+
```typescript
168+
try {
169+
const rates = await this.request.getJson("https://api.exchangeratesapi.io/latest");
170+
rates.body // JSON
171+
} catch (err) {
172+
this.logger.error(err);
173+
}
174+
```
150175

151-
### Send message to your Slack [WIP]
176+
Note: If you want to use external APIs, you must upgrade your Firebase account to Blaze plan.
152177

153-
### Directory structure [WIP]
178+
### Use the Logger class
179+
180+
If you want to debug to your console and Firebase functions log, you can use class `src/utils/log/Logger` as a dependency. Then you can call methods `trace`, `debug`, `info`, `warn`, `error`, `fatal`. Logger internally use `fancylog` library.
181+
182+
### Send message to your Slack
183+
184+
If you want to send message to your Slack channel as a part of your fulfillment, then create an Incoming Webhook as instructed here: https://api.slack.com/incoming-webhooks. Then create your slack hook as an implementation of `src/utils/slack/hooks/SlackHook.ts` in `src/utils/slack/hooks/impl`. Then use `SlackUtils.sendMessage` method.
185+
186+
### Directory structure
187+
188+
```
189+
app
190+
└───functions ... this functions project will be deployed to Firebase Functions
191+
│ └───src
192+
│ │ └───config ... configuration files
193+
│ │ └───di ... dependency injection configuration for the functions project
194+
│ │ └───fulfillments ... fulfillments logic
195+
│ │ └───managers ... classes with business logic for your fulfillments
196+
│ │ └───model ... models which is used in the project
197+
│ │ └───storages ... storages for the models
198+
│ │ └───utils ... utilities for logging, networking, firebase integration, ...
199+
│ │ │ FunctionsApp.ts
200+
│ │ │ main.ts
201+
│ │ package.json ... dependencies for the functions project
202+
│ │ tsconfig.json ... TypeScript configuration for the functions project
203+
└───src ... main project for testing locally
204+
│ └───di ... dependency injection for the main project
205+
│ │ │ container.ts
206+
│ │ │ types.ts
207+
│ │ main.ts ... entry point for executing TestApp
208+
│ │ TestApp.ts ... main class for testing locally
209+
| firebase.json ... location of the functions project
210+
│ gulpfile.js ... configuration for the Gulp build system
211+
│ nodemon.js ... configuration for the Nodemon, tool for monitoring changes in code
212+
│ package.json ... dependencies for the main project
213+
│ README.md ... this readme
214+
│ tsconfig.json ... TypeScript configuration for the main project
215+
```
154216

155217
### See Firebase Functions log
156218

157219
If your Firebase function is not working, most likely you'll find some useful info in Firebase console. Choose `Develop > Functions > Log`
158220

159-
## Background
221+
## Actions build with this project
222+
223+
- (your project)
224+
225+
## Support
160226

161-
This project was created as a base for project at "Ok, Google, do a hackathon" which was held in December 2018 in coworking center [Vault 42](http://www.vault42.cz), Olomouc, Czech Republic. Our team built voice receptionist which is now in use by the coworking center.
227+
Feel free to use this project for building your actions. Pull request welcome.
228+
229+
If you like to support me, buy me a beer using this PayPal link: [paypal.me/novalu](https://www.paypal.com/paypalme/my/profile). Thank you!
230+
231+
## Background
162232

163-
![Vault 42](https://www.vault42.cz/wp-content/uploads/2018/10/Vault42_interioriorphoto_byJK-10.png)
233+
This project was created as a base for project at "[Ok, Google, do a hackathon](https://www.meetup.com/GDG-Olomouc/events/256177266/)" which was held in December 2018 in coworking center [Vault 42](http://www.vault42.cz), Olomouc, Czech Republic. Our team built voice receptionist which is now in use by the coworking center.
164234

165-
## Support [WIP]
235+
![Vault 42](https://www.vault42.cz/wp-content/uploads/2018/10/Vault42_interioriorphoto_byJK-10.png)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "actions-on-google-typescript-template",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "",
55
"main": "src/main.js",
66
"scripts": {

0 commit comments

Comments
 (0)