Skip to content

Commit

Permalink
Merge branch 'vNext' of github.com:chrispanag/ebony into vNext
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispanag committed May 18, 2021
2 parents 2869ef4 + 739648c commit 236797d
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 76 deletions.
71 changes: 69 additions & 2 deletions examples/viber-demo-bot/src/modules/getStarted/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bot } from '../../bot';
import { Carousel, Button, Message } from '@ebenos/viber-elements';
import { RichMedia, Button, Message } from '@ebenos/viber-elements';
import { Carousel } from '@ebenos/viber-elements';
import { addAction, addTextRule, InMemoryUser } from '@ebenos/framework';
import getStartedModule from '.';

Expand Down Expand Up @@ -46,7 +47,7 @@ async function getTest6(user: InMemoryUser, payload: string) {
sender: {
name: 'Giorgos'
},
rich_media: new Carousel({
rich_media: new RichMedia({
ButtonsGroupColumns: 2,
ButtonsGroupRows: 1,
BgColor: '#FFFFFF',
Expand All @@ -69,6 +70,72 @@ async function getTest6(user: InMemoryUser, payload: string) {
.end();
}

const staticButtons = [
{
title: '❓ Δεν μπορώ να συνδεθώ στον λογαριασμό μου.',
subtitle: 'Προβλήματα σύνδεσης στο νέο Pamestoixima.gr.',
buttons: [
{
text: 'ΠΕΡΙΣΣΟΤΕΡΑ...',
payload: 'faq_account'
}
]
},
{
title: '❓ Δεν μπορώ να δω το ιστορικό συναλλαγών/στοιχημάτων.',
subtitle: 'Τοποθετημένα στοιχήματα, παλιά στοιχήματα και συναλλαγές.',
buttons: [
{
text: 'ΠΕΡΙΣΣΟΤΕΡΑ...',
payload: 'faq_history'
}
]
},
{
title: '❓ Έχω πρόβλημα με το site.',
subtitle: 'Δεν ανοίγει ή δεν μπορώ να μπω καθόλου στο site του νέου Pamestoixima.gr',
buttons: [
{
text: 'ΠΕΡΙΣΣΟΤΕΡΑ...',
payload: 'technical'
}
]
}
];

addAction(getStartedModule, getTest7);
addTextRule(getStartedModule, getTest7, /TEST7/);
async function getTest7(user: InMemoryUser, payload: string) {
await bot
.scenario(user)
.send(
new Message({
sender: {
name: 'Giorgos'
},
rich_media: new Carousel(staticButtons)
})
)
.end();
}

addAction(getStartedModule, getTest8);
addTextRule(getStartedModule, getTest8, /TEST9/);
async function getTest8(user: InMemoryUser, payload: string) {
await bot
.scenario(user)
.send(
new Message({
sender: {
name: 'Giorgos'
},
media:
'https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif'
})
)
.end();
}

addAction(getStartedModule, getStartedSecond);
addTextRule(getStartedModule, getStartedSecond, /.*/);
async function getStartedSecond(user: InMemoryUser, payload: string) {
Expand Down
16 changes: 8 additions & 8 deletions packages/viber-elements/lib/viberAPI/attachments.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
ICarouselOptions,
IRichMediaOptions,
IPictureOptions,
ActionType,
TextVAlign,
TextHAlign,
TextSize,
ISerializedCarousel,
ISerializedRichMedia,
MediaType,
ScaleType,
OpenURLType,
Expand All @@ -19,7 +19,7 @@ import {
} from './interfaces';
import { ISerializable } from '@ebenos/framework';

/**Viber Carousel Button */
/**Viber RichMedia Button */
export class Button implements ISerializable {
public Columns = 6;
public Rows = 1;
Expand Down Expand Up @@ -166,15 +166,15 @@ export class Button implements ISerializable {
}
}

/** Viber Carousel Attachment */
export class Carousel implements ISerializable {
/** Viber RichMedia Attachment */
export class RichMedia implements ISerializable {
public ButtonsGroupColumns?: number;
public ButtonsGroupRows?: number;
public BgColor?: string;
public Buttons: Button[];
public HeightScale = 100;

constructor(options: ICarouselOptions) {
constructor(options: IRichMediaOptions) {
const { ButtonsGroupColumns, ButtonsGroupRows, BgColor, Buttons, HeightScale } = options;

this.ButtonsGroupColumns = ButtonsGroupColumns;
Expand All @@ -187,8 +187,8 @@ export class Carousel implements ISerializable {
}
}

public serialize(): ISerializedCarousel {
const obj: ISerializedCarousel = {
public serialize(): ISerializedRichMedia {
const obj: ISerializedRichMedia = {
Type: 'rich_media',
Buttons: this.Buttons.map((b) => b.serialize()),
HeightScale: this.HeightScale
Expand Down
167 changes: 167 additions & 0 deletions packages/viber-elements/lib/viberAPI/carousel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { Button, RichMedia } from './attachments';
import { IFrame, IRichMediaOptions } from './interfaces';
import { ISerializable } from '@ebenos/framework';

export interface ICarouselElement {
title?: string;
image?: string;
subtitle?: string;
buttons: Array<{ text: string; url: string } | { text: string; payload: string }>;
}

interface ICarouselElementStyle {
textColor: string;
backgroundColor: string;
}

type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};

export interface ICarouselStyle {
button: ICarouselElementStyle;
title: ICarouselElementStyle;
subtitle: ICarouselElementStyle;
imageBackgroundColor: string;
backgroundColor: string;
}
export interface ICarouselOptions {
style?: DeepPartial<ICarouselStyle>;
frame?: IFrame;
}

function createCarousel(
elements: ICarouselElement[],
options?: ICarouselOptions
): IRichMediaOptions {
const color = (k: keyof ICarouselStyle, t?: keyof ICarouselElementStyle) => {
const defaultStyle: ICarouselStyle = {
title: {
textColor: '#ffffff',
backgroundColor: '#ffffff'
},
button: {
textColor: '#ffffff',
backgroundColor: '#2db9b9'
},
subtitle: {
backgroundColor: '#ffffff',
textColor: '#696969'
},
backgroundColor: '#ffffff',
imageBackgroundColor: '#ffffff'
};

if (k === 'imageBackgroundColor' || k === 'backgroundColor') {
return options?.style ? options.style[k] : defaultStyle[k];
}

if (t === undefined) {
throw new Error('Need to identify subproperty');
}

const element = options?.style ? options.style[k] : defaultStyle[k];
if (element === undefined) {
throw new Error('Non-existant element');
}

const style = element[t];

return style ? style : defaultStyle[k][t];
};

const frame = options?.frame ? options.frame : undefined;

let hasSubtitle = false,
hasTitle = false,
hasImage = false,
largerButtons = 0;
for (const e of elements) {
if (e.subtitle !== undefined) {
hasSubtitle = true;
}

if (e.title !== undefined) {
hasTitle = true;
}

if (e.image !== undefined) {
hasImage = true;
}

if (largerButtons < e.buttons.length) {
largerButtons = e.buttons.length;
}
}

const maxRows = largerButtons + (hasSubtitle ? 1 : 0) + (hasTitle ? 1 : 0) + (hasImage ? 3 : 0);
const buttonsArray = elements.map((e) => [
...(hasImage
? [
new Button({
Rows: 3,
BgColor: color('imageBackgroundColor'),
Columns: 6,
Image: e.image,
ActionType: 'none',
ActionBody: ''
})
]
: []),
...(hasTitle
? [
new Button({
Rows: 1,
Columns: 6,
BgColor: color('title', 'backgroundColor'),
Text: `<b><font color="${color('title', 'textColor')}">${e.title}</font></b>`,
ActionType: 'none',
ActionBody: ''
})
]
: []),
...(hasSubtitle
? [
new Button({
Rows: 1,
Columns: 6,
BgColor: color('subtitle', 'backgroundColor'),
Text: `<font color="${color('subtitle', 'textColor')}">${
e.subtitle ? e.subtitle : ''
}</font>`,
ActionType: 'none',
TextSize: 'small',
TextHAlign: 'left',
ActionBody: ''
})
]
: []),
...e.buttons.map(
(button) =>
new Button({
Rows: 1,
Columns: 6,
Text: `<font color="${color('button', 'textColor')}"><b>${
button.text
}</b></font>`,
BgColor: color('button', 'backgroundColor'),
ActionType: 'url' in button ? 'open-url' : 'reply',
Silent: 'url' in button,
ActionBody: 'url' in button ? button.url : button.payload,
Frame: frame
})
)
]);
return {
ButtonsGroupColumns: 6,
ButtonsGroupRows: maxRows,
BgColor: color('backgroundColor'),
Buttons: buttonsArray.flat()
};
}

export class Carousel extends RichMedia implements ISerializable {
constructor(elements: ICarouselElement[], options?: ICarouselOptions) {
super(createCarousel(elements, options));
}
}
1 change: 1 addition & 0 deletions packages/viber-elements/lib/viberAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './attachments';
export * from './keyboard';
export * from './message';
export * from './interfaces';
export * from './carousel';
Loading

0 comments on commit 236797d

Please sign in to comment.