Meta Uses Cases
import { createBot, createProvider, createFlow, addKeyword, utils } from '@builderbot/bot'
import { MemoryDB as Database } from '@builderbot/bot'
import { MetaProvider as Provider } from '@builderbot/provider-meta'
const PORT = process.env.PORT ?? 3008
const welcomeFlow = addKeyword<Provider, Database>(['ey','test'])
.addAnswer('send files...')
.addAnswer(`Send image from URL`,
{ media: 'https://i.imgur.com/0HpzsEm.png' }
)
.addAnswer(`Send video from URL`,
{ media: 'https://media.giphy.com/media/KWZKwdBC2ODWlQ8kgt/giphy.mp4' }
)
.addAnswer(`Send audio from URL`,
{ media: 'https://cdn.freesound.org/previews/728/728142_11861866-lq.mp3' }
)
.addAnswer(`Send file from URL`,
{ media: 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf' }
)
.addAnswer(`First Way to Send Buttons`, {
buttons:
[
{ body: 'Order' },
{ body: 'Register' },
{ body: 'Catalog' }
]
})
.addAction(async (ctx, { provider }) => {
await provider.sendButtons(ctx.from, [
{ body: 'Option A' },
{ body: 'Option B' },
{ body: 'Option C' }
], `Send Buttons Alternative`)
})
.addAction(async (ctx, { provider }) => {
await provider.sendButtonUrl(ctx.from, { body: 'View Doc', url: 'https://builderbot.vercel.app' }, 'Send Buttons URL')
})
.addAnswer('Send Contact', null, async (ctx, { provider }) => {
await provider.sendContacts(ctx.from, [
{
name: {
formatted_name: 'Leifer',
first_name: 'Leifer M'
},
phones: [{
phone: '34000000',
type: 'HOME',
wa_id: '34000000' // (optional) makes META identify the number as an active wpp user
}]
}
])
})
.addAnswer('Send Location', null, async (ctx, { provider }) => {
await provider.sendLocation(ctx.from, {
address: 'CDMX Centro',
lat_number: '19.3909832',
long_number: '-99.3084209',
name: 'CDMX Office'
})
})
.addAnswer('Send List', null, async (ctx, { provider }) => {
const list = {
"header": {
"type": "text",
"text": "Weekly Promotions"
},
"body": {
"text": "Discover our amazing offers!"
},
"footer": {
"text": "Visit our website for more details"
},
"action": {
"button": "See more",
"sections": [
{
"title": "Clothing",
"rows": [
{
"id": "001",
"title": "Printed T-shirt",
"description": "Cotton t-shirt with printed design"
},
{
"id": "002",
"title": "Slim-fit Jeans",
"description": "Slim-fit denim jeans for men and women"
}
]
},
{
"title": "Electronics",
"rows": [
{
"id": "003",
"title": "Smartphone",
"description": "Smartphone with HD display and high-resolution camera"
},
{
"id": "004",
"title": "Wireless Headphones",
"description": "Headphones with noise cancellation and Bluetooth connectivity"
}
]
}
]
}
};
await provider.sendList(ctx.from, list)
})
const main = async () => {
const adapterFlow = createFlow([welcomeFlow])
const adapterProvider = createProvider(Provider, {
jwtToken: process.env.JWT_TOKEN,
numberId: process.env.NUMBER_ID,
verifyToken: process.env.VERIFY_TOKEN,
version: 'v19.0'
})
const adapterDB = new Database()
const { handleCtx, httpServer } = await createBot<Provider>({
flow: adapterFlow,
provider: adapterProvider,
database: adapterDB,
})
adapterProvider.server.post(
'/v1/samples',
handleCtx(async (bot, req, res) => {
try {
const { number, message, urlMedia } = req.body
await bot.sendMessage(number, message, { media: urlMedia ?? null })
await bot.provider.sendMessage(number, message, {})
await bot.provider.sendButtons(number, [
{ body: 'Order' },
{ body: 'Register' },
{ body: 'Catalog' }
], 'Send Buttons API')
await bot.provider.sendButtonUrl(number,
{
body: 'View Doc',
url: 'http://builderbot.app'
},
'Send Buttons API CTA')
return res.end('ok')
} catch (error) {
console.log(error)
return res.end('error')
}
})
)
adapterProvider.server.post(
'/v1/flow',
handleCtx(async (bot, req, res) => {
try {
const { number } = req.body
await bot.dispatch('TEST', { from: number, name: 'bot' })
return res.end('ok')
} catch (error) {
console.log(error)
return res.end('error')
}
})
)
httpServer(+PORT)
}
main()
There are several features that the default provider does not provide and you can create them along the way
today I want to teach you a very basic but effective, the double tick or popcorn in English
Send Double Tick
to send the double tick or double popcorn you only need the ID of each new message that arrives, simple no?
let's see a clear example of this
app.ts
import { createBot, MemoryDB, createProvider } from '@builderbot/bot'
import { MetaProvider } from '@builderbot/provider-meta'
const PORT = process.env.PORT ?? 3001
const welcome = addKeyword(EVENTS.WELCOME)
.addAction(async (ctx) => {
/*
{
"messaging_product": "whatsapp",
"status": "read",
"message_id": "MESSAGE_ID"
}
*/
//all you need to add is the ID of each new message
provider.sendMessageToApi({
"messaging_product": "whatsapp",
"status": "read",
message_id: ctx?.message_id
} as any)
})
const main = async () => {
const provider = createProvider(MetaProvider)
const { handleCtx, httpServer } = await createBot({
database: new MemoryDB(),
provider,
flow: createFlow([welcome]),
})
httpServer(+PORT)
provider.server.post('/v1/register', handleCtx(async (bot, req, res) => {
const { number, name } = req.body
await bot.dispatch('EVENT_REGISTER', { from: number, name })
return res.end('trigger')
}))
}
main()