|
| 1 | +# Advanced |
| 2 | + |
| 3 | +This article covers advanced usage of the bridge. |
| 4 | + |
| 5 | +## Calling Method, Receiving Event |
| 6 | + |
| 7 | +The `request` function should be used when a developer needs to call a Telegram Mini Apps method and |
| 8 | +receive a specific event. |
| 9 | + |
| 10 | +For example, to call |
| 11 | +the [web_app_request_viewport](../../../platform/methods.md#web-app-request-viewport) method and catch |
| 12 | +the [viewport_changed](../../../platform/events.md#viewport-changed) event for actual viewport data: |
| 13 | + |
| 14 | +```typescript |
| 15 | +import { request } from '@telegram-apps/bridge'; |
| 16 | + |
| 17 | +await request('web_app_request_viewport', 'viewport_changed'); |
| 18 | +// { |
| 19 | +// is_state_stable: true, |
| 20 | +// is_expanded: false, |
| 21 | +// height: 320 |
| 22 | +// }; |
| 23 | +``` |
| 24 | + |
| 25 | +If the Telegram Mini Apps method accepts parameters, they should be passed in the `params` property |
| 26 | +of the third argument: |
| 27 | + |
| 28 | +```typescript |
| 29 | +const { button_id } = await request('web_app_open_popup', 'popup_closed', { |
| 30 | + params: { |
| 31 | + title: 'Caution', |
| 32 | + message: 'Should we delete your account?', |
| 33 | + buttons: [ |
| 34 | + { id: 'yes', type: 'ok' }, |
| 35 | + { id: 'no', type: 'cancel' }, |
| 36 | + ], |
| 37 | + }, |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +It is also allowed to track several events at the same time: |
| 42 | + |
| 43 | +```typescript |
| 44 | +await request( |
| 45 | + 'web_app_open_scan_qr_popup', |
| 46 | + ['qr_text_received', 'scan_qr_popup_closed'], |
| 47 | +); |
| 48 | + |
| 49 | +// The result will either be the qr_text_received |
| 50 | +// or scan_qr_popup_closed event payload. |
| 51 | +``` |
| 52 | + |
| 53 | +This function allows passing additional options such as `postEvent`, `abortSignal`, `timeout`, |
| 54 | +and `capture`. |
| 55 | + |
| 56 | +### `postEvent` |
| 57 | + |
| 58 | +The `postEvent` option allows a developer to override the method used to call the Telegram Mini Apps |
| 59 | +method. |
| 60 | + |
| 61 | +```typescript |
| 62 | +request('web_app_request_viewport', 'viewport_changed', { |
| 63 | + postEvent() { |
| 64 | + console.log('Hey, I am not going to do anything. Live with that'); |
| 65 | + }, |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +### `abortSignal` |
| 70 | + |
| 71 | +To abort the returned promise externally, the `abortSignal` option is used. |
| 72 | + |
| 73 | +```ts |
| 74 | +const controller = new AbortController(); |
| 75 | + |
| 76 | +request('web_app_request_viewport', 'viewport_changed', { |
| 77 | + abortSignal: controller.signal, |
| 78 | +}); |
| 79 | + |
| 80 | +setTimeout(() => { |
| 81 | + controller.abort(new Error('Not going to wait anymore')); |
| 82 | +}, 500); |
| 83 | +``` |
| 84 | + |
| 85 | +### `timeout` |
| 86 | + |
| 87 | +The `timeout` option assigns a timeout to the request. |
| 88 | + |
| 89 | +```typescript |
| 90 | +import { request } from '@telegram-apps/bridge'; |
| 91 | + |
| 92 | +try { |
| 93 | + await request( |
| 94 | + 'web_app_invoke_custom_method', |
| 95 | + 'custom_method_invoked', |
| 96 | + { |
| 97 | + timeout: 5000, |
| 98 | + params: { |
| 99 | + req_id: '1', |
| 100 | + method: 'deleteStorageValues', |
| 101 | + params: { keys: ['a'] }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + ); |
| 105 | +} catch (e) { |
| 106 | + console.error(e); // e.name will be 'TimeoutError' |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +#### `capture` |
| 111 | + |
| 112 | +The `capture` property is a function that allows developers to determine if an occurred Mini Apps |
| 113 | +event should be captured and returned from the `request` function: |
| 114 | + |
| 115 | +```typescript |
| 116 | +const slug = 'jjKSJnm1k23lodd'; |
| 117 | + |
| 118 | +request('web_app_open_invoice', 'invoice_closed', { |
| 119 | + params: { slug }, |
| 120 | + capture(data) { |
| 121 | + return slug === data.slug; |
| 122 | + }, |
| 123 | +}); |
| 124 | +``` |
| 125 | + |
| 126 | +By default, the `request` function captures the first event with the required name. In this case, |
| 127 | +the function will capture the event only if it has the expected slug, specific for the |
| 128 | +[invoice_closed](../../../platform/events.md#invoice-closed) event. |
| 129 | + |
| 130 | +When passing an array of events, the `capture` function will receive an object with |
| 131 | +the `event: EventName` and `payload?: EventPayload` properties. |
| 132 | + |
| 133 | +## Invoking Custom Methods |
| 134 | + |
| 135 | +Custom methods are those methods which can be used with |
| 136 | +the [web_app_invoke_custom_method](../../../platform/methods.md#web-app-invoke-custom-method) Mini |
| 137 | +Apps |
| 138 | +method. |
| 139 | + |
| 140 | +The `invokeCustomMethod` function simplifies the usage of such methods by reusing the `request` |
| 141 | +function. |
| 142 | + |
| 143 | +Here’s an example without using this function: |
| 144 | + |
| 145 | +```typescript |
| 146 | +const reqId = 'ABC'; |
| 147 | + |
| 148 | +request('web_app_invoke_custom_method', 'custom_method_invoked', { |
| 149 | + params: { |
| 150 | + req_id: reqId, |
| 151 | + method: 'deleteStorageValues', |
| 152 | + params: { keys: ['a'] }, |
| 153 | + }, |
| 154 | + capture(data) { |
| 155 | + return data.req_id === reqId; |
| 156 | + } |
| 157 | +}) |
| 158 | + .then(({ data, error }) => { |
| 159 | + if (error) { |
| 160 | + throw new Error(error); |
| 161 | + } |
| 162 | + return data; |
| 163 | + }) |
| 164 | + .then(data => { |
| 165 | + console.log('We got some data', data); |
| 166 | + }); |
| 167 | +``` |
| 168 | + |
| 169 | +Now, rewritten using the `invokeCustomMethod` function: |
| 170 | + |
| 171 | +```typescript |
| 172 | +import { invokeCustomMethod } from '@telegram-apps/bridge'; |
| 173 | + |
| 174 | +invokeCustomMethod('deleteStorageValues', { keys: ['a'] }, 'ABC') |
| 175 | + .then(data => { |
| 176 | + console.log('We got some data', data); |
| 177 | + }); |
| 178 | +``` |
| 179 | + |
| 180 | +Internally, it just encapsulates a specific logic related to the methods, so a developer shouldn't |
| 181 | +do it. |
| 182 | + |
| 183 | +Unlike the `request` function, the `invokeCustomMethod` function parses the result and checks if it |
| 184 | +contains the `error` property. If it does, the function throws the corresponding error; otherwise, |
| 185 | +the `result` property is returned. |
0 commit comments