All files / src/components/Messaging/QuickReply index.js

78.26% Statements 18/23
78.26% Branches 18/23
50% Functions 2/4
94.74% Lines 18/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58              14x 14x 14x 14x 14x 14x   14x 14x 14x                                 14x       14x   1x 1x 1x   1x 1x     13x 12x                  
/* eslint-disable no-param-reassign */
import { QUICKREPLY_TYPE } from '../constants';
/*
  ref: https://developers.facebook.com/docs/messenger-platform/reference/send-api/quick-replies
*/
class QuickReply {
  constructor(root, props) {
    this.root = root;
    this.props = props;
    this.type = props.type || QUICKREPLY_TYPE.TEXT;
    this.payload = props.payload || false;
    this.image_url = props.image || false;
    this.title = props.title || '';
 
    Iif (this.type === QUICKREPLY_TYPE.TEXT && !props.title) throw new Error('Title is required for QuickReply');
    Iif (this.title.length > 20) throw new Error('Quickreply title can not be over 20 character');
    Iif (this.type === QUICKREPLY_TYPE.TEXT && !props.payload) throw new Error('Must provide a payload for Text quickreply');
  }
 
  appendChild() {
    // noop
  }
 
  removeChild() {
    // noop
  }
 
  render(Message) {
    /*
    Required if content_type is 'text'.
     Custom data that will be sent back to you via
     the messaging_postbacks webhook event. 1000 character limit.
    */
    const quickreply = {
      content_type: this.type
    }
 
    switch (this.type) {
      case QUICKREPLY_TYPE.TEXT:
        quickreply.title = this.title;
        quickreply.payload = this.payload;
        Iif (this.image_url) quickreply.image_url = this.image_url;
 
        Message.addQuickreply(quickreply);
        break;
 
      case QUICKREPLY_TYPE.LOCATION:
        Message.addQuickreply(quickreply);
        break;
 
      default:
        throw new Error('quickreply type is not valid');
    }
  }
}
 
export default QuickReply;