bm-webapp-sdk 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Installation
2
+
3
+ ## CDN
4
+
5
+ ```html
6
+ <script src="https://cdn.jsdelivr.net/npm/bm-webapp-sdk@latest/dist/bm-webapp-sdk.min.js"></script>
7
+ ```
8
+
9
+ ## NPM
10
+
11
+ ```
12
+ npm i --save-dev bm-webapp-sdk
13
+ ```
14
+
15
+ # Usage
16
+
17
+ ## Browser
18
+
19
+ Include script from CDN. You can then use a script like that:
20
+
21
+ ```javascript
22
+ const handler = new BotMarketing.WebAppHandler({
23
+ scenarioCode: 'my-scenario-code',
24
+ });
25
+
26
+ await handler.init();
27
+
28
+ if (handler.isActive()) {
29
+ await handler.request('my-request-code', { param: 'value' });
30
+
31
+ // To get any value from session variables
32
+ handler.data.variables.response;
33
+ }
34
+ ```
35
+
36
+ ## Webpack
37
+
38
+ Import handler class:
39
+
40
+ ```javascript
41
+ import { WebAppHandler } from 'bm-webapp-sdk';
42
+ ```
43
+
44
+ You can then basically do same as for browser:
45
+
46
+ ```javascript
47
+ const handler = new WebAppHandler({
48
+ scenarioCode: 'my-scenario-code',
49
+ });
50
+
51
+ await handler.init();
52
+
53
+ if (handler.isActive()) {
54
+ await handler.request('my-request-code', { param: 'value' });
55
+
56
+ // To get any value from session variables
57
+ handler.data.variables.response;
58
+ }
59
+ ```
60
+
61
+ ## Telegram support
62
+
63
+ When running inside Telegram, handler can automatically init session. You need to pass `scenarioCode` when initializing
64
+ the handler.
65
+
66
+ Scenario must be within a project, project must have telegram api token defined in param `tg_api_token`. You can change
67
+ param name by setting `botTokenParamName` option. Also, project can have only one telegram bot messenger set in project settings.
68
+
69
+ __Remember that you have still to include telegram mini app scripts to enable telegram support.__
70
+
71
+ ## Request timeout
72
+
73
+ This library automatically handles request timeout which is 20s by default. If scenario is handling request too long,
74
+ we will wait until it finishes handling by polling session data each `retryDelayOnTimeout` (2000 by default) milliseconds.
75
+ But at most `requestTimeout` (60000) milliseconds.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,63 @@
1
- import WebAppHandler from './WebAppHandler';
2
- import { TelegramWebApps } from 'telegram-webapps-types';
1
+ interface HandlerOptions {
2
+ sessionHash?: string;
3
+ scenarioCode?: string;
4
+ initVariables?: TData;
5
+ botTokenParamName?: string;
6
+ userApiToken?: string;
7
+ retryDelayOnTimeout?: number;
8
+ requestTimeout?: number;
9
+ responseVar?: string;
10
+ apiEndpoint?: string;
11
+ }
12
+ type TData = {
13
+ [key: string]: any;
14
+ };
15
+ interface INodeEventsDecl {
16
+ inbox: boolean;
17
+ outbox: boolean;
18
+ anyInbox: boolean;
19
+ dialogClosed: boolean;
20
+ invoicePaid: boolean;
21
+ externalRequest: boolean;
22
+ buttons: boolean;
23
+ customEvents: boolean;
24
+ }
25
+ interface INode {
26
+ id: number;
27
+ title: string;
28
+ typeName: "basic" | "event" | "eventListener" | "condition" | "conditionBranch";
29
+ waitingForEvents?: INodeEventsDecl;
30
+ }
31
+ type TScenarioSessionStatus = "active" | "stopped" | "ended" | "unknownError" | "tunnelDisabled" | "expired" | "userDisabled" | "stepDeleted" | "transitionsLimit" | "parentStopped";
32
+ interface ScenarioSession {
33
+ id: number;
34
+ hash: string;
35
+ statusName: TScenarioSessionStatus;
36
+ ref: string | null;
37
+ variables: TData;
38
+ isTransitioning: boolean;
39
+ currentNode: INode | null;
40
+ }
41
+ export default class WebAppHandler {
42
+ readonly options: HandlerOptions;
43
+ data?: ScenarioSession;
44
+ private currentRequest?;
45
+ constructor(options: HandlerOptions);
46
+ isActive(): boolean;
47
+ init(): Promise<WebAppHandler>;
48
+ refreshData(): Promise<WebAppHandler>;
49
+ request(code: string, params?: TData, async?: boolean): Promise<WebAppHandler>;
50
+ abort(): void;
51
+ isTelegramInitAvailable(): boolean;
52
+ protected initNewSession(): Promise<WebAppHandler>;
53
+ protected makeRequest(method: string, path: string, json?: TData, signal?: AbortSignal): Promise<any>;
54
+ private waitUntilSessionFinishesTransition;
55
+ }
56
+
3
57
  export { WebAppHandler };
