BaseBot

The class from which all Bot classes mus inherit. It contains all the base methods that are accessible via all bot classes. Classes that inherit from BaseBot and want to make implementation specific methods available have to prepend the method name with an underscore; e.g. in botmaster-messenger: _getGetStartedButton

Kind: global class

new BaseBot(settings)

Constructor to the BaseBot class from which all the bot classes inherit. A set a basic functionalities are defined here that have to be implemented in the subclasses in order for them to work.

Param Type Description
settings object inheritors of BaseBot take a settings object as first param.

Example

// In general however, one can instantiate a bot object like this:
const bot = new BaseBotSubClass({ // e.g. MessengerBot
  credentials: <my_base_bot_sub_class_credentials>,
  webhookEnpoint: 'someEndpoint' // only if class requires them
})

baseBot.createOutgoingMessage(message) ⇒ OutgoingMessage

createOutgoingMessage exposes the OutgoingMessage constructor via BaseBot. This simply means one can create their own OutgoingMessage object using any bot object. They can then compose it with all its helper functions

This is the instance version of this method

Kind: instance method of BaseBot
Returns: OutgoingMessage - outgoingMessage. The same object passed in with all the helper functions from OutgoingMessage

Param Type Description
message object base object that the outgoing Message should be based on

baseBot.createOutgoingMessageFor(recipientId) ⇒ OutgoingMessage

same as #createOutgoingMessage, creates empty outgoingMessage with id of the recipient set. Again, this is jut sugar syntax for creating a new outgoingMessage object

This is the instance version of this method

Kind: instance method of BaseBot
Returns: OutgoingMessage - outgoingMessage. A valid OutgoingMessage object with recipient set.

Param Type Description
recipientId string id of the recipient the message is for

baseBot.sendMessage(message, [sendOptions]) ⇒ Promise

sendMessage() falls back to the sendMessage implementation of whatever subclass inherits form BaseBot. The expected format is normally any type of message object that could be sent on to messenger

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with a body object (see example)

Param Type Description
message object
[sendOptions] boolean an object containing options regarding the sending of the message. Currently the only valid options is: ignoreMiddleware.

Example

const outgoingMessage = bot.createOutgoingMessageFor(update.sender.id);
outgoingMessage.addText('Hello world');

bot.sendMessage(outgoingMessage);

Example

// The returned promise for all sendMessage type events resolves with
// a body that looks something like this:
 {
  sentOutgoingMessage: // the OutgoingMessage instance before being formatted
  sentRawMessage: // the OutgoingMessage object after being formatted for the platforms
  raw: rawBody, // the raw response from the platforms received from sending the message
  recipient_id: <id_of_user>,
  message_id: <message_id_of_what_was_just_sent>
 }

// Some platforms may not have either of these parameters. If that's the case,
// the value assigned will be a falsy value

baseBot.sendMessageTo(message, recipientId, [sendOptions]) ⇒ Promise

sendMessageTo() Just makes it easier to send a message without as much structure.

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with a body object (see sendMessage example)

Param Type Description
message object NOT an instance of OutgoingMessage. Use #sendMessage if you want to send instances of OutgoingMessage
recipientId string a string representing the id of the user to whom you want to send the message.
[sendOptions] object see sendOptions for sendMessage

Example

// message object can look something like this:
// as you can see, this is not an OutgoingMessage instance
const message = {
 text: 'Some random text'
}

bot.sendMessageTo(message, update.sender.id);

baseBot.sendTextMessageTo(text, recipientId, [sendOptions]) ⇒ Promise

sendTextMessageTo() Just makes it easier to send a text message with minimal structure.

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with a body object (see sendMessage example)

Param Type Description
text string
recipientId string a string representing the id of the user to whom you want to send the message.
[sendOptions] object see sendOptions for sendMessage

Example

bot.sendTextMessageTo('something super important', update.sender.id);

baseBot.reply(incomingUpdate, text, [sendOptions]) ⇒ Promise

reply() Another way to easily send a text message. In this case, we just send the update that came in as is and then the text we want to send as a reply.

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with a body object (see sendMessage example)

Param Type Description
incomingUpdate object
text string text to send to the user associated with the received update
[sendOptions] object see sendOptions for sendMessage

Example

bot.reply(update, 'something super important!');

baseBot.sendAttachmentTo(attachment, recipientId, [sendOptions]) ⇒ Promise

sendAttachmentTo() makes it easier to send an attachment message with less structure.

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with a body object (see sendMessage example)

Param Type Description
attachment object a valid Messenger style attachment. See here for more on that.
recipientId string a string representing the id of the user to whom you want to send the message.
[sendOptions] object see sendOptions for sendMessage

Example

// attachment object typically looks something like this:
const attachment = {
  type: 'image',
  payload: {
    url: "some_valid_url_of_some_image"
  },
};

bot.sendAttachmentTo(attachment, update.sender.id);

