First, put the script to a common location e.g. /usr/local/bin/vm-post.py
Then, point your Asterisk mail command to this script to invoke it when a voice mail is received.
For example, in FreePBX:

The script will decode the text message (as configured from Asterisk) and the voice message (stored with wav encoding).
Since Telegram only accepts the .ogg
format as voice message, the script also performs wav -> ogg
conversion with ffmpeg
.
Finally, change the telegram bot token and chat id in the tg_post_voicemail()
function.
To test, dial an extension with voicemail enabled and leave a voice message. You should be able to receive it from your telegram chat window.
If it doesn't work for you, you can inspect the log from /tmp/vm_bot.log
.
The script tries to keep things simple and avoids introducing the telegram bot python package.
Last strings in my vm_bot.log:
2025-02-14 07:15:00.419574
Text message response: <Response [400]>
2025-02-14 07:43:05.694019
Text message response: <Response [400]>
2025-02-14 08:18:41.142118
Text message response: <Response [400]>
2025-02-14 08:28:47.346512
Text message response: <Response [400]>
2025-02-14 08:48:41.403831
Text message response: <Response [400]>
Which email system do you use? For me it is local postfix. Because after adding a mail command in config, emails didnt come.