4
58
  declare global {
5
59
  interface Window {
6
- Telegram: TelegramWebApps.SDK | undefined;
60
+ Telegram: any;
7
61
  }
8
62
  }
63
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bm-webapp-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "",
5
5
  "main": "dist/bundle.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,5 +22,8 @@
22
22
  "typescript": "^5.3.3",
23
23
  "webpack-cli": "^5.1.4",
24
24
  "webpack-dev-server": "^5.0.2"
25
+ },
26
+ "dependencies": {
27
+ "typescript-declaration-webpack-plugin": "^0.3.0"
25
28
  }
26
29
  }
@@ -1,56 +0,0 @@
1
- interface HandlerOptions {
2
- sessionHash?: string;
3
- scenarioCode?: string;
4
- initVariables?: TData;
5
- botTokenParamName?: string;
6
- userApiToken?: string;
7
- retryDelayOnTimeout?: number;
8
- requestTimeout?: number;
9
- responseVar?: string;
10
- apiEndpoint?: string;
11
- }
12
- type TData = {
13
- [key: string]: any;
14
- };
15
- interface INodeEventsDecl {
16
- inbox: boolean;
17
- outbox: boolean;
18
- anyInbox: boolean;
19
- dialogClosed: boolean;
20
- invoicePaid: boolean;
21
- externalRequest: boolean;
22
- buttons: boolean;
23
- customEvents: boolean;
24
- }
25
- interface INode {
26
- id: number;
27
- title: string;
28
- typeName: "basic" | "event" | "eventListener" | "condition" | "conditionBranch";
29
- waitingForEvents?: INodeEventsDecl;
30
- }
31
- type TScenarioSessionStatus = "active" | "stopped" | "ended" | "unknownError" | "tunnelDisabled" | "expired" | "userDisabled" | "stepDeleted" | "transitionsLimit" | "parentStopped";
32
- interface ScenarioSession {
33
- id: number;
34
- hash: string;
35
- statusName: TScenarioSessionStatus;
36
- ref: string | null;
37
- variables: TData;
38
- isTransitioning: boolean;
39
- currentNode: INode | null;
40
- }
41
- export default class WebAppHandler {
42
- readonly options: HandlerOptions;
43
- data?: ScenarioSession;
44
- private currentRequest?;
45
- constructor(options: HandlerOptions);
46
- isActive(): boolean;
47
- init(): Promise<WebAppHandler>;
48
- refreshData(): Promise<WebAppHandler>;
49
- request(code: string, params?: TData, async?: boolean): Promise<WebAppHandler>;
50
- abort(): void;
51
- isTelegramInitAvailable(): boolean;
52
- protected initNewSession(): Promise<WebAppHandler>;
53
- protected makeRequest(method: string, path: string, json?: TData, signal?: AbortSignal): Promise<any>;
54
- private waitUntilSessionFinishesTransition;
55
- }
56
- export {};