forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Telegram Bot Historical Events (PipedreamHQ#3429)
* add common folder * common webhook base * use random uuid as webhook secret token * get last 25 events * fix event ts * fix new updates summary * change new channel updates description
- Loading branch information
1 parent
95bce05
commit 9a72c1c
Showing
22 changed files
with
177 additions
and
150 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
components/telegram_bot_api/actions/edit-media-message/edit-media-message.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
components/telegram_bot_api/actions/send-album/send-album.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
components/telegram_bot_api/actions/send-audio-file/send-audio-file.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
components/telegram_bot_api/actions/send-document-or-image/send-document-or-image.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
components/telegram_bot_api/actions/send-media-by-url-or-id/send-media-by-url-or-id.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
components/telegram_bot_api/actions/send-photo/send-photo.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
components/telegram_bot_api/actions/send-video-note/send-video-note.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
components/telegram_bot_api/actions/send-video/send-video.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
components/telegram_bot_api/actions/send-voice-message/send-voice-message.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 24 additions & 45 deletions
69
components/telegram_bot_api/sources/channel-updates/channel-updates.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,36 @@ | ||
// eslint-disable-next-line camelcase | ||
import telegramBotApi from "../../telegram_bot_api.app.mjs"; | ||
import base from "../common/webhooks.mjs"; | ||
|
||
export default { | ||
type: "source", | ||
...base, | ||
key: "telegram_bot_api-channel-updates", | ||
name: "Channel Updates (Instant)", | ||
description: "Emit new event each time a channel message is created or updated.", | ||
version: "0.0.4", | ||
name: "New Channel Updates (Instant)", | ||
description: "Emit new event each time a channel post is created or updated.", | ||
version: "0.1.0", | ||
type: "source", | ||
dedupe: "unique", | ||
props: { | ||
db: "$.service.db", | ||
// eslint-disable-next-line pipedream/props-label,pipedream/props-description | ||
http: { | ||
type: "$.interface.http", | ||
customResponse: true, | ||
}, | ||
telegramBotApi, | ||
}, | ||
hooks: { | ||
async activate() { | ||
await this.telegramBotApi.createHook(this.http.endpoint, [ | ||
methods: { | ||
...base.methods, | ||
getEventTypes() { | ||
return [ | ||
"channel_post", | ||
"edited_channel_post", | ||
]); | ||
]; | ||
}, | ||
async deactivate() { | ||
await this.telegramBotApi.deleteHook(); | ||
getMeta(event, channelPost) { | ||
return { | ||
id: event.update_id, | ||
summary: `${channelPost.chat.title} - ${channelPost.text}`, | ||
ts: new Date(channelPost.edit_date ?? channelPost.date), | ||
}; | ||
}, | ||
}, | ||
async run(event) { | ||
// if the event doesn't contain the same API token secret, | ||
// then it's an unauthorized replay attack. | ||
if ((event.path).substring(1) !== this.telegramBotApi.$auth.token) { | ||
return; | ||
} | ||
this.http.respond({ | ||
status: 200, | ||
}); | ||
const { body } = event; | ||
if (!body) { | ||
return; | ||
} | ||
const channelPost = body.edited_channel_post ?? body.channel_post; | ||
processEvent(event) { | ||
const channelPost = event.edited_channel_post ?? event.channel_post; | ||
|
||
if (!channelPost?.chat) { | ||
throw new Error(`Expected body to contain a chat, but received: ${JSON.stringify(body)}`); | ||
} | ||
if (!channelPost?.chat) { | ||
throw new Error(`Expected event to contain a chat, but received: ${JSON.stringify(event)}`); | ||
} | ||
|
||
this.$emit(body, | ||
{ | ||
id: body.update_id, | ||
summary: `${channelPost.chat.title} - ${channelPost.text}`, | ||
ts: Date.now(), | ||
}); | ||
this.$emit(event, this.getMeta(event, channelPost)); | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
DEPLOY_OFFSET: -25, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import telegramBotApi from "../../telegram_bot_api.app.mjs"; | ||
import constants from "./constants.mjs"; | ||
import { v4 as uuid } from "uuid"; | ||
|
||
export default { | ||
props: { | ||
telegramBotApi, | ||
db: "$.service.db", | ||
http: { | ||
label: "HTTP Responder", | ||
description: "Exposes a `respond()` method that lets the source issue HTTP responses", | ||
type: "$.interface.http", | ||
customResponse: true, | ||
}, | ||
}, | ||
hooks: { | ||
async deploy() { | ||
/** | ||
* From the docs: https://core.telegram.org/bots/api#getting-updates | ||
* | ||
* Incoming updates are stored on the server until the bot receives them either way, | ||
* but they will not be kept longer than 24 hours. | ||
* | ||
* So there's a big change that no historical event is emitted. | ||
*/ | ||
console.log("Fetching most recent events..."); | ||
const events = await this.telegramBotApi.getUpdates({ | ||
offset: constants.DEPLOY_OFFSET, | ||
allowed_updates: this.getEventTypes(), | ||
}); | ||
console.log(`Received ${events.length} event(s)`); | ||
for (const event of events) { | ||
this.processEvent(event); | ||
} | ||
}, | ||
async activate() { | ||
const secret = uuid(); | ||
this.setSecret(secret); | ||
await this.telegramBotApi.createHook(this.http.endpoint, this.getEventTypes(), secret); | ||
}, | ||
async deactivate() { | ||
await this.telegramBotApi.deleteHook(); | ||
}, | ||
}, | ||
methods: { | ||
getSecret() { | ||
return this.db.get("secret"); | ||
}, | ||
setSecret(secret) { | ||
this.db.set("secret", secret); | ||
}, | ||
getEventTypes() { | ||
throw new Error("getEventTypes is not implemented"); | ||
}, | ||
processEvent() { | ||
throw new Error("processEvent is not implemented"); | ||
}, | ||
}, | ||
async run(event) { | ||
// check if event has the same secret | ||
if (event.headers["x-telegram-bot-api-secret-token"] !== this.getSecret()) { | ||
console.log("Could not identify sender identity, exiting..."); | ||
return; | ||
} | ||
|
||
this.http.respond({ | ||
status: 200, | ||
}); | ||
|
||
const { body } = event; | ||
|
||
if (!body) { | ||
console.log("No body received, exiting..."); | ||
return; | ||
} | ||
|
||
this.processEvent(body); | ||
}, | ||
}; |
57 changes: 19 additions & 38 deletions
57
components/telegram_bot_api/sources/message-updates/message-updates.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,31 @@ | ||
// eslint-disable-next-line camelcase | ||
import telegramBotApi from "../../telegram_bot_api.app.mjs"; | ||
import base from "../common/webhooks.mjs"; | ||
|
||
export default { | ||
type: "source", | ||
...base, | ||
key: "telegram_bot_api-message-updates", | ||
name: "Message Updates (Instant)", | ||
name: "New Message Updates (Instant)", | ||
description: "Emit new event each time a Telegram message is created or updated.", | ||
version: "0.0.3", | ||
version: "0.1.0", | ||
type: "source", | ||
dedupe: "unique", | ||
props: { | ||
db: "$.service.db", | ||
// eslint-disable-next-line pipedream/props-label,pipedream/props-description | ||
http: { | ||
type: "$.interface.http", | ||
customResponse: true, | ||
methods: { | ||
...base.methods, | ||
getMeta(event, message) { | ||
return { | ||
id: event.update_id, | ||
summary: message.text, | ||
ts: new Date(message.edit_date ?? message.date), | ||
}; | ||
}, | ||
telegramBotApi, | ||
}, | ||
hooks: { | ||
async activate() { | ||
await this.telegramBotApi.createHook(this.http.endpoint, [ | ||
getEventTypes() { | ||
return [ | ||
"message", | ||
"edited_message", | ||
]); | ||
]; | ||
}, | ||
async deactivate() { | ||
await this.telegramBotApi.deleteHook(); | ||
processEvent(event) { | ||
const message = event.edited_message ?? event.message; | ||
this.$emit(event, this.getMeta(event, message)); | ||
}, | ||
}, | ||
async run(event) { | ||
if ((event.path).substring(1) !== this.telegramBotApi.$auth.token) { | ||
return; | ||
} | ||
this.http.respond({ | ||
status: 200, | ||
}); | ||
const { body } = event; | ||
if (!body) { | ||
return; | ||
} | ||
const message = body.edited_message ?? body.message; | ||
this.$emit(body, | ||
{ | ||
id: body.update_id, | ||
summary: message.text, | ||
ts: Date.now(), | ||
}); | ||
}, | ||
}; |
Oops, something went wrong.