-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
d334859
884bd8a
0514296
613cbf8
0823f01
e42ce24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,39 @@ import { EbonyHandlers, GenericAdapter, GenericAttachment, IRouters } from '@ebe | |
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 '@ebenos/framework'; | ||
|
||
export interface IViberOptions { | ||
export interface IViberOptions<U> { | ||
route?: string; | ||
authToken: string; | ||
welcomeMessage?: Record<string, unknown>; | ||
userLoader?: (userData: IUser) => Promise<U>; | ||
webhookHanlers?: IViberWebhookHandlers; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!'); | ||
|
@@ -31,25 +45,41 @@ export default class ViberAdapter extends GenericAdapter { | |
public sender; | ||
|
||
private welcomeMessage?: Record<string, unknown>; | ||
private webhookHanlers?: IViberWebhookHandlers; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
) | ||
); | ||
} | ||
|
||
|
@@ -110,19 +140,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) { | ||
|
@@ -152,6 +190,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 +202,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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`); | ||
|
There was a problem hiding this comment.
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?