MCPcopy Create free account
hub / github.com/botmasterai/botmaster / sendDefaultButtonMessageTo

Method sendDefaultButtonMessageTo

lib/base_bot.js:498–560  ·  view source on GitHub ↗

* sendDefaultButtonMessageTo() makes it easier to send a default set of * buttons. The default button type is the Messenger quick_replies, where * the payload is the same as the button title and the content_type is text. * * @param {Array} buttonTitles array of button titles (no longer t

(buttonTitles, textOrAttachment, recipientId)

Source from the content-addressed store, hash-verified

496 * 'Please select "button1" or "button2"', update.sender.id,);
497 */
498 sendDefaultButtonMessageTo(buttonTitles, textOrAttachment, recipientId) {
499 const validateSendDefaultButtonMessageToArguments = () => {
500 let err = null;
501 if (!this.sends.quickReply) {
502 err = new SendMessageTypeError(this.type, 'quick replies');
503 } else if (buttonTitles.length > 10) {
504 err = new RangeError('buttonTitles must be of length 10 or less');
505 }
506
507 if (textOrAttachment) {
508 if (textOrAttachment.constructor === String) {
509 if (!this.sends.text) {
510 err = new SendMessageTypeError(this.type, 'text');
511 }
512 } else if (textOrAttachment.constructor === Object && textOrAttachment.type) {
513 if (!this.sends.attachment) {
514 err = new SendMessageTypeError(this.type, 'attachment');
515 } else if (!this.sends.attachment[textOrAttachment.type]) {
516 err = new SendMessageTypeError(this.type,
517 `${textOrAttachment.type} attachment`);
518 }
519 } else {
520 err = new TypeError('third argument must be a "String", an ' +
521 'attachment "Object" or absent');
522 }
523 }
524
525 return err;
526 };
527
528 const potentialError = validateSendDefaultButtonMessageToArguments();
529 if (potentialError) {
530 return Promise.reject(potentialError);
531 }
532
533 // //////////////////////////////////////////////////////
534 // actual code after validating with
535 // validateSendDefaultButtonMessageToArguments function
536 // //////////////////////////////////////////////////////
537
538 const outgoingMessage = this.createOutgoingMessage();
539 outgoingMessage.addRecipientById(recipientId);
540 // deal with textOrAttachment
541 if (!textOrAttachment && this.sends.text) {
542 outgoingMessage.addText('Please select one of:');
543 } else if (textOrAttachment.constructor === String) {
544 outgoingMessage.addText(textOrAttachment);
545 } else {
546 // it must be an attachment or an error would have been thrown
547 outgoingMessage.addAttachment(textOrAttachment);
548 }
549
550 const quickReplies = [];
551 for (const buttonTitle of buttonTitles) {
552 quickReplies.push({
553 content_type: 'text',
554 title: buttonTitle,
555 payload: buttonTitle, // indeed, in default mode payload is buttonTitle

Callers 1

use.jsFile · 0.80

Calls 6

createOutgoingMessageMethod · 0.95
sendMessageMethod · 0.95
addRecipientByIdMethod · 0.80
addTextMethod · 0.80
addAttachmentMethod · 0.80
addQuickRepliesMethod · 0.80

Tested by

no test coverage detected