baseBot.sendAttachmentFromUrlTo(type, url, recipientId, [sendOptions]) ⇒ Promise

sendAttachmentFromUrlTo() makes it easier to send an attachment message with minimal structure.

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with a body object (see sendMessage example)

Param Type Description
type string string representing the type of attachment (audio, video, image or file)
url string the url to your file
recipientId string a string representing the id of the user to whom you want to send the message.
[sendOptions] object see sendOptions for sendMessage

Example

bot.sendAttachmentFromURLTo('image', "some image url you've got", update.sender.id);

baseBot.sendDefaultButtonMessageTo(buttonTitles, textOrAttachment, recipientId, [sendOptions]) ⇒ Promise

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.

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with a body object (see sendMessage example)

Param Type Description
buttonTitles Array array of button titles (no longer than 10 in size).
textOrAttachment string_OR_object a string or an attachment object similar to the ones required in bot.sendAttachmentTo. This is meant to provide context to the buttons. I.e. why are there buttons here. A piece of text or an attachment could detail that. If falsy, text will be added that reads: 'Please select one of:'.
recipientId string a string representing the id of the user to whom you want to send the message.
[sendOptions] object see sendOptions for sendMessage

Example

const buttonArray = ['button1', 'button2'];
bot.sendDefaultButtonMessageTo(buttonArray,
  'Please select "button1" or "button2"', update.sender.id,);

baseBot.sendIsTypingMessageTo(recipientId, [sendOptions]) ⇒ Promise

sendIsTypingMessageTo() just sets the is typing status to the platform if available.

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with a body object (see sendMessage example)

Param Type Description
recipientId string a string representing the id of the user to whom you want to send the message.
[sendOptions] object see sendOptions for sendMessage

Example

bot.sendIsTypingMessageTo(update.sender.id);
// the returned value is different from the standard one. it won't have a message_id

baseBot.sendCascade(messageArray, [sendOptions]) ⇒ Promise

sendCascade() allows developers to send a cascade of messages in a sequence. All types of messages can be sent (including raw messages).

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with an array of body objects (see sendMessage example for one said object)

Param Type Description
messageArray Array of messages in a format as such: [{raw: someRawObject}, {message: some valid outgoingMessage}]
[sendOptions] object see sendOptions for sendMessage. will only apply to non rawMessages. (remember that for rawMessages, outgoing middleware is bypassed anyways).

Example

const rawMessage1 = {
  nonStandard: 'message1',
  recipient: {
    id: 'user_id',
  },
};
const message2 = bot.createOutgoingMessageFor(update.sender.id);
message2.addText('some text');

const messageArray = [{ raw: rawMessage1 }, { message: message2 }];

bot.sendCascade(messageArray);

baseBot.sendTextCascadeTo(textArray, recipientId, [sendOptions]) ⇒ Promise

sendTextCascadeTo() is simply a helper function around sendCascadeTo. It allows developers to send a cascade of text messages more easily.

Kind: instance method of BaseBot
Returns: Promise - promise that resolves with an array of body objects (see sendMessage example for one said object)

Param Type Description
textArray Array of messages.
recipientId string a string representing the id of the user to whom you want to send the message.
[sendOptions] object see sendOptions for sendMessage

Example

bot.sendTextCascadeTo(['message1', 'message2'], user.sender.id);

baseBot.sendRawMessage(rawMessage) ⇒ Promise

sendRawMessage() simply sends a raw platform dependent message. This method calls sendMessage in each botClass without calling formatOutgoingMessage before. It's really just sugar around sendMessage which shouldn't be used directly.

Kind: instance method of BaseBot
Returns: Promise - promise

Param Type
rawMessage Object

baseBot.getUserInfo(userId) ⇒ Promise

Retrieves the basic user info from a user if platform supports it

Kind: instance method of BaseBot
Returns: Promise - promise that resolves into the user info or an empty object by default

Param Type
userId string

BaseBot.createOutgoingMessage(message) ⇒ OutgoingMessage

createOutgoingMessage exposes the OutgoingMessage constructor via BaseBot. This simply means one can create their own OutgoingMessage object using any bot object. They can then compose it with all its helper functions

This is the static version of this method

Kind: static method of BaseBot
Returns: OutgoingMessage - outgoingMessage. The same object passed in with all the helper functions from OutgoingMessage

Param Type Description
message object base object that the outgoing Message should be based on

BaseBot.createOutgoingMessageFor(recipientId) ⇒ OutgoingMessage

same as #createOutgoingMessage, creates empty outgoingMessage with id of the recipient set. Again, this is jut sugar syntax for creating a new outgoingMessage object

This is the static version of this method

Kind: static method of BaseBot
Returns: OutgoingMessage - outgoingMessage. A valid OutgoingMessage object with recipient set.

Param Type Description
recipientId string id of the recipient the message is for

results matching ""

    No results matching ""