Create Messenger Bot in Minutes
- 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"
- Fixed "Request Aborted", Request Aborted Error is causing a long pending promise
- Fixed "master is not defined"
- Fixed "UNHANDLED REJECTION"
- 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
- Create a message callback
- Event (message)
- Returns (event)
- Event (message)
- Send Message
- from (event.sendMessage) in event (message) and (postback)
- param {message} (string)
- param {messagetype} (string)
- from (event.sendMessage) in event (message) and (postback)
- Send Action
- from (event.sendAction) in event (message) and (postback)
- param {action} (string)
- from (event.sendAction) in event (message) and (postback)
- 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)
- from (event.sendFile) in event (message) and (postback)
- Done of application
==============
- 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')
})
- Message callback
client.on('message', async(event) => {
console.log('New message: '+JSON.stringify(event))
})
- 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.
- Send message
await event.sendMessage('hello world', 'RESPONSE') // limit is 2000 characters
- 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
- 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')
- 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!!
Get the example
- "git clone https://github.com/FilipinoAkoShaders/messenbot-example.git"
- "cd messenbot-example"
- "npm install"
- "npm start"