Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
homerchen19 committed Dec 5, 2017
0 parents commit eaa8eaf
Show file tree
Hide file tree
Showing 14 changed files with 4,313 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"presets": [
[
"env",
{
"targets": {
"node": "7.6"
}
}
]
],
"plugins": ["transform-class-properties", "transform-object-rest-spread"]
}
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_size = 2

[*.md]
trim_trailing_whitespace = false
24 changes: 24 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
parser: 'babel-eslint',
extends: ['yoctol-base', 'prettier'],
env: {
node: true,
jest: true,
jasmine: true,
},
plugins: ['prettier'],
rules: {
'class-methods-use-this': 'off',
'consistent-return': 'off',
'global-require': 'off',
'no-param-reassign': 'off',
'prefer-destructuring': 'off',
'prettier/prettier': [
'error',
{
trailingComma: 'es5',
singleQuote: true,
},
],
},
};
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
node_modules
npm-debug.log*
yarn-error.log
coverage
junit.xml
lib
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: node_js
node_js:
- "stable"
- "9"
- "8"
cache:
yarn: true
directories:
- "node_modules"
script:
- npm run lint
- npm run testonly:cov

notifications:
email:
on_success: never
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 0.1.0 / 2017-12-05

* [new] Support express middleware
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017-present Yoctol (github.com/bottenderjs/bottender-chatbase)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Bottender Chatbase

[![npm](https://img.shields.io/npm/v/bottender-chatbase.svg?style=flat-square)](https://www.npmjs.com/package/bottender-chatbase)
[![Build Status](https://travis-ci.org/bottenderjs/bottender-chatbase.svg?branch=master)](https://travis-ci.org/bottenderjs/bottender-chatbase)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

> Middleware for using [Chatbase](https://chatbase.com/welcome) with
> [Bottender](https://github.com/Yoctol/bottender).
## Installation

```sh
npm install bottender-chatbase
```

## Example

```js
const { MessengerBot } = require('bottender');
const { createServer } = require('bottender/express');
const chatbaseMiddleware = require('bottender-chatbase/express');

const bot = new MessengerBot({
accessToken: '__FILL_YOUR_TOKEN_HERE__',
appSecret: '__FILL_YOUR_SECRET_HERE__',
});

bot.onEvent(async context => {
await context.sendText('Hello World');
});

const server = createServer(bot, {
verifyToken: '__FILL_YOUR_VERIFY_TOKEN_HERE__',
webhookMiddleware: chatbaseMiddleware(bot, {
apiKey: '__FILL_YOUR_CHATBASE_KEY_HERE__',
platform: 'Facebook',
}),
});

server.listen(5000, () => {
console.log('server is running on 5000 port...');
});
```

## Contributing

Pull Requests and issue reports are welcome. You can follow steps below to
submit your pull requests:

Fork, then clone the repo:

```sh
git clone git@github.com:your-username/bottender-chatbase.git
```

Install the dependencies:

```sh
cd bottender-chatbase
yarn
```

Make sure the tests pass (including eslint, flow checks and jest tests):

```sh
yarn test
```

Make your changes and tests, and make sure the tests pass.

## License

MIT © [Yoctol](https://github.com/bottenderjs/bottender-chatbase)
1 change: 1 addition & 0 deletions express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/express').default;
72 changes: 72 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "bottender-chatbase",
"version": "0.1.0",
"description": "Middleware for using Chatbase with Bottender.",
"main": "lib/index.js",
"keywords": [
"bottender",
"chatbase"
],
"license": "MIT",
"engines": {
"node": ">=7.6"
},
"repository": {
"type": "git",
"url": "https://github.com/bottenderjs/bottender-chatbase.git"
},
"files": [
"lib",
"express.js",
"koa.js",
"micro.js",
"restify.js"
],
"scripts": {
"build": "npm run clean && babel src -d lib --ignore __tests__",
"clean": "rimraf lib",
"lint": "eslint src",
"lint:fix": "npm run lint -- --fix",
"lint:staged": "lint-staged",
"precommit": "lint-staged",
"test": "npm run lint:fix && npm run testonly",
"testonly": "jest",
"testonly:cov": "jest --coverage --runInBand --forceExit",
"testonly:watch": "jest --watch",
"preversion": "npm test",
"prepublish": "npm run build"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^8.0.3",
"babel-jest": "^21.2.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"eslint": "^4.12.1",
"eslint-config-prettier": "^2.9.0",
"eslint-config-yoctol-base": "^0.14.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-prettier": "^2.3.1",
"husky": "^0.14.3",
"jest": "^21.2.1",
"lint-staged": "^6.0.0",
"prettier": "^1.8.2",
"rimraf": "^2.6.2"
},
"jest": {
"testEnvironment": "node",
"timers": "fake",
"resetModules": true,
"resetMocks": true
},
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
]
},
"dependencies": {
"@google/chatbase": "^1.0.0"
}
}
3 changes: 3 additions & 0 deletions src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('should print error when require it', () => {
expect(() => require('../')).toThrow();
});
44 changes: 44 additions & 0 deletions src/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import _chatbase from '@google/chatbase';

export default function chatbaseMiddleware(bot, { apiKey, platform }) {
const chatbase = _chatbase.setApiKey(apiKey).setPlatform(platform);

const { client } = bot.connector;
const axios = client.axios;

// Add a response interceptor
axios.interceptors.response.use(response => {
const { config } = response;
if (/graph\.facebook\.com.*\/me\/messages/.test(config.url)) {
const { recipient: { id }, message: { text } } = JSON.parse(config.data);

chatbase
.newMessage()
.setAsTypeAgent()
.setUserId(id)
.setTimestamp(Date.now().toString())
.setMessage(text)
.send()
.catch(e => console.error(e));
}
return response;
});

return (req, res, next) => {
const {
sender: { id },
message: { text },
} = req.body.entry[0].messaging[0];

chatbase
.newMessage()
.setAsTypeUser()
.setUserId(id)
.setTimestamp(Date.now().toString())
.setMessage(text)
.send()
.catch(e => console.error(e));

next();
};
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
throw new Error(
"do not `require('bottender-chatbase')` directly, use one of submoudles instead. For example, `require('bottender-chatbase/express')`."
);
Loading

0 comments on commit eaa8eaf

Please sign in to comment.