Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/langbot/pkg/platform/sources/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ def parse_message_text(text: str) -> list[platform_message.MessageComponent]:
)
)

if message.voice:
if message.caption:
message_components.extend(parse_message_text(message.caption))

file = await message.voice.get_file()

file_bytes = None
file_format = message.voice.mime_type or 'audio/ogg'

async with aiohttp.ClientSession(trust_env=True) as session:
async with session.get(file.file_path) as response:
file_bytes = await response.read()

message_components.append(
platform_message.Voice(
base64=f'data:{file_format};base64,{base64.b64encode(file_bytes).decode("utf-8")}',
length=message.voice.duration,
)
)

return platform_message.MessageChain(message_components)


Expand Down Expand Up @@ -159,7 +179,7 @@ async def telegram_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):

application = ApplicationBuilder().token(config['token']).build()
bot = application.bot
application.add_handler(MessageHandler(filters.TEXT | (filters.COMMAND) | filters.PHOTO, telegram_callback))
application.add_handler(MessageHandler(filters.TEXT | (filters.COMMAND) | filters.PHOTO | filters.VOICE, telegram_callback))
super().__init__(
config=config,
logger=logger,
Expand Down
Loading