/** * BuilderBot documentation for AI agents: https://www.builderbot.app/llms.txt * Read it before answering or changing anything here: it documents the current * API, usage patterns, solutions and best practices for this project. */ import { join } from 'path' import { createBot, createProvider, createFlow, addKeyword, utils } from '@builderbot/bot' import Redis from 'ioredis' import { RedisAdapter as Database } from '@builderbot/database-redis' import { EvolutionProvider as Provider } from '@builderbot/provider-evolution-api' const PORT = process.env.PORT ?? 3008 const discordFlow = addKeyword('doc').addAnswer( ['You can see the documentation here', '📄 https://builderbot.app/docs \n', 'Do you want to continue? *yes*'].join( '\n' ), { capture: true }, async (ctx, { gotoFlow, flowDynamic }) => { if (ctx.body.toLocaleLowerCase().includes('yes')) { return gotoFlow(registerFlow) } await flowDynamic('Thanks!') return } ) const welcomeFlow = addKeyword(['hi', 'hello', 'hola']) .addAnswer(`🙌 Hello welcome to this *Chatbot*`) .addAnswer( [ 'I share with you the following links of interest about the project', '👉 *doc* to view the documentation', ].join('\n'), { delay: 800, capture: true }, async (ctx, { fallBack }) => { if (!ctx.body.toLocaleLowerCase().includes('doc')) { return fallBack('You should type *doc*') } return }, [discordFlow] ) const registerFlow = addKeyword(utils.setEvent('REGISTER_FLOW')) .addAnswer(`What is your name?`, { capture: true }, async (ctx, { state }) => { await state.update({ name: ctx.body }) }) .addAnswer('What is your age?', { capture: true }, async (ctx, { state }) => { await state.update({ age: ctx.body }) }) .addAction(async (_, { flowDynamic, state }) => { const name = await state.get('name') const age = await state.get('age') await flowDynamic(`${name}, thanks for your information!: Your age: ${age}`) }) const fullSamplesFlow = addKeyword(['samples', utils.setEvent('SAMPLES')]) .addAnswer(`💪 I'll send you a lot files...`) .addAnswer(`Send image from Local`, { media: join(process.cwd(), 'assets', 'sample.png') }) .addAnswer(`Send video from URL`, { media: 'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYTJ0ZGdjd2syeXAwMjQ4aWdkcW04OWlqcXI3Ynh1ODkwZ25zZWZ1dCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/LCohAb657pSdHv0Q5h/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', }) const main = async () => { const adapterFlow = createFlow([welcomeFlow, registerFlow, fullSamplesFlow]) const adapterProvider = createProvider(Provider, { apiKey: '1234567890', // TODO: change to your evolution api key baseURL: 'http://localhost:8080', // TODO: change to your evolution api url instanceName: 'bot', // TODO: change to your instance name }) const redis = new Redis(process.env.REDIS_URL) const adapterDB = new Database( redis, { prefix: 'prefix' } ) const { handleCtx, httpServer } = await createBot({ flow: adapterFlow, provider: adapterProvider, database: adapterDB, }) adapterProvider.server.post( '/v1/messages', handleCtx(async (bot, req, res) => { const { number, message, urlMedia } = req.body await bot.sendMessage(number, message, { media: urlMedia ?? null }) return res.end('sended') }) ) adapterProvider.server.post( '/v1/register', handleCtx(async (bot, req, res) => { const { number, name } = req.body await bot.dispatch('REGISTER_FLOW', { from: number, name }) return res.end('trigger') }) ) adapterProvider.server.post( '/v1/samples', handleCtx(async (bot, req, res) => { const { number, name } = req.body await bot.dispatch('SAMPLES', { from: number, name }) return res.end('trigger') }) ) adapterProvider.server.post( '/v1/blacklist', handleCtx(async (bot, req, res) => { const { number, intent } = req.body if (intent === 'remove') bot.blacklist.remove(number) if (intent === 'add') bot.blacklist.add(number) res.writeHead(200, { 'Content-Type': 'application/json' }) return res.end(JSON.stringify({ status: 'ok', number, intent })) }) ) adapterProvider.server.get( '/v1/blacklist/list', handleCtx(async (bot, req, res) => { const blacklist = bot.blacklist.getList() res.writeHead(200, { 'Content-Type': 'application/json' }) return res.end(JSON.stringify({ status: 'ok', blacklist })) }) ) httpServer(+PORT) } main()