2.0.1 • Published 9 months ago

@anthonypena/simple-bot v2.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

@anthonypena/simple-bot

Install

npm i @anthonypena/simple-bot

Examples

Push to Google Chat

Every time the script will be run, it will trigger the bot, build the static message and emit the message to the specified Google Chat space.

import { createBot, BotSpace } from '@anthonypena/simple-bot'
import { googleChat } from '@anthonypena/simple-bot/emitters'
import { nothing } from '@anthonypena/simple-bot/fetchers'
import { always } from '@anthonypena/simple-bot/triggers'

const staticBot = createBot({
    name: 'The Static Bot 🤖',
    trigger: always(),
    data: nothing(),
    message: () => 'This is a static message!',
    emitter: googleChat({ spaceUrl: 'https://chat.googleapis.com/...' })
});

new BotSpace({ env: process.env })
    .addBots(staticBot)
    .run();

Docs

Triggers

allTrigger

Trigger the bot when every triggers in parameter are triggered.

Parameters:

  • triggers: a list of Trigger

Exemple:

const bot = createBot({
    trigger: allTrigger(always(), whenEnvIsDefined('FOO')),
    // ...
})

always

Trigger the bot when every time.

Exemple:

const bot = createBot({
    trigger: always(),
    // ...
})

anyTrigger

Trigger the bot when any number (at least one) of the trigger in parameter is triggered.

Parameters:

  • triggers: a list of Trigger

Exemple:

const bot = createBot({
    trigger: anyTrigger(whenEnvIsDefined('FOO'), whenEnvIsDefined('BAR')),
    // ...
})

never

Never trigger the bot.

Exemple:

const bot = createBot({
    trigger: never(),
    // ...
})

whenEnvIsDefined

Trigger the bot if the environment variable in parameter is defined.

Parameters:

  • envVar: a string with the name of an environnement variable.

Exemple:

const bot = createBot({
    trigger: whenEnvIsDefined('FOO'),
    // ...
})

Fetchers

gpt

Ask OpenAI/GPT something.

Parameters:

Exemple:

const bot = createBot({
    data: gpt({ messages:  [{
                role: 'user',
                content: 'Tell me joke',
            }]
        }),
    // ...
})

http

Get some data with an HTTP call through fetch.

Parameters:

  • request: same as fetch parameter RequestInit with mandatory url field.
  • deserializer (optional): specify what to extract from http call response (default: extract body as json and deserialize it)

Exemple:

const bot = createBot({
    data: http({ url: 'http://example.com', headers: { 'Authorization': 'Bearer some-token' } }),
    // ...
})

nothing

Return an empty object.

Exemple:

const bot = createBot({
    data: nothing(),
    // ...
})

someData

Combine the result of multiple Fetchers as one unique object.

Parameters:

  • fetchers: a dictionary of Fetcher

Exemple:

const bot = createBot({
    data: someData({ foo: http({ url: 'http://foo.com' }), bar: http({ url: 'http://bar.com' }) }),
    // ...
})

Emitters

dispatch

Emit the message to every Emitters in parameters.

Parameters:

  • emitters: every emitters you want to use.

Exemple:

const bot = createBot({
    emitter: dispatch(
        googleChat({ spaceUrl: 'https://chat.googleapis.com/...' }),
        googleChat({ spaceUrl: 'https://chat.googleapis.com/...' }),
        printInConsole(),
    ),
    // ...
})

freeMobile

Emit the message to a Free Mobile phone number.

Note: to use Free Mobile notification, you should active it in your account.

Parameters:

  • {login} (optional): the Free Mobile login of the target phone. (default: env.FREEMOBILE_LOGIN)
  • {apikey} (optional): the secret apikey given in the Free Mobile account. (default: env.FREEMOBILE_APIKEY)

Exemple:

const bot = createBot({
    emitter: freeMobile(),
    // ...
})

googleChat

Emit the message to a Google Chat space.

Parameters:

  • {spaceUrl}: the webhook URL of the chat.

Exemple:

const bot = createBot({
    emitter: googleChat({ spaceUrl: 'https://chat.googleapis.com/...' }),
    // ...
})

printInConsole

Emit the message in the console.

Exemple:

const bot = createBot({
    emitter: printInConsole(),
    // ...
})

sendWithFetch

Emit the message with plain HTTP Fetch client.

Parameters:

  • {reqBuilder}: function to build a request.

Exemple:

const bot = createBot({
    emitter: sendWithFetch({
        reqBuilder({ message, env }) {
            return { method: 'POST', url: env.MY_CUSTOM_HTTP_SERVER, body: message }
        }
    }),
    // ...
})

Environment Variables

  • DRY: force dry mode. When DRY=true, replace the emitters with the printInConsole emitter.
2.0.1

9 months ago

1.3.1

9 months ago

1.3.0

9 months ago

2.0.0

9 months ago

1.2.0

9 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.4

10 months ago

1.2.1

9 months ago

1.1.0-beta.1

10 months ago

1.1.0-beta

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago