1.0.9 • Published 1 year ago

messenbot.js v1.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

MessenBot

Create Messenger Bot in Minutes

Note

  • This uses Facebook graph api
  • This is not 100% success its still in test
  • This only needs "PAGE_ID", "ACCESS_TOKEN" and "WEBHOOK_VERIFY_TOKEN"

Log

  • Fixed "Request Aborted", Request Aborted Error is causing a long pending promise
  • Fixed "master is not defined"
  • Fixed "UNHANDLED REJECTION"

Documentation

  1. Create new Application
    • Event (verified) is fired when the bot is ready to recieve messages
    • Event (ready) is fired when the server is up
    • Event (message) is fired when theres a new message
      • Returns (event)
    • Event (postback) is fired when theres a post back (Not Tested Yet!)
    • Event (error) is fired when theres a new error recieved
  2. Create a message callback
    • Event (message)
      • Returns (event)
  3. Send Message
    • from (event.sendMessage) in event (message) and (postback)
      • param {message} (string)
      • param {messagetype} (string)
  4. Send Action
    • from (event.sendAction) in event (message) and (postback)
      • param {action} (string)
  5. Send File/Attachment
    • from (event.sendFile) in event (message) and (postback)
      • param {filepath} (string) - path of the file
      • param {filetype} (string) - can be (image), (video), (audio), and, (file)
      • param {messagetype} (string)
  6. Done of application

==============

  1. Create new Application
const MessenBot = require('messenbot.js')

let client = new MessenBot.Bot({
        accessToken: "Your bots page access token",
        verifyToken: "Your bots webhook verify token",
        pageId: "Your bots page id"
})

client.login() // Login to the page account
// also verifying the token

client.on('verified', () => {
        console.log('bot is verified')
})

client.on('ready', () => {
        console.log('server is up')
})
  1. Message callback
client.on('message', async(event) => {
        console.log('New message: '+JSON.stringify(event))
})
  1. Postback callback
client.on('postback', async(event) => {
        console.log('New postback: '+JSON.stringify(event))
})

• Messenger Message Types

  • RESPONSE - Message is in response to a received message. This includes promotional and non-promotional messages sent inside the "24-hour standard messaging" window. For example, use this tag to respond if a person asks for a reservation confirmation or an status update.
  • UPDATE - Message is being sent proactively and is not in response to a received message. This includes promotional and non-promotional messages sent inside the the "24-hour standard messaging" window.
  • MESSAGE_TAG - Message is non-promotional and is being sent outside the "24-hour standard messaging" window with a message tag. The message must match the allowed use case for the tag.
  1. Send message
await event.sendMessage('hello world', 'RESPONSE') // limit is 2000 characters
  1. Send action
await event.sendAction('mask_seen')

Actions:

  • "mask_seen": make the bot seen the message
  • "typing_on": make the bot in typing state (lasts 20 seconds or if bot send a message)
  • "typing_off": make the bot remove the typing state
  1. Send File/Attachment
await event.sendFile('./myimg.png', 'image', 'RESPONSE')
await event.sendFile('./myvid.mp4', 'video', 'RESPONSE')
await event.sendFile('./myaudio.mp4', 'audio', 'RESPONSE')
await event.sendFile('./myrandomfile.txt', 'file', 'RESPONSE')
  1. Done of application

Full code

const MessenBot = require('messenbot.js')

let client = new MessenBot.Bot({
        accessToken: "Your bots page access token",
        verifyToken: "Your bots webhook verify token",
        pageId: "Your bots page id"
})

client.login() // Login to the page account
// also verifying the token

client.on('verified', () => {
        console.log('bot is verified')
})

client.on('ready', () => {
        console.log('server is up')
})

client.on('message', async(event) => {
        console.log('New message: '+JSON.stringify(event))

        await event.sendAction('typing_on')

        await event.sendMessage('hello world', 'RESPONSE') // limit is 2000 characters

        await event.sendAction('typing_on')
        
        await event.sendFile('./myaudio.mp4', 'audio', 'RESPONSE')
})

client.on('postback', async(event) => {
        console.log('New postback: '+JSON.stringify(event))
})

Now you have a full setup of your messenger bot!!

Done!!

Get the example

  1. "git clone https://github.com/FilipinoAkoShaders/messenbot-example.git"
  2. "cd messenbot-example"
  3. "npm install"
  4. "npm start"