All files / src Messenger.js

100% Statements 148/148
100% Branches 81/81
100% Functions 43/43
100% Lines 146/146

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586                                                                                                          2x   2x 1x     1x       10x 10x 10x 10x 1x   10x 1x   10x 1x   10x 1x   10x 1x   10x 1x   10x 1x   10x 1x   10x 1x   10x 1x             18x     18x 18x             18x         15x                                                                                                 15x       3x           3x 3x       10x             10x 10x       2x             2x 2x       2x 1x     1x                     1x       1x       4x 1x   3x 1x     2x 2x         2x 1x     2x       3x 1x     2x 1x     1x 1x         1x       1x 1x     1x       5x 5x 2x     3x         3x 2x 2x 1x   1x     2x               2x 2x 2x 1x   2x 2x               1x 1x                 2x 2x 1x   2x 2x                 1x 1x                 2x 2x 1x     2x 2x                 2x 1x   1x     1x               1x                 3x 3x 1x     3x 3x 1x   2x     2x               1x                 3x 1x   2x 1x   1x     1x               1x                 3x 3x 1x     4x 3x 1x     2x     2x               1x                 2x 1x   1x     1x               1x                 2x 1x   1x     1x               1x                 2x 1x   1x     1x               1x                   2x 1x   1x     1x               1x                                                          
import EventEmitter from 'events';
 
import * as NodeUrl from 'url';
import fetch from 'node-fetch';
 
import {
  Text,
  Button,
  Address,
  Element,
  Summary,
  Adjustment,
  DefaultAction,
  OpenGraphElement,
} from './elements';
 
import {
  Image,
  Audio,
  Video,
  File,
} from './attachments';
 
import {
  ButtonTemplate,
  GenericTemplate,
  ReceiptTemplate,
  ListTemplate,
  OpenGraphTemplate,
} from './templates';
 
import {
  FB_API_VERSION,
  SENDER_ACTIONS,
  GET_STARTED_LIMIT,
  WHITELISTED_DOMAIN_MAX,
  TAGS,
} from './constants';
 
import {
  QuickReplies,
  QuickReply,
} from './QuickReplies';
 
import {
  GreetingText,
  PersistentMenu,
  PersistentMenuItem,
} from './messenger_profile';
 
 
class Messenger extends EventEmitter {
  constructor(opts = {}) {
    super();
 
    if (!Object.prototype.hasOwnProperty.call(opts, 'pageAccessToken')) {
      throw new Error('PAGE_ACCESS_TOKEN is missing.');
    }
 
    this.pageAccessToken = opts.pageAccessToken;
  }
 
  handle(payload) {
    const entries = payload.entry;
    entries.forEach((entry) => {
      entry.messaging.forEach((message) => {
        if (message.message) {
          this.emit('message', message);
        }
        if (message.delivery) {
          this.emit('delivery', message);
        }
        if (message.read) {
          this.emit('read', message);
        }
        if (message.postback) {
          this.emit('postback', message);
        }
        if (message.account_linking) {
          this.emit('account_linking', message);
        }
        if (message.optin) {
          this.emit('optin', message);
        }
        if (message.referral) {
          this.emit('referral', message);
        }
        if (message.payment) {
          this.emit('payment', message);
        }
        if (message.checkout_update) {
          this.emit('checkout_update', message);
        }
        if (message.pre_checkout) {
          this.emit('pre_checkout', message);
        }
      });
    });
  }
 
  buildURL(pathname, queryParams) {
    const defaultqueryParams = {
      access_token: this.pageAccessToken,
    };
    const query = Object.assign({}, queryParams, defaultqueryParams);
    const obj = {
      protocol: 'https',
      host: 'graph.facebook.com',
      pathname: `/${FB_API_VERSION}/${pathname}`,
      query,
    };
 
    return NodeUrl.format(obj);
  }
 
  static handleError(result) {
    /* istanbul ignore if */
    if ({}.hasOwnProperty.call(result, 'error')) {
      switch (result.error.code) {
        case 4:
          if (result.error.subcode === 2018022) {
            throw new Error('Too many send requests to phone numbers.');
          }
          break;
        case 1200:
          throw new Error('Temporary send message failure. Please try again later.');
        case 613:
          throw new Error('Calls to this API have exceeded the rate limit.');
        case 100:
          switch (result.error.subcode) {
            case 2018109:
              throw new Error('Attachment size exceeds allowable limit.');
            case 2018001:
              throw new Error('No matching user found.');
            case 2018034:
              throw new Error('Message cannot be empty.');
            default:
              throw new Error(result.error.message);
          }
        case 10:
          switch (result.error.subcode) {
            case 2018065:
              throw new Error('This message is sent outside of allowed window. You need page_messaging_subscriptions permission to be able to do it.');
            case 2018108:
            default:
              throw new Error('This Person Cannot Receive Messages: This person isn\'t receiving messages from you right now.');
          }
        case 200:
          switch (result.error.subcode) {
            case 2018028:
              throw new Error('Cannot message users who are not admins, developers or testers of the app until pages_messaging permission is reviewed and the app is live.');
            case 2018027:
              throw new Error('Cannot message users who are not admins, developers or testers of the app until pages_messaging_phone_number permission is reviewed and the app is live.');
            case 2018021:
              throw new Error('Requires phone matching access fee to be paid by this page unless the recipient user is an admin, developer, or tester of the app.');
            default:
              throw new Error(result.error.message || 'Unknown error occurred.');
          }
        case 190:
          throw new Error('Invalid OAuth access token.');
        case 10303:
          throw new Error('Invalid account_linking_token.');
        default:
          throw new Error(result.error.message || 'Unknown error occurred.');
      }
    }
    return result;
  }
 
