Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New viber features #36

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion examples/viber-demo-bot/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@ import { ViberAdapter } from '@ebenos/viber-adapter';
import { viberConfig } from './secret';

import getStartedModule from './modules/getStarted';
import {
IViberConversationStartedEvent,
IViberSubscribedEvent,
IViberUnsubscribedEvent
} from '@ebenos/viber-adapter';

export const adapter = new ViberAdapter({
authToken: viberConfig.auth_token
authToken: viberConfig.auth_token,
webhookHandlers: {
conversationStartedWebhook: async (e: IViberConversationStartedEvent) => {
console.log(e);
},
subscribeWebhook: async (e: IViberSubscribedEvent) => {
console.log(e);
},
unsubscribeWebhook: async (e: IViberUnsubscribedEvent) => {
console.log(e);
}
}
});

export const bot = new Bot<InMemoryUser>(adapter, {});
Expand Down
48 changes: 35 additions & 13 deletions examples/viber-demo-bot/src/modules/getStarted/actions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { bot } from '../../bot';
import { RichMedia, Button, Message, Keyboard } from '@ebenos/viber-elements';
import { Carousel } from '@ebenos/viber-elements';
import {
addAction,
addPostbackRule,
addTextRule,
InMemoryUser,
addBaseLocationRule,
IPayload
} from '@ebenos/framework';
import { addAction, addPostbackRule, addTextRule, InMemoryUser, IPayload } from '@ebenos/framework';
import getStartedModule from '.';

const testArray = [
Expand Down Expand Up @@ -199,7 +192,7 @@ async function getTest12(user: InMemoryUser) {
}

addAction(getStartedModule, location);
addBaseLocationRule(getStartedModule, location);

async function location(
user: InMemoryUser,
payload?: { text: string; location?: { lat: number; lon: number }; somethingNew?: string }
Expand Down Expand Up @@ -249,17 +242,46 @@ async function location1(user: InMemoryUser) {
.end();
}

addAction(getStartedModule, location2);
addTextRule(getStartedModule, location2, /PAME/);
async function location2(user: InMemoryUser, payload?: IPayload) {
addAction(getStartedModule, phone);
addTextRule(getStartedModule, phone, /PHONE_TEST/);
async function phone(user: InMemoryUser) {
await bot
.scenario(user)
.send(
new Message({
sender: {
name: 'Giorgos'
},
text: 'Send Phone',
keyboard: new Keyboard({
ButtonsGroupColumns: 3,
ButtonsGroupRows: 3,
Buttons: [
new Button({
Rows: 3,
Columns: 3,
ActionBody: 'pame_phone',
Text: 'tel',
ActionType: 'share-phone'
})
]
})
})
)
.end();
}

addAction(getStartedModule, phone2);
addTextRule(getStartedModule, phone2, /PAME_PHONE/);
async function phone2(user: InMemoryUser, payload: IPayload) {
await bot
.scenario(user)
.send(
new Message({
sender: {
name: 'Giorgos'
},
text: 'PAMEEE The location has lat ' + payload?.location?.lat,
text: 'Your phone number is ' + payload?.contact?.phone_number,
tracking_data: {
age: 18
}
Expand Down
10 changes: 10 additions & 0 deletions packages/framework/lib/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface EbonyHandlers<U> {
text: string;
tracking_data?: ITrackingData;
location?: { lon: number; lat: number };
contact?: {
name?: string;
phone_number: string;
avatar?: string;
};
},
nlp: WitNLP | undefined,
user: U
Expand Down Expand Up @@ -96,6 +101,11 @@ interface InitOptionsHandlers<U> {
text: string;
tracking_data?: ITrackingData;
location?: { lon: number; lat: number };
contact?: {
name?: string;
phone_number: string;
avatar?: string;
};
},
nlp: WitNLP | undefined,
user: U
Expand Down
5 changes: 5 additions & 0 deletions packages/framework/lib/handlers/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export default function textHandlerFactory<U extends User<any>>(
text: string;
tracking_data?: ITrackingData;
location?: { lon: number; lat: number };
contact?: {
name?: string;
phone_number: string;
avatar?: string;
};
},
nlp: WitNLP | undefined,
user: U
Expand Down
4 changes: 3 additions & 1 deletion packages/framework/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Bot from './bot';
import User from './models/User';
import DatabaseUser, { userLoader as databaseUserLoader } from './models/DatabaseUser';
import InMemoryUser, { userLoader as inMemoryUserLoader } from './models/InMemoryUser';
import { IUser } from './models/UserSchema';
import GenericAdapter from './adapter';
import TestAdapter from './tests/test-adapter';
export { Module, BotOptions, Scenario } from './interfaces/bot';
Expand All @@ -33,5 +34,6 @@ export {
databaseUserLoader,
InMemoryUser,
inMemoryUserLoader,
TestAdapter
TestAdapter,
IUser
};
13 changes: 9 additions & 4 deletions packages/framework/lib/interfaces/nlp.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
export interface WitNLP {
entities: {
intent?: WitEntityNLP[];
sentiment?: WitEntityNLP[];
};
text: string;
intents: WitIntentNLP[];
entities: any;
}

export interface WitEntityNLP {
value: string;
confidence: number;
}

export interface WitIntentNLP {
id: string;
name: string;
confidence: number;
}
5 changes: 5 additions & 0 deletions packages/framework/lib/interfaces/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ export interface IPayload extends Record<string, any> {
text: string;
tracking_data?: ITrackingData;
location?: { lon: number; lat: number };
contact?: {
name?: string;
phone_number: string;
avatar?: string;
};
type?: string;
}
6 changes: 1 addition & 5 deletions packages/framework/lib/interfaces/trackingData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export type TrackingDataPrimitives = null | string | number | boolean;
export type ITrackingData =
| TrackingDataPrimitives
| Array<ITrackingData>
| { [key: string]: ITrackingData };
export type ITrackingData = Record<string, any>;
9 changes: 1 addition & 8 deletions packages/framework/lib/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,9 @@ export function addPostbackRule<U extends User<any>>(
return { type: actionName };
}

export function addBaseLocationRule<U extends User<any>>(
module: Module<U>,
action: (user: U, payload?: IPayload, ...args: any[]) => Promise<any>
): void {
return addTextRule<U>(module, action, /USER_SEND_LOCATION/);
}

export function addTextRule<U extends User<any>>(
module: Module<U>,
action: (user: U, payload?: IPayload, ...args: any[]) => Promise<any>,
action: (user: U, payload: IPayload, ...args: any[]) => Promise<any>,
rule: RegExp | RegExp[]
): void {
const actionName = module.name + '/' + action.name;
Expand Down
Loading