|
1 | 1 | import { Response } from 'express'
|
| 2 | +import _ from 'lodash' |
2 | 3 | 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' |
4 | 5 | import yn from 'yn'
|
5 | 6 | import { ChannelApi, ChannelApiManager, ChannelApiRequest } from '../base/api'
|
6 | 7 | import { ChannelInitializeEvent, ChannelStartEvent, ChannelStopEvent } from '../base/service'
|
@@ -58,6 +59,36 @@ export class TelegramApi extends ChannelApi<TelegramService> {
|
58 | 59 | private async handleTelegrafMessage(scope: string, ctx: NarrowedContext<Context<Update>, Update.MessageUpdate>) {
|
59 | 60 | if ('text' in ctx.message) {
|
60 | 61 | 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 | + }) |
61 | 92 | }
|
62 | 93 | }
|
63 | 94 |
|
@@ -103,4 +134,15 @@ export class TelegramApi extends ChannelApi<TelegramService> {
|
103 | 134 | // TODO: remove this dependency on server env vars
|
104 | 135 | return !yn(process.env.SPINNED) || yn(process.env.CLUSTER_ENABLED)
|
105 | 136 | }
|
| 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 | + } |
106 | 148 | }
|
0 commit comments