  get(url) {
    return fetch(url, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    })
      .then(response => response.json())
      .then(res => this.constructor.handleError(res));
  }
 
  post(url, body = {}) {
    return fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(body),
    })
      .then(response => response.json())
      .then(res => this.constructor.handleError(res));
  }
 
  delete(url, body) {
    return fetch(url, {
      method: 'DELETE',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(body),
    })
      .then(response => response.json())
      .then(res => this.constructor.handleError(res));
  }
 
  getUser(id) {
    if (!id) {
      throw new Error('A user ID is required.');
    }
 
    const fields = [
      'first_name',
      'last_name',
      'profile_pic',
      'locale',
      'timezone',
      'gender',
      'is_payment_enabled',
      'last_ad_referral',
    ].join();
 
    const url = this.buildURL(id, {
      fields,
    });
 
    return this.get(url);
  }
 
  send(payload, id, tag = '') {
    if (!id) {
      throw new Error('A user ID is required.');
    }
    if (tag && TAGS.indexOf(tag) === -1) {
      throw new Error('Invalid tag provided.');
    }
 
    const url = this.buildURL('me/messages');
    const body = {
      recipient: { id },
      message: payload,
    };
 
    if (tag) {
      body.tag = tag;
    }
 
    return this.post(url, body);
  }
 
  senderAction(senderAction, id) {
    if (SENDER_ACTIONS.indexOf(senderAction) === -1) {
      throw new Error('Invalid sender_action provided.');
    }
 
    if (!id) {
      throw new Error('A user ID is required.');
    }
 
    const url = this.buildURL('me/messages');
    const body = {
      recipient: { id },
      sender_action: senderAction,
    };
 
    return this.post(url, body);
  }
 
  messageAttachment(payload) {
    const url = this.buildURL('me/message_attachments');
    const body = {
      message: payload,
    };
    return this.post(url, body);
  }
 
  messengerCode({ type = 'standard', image_size = 1000, ref = '' } = {}) {
    const url = this.buildURL('me/messenger_codes');
    if (image_size < 100 || image_size > 2000) {
      throw new Error('Size Supported range: 100-2000 px');
    }
 
    const body = {
      type,
      image_size,
    };
 
    if (ref) {
      const isValid = /^[\w+/=\-.:]{1,250}$/.test(ref);
      if (!isValid) {
        throw new Error(`Invalid ref provided: ${ref}`);
      }
      body.data = { ref };
    }
 
    return this.post(url, body);
  }
 
  /**
   * Enable/Disable NLP
   * @param {boolean} value True or false
   */
  setNLP(value, custom_token = '') {
    const nlp_enabled = Boolean(value);
    const params = { nlp_enabled };
    if (custom_token) {
      params.custom_token = custom_token;
    }
    const url = this.buildURL('me/nlp_configs', params);
    return this.post(url);
  }
 
  /**
  * Subscribes an app to a page
  * @return {Object} JSON Response object
  */
  subscribeAppToPage() {
    const url = this.buildURL('me/subscribed_apps');
    return this.post(url);
  }
 
  /**
   * Read multiple Messenger Profile properties at the same time
   * @param  {Mixed} props Array or comma separated list of properties to read
   * @return {Object} JSON Response object
   */
  getMessengerProfile(props) {
    let fields = props;
    if (Array.isArray(props)) {
      fields = props.join();
    }
    const url = this.buildURL('me/messenger_profile', { fields });
    return this.get(url, fields);
  }
 
  /**
   * Set multiple Messenger Profile properties at the same time
   * @param {Array} payload Property names and their new settings
   * @return {Object} JSON Response object
   */
  setMessengerProfile(payload) {
    const url = this.buildURL('me/messenger_profile');
    return this.post(url, payload);
  }
 
  /**
   * Delete multiple Messenger Profile properties at the same time
   * @param  {Array} payload Properties to be deleted
   * @return {Object} JSON Response object
   */
  deleteMessengerProfile(fields) {
    let payload = fields;
    if (!Array.isArray(fields)) {
      payload = [fields];
    }
 
    const url = this.buildURL('me/messenger_profile');
    return this.delete(url, { payload });
  }
 
  /**
   * Ses the Get Started Button
   * @param {string} payload Payload that will be sent back to your webhook
   * @return {Object} JSON Response object
   */
  setGetStarted(payload) {
    if (payload.length > GET_STARTED_LIMIT) {
      throw new Error(`Get Started payload limit is ${GET_STARTED_LIMIT}.`);
    }
    const params = {
      get_started: { payload },
    };
    return this.setMessengerProfile(params);
  }
 
  /**
   * Deletes the get started button
   * @return {Object} JSON Response object
   */
  deleteGetStarted() {
    return this.deleteMessengerProfile(['get_started']);
  }
 
  /**
   * Sets the Greeting Text
   * @param {Array} payload Array of Greeting text objects
   * @return {Object} JSON Response object
   */
  setGreetingText(greetings) {
    let payload = greetings;
    if (!Array.isArray(greetings)) {
      payload = [greetings];
    }
 
    const hasDefault = payload.some(x => x.locale === 'default');
    if (!hasDefault) {
      throw new Error('You must provide a default locale');
    }
    const params = {
      greeting: payload,
    };
    return this.setMessengerProfile(params);
  }
 
  /**
   * Deletes the greeting text
   * @return {Object} JSON Response object
   */
  deleteGreetingText() {
    return this.deleteMessengerProfile(['greeting']);
  }
 
  /**
   * Sets whitelisted domains
   * @param {Array} payload Domains to whitelist
   * @return {Object} JSON Response object
   */
  setDomainWhitelist(payload) {
    if (!payload) {
      throw new Error('A domain must be provided.');
    }
    if (payload.length > WHITELISTED_DOMAIN_MAX) {
      throw new Error(`You may only whitelist ${WHITELISTED_DOMAIN_MAX} domains.`);
    }
    const params = {
      whitelisted_domains: payload,
    };
    return this.setMessengerProfile(params);
  }
 
  /**
   * Deletes whitelisted domains
   * @return {Object} JSON Response object
   */
  deleteDomainWhitelist() {
    return this.deleteMessengerProfile(['whitelisted_domains']);
  }
 
  /**
   * Sets the persistent menu
   * @param {Object} payload PersistentMenu settings
   * @return {Object} JSON Response object
   */
  setPersistentMenu(menus) {
    let payload = menus;
    if (!Array.isArray(menus)) {
      payload = [menus];
    }
 
    const hasDefault = payload.some(x => x.locale === 'default');
    if (!hasDefault) {
      throw new Error('You must provide a default locale');
    }
 
    const params = {
      persistent_menu: payload,
    };
    return this.setMessengerProfile(params);
  }
 
  /**
   * Deletes the persistent menu
   * @return {Object} JSON Response object
   */
  deletePersistentMenu() {
    return this.deleteMessengerProfile(['persistent_menu']);
  }
 
  /**
   * Sets an account linking url
   * @param {string} payload Account linking URL
   * @return {Object} JSON Response object
   */
  setAccountLinkingURL(payload) {
    if (!payload) {
      throw new Error('A URL must be provided.');
    }
    const params = {
      account_linking_url: payload,
    };
    return this.setMessengerProfile(params);
  }
 
  /**
   * Deletes the account linking URL
   * @return {Object} JSON Response object
   */
  deleteAccountLinkingURL() {
    return this.deleteMessengerProfile(['account_linking_url']);
  }
 
  /**
   * Sets payment settings
   * @param {Object} payload Target Audience settings
   * @return {Object} JSON Response object
   */
  setPaymentSettings(payload) {
    if (!payload) {
      throw new Error('Payment settings must be provided.');
    }
    const params = {
      payment_settings: payload,
    };
    return this.setMessengerProfile(params);
  }
 
  /**
   * Deletes payment settings
   * @return {Object} JSON Response object
   */
  deletePaymentSettings() {
    return this.deleteMessengerProfile(['payment_settings']);
  }
 
  /**
   * Sets the target audience
   * @param {Object} payload Target Audience settings
   * @return {Object} JSON Response object
   */
  setTargetAudience(payload) {
    if (!payload) {
      throw new Error('A target audience must be provided.');
    }
    const params = {
      target_audience: payload,
    };
    return this.setMessengerProfile(params);
  }
 
  /**
   * Deletes the target audience
   * @return {Object} JSON Response object
   */
  deleteTargetAudience() {
    return this.deleteMessengerProfile(['target_audience']);
  }
 
  /**
   * Sets the home url
   * See: https://developers.facebook.com/docs/messenger-platform/messenger-profile/home-url
   * @param {Object} payload Home URL settings
   * @return {Object} JSON Response object
   */
  setHomeURL(payload) {
    if (!payload) {
      throw new Error('A URL must be provided.');
    }
    const params = {
      home_url: payload,
    };
    return this.setMessengerProfile(params);
  }
 
  /**
   * Deletes the home URL
   * @return {Object} JSON Response object
   */
  deleteHomeURL() {
    return this.deleteMessengerProfile(['home_url']);
  }
}
 
export {
  Messenger,
  Text,
  Button,
  Address,
  Summary,
  Adjustment,
  DefaultAction,
  OpenGraphElement,
  Element,
  Image,
  Audio,
  Video,
  File,
  ButtonTemplate,
  GenericTemplate,
  ReceiptTemplate,
  ListTemplate,
  OpenGraphTemplate,
  QuickReplies,
  QuickReply,
  GreetingText,
  PersistentMenu,
  PersistentMenuItem,
};