Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Button
- {
- private title:string;
- private type:string;
- constructor(title:string, type:string)
- {
- this.title = title;
- this.type = type;
- }
- }
- class PostbackButton extends Button
- {
- private payload:string;
- constructor(title:string, payload:string)
- {
- super(title, "postback");
- this.payload = payload;
- }
- }
- class URLButton extends Button
- {
- private url:string;
- private webview_height_ratio:string = "compact";
- constructor(title:string, url:string)
- {
- super(title, "web_url");
- this.url = url;
- }
- }
- class QuickReply
- {
- }
- class QuickText extends QuickReply{
- private title:string;
- private payload:string;
- constructor(title:string, payload:string)
- {
- super();
- this.title = title;
- this.payload = payload;
- }
- }
- class loc extends QuickReply
- {
- private content_type:string = "location";
- constructor()
- {
- super();
- }
- }
- class Payload
- {}
- class Attachment
- {
- type:string;
- public static TYPE_TEMPLATE:string = "template";
- public static TYPE_IMAGE:string = "image";
- private payload:Payload;
- constructor(type:string)
- {
- this.type = type;
- }
- insertPayload(payload:Payload)
- {
- this.payload = payload;
- }
- }
- class ButtonPayload extends Payload
- {
- text:string;
- buttons:Array<Button>;
- template_type:string = "button";
- constructor(text:string)
- {
- super();
- this.text = text;
- this.buttons = new Array<Button>();
- }
- addButton(b:Button)
- {
- this.buttons.push(b);
- }
- }
- class ImagePayload
- {
- url:string;
- constructor(url:string)
- {
- this.url = url;
- }
- }
- class Recipient
- {
- private id:string;
- constructor(id:string)
- {
- this.id = id;
- }
- }
- class SenderStatus
- {
- public static SEEN = "mark_seen";
- public static TYPING_OFF = "typing_off";
- public static TYPING_ON = "typing_on";
- }
- class Message {
- fb_id:string;
- textMsg:string = "";
- quickreplies:Array<QuickReply>;
- buttons:Array<Button>;
- senderstatus:string;;
- seen:string;
- attachment:Attachment;
- constructor()
- {
- this.buttons = new Array<Button>();
- this.quickreplies = new Array<QuickReply>();
- }
- to(facebook_id:string)
- {
- this.fb_id = facebook_id;
- }
- text(textMsg:string)
- {
- this.textMsg = textMsg;
- }
- quickText(title:string, payload:string)
- {
- this.quickreplies.push(new QuickText(title, payload));
- }
- postbackButton(title:string, payload:string)
- {
- this.buttons.push(new PostbackButton(title, payload));
- }
- urlButton(title:string, url:string)
- {
- this.buttons.push(new URLButton(title, url));
- }
- senderStatus(senderstatus:string)
- {
- this.senderstatus = senderstatus;
- }
- requestLocation()
- {
- this.quickreplies.push(new loc());
- }
- addAttachment(attachment:Attachment)
- {
- this.attachment = attachment;
- }
- generate()
- {
- let main_reply:any = {};
- //Recipient
- let recipient:any = {id:this.fb_id};
- main_reply['recipient'] = recipient;
- //Defining Message Body
- let msg:any = {};
- if(this.textMsg!="")
- {
- msg['text'] = this.textMsg;
- }
- if(this.quickreplies.length>0)
- {
- msg['quick_replies'] = this.quickreplies;
- }
- if(this.buttons.length>0)
- {
- msg['buttons'] = this.buttons;
- }
- if(this.senderstatus!=null || this.senderstatus!="")
- {
- msg['sender_action'] = this.senderstatus;
- }
- if(this.attachment!=null)
- {
- msg['attachment'] = this.attachment;
- }
- main_reply['message'] = msg;
- return JSON.stringify(main_reply, null, 2);
- }
- }
- let attach = new Attachment(Attachment.TYPE_IMAGE);
- let i = new ImagePayload("http://imageurl.com");
- attach.insertPayload(i);
- let msg = new Message();
- msg.to("1234");
- msg.text("Have ?");
- msg.quickText("yes", 'y');
- msg.quickText("No", 'n');
- msg.postbackButton("Stop msg", 'stop');
- msg.urlButton("Test", "http://google.com");
- msg.requestLocation();
- msg.addAttachment(attach);
- msg.senderStatus(SenderStatus.TYPING_ON);
- let res = msg.generate();
- console.log(res);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement