- 96.0.0Latest
- 95.0.0
- 94.0.0
- 93.0.0
- 92.0.0
- 91.0.0
- 90.0.0
- 89.0.0
- 88.0.0
- 87.0.0
- 86.0.0
- 85.0.0
- 85.0.0
- 84.0.0
- 83.0.0
- 83.0.0
- 82.0.0
- 81.0.0
- 80.0.0
- 79.0.0
- 78.0.0
- 77.0.0
- 76.0.0
- 75.0.0
- 74.0.0
- 73.0.0
- 73.0.0
- 72.0.0
- 71.0.0
- 70.0.0
- 69.0.0
- 69.0.0
- 68.0.0
- 67.0.0
- 66.0.0
- 65.0.0
- 64.0.0
- 63.0.0
- 62.0.0
- 61.0.0
- 60.0.0
- 59.0.0
- 58.0.0
- 57.0.0
- 56.0.0
- 55.0.0
- 54.0.0
- 53.0.0
- 53.0.0
- 52.0.0
- 51.0.0
- 50.0.0
- 49.0.0
- 48.0.0
- 47.0.0
- 46.0.0
- 45.0.0
- 44.0.0
- 43.0.0
- 42.0.0
- 41.0.0
- 40.0.0
- 39.0.0
- 39.0.0
- 38.0.0
- 38.0.0
- 37.0.0
- 36.0.0
- 35.0.0
- 34.0.0
- 33.0.0
- 32.0.0
- 31.0.0
- 30.0.0
- 29.0.0
- 28.0.0
abstract-bot-api
This library solves two problems:
- You want to switch between chat providers and not change your code.
- You have deep stacked code that needs to access the chat api (e.g. to send a loading message from some internal method), and you donāt want to carry around credentials as globals (because maybe you have two bots running in the same server).
The first problem is solved by making a simple common api for all the services, while the second is solved using https://github.com/uriva/context-inject.
This library provides a unified API over:
- telegram
- green-api (unofficial whatsapp api)
- websocket (for web page chat) 1
Installation
node.js:
npm i abstract-bot-api
deno:
import abc from "https://deno.land/x/abstract_bot_api/src/index.ts";
API
The abstract api methods:
reply: (text: string) => Promise<string>
- a simple reply that gets text and
returns the sent message id.
messageId: () => string
- the incoming message id
referenceId: () => string
- if the message quoted another message
medium: () => string
- which service was used (āwhatsappā, āgreen-apiā etcā)
userId: () => string
- the user id who sent the current message
withSpinner: (text: string, f: Function) => Function
- wraps an async function
with logic that provides a waiting animation for users
progressBar: (text: string) => Promise<(percentage: number) => Promise<void>>
-
get a way to send progress updates that appear in a loading bar animation
The nice thing is you can call these methods from anywhere in your code, so you donāt need to pass through things to deeply nested functions. The library understands by the call stack the context the messages should go to (see example below).
example
Hereās an example usage:
import {
AbstractIncomingMessage,
bouncerServer,
makeTelegramHandler,
setTelegramWebhook,
withSpinner,
} from "../src/index.ts";
import { gamla } from "../deps.ts";
import { reply } from "../src/api.ts";
import {
whatsappBusinessHandler,
whatsappWebhookVerificationHandler,
} from "../src/whatsapp.ts";
const { coerce, sleep } = gamla;
const telegramToken = coerce(Deno.env.get("TELEGRAM_TOKEN"));
const botServerSuffix = "/bot-url-suffix";
const whatsappPath = "/whatsapp-url-suffix";
const handleMessage = async (task: AbstractIncomingMessage) => {
console.log("got task", task);
await withSpinner("waiting needlessly", sleep)(5000);
return reply("hi there i got " + JSON.stringify(task));
};
const url = coerce(Deno.env.get("URL"));
await bouncerServer(
url,
coerce(Deno.env.get("PORT")),
[
makeTelegramHandler(telegramToken, botServerSuffix, handleMessage),
whatsappBusinessHandler(
coerce(Deno.env.get("WHATSAPP_ACCESS_TOKEN")),
whatsappPath,
handleMessage,
),
whatsappWebhookVerificationHandler(
coerce(Deno.env.get("WHATSAPP_VERIFICATION_TOKEN")),
whatsappPath,
),
],
);
await setTelegramWebhook(telegramToken, url + botServerSuffix).then(
console.log,
);