How to send a voice note file to a Telegram group from bash shell script

Using the WhatsMate Telegram Gateway REST API



This article shows you how to send a voice note file (e.g. an OPUS file) to a registered Telegram group from a bash shell script.

To send a Telegram message containing a voice note file to a group from a bash shell script, do this:

  1. First, learn how to send a simple text message to a group on the official site.
  2. With the above knowledge, you can now download our sample code.
  3. Locate the file bash/group-send-telegram-opus.sh.
    #!/bin/bash
    #######################################################################
    # You will need to have the utility `base64` available on your Linux
    # system to run this script.
    #
    # To install it:
    # sudo apt-get install coreutils
    #######################################################################
    # TODO: Put down your own Client ID and secret here:
    INSTANCE_ID="YOUR_OWN_INSTANCE_ID_HERE"
    CLIENT_ID="YOUR_OWN_CLIENT_ID_HERE"
    CLIENT_SECRET="YOUR_OWN_SECRET_HERE"
    # TODO: Customize the following 5 files:
    group_name="Muscle Men Club"
    group_admin="12025550108"
    base64_audio=`base64 -w 0 ../assets/martin-luther-king.opus`
    fn="anyname.opus"
    caption="I have a dream"
    cat > /tmp/jsonbody.txt << _EOM_
    {
    "group_name": "$group_name",
    "group_admin": "$group_admin",
    "voice_note": "$base64_audio",
    "filename": "$fn",
    "caption": "$caption"
    }
    _EOM_
    curl --show-error -X POST \
    -H "X-WM-CLIENT-ID: $CLIENT_ID" \
    -H "X-WM-CLIENT-SECRET: $CLIENT_SECRET" \
    -H "Content-Type: application/json" \
    --data-binary @/tmp/jsonbody.txt \
    https://api.whatsmate.net/v3/telegram/group/voice_note/message/$INSTANCE_ID
    echo -e "\n=== END OF DEMO ==="
  4. Study the script and customize the TODO/FIXME lines.
  5. Change to the directory containing the script: cd bash
  6. Run the script to send your group the first OPUS file: ./group-send-telegram-opus.sh

Happy coding :)