From 838f9f6d4e5a60a3ab2722752416d9142341525c Mon Sep 17 00:00:00 2001 From: Dimitrios Gkegkas Date: Mon, 28 Feb 2022 12:56:14 +0200 Subject: [PATCH 1/9] Enhance viber --- packages/framework/lib/index.ts | 4 +- packages/framework/lib/interfaces/nlp.ts | 13 ++- .../framework/lib/interfaces/trackingData.ts | 6 +- packages/framework/lib/modules/index.ts | 2 +- packages/viber-adapter/lib/adapter.ts | 79 +++++++++++++++---- 5 files changed, 77 insertions(+), 27 deletions(-) diff --git a/packages/framework/lib/index.ts b/packages/framework/lib/index.ts index 2fcbc3f..b53b50c 100644 --- a/packages/framework/lib/index.ts +++ b/packages/framework/lib/index.ts @@ -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'; @@ -33,5 +34,6 @@ export { databaseUserLoader, InMemoryUser, inMemoryUserLoader, - TestAdapter + TestAdapter, + IUser }; diff --git a/packages/framework/lib/interfaces/nlp.ts b/packages/framework/lib/interfaces/nlp.ts index 34b8747..c9d4d74 100644 --- a/packages/framework/lib/interfaces/nlp.ts +++ b/packages/framework/lib/interfaces/nlp.ts @@ -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; +} diff --git a/packages/framework/lib/interfaces/trackingData.ts b/packages/framework/lib/interfaces/trackingData.ts index 86910fa..86f58e1 100644 --- a/packages/framework/lib/interfaces/trackingData.ts +++ b/packages/framework/lib/interfaces/trackingData.ts @@ -1,5 +1 @@ -export type TrackingDataPrimitives = null | string | number | boolean; -export type ITrackingData = - | TrackingDataPrimitives - | Array - | { [key: string]: ITrackingData }; +export type ITrackingData = Record | string; diff --git a/packages/framework/lib/modules/index.ts b/packages/framework/lib/modules/index.ts index 8ce5da4..78957f5 100644 --- a/packages/framework/lib/modules/index.ts +++ b/packages/framework/lib/modules/index.ts @@ -79,7 +79,7 @@ export function addBaseLocationRule>( export function addTextRule>( module: Module, - action: (user: U, payload?: IPayload, ...args: any[]) => Promise, + action: (user: U, payload: IPayload, ...args: any[]) => Promise, rule: RegExp | RegExp[] ): void { const actionName = module.name + '/' + action.name; diff --git a/packages/viber-adapter/lib/adapter.ts b/packages/viber-adapter/lib/adapter.ts index bb9a221..0625d06 100644 --- a/packages/viber-adapter/lib/adapter.ts +++ b/packages/viber-adapter/lib/adapter.ts @@ -1,26 +1,46 @@ -import { EbonyHandlers, GenericAdapter, GenericAttachment, IRouters } from '@ebenos/framework'; +import { + EbonyHandlers, + GenericAdapter, + GenericAttachment, + IRouters, + ITrackingData, + IUser +} from '@ebenos/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'; -export interface IViberOptions { +export interface IViberOptions { route?: string; authToken: string; welcomeMessage?: Record; + userLoader?: (userData: IUser) => Promise; + webhookHanlers?: IViberWebhookHandlers; +} + +export interface IViberWebhookHandlers { + unsubscribeWebhook?: (e: IViberUnsubscribedEvent) => Promise; + subscribeWebhook?: (e: IViberSubscribedEvent) => Promise; + conversationStartedWebhook?: (e: IViberConversationStartedEvent) => Promise; } -export default class ViberAdapter extends GenericAdapter { +export default class ViberAdapter extends GenericAdapter { public operations = { handover: (): Promise => { console.log('Not implemented!'); @@ -31,25 +51,41 @@ export default class ViberAdapter extends GenericAdapter { public sender; private welcomeMessage?: Record; + private webhookHanlers?: IViberWebhookHandlers; private route: string; private authToken: string; public webhook = express(); + private userLoader?: (userData: IUser) => Promise; - constructor(options: IViberOptions) { + constructor(options: IViberOptions) { 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 { 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 + ) ); } @@ -110,19 +146,27 @@ function handleTextMessage( return; } -function viberWebhookFactory( +async function viberWebhookFactory( routers: IRouters, handlers: EbonyHandlers, - welcomeMessage?: Record + welcomeMessage?: Record, + userLoader?: (userData: IUser) => Promise, + webhooks?: { + unsubscribeWebhook?: (e: IViberUnsubscribedEvent) => Promise; + subscribeWebhook?: (e: IViberSubscribedEvent) => Promise; + conversationStartedWebhook?: (e: IViberConversationStartedEvent) => Promise; + } ) { - function messageWebhook(e: IViberMessageEvent): void { - const user = convertViberSenderToUser(e.sender); + async function messageWebhook(e: IViberMessageEvent): Promise { + 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) { @@ -152,6 +196,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); @@ -163,10 +208,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); + else console.log('unsubscribed'); return; case 'failed': console.log(`Failed: ${body.desc}`); From 2843afbf40afd9b0d44e64d33ee55d3598cc6e3a Mon Sep 17 00:00:00 2001 From: Dimitrios Gkegkas Date: Mon, 28 Feb 2022 18:05:35 +0200 Subject: [PATCH 2/9] Add share phone --- examples/viber-demo-bot/src/bot.ts | 6 ++- .../src/modules/getStarted/actions.ts | 48 ++++++++++++++----- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/examples/viber-demo-bot/src/bot.ts b/examples/viber-demo-bot/src/bot.ts index 0db50e6..4ce7901 100644 --- a/examples/viber-demo-bot/src/bot.ts +++ b/examples/viber-demo-bot/src/bot.ts @@ -7,7 +7,11 @@ import getStartedModule from './modules/getStarted'; export const adapter = new ViberAdapter({ authToken: viberConfig.auth_token }); - export const bot = new Bot(adapter, {}); +adapter.webhook.use((req, res, next) => { + console.log(req.body); + next(); +}); + bot.addModule(getStartedModule); diff --git a/examples/viber-demo-bot/src/modules/getStarted/actions.ts b/examples/viber-demo-bot/src/modules/getStarted/actions.ts index 9418616..3d86fa6 100644 --- a/examples/viber-demo-bot/src/modules/getStarted/actions.ts +++ b/examples/viber-demo-bot/src/modules/getStarted/actions.ts @@ -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 = [ @@ -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 } @@ -249,9 +242,38 @@ 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( @@ -259,7 +281,7 @@ async function location2(user: InMemoryUser, payload?: IPayload) { 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 } From e5e89dd0016f651403de767d304c2e355ebd423f Mon Sep 17 00:00:00 2001 From: Dimitrios Gkegkas Date: Mon, 28 Feb 2022 18:09:08 +0200 Subject: [PATCH 3/9] Add share phone --- examples/viber-demo-bot/src/bot.ts | 6 +- packages/framework/lib/adapter.ts | 10 ++++ packages/framework/lib/handlers/text.ts | 5 ++ packages/framework/lib/interfaces/payload.ts | 5 ++ .../framework/lib/interfaces/trackingData.ts | 2 +- packages/framework/lib/modules/index.ts | 7 --- packages/viber-adapter/lib/adapter.ts | 55 +++++++++---------- .../lib/interfaces/message_types.ts | 3 +- 8 files changed, 51 insertions(+), 42 deletions(-) diff --git a/examples/viber-demo-bot/src/bot.ts b/examples/viber-demo-bot/src/bot.ts index 4ce7901..0db50e6 100644 --- a/examples/viber-demo-bot/src/bot.ts +++ b/examples/viber-demo-bot/src/bot.ts @@ -7,11 +7,7 @@ import getStartedModule from './modules/getStarted'; export const adapter = new ViberAdapter({ authToken: viberConfig.auth_token }); -export const bot = new Bot(adapter, {}); -adapter.webhook.use((req, res, next) => { - console.log(req.body); - next(); -}); +export const bot = new Bot(adapter, {}); bot.addModule(getStartedModule); diff --git a/packages/framework/lib/adapter.ts b/packages/framework/lib/adapter.ts index 4071b44..438a234 100644 --- a/packages/framework/lib/adapter.ts +++ b/packages/framework/lib/adapter.ts @@ -20,6 +20,11 @@ export interface EbonyHandlers { text: string; tracking_data?: ITrackingData; location?: { lon: number; lat: number }; + contact?: { + name?: string; + phone_number: string; + avatar?: string; + }; }, nlp: WitNLP | undefined, user: U @@ -96,6 +101,11 @@ interface InitOptionsHandlers { text: string; tracking_data?: ITrackingData; location?: { lon: number; lat: number }; + contact?: { + name?: string; + phone_number: string; + avatar?: string; + }; }, nlp: WitNLP | undefined, user: U diff --git a/packages/framework/lib/handlers/text.ts b/packages/framework/lib/handlers/text.ts index f0542e8..20b4ef3 100644 --- a/packages/framework/lib/handlers/text.ts +++ b/packages/framework/lib/handlers/text.ts @@ -40,6 +40,11 @@ export default function textHandlerFactory>( text: string; tracking_data?: ITrackingData; location?: { lon: number; lat: number }; + contact?: { + name?: string; + phone_number: string; + avatar?: string; + }; }, nlp: WitNLP | undefined, user: U diff --git a/packages/framework/lib/interfaces/payload.ts b/packages/framework/lib/interfaces/payload.ts index f653b79..fc7e2c2 100644 --- a/packages/framework/lib/interfaces/payload.ts +++ b/packages/framework/lib/interfaces/payload.ts @@ -3,5 +3,10 @@ export interface IPayload extends Record { text: string; tracking_data?: ITrackingData; location?: { lon: number; lat: number }; + contact?: { + name?: string; + phone_number: string; + avatar?: string; + }; type?: string; } diff --git a/packages/framework/lib/interfaces/trackingData.ts b/packages/framework/lib/interfaces/trackingData.ts index 86f58e1..dcbc9f4 100644 --- a/packages/framework/lib/interfaces/trackingData.ts +++ b/packages/framework/lib/interfaces/trackingData.ts @@ -1 +1 @@ -export type ITrackingData = Record | string; +export type ITrackingData = Record; diff --git a/packages/framework/lib/modules/index.ts b/packages/framework/lib/modules/index.ts index 78957f5..504ed87 100644 --- a/packages/framework/lib/modules/index.ts +++ b/packages/framework/lib/modules/index.ts @@ -70,13 +70,6 @@ export function addPostbackRule>( return { type: actionName }; } -export function addBaseLocationRule>( - module: Module, - action: (user: U, payload?: IPayload, ...args: any[]) => Promise -): void { - return addTextRule(module, action, /USER_SEND_LOCATION/); -} - export function addTextRule>( module: Module, action: (user: U, payload: IPayload, ...args: any[]) => Promise, diff --git a/packages/viber-adapter/lib/adapter.ts b/packages/viber-adapter/lib/adapter.ts index 0625d06..7868345 100644 --- a/packages/viber-adapter/lib/adapter.ts +++ b/packages/viber-adapter/lib/adapter.ts @@ -21,6 +21,7 @@ import { setWebhook } from './api/requests'; import { IViberSetWebhookResult } from './interfaces/api'; import { isMediaMessage, + IViberContactMessage, IViberLocationMessage, IViberTextMessage } from './interfaces/message_types'; @@ -109,7 +110,7 @@ function convertViberSenderToUser(sender: IViberSender): IUser { } function handleTextMessage( - m: IViberTextMessage | IViberLocationMessage, + m: IViberTextMessage | IViberLocationMessage | IViberContactMessage, user: IUser, textHandler: EbonyHandlers['text'], routers: IRouters @@ -118,16 +119,17 @@ function handleTextMessage( console.log('No text handler'); return; } - const text = m.text ? m.text : 'user_send_location'; + const text = m.text; const location = m.type === 'location' ? m.location : undefined; + const contact = m.type === 'contact' ? m.contact : undefined; let tracking_data: ITrackingData; try { // Tracking Data is an object tracking_data = JSON.parse(m.tracking_data) as ITrackingData; } catch { - // Tracking Data is a string - tracking_data = m.tracking_data; + console.log('Tracking data not an oject'); + tracking_data = { data: m.tracking_data }; } if (isPostbackTrackingData(tracking_data)) { @@ -136,13 +138,14 @@ function handleTextMessage( type: tracking_data.type, text, location, + contact, tracking_data }); routerExists(routers.PostbackRouter).objectPayloadHandler(payload, user); return; } - textHandler({ text, tracking_data, location }, undefined, user); + textHandler({ text, tracking_data, location, contact }, undefined, user); return; } @@ -165,6 +168,7 @@ async function viberWebhookFactory( switch (e.message.type) { case 'text': case 'location': + case 'contact': handleTextMessage(e.message, user, handlers.text, routers); return; default: @@ -183,51 +187,46 @@ async function viberWebhookFactory( return (req: Request, res: Response) => { const body = req.body as WebhookIncomingViberEvent; - - if (body.event !== 'conversation_started') { - res.status(200).send(); - } - switch (body.event) { case 'message': messageWebhook(body); - return; - case 'seen': - console.log('seen'); - return; + break; case 'conversation_started': - if (webhooks?.conversationStartedWebhook) webhooks.conversationStartedWebhook(body); console.log('conversation_started'); + if (webhooks?.conversationStartedWebhook) webhooks.conversationStartedWebhook(body); if (welcomeMessage !== undefined) { res.json(welcomeMessage); - } else { - res.status(200).send(); + return; } - return; - case 'delivered': - console.log('delivered'); - return; + break; case 'subscribed': if (webhooks?.subscribeWebhook) webhooks.subscribeWebhook(body); console.log('subscribed'); - return; + break; case 'unsubscribed': if (webhooks?.unsubscribeWebhook) webhooks.unsubscribeWebhook(body); - else console.log('unsubscribed'); - return; + console.log('unsubscribed'); + break; + case 'seen': + console.log('seen'); + break; + case 'delivered': + console.log('delivered'); + break; case 'failed': console.log(`Failed: ${body.desc}`); - return; + break; case 'client_status': console.log('client_status'); - return; + break; case 'webhook': console.log('Webhook connected'); - return; + break; default: console.log('Unknown event type: ' + body); - return; + break; } + res.status(200).send(); }; } diff --git a/packages/viber-adapter/lib/interfaces/message_types.ts b/packages/viber-adapter/lib/interfaces/message_types.ts index 9d249a9..bcdd66b 100644 --- a/packages/viber-adapter/lib/interfaces/message_types.ts +++ b/packages/viber-adapter/lib/interfaces/message_types.ts @@ -45,6 +45,7 @@ export interface IViberStickerMessage extends IViberMessage { export interface IViberContactMessage extends IViberMessage { type: 'contact'; + text: string; contact: { name?: string; phone_number: string; @@ -54,7 +55,7 @@ export interface IViberContactMessage extends IViberMessage { export interface IViberLocationMessage extends IViberMessage { type: 'location'; - text?: string; + text: string; location: { lat: number; lon: number; From d1c1bec9e539b68a239db1f239b3fc6e7c3bc287 Mon Sep 17 00:00:00 2001 From: Dimitrios Gkegkas Date: Mon, 28 Feb 2022 18:16:20 +0200 Subject: [PATCH 4/9] Add share phone --- packages/viber-adapter/lib/adapter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/viber-adapter/lib/adapter.ts b/packages/viber-adapter/lib/adapter.ts index 7868345..11dcc76 100644 --- a/packages/viber-adapter/lib/adapter.ts +++ b/packages/viber-adapter/lib/adapter.ts @@ -128,7 +128,7 @@ function handleTextMessage( // Tracking Data is an object tracking_data = JSON.parse(m.tracking_data) as ITrackingData; } catch { - console.log('Tracking data not an oject'); + console.log('Tracking data not an object'); tracking_data = { data: m.tracking_data }; } From 0fd74478b00ab986e20c109cd8d3ecd5e84d6ec8 Mon Sep 17 00:00:00 2001 From: Dimitrios Gkegkas Date: Mon, 28 Feb 2022 18:30:15 +0200 Subject: [PATCH 5/9] Fix --- examples/viber-demo-bot/src/bot.ts | 18 +++++++++++++++++- packages/viber-adapter/lib/index.ts | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/examples/viber-demo-bot/src/bot.ts b/examples/viber-demo-bot/src/bot.ts index 0db50e6..ad741e8 100644 --- a/examples/viber-demo-bot/src/bot.ts +++ b/examples/viber-demo-bot/src/bot.ts @@ -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, + webhookHanlers: { + 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(adapter, {}); diff --git a/packages/viber-adapter/lib/index.ts b/packages/viber-adapter/lib/index.ts index 0a59a9a..e93eca3 100644 --- a/packages/viber-adapter/lib/index.ts +++ b/packages/viber-adapter/lib/index.ts @@ -1,3 +1,8 @@ import ViberAdapter from './adapter'; +export * from './interfaces/message_types'; +export * from './interfaces/tracking_data'; +export * from './interfaces/api'; +export * from './interfaces/webhook'; +export * from './adapter'; export { ViberAdapter }; From 2f51737c3b794f4fa149856117e158c5c2823104 Mon Sep 17 00:00:00 2001 From: Dimitrios Gkegkas Date: Tue, 1 Mar 2022 11:21:14 +0200 Subject: [PATCH 6/9] Change hanle to handle --- examples/viber-demo-bot/src/bot.ts | 2 +- packages/viber-adapter/lib/adapter.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/viber-demo-bot/src/bot.ts b/examples/viber-demo-bot/src/bot.ts index ad741e8..54c7fb9 100644 --- a/examples/viber-demo-bot/src/bot.ts +++ b/examples/viber-demo-bot/src/bot.ts @@ -11,7 +11,7 @@ import { export const adapter = new ViberAdapter({ authToken: viberConfig.auth_token, - webhookHanlers: { + webhookHandlers: { conversationStartedWebhook: async (e: IViberConversationStartedEvent) => { console.log(e); }, diff --git a/packages/viber-adapter/lib/adapter.ts b/packages/viber-adapter/lib/adapter.ts index 11dcc76..c9c9ab0 100644 --- a/packages/viber-adapter/lib/adapter.ts +++ b/packages/viber-adapter/lib/adapter.ts @@ -32,7 +32,7 @@ export interface IViberOptions { authToken: string; welcomeMessage?: Record; userLoader?: (userData: IUser) => Promise; - webhookHanlers?: IViberWebhookHandlers; + webhookHandlers?: IViberWebhookHandlers; } export interface IViberWebhookHandlers { @@ -52,7 +52,7 @@ export default class ViberAdapter extends GenericAdapter { public sender; private welcomeMessage?: Record; - private webhookHanlers?: IViberWebhookHandlers; + private webhookHandlers?: IViberWebhookHandlers; private route: string; private authToken: string; public webhook = express(); @@ -65,7 +65,7 @@ export default class ViberAdapter extends GenericAdapter { authToken, welcomeMessage, userLoader, - webhookHanlers + webhookHandlers } = options; this.route = route; @@ -73,7 +73,7 @@ export default class ViberAdapter extends GenericAdapter { this.sender = senderFactory(this.authToken); this.welcomeMessage = welcomeMessage; this.userLoader = userLoader; - this.webhookHanlers = webhookHanlers; + this.webhookHandlers = webhookHandlers; } public async initialization(): Promise { @@ -85,7 +85,7 @@ export default class ViberAdapter extends GenericAdapter { this.handlers, this.welcomeMessage, this.userLoader, - this.webhookHanlers + this.webhookHandlers ) ); } From d62d4e8494ae75486509950483f1efe66d0c4f76 Mon Sep 17 00:00:00 2001 From: Dimitrios Gkegkas Date: Wed, 2 Mar 2022 18:40:20 +0200 Subject: [PATCH 7/9] Delete Info console --- packages/viber-adapter/lib/adapter.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/viber-adapter/lib/adapter.ts b/packages/viber-adapter/lib/adapter.ts index c9c9ab0..b4fc7be 100644 --- a/packages/viber-adapter/lib/adapter.ts +++ b/packages/viber-adapter/lib/adapter.ts @@ -128,7 +128,6 @@ function handleTextMessage( // Tracking Data is an object tracking_data = JSON.parse(m.tracking_data) as ITrackingData; } catch { - console.log('Tracking data not an object'); tracking_data = { data: m.tracking_data }; } From a07b4b6a7dc8e6ae5d61b6f6831ae16492c3e794 Mon Sep 17 00:00:00 2001 From: Dimitrios Gkegkas Date: Wed, 2 Mar 2022 19:02:20 +0200 Subject: [PATCH 8/9] Change webhook for conversation started --- packages/viber-adapter/lib/adapter.ts | 29 ++++++++++++--------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/viber-adapter/lib/adapter.ts b/packages/viber-adapter/lib/adapter.ts index b4fc7be..e40bf2f 100644 --- a/packages/viber-adapter/lib/adapter.ts +++ b/packages/viber-adapter/lib/adapter.ts @@ -38,7 +38,9 @@ export interface IViberOptions { export interface IViberWebhookHandlers { unsubscribeWebhook?: (e: IViberUnsubscribedEvent) => Promise; subscribeWebhook?: (e: IViberSubscribedEvent) => Promise; - conversationStartedWebhook?: (e: IViberConversationStartedEvent) => Promise; + conversationStartedWebhook?: ( + e: IViberConversationStartedEvent + ) => Promise | void>; } export default class ViberAdapter extends GenericAdapter { @@ -60,18 +62,11 @@ export default class ViberAdapter extends GenericAdapter { constructor(options: IViberOptions) { super(); - const { - route = '/viber/webhook', - authToken, - welcomeMessage, - userLoader, - webhookHandlers - } = options; + const { route = '/viber/webhook', authToken, userLoader, webhookHandlers } = options; this.route = route; this.authToken = authToken; this.sender = senderFactory(this.authToken); - this.welcomeMessage = welcomeMessage; this.userLoader = userLoader; this.webhookHandlers = webhookHandlers; } @@ -83,7 +78,6 @@ export default class ViberAdapter extends GenericAdapter { await viberWebhookFactory( this.routers, this.handlers, - this.welcomeMessage, this.userLoader, this.webhookHandlers ) @@ -151,12 +145,13 @@ function handleTextMessage( async function viberWebhookFactory( routers: IRouters, handlers: EbonyHandlers, - welcomeMessage?: Record, userLoader?: (userData: IUser) => Promise, webhooks?: { unsubscribeWebhook?: (e: IViberUnsubscribedEvent) => Promise; subscribeWebhook?: (e: IViberSubscribedEvent) => Promise; - conversationStartedWebhook?: (e: IViberConversationStartedEvent) => Promise; + conversationStartedWebhook?: ( + e: IViberConversationStartedEvent + ) => Promise | void>; } ) { async function messageWebhook(e: IViberMessageEvent): Promise { @@ -192,10 +187,12 @@ async function viberWebhookFactory( break; case 'conversation_started': console.log('conversation_started'); - if (webhooks?.conversationStartedWebhook) webhooks.conversationStartedWebhook(body); - if (welcomeMessage !== undefined) { - res.json(welcomeMessage); - return; + if (webhooks?.conversationStartedWebhook) { + const welcomeMessage = webhooks.conversationStartedWebhook(body); + if (welcomeMessage !== undefined) { + res.json(welcomeMessage); + return; + } } break; case 'subscribed': From 58371f2d00cd2694f1e1aa1e5815d496d2e2a00f Mon Sep 17 00:00:00 2001 From: Dimitrios Gkegkas Date: Thu, 3 Mar 2022 14:03:18 +0200 Subject: [PATCH 9/9] User loader logic change --- packages/viber-adapter/lib/adapter.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/viber-adapter/lib/adapter.ts b/packages/viber-adapter/lib/adapter.ts index e40bf2f..10f6f8d 100644 --- a/packages/viber-adapter/lib/adapter.ts +++ b/packages/viber-adapter/lib/adapter.ts @@ -31,7 +31,7 @@ export interface IViberOptions { route?: string; authToken: string; welcomeMessage?: Record; - userLoader?: (userData: IUser) => Promise; + userLoader?: (userData: IViberSender) => Promise; webhookHandlers?: IViberWebhookHandlers; } @@ -58,7 +58,7 @@ export default class ViberAdapter extends GenericAdapter { private route: string; private authToken: string; public webhook = express(); - private userLoader?: (userData: IUser) => Promise; + private userLoader?: (userData: IViberSender) => Promise; constructor(options: IViberOptions) { super(); @@ -145,7 +145,7 @@ function handleTextMessage( async function viberWebhookFactory( routers: IRouters, handlers: EbonyHandlers, - userLoader?: (userData: IUser) => Promise, + userLoader?: (userData: IViberSender) => Promise, webhooks?: { unsubscribeWebhook?: (e: IViberUnsubscribedEvent) => Promise; subscribeWebhook?: (e: IViberSubscribedEvent) => Promise; @@ -155,9 +155,7 @@ async function viberWebhookFactory( } ) { async function messageWebhook(e: IViberMessageEvent): Promise { - const user = userLoader - ? await userLoader(convertViberSenderToUser(e.sender)) - : convertViberSenderToUser(e.sender); + const user = userLoader ? await userLoader(e.sender) : convertViberSenderToUser(e.sender); switch (e.message.type) { case 'text': @@ -179,7 +177,7 @@ async function viberWebhookFactory( } } - return (req: Request, res: Response) => { + return async (req: Request, res: Response) => { const body = req.body as WebhookIncomingViberEvent; switch (body.event) { case 'message': @@ -188,19 +186,19 @@ async function viberWebhookFactory( case 'conversation_started': console.log('conversation_started'); if (webhooks?.conversationStartedWebhook) { - const welcomeMessage = webhooks.conversationStartedWebhook(body); + const welcomeMessage = await webhooks.conversationStartedWebhook(body); if (welcomeMessage !== undefined) { + console.log(welcomeMessage); res.json(welcomeMessage); - return; } } break; case 'subscribed': - if (webhooks?.subscribeWebhook) webhooks.subscribeWebhook(body); + if (webhooks?.subscribeWebhook) await webhooks.subscribeWebhook(body); console.log('subscribed'); break; case 'unsubscribed': - if (webhooks?.unsubscribeWebhook) webhooks.unsubscribeWebhook(body); + if (webhooks?.unsubscribeWebhook) await webhooks.unsubscribeWebhook(body); console.log('unsubscribed'); break; case 'seen':