Skip to content

Commit ba628dd

Browse files
authoredOct 14, 2022
feat(telegram): allow receiving file/audio/video/image (#543)
* feat(telegram): allow receiving file/audio/video/image * Pr changes
1 parent 15205a0 commit ba628dd

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed
 

‎packages/channels/src/telegram/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
| Postback || |
2525
| Say Something || |
2626
| Voice || |
27-
| Image | | |
28-
| File | | |
29-
| Audio | | |
30-
| Video | | |
27+
| Image | | |
28+
| File | | |
29+
| Audio | | |
30+
| Video | | |
3131
| Location || |

‎packages/channels/src/telegram/api.ts

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Response } from 'express'
2+
import _ from 'lodash'
23
import { Context, NarrowedContext } from 'telegraf'
3-
import { Update } from 'telegraf/typings/core/types/typegram'
4+
import { Update, Document, Video, Audio, Message } from 'telegraf/typings/core/types/typegram'
45
import yn from 'yn'
56
import { ChannelApi, ChannelApiManager, ChannelApiRequest } from '../base/api'
67
import { ChannelInitializeEvent, ChannelStartEvent, ChannelStopEvent } from '../base/service'
@@ -58,6 +59,36 @@ export class TelegramApi extends ChannelApi<TelegramService> {
5859
private async handleTelegrafMessage(scope: string, ctx: NarrowedContext<Context<Update>, Update.MessageUpdate>) {
5960
if ('text' in ctx.message) {
6061
await this.service.receive(scope, this.extractEndpoint(ctx), { type: 'text', text: ctx.message.text })
62+
} else if ('photo' in ctx.message) {
63+
// Same photo in different sizes, get the last one since it
64+
// has the best quality
65+
const item = ctx.message.photo.pop()
66+
67+
if (!item) {
68+
return
69+
}
70+
71+
await this.service.receive(scope, this.extractEndpoint(ctx), {
72+
type: this.mapTypeToStandardType('photo'),
73+
url: await ctx.telegram.getFileLink(item.file_id),
74+
title: ctx.message.caption
75+
})
76+
} else {
77+
const message = ctx.message as Message.CaptionableMessage
78+
const type = Object.keys(message).find((i) => ['document', 'video', 'audio'].includes(i))
79+
80+
if (!type) {
81+
// Type not supported, swallow
82+
return
83+
}
84+
85+
const item = _.get(message, type) as Document | Video | Audio
86+
87+
await this.service.receive(scope, this.extractEndpoint(ctx), {
88+
type: this.mapTypeToStandardType(type),
89+
url: await ctx.telegram.getFileLink(item.file_id),
90+
title: message.caption
91+
})
6192
}
6293
}
6394

@@ -103,4 +134,15 @@ export class TelegramApi extends ChannelApi<TelegramService> {
103134
// TODO: remove this dependency on server env vars
104135
return !yn(process.env.SPINNED) || yn(process.env.CLUSTER_ENABLED)
105136
}
137+
138+
private mapTypeToStandardType(type: string) {
139+
switch (type) {
140+
case 'document':
141+
return 'file'
142+
case 'photo':
143+
return 'image'
144+
default:
145+
return type
146+
}
147+
}
106148
}

0 commit comments

Comments
 (0)