116.0.0 • Published 4 months ago

abstract-bot-api v116.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

abstract-bot-api

This library solves two problems:

  1. You want to switch between chat providers and not change your code.
  2. 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:

  1. telegram
  2. whatsapp
  3. green-api (unofficial whatsapp api)
  4. 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,
);
114.0.0

4 months ago

116.0.0

4 months ago

115.0.0

4 months ago

113.0.0

5 months ago

112.0.0

5 months ago

110.0.0

5 months ago

109.0.0

5 months ago

107.0.0

6 months ago

106.0.0

6 months ago

105.0.0

6 months ago

103.0.0

6 months ago

104.0.0

6 months ago

99.0.0

7 months ago

98.0.0

7 months ago

96.0.0

7 months ago

95.0.0

7 months ago

88.0.0

7 months ago

87.0.0

7 months ago

86.0.0

7 months ago

81.0.0

7 months ago

85.0.0

7 months ago

84.0.0

7 months ago

79.0.0

8 months ago

78.0.0

8 months ago

80.0.0

8 months ago

77.0.0

8 months ago

73.0.0

8 months ago

75.0.0

8 months ago

74.0.0

8 months ago

76.0.0

8 months ago

71.0.0

9 months ago

72.0.0

9 months ago

66.0.0

10 months ago

65.0.0

10 months ago

60.0.0

10 months ago

64.0.0

10 months ago

56.0.0

10 months ago

63.0.0

10 months ago

70.0.0

9 months ago

58.0.0

10 months ago

62.0.0

10 months ago

69.0.0

9 months ago

68.0.0

9 months ago

67.0.0

10 months ago

59.0.0

10 months ago

53.0.0

1 year ago

52.0.0

1 year ago

55.0.0

1 year ago

54.0.0

1 year ago

51.0.0

1 year ago

50.0.0

1 year ago

48.0.0

1 year ago

47.0.0

1 year ago

45.0.0

1 year ago

46.0.0

1 year ago

44.0.0

1 year ago

43.0.0

1 year ago

42.0.0

1 year ago

41.0.0

1 year ago

32.0.0

1 year ago

28.0.0

1 year ago

40.0.0

1 year ago

36.0.0

1 year ago

31.0.0

1 year ago

39.0.0

1 year ago

27.0.0

1 year ago

35.0.0

1 year ago

30.0.0

1 year ago

34.0.0

1 year ago

38.0.0

1 year ago

33.0.0

1 year ago

37.0.0

1 year ago

29.0.0

1 year ago

25.0.0

1 year ago

23.0.0

1 year ago

26.0.0

1 year ago

24.0.0

1 year ago

20.0.0

1 year ago

22.0.0

1 year ago

19.0.0

1 year ago

15.0.0

1 year ago

17.0.0

1 year ago

14.0.0

1 year ago

13.0.0

1 year ago

16.0.0

1 year ago

18.0.0

1 year ago

12.0.0

1 year ago

0.0.8

1 year ago

10.0.0

1 year ago

11.0.0

1 year ago

0.0.2

1 year ago

0.0.5

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.1

1 year ago