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

User Loader functionality for Viber adapter #34

Draft
wants to merge 6 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ebenos/monorepo",
"version": "3.1.0",
"version": "3.1.1",
"main": "index.js",
"license": "MIT",
"private": true,
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;
}
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> | string;
2 changes: 1 addition & 1 deletion packages/framework/lib/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function addBaseLocationRule<U extends User<any>>(

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>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what will the payload be like there?

rule: RegExp | RegExp[]
): void {
const actionName = module.name + '/' + action.name;
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ebenos/framework",
"version": "4.0.0-alpha.29",
"name": "@ebonydevcopy/framework",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't these references be changed here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this pull request is just to keep track of the changes in the copy package. I'll make a new one with the correct changes for review and merging OTD

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

"version": "4.0.0-alpha.30",
"description": "A module-based NodeJS chatbot framework.",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand Down
78 changes: 62 additions & 16 deletions packages/viber-adapter/lib/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import { EbonyHandlers, GenericAdapter, GenericAttachment, IRouters } from '@ebenos/framework';
import {
EbonyHandlers,
GenericAdapter,
GenericAttachment,
IRouters
} from '@ebonydevcopy/framework';
import express, { Request, Response } from 'express';
import { json as bodyParser } from 'body-parser';
import senderFactory from './sender';
import { IViberMessageEvent, IViberSender, WebhookIncomingViberEvent } from './interfaces/webhook';
import {
IViberMessageEvent,
IViberSender,
WebhookIncomingViberEvent,
IViberUnsubscribedEvent,
IViberSubscribedEvent,
IViberConversationStartedEvent
} from './interfaces/webhook';
import { setWebhook } from './api/requests';
import { IViberSetWebhookResult } from './interfaces/api';
import { IUser } from '@ebenos/framework/lib/models/UserSchema';
import {
isMediaMessage,
IViberLocationMessage,
IViberTextMessage
} from './interfaces/message_types';
import { isPostbackTrackingData } from './interfaces/tracking_data';
import { ITrackingData } from '@ebenos/framework';
import { ITrackingData, IUser } from '@ebonydevcopy/framework';

export interface IViberOptions {
export interface IViberOptions<U> {
route?: string;
authToken: string;
welcomeMessage?: Record<string, unknown>;
userLoader?: (userData: IUser) => Promise<U>;
webhookHanlers?: IViberWebhookHandlers;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: webhookHanlers -> webhookHandlers

}

export interface IViberWebhookHandlers {
unsubscribeWebhook?: (e: IViberUnsubscribedEvent) => Promise<void>;
subscribeWebhook?: (e: IViberSubscribedEvent) => Promise<void>;
conversationStartedWebhook?: (e: IViberConversationStartedEvent) => Promise<void>;
}

export default class ViberAdapter extends GenericAdapter {
export default class ViberAdapter<U extends IUser> extends GenericAdapter {
public operations = {
handover: (): Promise<void> => {
console.log('Not implemented!');
Expand All @@ -31,25 +50,41 @@ export default class ViberAdapter extends GenericAdapter {
public sender;

private welcomeMessage?: Record<string, unknown>;
private webhookHanlers?: IViberWebhookHandlers;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo webhookHanlers -> webhookHandlers

private route: string;
private authToken: string;
public webhook = express();
private userLoader?: (userData: IUser) => Promise<U>;

constructor(options: IViberOptions) {
constructor(options: IViberOptions<U>) {
super();
const { route = '/viber/webhook', authToken, welcomeMessage } = options;
const {
route = '/viber/webhook',
authToken,
welcomeMessage,
userLoader,
webhookHanlers
} = options;

this.route = route;
this.authToken = authToken;
this.sender = senderFactory(this.authToken);
this.welcomeMessage = welcomeMessage;
this.userLoader = userLoader;
this.webhookHanlers = webhookHanlers;
}

public initialization(): void {
public async initialization(): Promise<void> {
this.webhook.use(bodyParser());
this.webhook.post(
this.route,
viberWebhookFactory(this.routers, this.handlers, this.welcomeMessage)
await viberWebhookFactory(
this.routers,
this.handlers,
this.welcomeMessage,
this.userLoader,
this.webhookHanlers
)
);
}

Expand Down Expand Up @@ -110,19 +145,27 @@ function handleTextMessage(
return;
}

function viberWebhookFactory(
async function viberWebhookFactory<U extends IUser>(
routers: IRouters,
handlers: EbonyHandlers<any>,
welcomeMessage?: Record<string, unknown>
welcomeMessage?: Record<string, unknown>,
userLoader?: (userData: IUser) => Promise<U>,
webhooks?: {
unsubscribeWebhook?: (e: IViberUnsubscribedEvent) => Promise<void>;
subscribeWebhook?: (e: IViberSubscribedEvent) => Promise<void>;
conversationStartedWebhook?: (e: IViberConversationStartedEvent) => Promise<void>;
}
) {
function messageWebhook(e: IViberMessageEvent): void {
const user = convertViberSenderToUser(e.sender);
async function messageWebhook(e: IViberMessageEvent): Promise<void> {
const user = userLoader
? await userLoader(convertViberSenderToUser(e.sender))
: convertViberSenderToUser(e.sender);

switch (e.message.type) {
case 'text':
case 'location':
handleTextMessage(e.message, user, handlers.text, routers);
return;

default:
if (isMediaMessage(e.message)) {
if (handlers.attachment !== undefined) {
Expand Down Expand Up @@ -152,6 +195,7 @@ function viberWebhookFactory(
console.log('seen');
return;
case 'conversation_started':
if (webhooks?.conversationStartedWebhook) webhooks.conversationStartedWebhook(body);
console.log('conversation_started');
if (welcomeMessage !== undefined) {
res.json(welcomeMessage);
Expand All @@ -163,10 +207,12 @@ function viberWebhookFactory(
console.log('delivered');
return;
case 'subscribed':
if (webhooks?.subscribeWebhook) webhooks.subscribeWebhook(body);
console.log('subscribed');
return;
case 'unsubscribed':
console.log('unsubscribed');
if (webhooks?.unsubscribeWebhook) webhooks.unsubscribeWebhook(body);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add brackets ({}) on all ifs

else console.log('unsubscribed');
return;
case 'failed':
console.log(`Failed: ${body.desc}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/viber-adapter/lib/sender.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IInteraction } from '@ebenos/framework';
import { IInteraction } from '@ebonydevcopy/framework';
import { sendMessage } from './api/requests';

export default function senderFactory(viberToken: string) {
Expand Down
6 changes: 3 additions & 3 deletions packages/viber-adapter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ebenos/viber-adapter",
"version": "4.0.0-alpha.29",
"name": "@ebonydevcopy/viber-adapter",
"version": "4.0.0-alpha.31",
"description": "Viber adapter for the Ebony framework.",
"main": "./build/index.js",
"publishConfig": {
Expand Down Expand Up @@ -33,7 +33,7 @@
"node-fetch": "^2.6.6"
},
"devDependencies": {
"@ebenos/framework": "^4.0.0-alpha.29",
"@ebonydevcopy/framework": "^4.0.0-alpha.29",
"@types/body-parser": "^1.19.2",
"@types/express": "^4.17.13",
"@types/node-fetch": "^2.5.12"
Expand Down
2 changes: 1 addition & 1 deletion packages/viber-elements/lib/viberAPI/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
OpenURLMediaType,
ISerializedButton
} from './interfaces';
import { ISerializable } from '@ebenos/framework';
import { ISerializable } from '@ebonydevcopy/framework';

/**Viber RichMedia Button */
export class Button implements ISerializable {
Expand Down
2 changes: 1 addition & 1 deletion packages/viber-elements/lib/viberAPI/carousel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button, RichMedia } from './attachments';
import { IFrame, IRichMediaOptions } from './interfaces';
import { ISerializable } from '@ebenos/framework';
import { ISerializable } from '@ebonydevcopy/framework';

export interface ICarouselElement {
title?: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/viber-elements/lib/viberAPI/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RichMedia, Picture, Button } from './attachments';
import { Carousel } from './carousel';
import { Keyboard } from './keyboard';
import { ITrackingData } from '@ebenos/framework';
import { ITrackingData } from '@ebonydevcopy/framework';

export type ScaleType = 'crop' | 'fill' | 'fit';
export type ActionType = 'reply' | 'open-url' | 'location-picker' | 'share-phone' | 'none';
Expand Down
2 changes: 1 addition & 1 deletion packages/viber-elements/lib/viberAPI/keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISerializable } from '@ebenos/framework';
import { ISerializable } from '@ebonydevcopy/framework';
import { Button } from './attachments';
import { IKeyboardOptions, InputFieldState, ISerializedKeyboard } from './interfaces';

Expand Down
4 changes: 2 additions & 2 deletions packages/viber-elements/lib/viberAPI/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISerializable } from '@ebenos/framework';
import { ISerializable } from '@ebonydevcopy/framework';
import {
IMessageOptions,
ISender,
Expand All @@ -12,7 +12,7 @@ import {
import { Picture, RichMedia } from './attachments';
import { Keyboard } from './keyboard';
import { Carousel } from './carousel';
import { ITrackingData } from '@ebenos/framework';
import { ITrackingData } from '@ebonydevcopy/framework';

function isText(options: IMessageOptions): options is ITextOptions {
return (options as ITextOptions).text !== undefined;
Expand Down
8 changes: 4 additions & 4 deletions packages/viber-elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ebenos/viber-elements",
"version": "4.0.0-alpha.29",
"name": "@ebonydevcopy/viber-elements",
"version": "4.0.0-alpha.31",
"description": "Elements Library for the Ebony framework.",
"main": "./build/index.js",
"publishConfig": {
Expand Down Expand Up @@ -29,10 +29,10 @@
],
"license": "MIT",
"peerDependencies": {
"@ebenos/framework": "*"
"@ebonydevcopy/framework": "*"
},
"devDependencies": {
"@ebenos/framework": "^4.0.0-alpha.29"
"@ebonydevcopy/framework": "^4.0.0-alpha.29"
},
"engines": {
"node": ">=14.0.0"
Expand Down
Loading