Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Button
  2. {
  3.     private title:string;
  4.     private type:string;
  5.    
  6.     constructor(title:string, type:string)
  7.     {
  8.         this.title = title;
  9.         this.type = type;
  10.     }
  11.    
  12. }
  13. class PostbackButton extends Button
  14. {
  15.     private payload:string;
  16.     constructor(title:string, payload:string)
  17.     {
  18.         super(title, "postback");
  19.         this.payload = payload;
  20.     }
  21. }
  22.  
  23.  
  24. class URLButton extends Button
  25. {
  26.    
  27.     private url:string;
  28.    
  29.     private webview_height_ratio:string = "compact";
  30.     constructor(title:string, url:string)
  31.     {
  32.         super(title, "web_url");
  33.         this.url = url;
  34.        
  35.     }
  36.  
  37.  
  38. }
  39. class QuickReply
  40. {
  41.  
  42. }
  43. class QuickText extends QuickReply{
  44.     private title:string;
  45.     private payload:string;
  46.     constructor(title:string, payload:string)
  47.     {
  48.         super();
  49.         this.title = title;
  50.         this.payload = payload;
  51.     }
  52.  
  53. }
  54. class loc extends QuickReply
  55. {
  56.     private content_type:string = "location";
  57.     constructor()
  58.     {
  59.         super();
  60.     }
  61.  
  62. }
  63. class Payload
  64. {}
  65.  
  66. class Attachment
  67. {
  68.     type:string;
  69.     public static TYPE_TEMPLATE:string = "template";
  70.     public static TYPE_IMAGE:string = "image";
  71.     private payload:Payload;
  72.  
  73.     constructor(type:string)
  74.     {
  75.         this.type = type;
  76.        
  77.     }
  78.     insertPayload(payload:Payload)
  79.     {
  80.         this.payload = payload;
  81.     }
  82. }
  83. class ButtonPayload extends Payload
  84. {
  85.     text:string;
  86.     buttons:Array<Button>;
  87.     template_type:string = "button";
  88.     constructor(text:string)
  89.     {
  90.         super();
  91.         this.text = text;
  92.         this.buttons = new Array<Button>();
  93.     }
  94.     addButton(b:Button)
  95.     {
  96.         this.buttons.push(b);
  97.     }
  98. }
  99.  
  100.  
  101.  
  102. class ImagePayload
  103. {
  104.     url:string;
  105.     constructor(url:string)
  106.     {
  107.         this.url = url;
  108.     }
  109. }
  110. class Recipient
  111. {
  112.     private id:string;
  113.     constructor(id:string)
  114.  
  115.     {
  116.         this.id = id;
  117.     }
  118. }
  119. class SenderStatus
  120. {
  121.     public static SEEN = "mark_seen";
  122.     public static TYPING_OFF = "typing_off";
  123.     public static TYPING_ON = "typing_on";
  124. }
  125.  
  126. class Message {
  127.     fb_id:string;
  128.     textMsg:string = "";
  129.     quickreplies:Array<QuickReply>;
  130.     buttons:Array<Button>;
  131.     senderstatus:string;;
  132.     seen:string;
  133.  
  134.     attachment:Attachment;
  135.  
  136.     constructor()
  137.     {
  138.         this.buttons = new Array<Button>();
  139.         this.quickreplies = new Array<QuickReply>();
  140.        
  141.        
  142.     }
  143.  
  144.     to(facebook_id:string)
  145.     {
  146.         this.fb_id = facebook_id;
  147.     }
  148.     text(textMsg:string)
  149.     {
  150.         this.textMsg = textMsg;
  151.     }
  152.     quickText(title:string, payload:string)
  153.     {
  154.         this.quickreplies.push(new QuickText(title, payload));
  155.     }
  156.     postbackButton(title:string, payload:string)
  157.     {
  158.         this.buttons.push(new PostbackButton(title, payload));
  159.     }
  160.     urlButton(title:string, url:string)
  161.     {
  162.         this.buttons.push(new URLButton(title, url));
  163.     }
  164.     senderStatus(senderstatus:string)
  165.     {
  166.         this.senderstatus = senderstatus;
  167.     }
  168.  
  169.     requestLocation()
  170.     {
  171.         this.quickreplies.push(new loc());
  172.     }
  173.     addAttachment(attachment:Attachment)
  174.     {
  175.         this.attachment = attachment;
  176.     }
  177.    
  178.     generate()
  179.     {
  180.  
  181.         let main_reply:any = {};
  182.         //Recipient
  183.         let recipient:any = {id:this.fb_id};
  184.         main_reply['recipient'] = recipient;
  185.        
  186.         //Defining Message Body
  187.  
  188.         let msg:any = {};  
  189.         if(this.textMsg!="")      
  190.         {
  191.             msg['text'] = this.textMsg;
  192.         }
  193.         if(this.quickreplies.length>0)
  194.         {
  195.             msg['quick_replies'] = this.quickreplies;
  196.  
  197.         }
  198.         if(this.buttons.length>0)
  199.         {
  200.             msg['buttons'] = this.buttons;
  201.  
  202.         }
  203.         if(this.senderstatus!=null || this.senderstatus!="")      
  204.         {
  205.              msg['sender_action'] = this.senderstatus;
  206.         }
  207.         if(this.attachment!=null)
  208.         {
  209.             msg['attachment'] = this.attachment;
  210.         }
  211.        
  212.         main_reply['message'] = msg;
  213.         return JSON.stringify(main_reply, null, 2);
  214.     }
  215.  
  216. }
  217.  
  218. let attach = new Attachment(Attachment.TYPE_IMAGE);
  219. let i = new ImagePayload("http://imageurl.com");
  220. attach.insertPayload(i);
  221.  
  222. let msg = new Message();
  223. msg.to("1234");
  224. msg.text("Have ?");
  225. msg.quickText("yes", 'y');
  226. msg.quickText("No", 'n');
  227. msg.postbackButton("Stop msg", 'stop');
  228. msg.urlButton("Test", "http://google.com");
  229. msg.requestLocation();
  230. msg.addAttachment(attach);
  231. msg.senderStatus(SenderStatus.TYPING_ON);
  232. let res = msg.generate();
  233. console.log(res);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement