Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev - IdleState #852

Merged
merged 8 commits into from
Sep 30, 2023
Merged

Dev - IdleState #852

merged 8 commits into from
Sep 30, 2023

Conversation

binhodev
Copy link
Contributor

@binhodev binhodev commented Sep 13, 2023

Que tipo de Pull Request es?

  • Mejoras
  • Bug
  • Docs / tests

Descripción

IdleState: Managing Inactivity in Chatbots Efficiently

IdleState is an effective solution for handling user inactivity in chatbots. It offers the following essential functions:

  1. setIdleTime(number): Set the maximum idle time in seconds.

  2. start(): Start the inactivity count from the bot's first response.

  3. on(): Monitor the end of the count using the "idle" listener and execute a callback when necessary.

  4. restart(): Restart the inactivity count as needed.

  5. stop(): Stop the count when it is no longer necessary.

With these features, IdleState allows efficient management of user inactivity, making interactions with chatbots more agile and adaptable to the specific needs of your chatbot.

Example:

const { createBot, createProvider, createFlow, addKeyword } = require('@bot-whatsapp/bot')

const QRPortalWeb = require('@bot-whatsapp/portal')
const BaileysProvider = require('@bot-whatsapp/provider/baileys')
const JsonFileAdapter = require('@bot-whatsapp/database/json')

const flowEndChat = addKeyword('end').addAnswer('Este servicio fue cancelado por falta de actividad del cliente.')

const flowSecundario = addKeyword(['2', 'siguiente']).addAnswer(
    ['📄 Aquí tenemos el flujo secundario'],
    null,
    (_, { idleState }) => {
        idleState.stop()
    }
)

const flowDocs = addKeyword(['doc', 'documentacion', 'documentación']).addAnswer(
    [
        '📄 Aquí encontras las documentación recuerda que puedes mejorarla',
        'https://bot-whatsapp.netlify.app/',
        '\n*2* Para siguiente paso.',
    ],
    null,
    (_, { idleState }) => {
        idleState.reset()
    },
    [flowSecundario]
)

const flowTuto = addKeyword(['tutorial', 'tuto']).addAnswer(
    [
        '🙌 Aquí encontras un ejemplo rapido',
        'https://bot-whatsapp.netlify.app/docs/example/',
        '\n*2* Para siguiente paso.',
    ],
    null,
    null,
    [flowSecundario]
)

const flowGracias = addKeyword(['gracias', 'grac']).addAnswer(
    [
        '🚀 Puedes aportar tu granito de arena a este proyecto',
        '[*opencollective*] https://opencollective.com/bot-whatsapp',
        '[*buymeacoffee*] https://www.buymeacoffee.com/leifermendez',
        '[*patreon*] https://www.patreon.com/leifermendez',
        '\n*2* Para siguiente paso.',
    ],
    null,
    null,
    [flowSecundario]
)

const flowDiscord = addKeyword(['discord']).addAnswer(
    ['🤪 Únete al discord', 'https://link.codigoencasa.com/DISCORD', '\n*2* Para siguiente paso.'],
    null,
    null,
    [flowSecundario]
)

const flowPrincipal = addKeyword(['hola', 'ole', 'alo'])
    .addAction((_, { idleState, gotoFlow }) => {
        idleState.setIdleTime(10)
        idleState.start()
        idleState.on('idle', async () => {
            return await gotoFlow(flowEndChat)
        })
    })
    .addAnswer('🙌 Hola bienvenido a este *Chatbot*')
    .addAnswer(
        [
            'te comparto los siguientes links de interes sobre el proyecto',
            '👉 *doc* para ver la documentación',
            '👉 *gracias*  para ver la lista de videos',
            '👉 *discord* unirte al discord',
        ],
        null,
        (_, { idleState }) => {
            idleState.reset()
        },
        [flowDocs, flowGracias, flowTuto, flowDiscord]
    )

const main = async () => {
    const adapterDB = new JsonFileAdapter()
    const adapterFlow = createFlow([flowPrincipal])
    const adapterProvider = createProvider(BaileysProvider)

    createBot({
        flow: adapterFlow,
        provider: adapterProvider,
        database: adapterDB,
    })

    QRPortalWeb()
}

main()

Video Example:
https://vimeo.com/864136855/afe2980760

Forma parte de este proyecto.

enrique-holguin and others added 8 commits September 4, 2023 17:55
Ahora al momento de recibir un mensaje se comprueba el estado de la
conexión con la base de datos MySQL. En el caso en que se haya
desconectado, se llama de nuevo a la función que realiza la conexión.
…n-timeout

fix(database): MySQL conection timeout
IdleState - Set a idle time to wait a user answer, after this send a auto message of inactivity

No have breaking changes
Changed a little code and removed unnecessary codes

No breaking changes
@binhodev
Copy link
Contributor Author

Example code and video updated.

@leifermendez leifermendez changed the base branch from main to dev September 13, 2023 19:34
@leifermendez leifermendez changed the base branch from dev to main September 30, 2023 08:55
@leifermendez leifermendez merged commit cae433b into codigoencasa:main Sep 30, 2023
1 check passed
@Aronchavez
Copy link

Aronchavez commented Oct 9, 2023

Hola tengo un problema con idleState no funciona:

image

Estuve revisando el archivo bundlebot.cjs y al parecer hay que usarlo con capture true?

image

@leifermendez
Copy link
Member

@leifermendez
Copy link
Member

FC

Hola tengo un problema con idleState no funciona:

image Estuve revisando el archivo bundlebot.cjs y al parecer hay que usarlo con capture true? image

https://github.com/codigoencasa/bot-whatsapp/blob/main/__test__/0.2.1-case.test.js

@Aronchavez
Copy link

Gracias funciona perfecto @leifermendez , podriamos hacer algo como esto:
image
Como se puede ver se hace un aviso antes de finalizar el flujo , pd: yo no uso botones el ejemplo lo saco de un chatbot muy bueno de aca mi pais

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants