Skip to content

Commit

Permalink
Add example bot for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispanag committed Mar 19, 2021
1 parent 38de6d2 commit 164751e
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/viber-demo-bot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
dist
tsconfig.tsbuildinfo
.vscode
.env
2 changes: 2 additions & 0 deletions examples/viber-demo-bot/example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VIBER_AUTH_TOKEN=<insert_viber_auth_token>
WEBHOOK_DOMAIN=<WEBHOOK_DOMAIN>
23 changes: 23 additions & 0 deletions examples/viber-demo-bot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@ebenos/viber-demo-bot",
"version": "4.0.0-alpha.12",
"main": "./dist/server.js",
"license": "MIT",
"private": true,
"scripts": {
"start": "yarn node ./dist/server.js",
"build": "yarn tsc",
"dev": "yarn run build && yarn start",
"lint": "yarn eslint '*/**/*.ts' --quiet --fix -c ../../.eslintrc.js --ignore-path ../../.eslintignore"
},
"dependencies": {
"@ebenos/elements": "^4.0.0-alpha.12",
"@ebenos/framework": "^4.0.0-alpha.12",
"@ebenos/viber-adapter": "^4.0.0-alpha.12",
"@types/dotenv": "^8.2.0",
"@types/node-fetch": "^2.5.8",
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"node-fetch": "^2.6.1"
}
}
13 changes: 13 additions & 0 deletions examples/viber-demo-bot/src/bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Bot, InMemoryUser } from '@ebenos/framework';
import { ViberAdapter } from '@ebenos/viber-adapter';
import { viberConfig } from './secret';

import getStartedModule from './modules/getStarted';

export const adapter = new ViberAdapter({
authToken: viberConfig.auth_token
});

export const bot = new Bot<InMemoryUser>(adapter, {});

bot.addModule(getStartedModule);
61 changes: 61 additions & 0 deletions examples/viber-demo-bot/src/modules/getStarted/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { bot } from '../../bot';
import { Message, TextQuickReply } from '@ebenos/elements';
import {
addAction,
addPostbackRule,
addTextRule,
createPayload,
InMemoryUser
} from '@ebenos/framework';
import getStartedModule from '.';

addAction(getStartedModule, getStartedSecond);
addTextRule(getStartedModule, getStartedSecond, /SECOND/);
async function getStartedSecond(user: InMemoryUser) {
const now = new Date();
await bot
.scenario(user)
.send(
new Message({
text: `${now.toISOString()} Second`
})
)
.send(
new Message({
text: 'test1'
})
)
.send(
new Message({
text: 'test2'
})
)
.end();
}

addAction(getStartedModule, getStarted);
addPostbackRule(getStartedModule, getStarted, 'string');
addTextRule(getStartedModule, getStarted, /.*/);
async function getStarted(user: InMemoryUser) {
const now = new Date();
await bot
.scenario(user)
.send(
new Message({
text: `${now.toISOString()}`,
quickreplies: [
new TextQuickReply(
'Test Text Payload',
createPayload(getStartedModule, getStartedSecond, 'string')
),
new TextQuickReply(
'Test Object Payload',
createPayload(getStartedModule, getStartedSecond, 'object', {
data: 'isData'
})
)
]
})
)
.end();
}
10 changes: 10 additions & 0 deletions examples/viber-demo-bot/src/modules/getStarted/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createModule, InMemoryUser, Module } from '@ebenos/framework';

import { preMiddlewares } from './middlewares';

const getStartedModule: Module<InMemoryUser> = createModule('getStarted');
getStartedModule.preMiddlewares = preMiddlewares;

export default getStartedModule;

import './actions';
12 changes: 12 additions & 0 deletions examples/viber-demo-bot/src/modules/getStarted/middlewares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ActionMiddleware, InMemoryUser } from '@ebenos/framework';

export const preMiddlewares: Array<ActionMiddleware<InMemoryUser>> = [
(actionName: string, user: InMemoryUser, params: any[], next: () => void): void => {
console.log(
`User ${user.firstName} triggered action: ${actionName} with params ${JSON.stringify(
params
)}`
);
next();
}
];
17 changes: 17 additions & 0 deletions examples/viber-demo-bot/src/secret.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import dotenv from 'dotenv';
dotenv.config();

const VIBER_AUTH_TOKEN = process.env.VIBER_AUTH_TOKEN;
if (!VIBER_AUTH_TOKEN) {
throw new Error('Missing env: No VIBER_AUTH_TOKEN');
}

const WEBHOOK_DOMAIN = process.env.WEBHOOK_DOMAIN;
if (!WEBHOOK_DOMAIN) {
throw new Error('Missing env: No WEBHOOK_DOMAIN');
}

export const viberConfig = {
auth_token: VIBER_AUTH_TOKEN,
webhook_domain: WEBHOOK_DOMAIN
};
12 changes: 12 additions & 0 deletions examples/viber-demo-bot/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { adapter } from './bot';
import { viberConfig } from './secret';

adapter.webhook.listen(3000);
console.log('Listening...');

console.log('Setting up webhook...');

(async () => {
await adapter.setWebhook(viberConfig.webhook_domain);
console.log('Ready!');
})();
69 changes: 69 additions & 0 deletions examples/viber-demo-bot/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2015" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
"sourceMap": true /* Generates corresponding '.map' file. */,
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist" /* Redirect output structure to the directory. */,
"rootDir": "src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true /* Enable strict null checks. */,
"strictFunctionTypes": true /* Enable strict checking of function types. */,
"strictBindCallApply": true /* Enable strict 'bind', 'call', and 'apply' methods on functions. */,
"strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */,
"noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
"alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */,

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,

/* Module Resolution Options */
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"baseUrl": "./dist" /* Base directory to resolve non-absolute module names. */,
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"rootDirs": [
"./src/"
] /* List of root folders whose combined content represents the structure of the project at runtime. */,
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["./src/**/*"]
}
2 changes: 1 addition & 1 deletion packages/viber-adapter/lib/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface IViberOptions {

export default class ViberAdapter extends GenericAdapter {
public operations = {
handover: (id: string): Promise<void> => {
handover: (): Promise<void> => {
console.log('Not implemented!');
return Promise.resolve();
}
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,13 @@
dependencies:
"@types/node" "*"

"@types/dotenv@^8.2.0":
version "8.2.0"
resolved "https://registry.yarnpkg.com/@types/dotenv/-/dotenv-8.2.0.tgz#5cd64710c3c98e82d9d15844375a33bf1b45d053"
integrity sha512-ylSC9GhfRH7m1EUXBXofhgx4lUWmFeQDINW5oLuS+gxWdfUeW4zJdeVTYVkexEW+e2VUvlZR2kGnGGipAWR7kw==
dependencies:
dotenv "*"

"@types/express-serve-static-core@^4.17.18":
version "4.17.18"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.18.tgz#8371e260f40e0e1ca0c116a9afcd9426fa094c40"
Expand Down Expand Up @@ -2304,6 +2311,11 @@ dot-prop@^5.1.0:
dependencies:
is-obj "^2.0.0"

dotenv@*, dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

duplexer@^0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6"
Expand Down

0 comments on commit 164751e

Please sign in to comment.