Skip to content

Commit

Permalink
Viber-adapter ongoing development
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispanag committed Mar 19, 2021
1 parent 50c7987 commit 38de6d2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/viber-adapter/lib/api/requests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
IViberSendMessageResult,
IViberSetWebhook,
IViberSetWebhookResult
} from '../interfaces/api';
import viberRequest from './viberRequest';

export function setWebhook(webhookUrl: string, authToken: string): Promise<IViberSetWebhookResult> {
const body: IViberSetWebhook = {
url: webhookUrl
};
return viberRequest('set_webhook', body, authToken) as Promise<IViberSetWebhookResult>;
}

export function sendMessage(
receiver: string,
messageBody: Record<string, unknown>,
authToken: string
): Promise<IViberSendMessageResult> {
const body = {
receiver,
...messageBody
};

return viberRequest('send_message', body, authToken) as Promise<IViberSendMessageResult>;
}
20 changes: 20 additions & 0 deletions packages/viber-adapter/lib/api/viberRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fetch from 'node-fetch';

const viberUrl = 'https://chatapi.viber.com/pa/';

export default async function viberRequest(
path: string,
data: Record<string, any>,
authToken: string
): Promise<any> {
const res = await fetch(viberUrl + path, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'X-Viber-Auth-Token': authToken
}
});

const response = await res.json();
return response;
}
25 changes: 25 additions & 0 deletions packages/viber-adapter/lib/interfaces/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { EventType } from './webhook';

export interface IViberSetWebhook {
url: string;
event_types?: EventType[];
send_name?: boolean;
send_photo?: boolean;
}

export interface IViberSetWebhookResult {
status: number;
status_message:
| 'ok'
| 'invalidUrl'
| 'invalidAuthToken'
| 'badData'
| 'missingData'
| 'failure';
}

export interface IViberSendMessageResult {
status: number;
status_message?: string;
message_token: string;
}

0 comments on commit 38de6d2

Please sign in to comment.