Create an Audio Message and Share it on Slack

See how it is done!

1. Create your Slack account

Go to https://slack.com/intl/en-es/help and click on Sign up

👍

Already have a Slack account?

You're all set! Move on to step 2

2. Create your Slack app and set it up

1️⃣ Log into your Slack account, and select select the Slack Workspace to send messages to
2️⃣ Go to https://api.slack.com/ and click on Create a custom app
3️⃣ Choose a name and a Development Slack Workspace and click on Create App
4️⃣ On the left menu click on OAuth & Permissions and under "Bot Token Scopes" select Add an OAuth Scope. Select "files:write", "groups:write" and "chat:write".
5️⃣ Scroll up and click on Install to Workspace and Allow. Copy the Bot User OAuth Token.
6️⃣ Now, in Slack Workspace, find the group you want to send the audio message to and copy&paste: /invite @your_App_name.
Now we can start coding!

3. Create your audio file with the API.audio

1️⃣ Obtain and import your API key
2️⃣ Create and download a voice clip from text with API.audio

👍

Customise your script text and choose your favourite speaker and background track!

import apiaudio

apiaudio.api_key = "YOUR_API_KEY"

def apiaudio_create(scriptName, message):
    script = apiaudio.Script().create(scriptText= message, scriptName=scriptName, moduleName="hello_world", projectName="hello_world")
    print(script)
    response = apiaudio.Speech().create(scriptId=script.get("scriptId"), voice="Joanna")
    print(response)
    response = apiaudio.Mastering().create(scriptId=script.get("scriptId"),soundTemplate="heatwave")
    print(response)
    file = apiaudio.Mastering().download(scriptId=script.get("scriptId"), destination=".")
    file = apiaudio.Mastering().retrieve(scriptId=script.get("scriptId"))
    return file["url"]
apiaudio_create("hello_world", "Enjoy the Api! Write any message here!")
const Aflr = require("aflr").default;

const api_key = "YOUR_API_KEY";

const fileName = "";
const url_mp3 = "";

async function aflr_create(fileName) {
  try {
    Aflr.configure({ apiKey: api_key });
    let script = await Aflr.Script.create({
      scriptText: "Hey, Is it working??",
      proyectName: "Working_with_recipes",
      moduleName: "recipes",
      scriptName: fileName,
    });
    script = await Aflr.Script.retrieve(script["scriptId"]);

    let scripts = await Aflr.Script.list();
    console.log(scripts);

    let voices = await Aflr.Voice.list();
    console.log(voices);

    let speech = await Aflr.Speech.create({ scriptId: script["scriptId"] });
    console.log(speech);

    let speechResult = await Aflr.Speech.retrieve(script["scriptId"]);
    console.log(speechResult);

    let bg_tracks = await Aflr.Sound.list();
    console.log(bg_tracks);

    let mastering = await Aflr.Mastering.create({
      scriptId: script["scriptId"],
      backgroundTrackId: bg_tracks[4]["id"],
    });
    console.log(mastering);

    let masteringResult = await Aflr.Mastering.retrieve(script["scriptId"], {});
    console.log(masteringResult.url);
    url_mp3 = masteringResult.url;
    return url_mp3;
  } catch (e) {
    console.error(e);
  }
}

// Use the following function to download the mp3 from the url retrieved
const https = require("https");
const fs = require("fs");


async function downloadUrl(url_mp3) {
  try {
    const req = https.get(url_mp3, function (res) {
      const fileStream = fs.createWriteStream(fileName.concat(".mp3"));
      res.pipe(fileStream);
      fileStream.on("finish", function () {
        fileStream.close();
        console.log("Done!");
      });
    });
  } catch (e) {
    console.log(e);
  }
}

4. Send a message to the Slack channel with a file attached!

📘

The following sample code is given by the Slack API documentation. More information: https://api.slack.com/methods/files.upload/code

1️⃣Paste in the Bot User OAuth Token.
2️⃣Choose your target channel. (At the channel variable, it is possible to write the name of the channel or its id. More information about how to find the id here: https://www.wikihow.com/Find-a-Channel-ID-on-Slack-on-PC-or-Mac)

# Script to send files
# Documentation: https://api.slack.com/methods/files.upload/code
import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

client = WebClient("insert your token here")

try:
    filepath="hello_world.mp3"
    response = client.files_upload(channels='your_target_channel', initial_comment = "Hey! Listen to what I created for you :smile:", file=filepath)
    assert response["file"]
except SlackApiError as e:
    assert e.response["ok"] is False
    assert e.response["error"]
    print(f"Got an error: {e.response['error']}")
// Documentation: https://api.slack.com/methods/files.upload/code

const { createReadStream } = require("fs");
const { WebClient, LogLevel } = require("@slack/web-api");
const client = new WebClient("insert-your-token-here", {
  logLevel: LogLevel.DEBUG,
});

const fileName = "./default.mp3";
const channel = "your_target_channel";

async function SlackCall() {
  try {
    const result = await client.files.upload({
      // channels can be a list of one to many strings
      channels: channel,
      initial_comment: "Hey! Click here to download the audio :smile:",
      file: createReadStream(fileName),
    });
    console.log(result);
  } catch (error) {
    console.error(error);
  }
}

SlackCall();