@pipedream/telegram_bot_api 0.5.0 → 0.5.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pipedream/telegram_bot_api",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Pipedream Telegram_bot_api Components",
5
5
  "main": "telegram_bot_api.app.mjs",
6
6
  "keywords": [
@@ -6,7 +6,7 @@ export default {
6
6
  key: "telegram_bot_api-channel-updates",
7
7
  name: "New Channel Updates (Instant)",
8
8
  description: "Emit new event each time a channel post is created or updated.",
9
- version: "0.1.6",
9
+ version: "0.1.7",
10
10
 
11
11
  type: "source",
12
12
  dedupe: "unique",
@@ -3,6 +3,37 @@ import constants from "./constants.mjs";
3
3
  import { v4 as uuid } from "uuid";
4
4
  import { ConfigurationError } from "@pipedream/platform";
5
5
 
6
+ /**
7
+ * Origin + path of `url`, with any trailing slash removed, or `null` when the
8
+ * value is not a parseable absolute URL. Comparing the parsed form rather than
9
+ * the raw string avoids both false negatives from a trailing slash and
10
+ * substring-based spoofing such as `https://evil.com/?u=<our endpoint>`.
11
+ */
12
+ function normalizeUrl(url) {
13
+ try {
14
+ const {
15
+ origin, pathname,
16
+ } = new URL(url);
17
+ return `${origin}${pathname.replace(/\/+$/, "")}`;
18
+ } catch {
19
+ return null;
20
+ }
21
+ }
22
+
23
+ /**
24
+ * Returns true only when the webhook currently registered on the bot is this
25
+ * source's own endpoint. Ownership is decided by exact endpoint identity, so a
26
+ * webhook belonging to an external service — or to a different Pipedream source
27
+ * sharing the same bot token — is never treated as ours and is left untouched.
28
+ */
29
+ function isOwnWebhook(currentUrl, ownEndpoint) {
30
+ const current = normalizeUrl(currentUrl);
31
+ const own = ownEndpoint
32
+ ? normalizeUrl(ownEndpoint)
33
+ : null;
34
+ return !!current && !!own && current === own;
35
+ }
36
+
6
37
  export default {
7
38
  props: {
8
39
  telegramBotApi,
@@ -16,13 +47,27 @@ export default {
16
47
  },
17
48
  hooks: {
18
49
  async deploy() {
19
- /*
20
- * Telegram only supports a single webhook at a time for a given Bot. If a webhook
21
- * for this Bot already exists, display configuration error.
50
+ /**
51
+ * Telegram supports only one webhook per bot. If a stale registration for
52
+ * *this* source survives from a prior deploy (e.g. the source was removed
53
+ * with ignoreHookErrors so deactivate() never ran), clear it before
54
+ * registering again — otherwise activate() stores a fresh secret while
55
+ * Telegram keeps signing with the old one, and run() silently rejects
56
+ * every incoming event.
57
+ *
58
+ * Any webhook that is not this source's own endpoint — external services
59
+ * and other Pipedream sources alike — is left intact, and the original
60
+ * ConfigurationError is thrown so the user is warned before an existing
61
+ * integration is disrupted.
22
62
  */
23
63
  const { result } = await this.telegramBotApi.getWebhookInfo();
24
64
  if (result.url?.length > 0) {
25
- throw new ConfigurationError("[Telegram only supports](https://core.telegram.org/bots/api#setwebhook) a single webhook at a time, for a given Bot. To get around this, you can reuse an existing Telegram Bot source or disable the active source and try again. [View all your sources here](https://pipedream.com/sources).");
65
+ if (isOwnWebhook(result.url, this.http?.endpoint)) {
66
+ console.log(`Removing stale webhook for this source (${result.url}) before deploying...`);
67
+ await this.telegramBotApi.deleteHook();
68
+ } else {
69
+ throw new ConfigurationError("[Telegram only supports](https://core.telegram.org/bots/api#setwebhook) a single webhook at a time, for a given Bot. To get around this, you can reuse an existing Telegram Bot source or disable the active source and try again. [View all your sources here](https://pipedream.com/sources).");
70
+ }
26
71
  }
27
72
  /**
28
73
  * From the docs: https://core.telegram.org/bots/api#getting-updates
@@ -45,6 +90,20 @@ export default {
45
90
  async activate() {
46
91
  const secret = uuid();
47
92
  this.setSecret(secret);
93
+ /**
94
+ * Clear this source's own stale registration before registering again.
95
+ * Anything else is owned by another integration, so throw
96
+ * ConfigurationError before createHook() runs — Telegram's setWebhook
97
+ * would otherwise overwrite it and silently break that integration.
98
+ */
99
+ const { result } = await this.telegramBotApi.getWebhookInfo();
100
+ if (result.url?.length > 0) {
101
+ if (isOwnWebhook(result.url, this.http?.endpoint)) {
102
+ await this.telegramBotApi.deleteHook();
103
+ } else {
104
+ throw new ConfigurationError("[Telegram only supports](https://core.telegram.org/bots/api#setwebhook) a single webhook at a time, for a given Bot. To get around this, you can reuse an existing Telegram Bot source or disable the active source and try again. [View all your sources here](https://pipedream.com/sources).");
105
+ }
106
+ }
48
107
  await this.telegramBotApi.createHook(this.http.endpoint, this.getEventTypes(), secret);
49
108
  },
50
109
  async deactivate() {
@@ -6,7 +6,7 @@ export default {
6
6
  key: "telegram_bot_api-message-updates",
7
7
  name: "New Message Updates (Instant)",
8
8
  description: "Emit new event each time a Telegram message is created or updated.",
9
- version: "0.1.7",
9
+ version: "0.1.8",
10
10
  type: "source",
11
11
  dedupe: "unique",
12
12
  props: {
@@ -6,7 +6,7 @@ export default {
6
6
  key: "telegram_bot_api-new-bot-command-received",
7
7
  name: "New Bot Command Received (Instant)",
8
8
  description: "Emit new event each time a Telegram Bot command is received.",
9
- version: "0.0.6",
9
+ version: "0.0.7",
10
10
  type: "source",
11
11
  dedupe: "unique",
12
12
  props: {
@@ -6,7 +6,7 @@ export default {
6
6
  key: "telegram_bot_api-new-updates",
7
7
  name: "New Updates (Instant)",
8
8
  description: "Emit new event for each new Telegram event.",
9
- version: "0.1.6",
9
+ version: "0.1.7",
10
10
  type: "source",
11
11
  dedupe: "unique",
12
12
  props: {