soxtoby / SlackNet

A comprehensive Slack API client for .NET
MIT License
208 stars 65 forks source link

How to retrieve a voice message? #167

Closed gingters closed 10 months ago

gingters commented 1 year ago

I am very new to Slack (I searched but did not find anything in the API for that), and also very new to SlackNet, so please bear with me if this is a question that should be very easy to figure out.

I want a bot to receive a voice message and get the audio file for that, in order to send that through OpenAIs whisper (as the Slack transcriptions are okay, but not really good, whisper is way better).

I however can't figure out a way how to figure out if a message has audio and if yes, how to access that. What do I need to do for that?

soxtoby commented 1 year ago

Thanks for the question, @gingters. Audio and video files are available through the Files property on MessageEvents. The Files have a UrlPrivateDownload property that provides a URL to download the file from, which you can then upload to a 3rd party service. Unfortunately there's no way to update Slack's automatic transcription as far as I can tell, but you could post a message in the original message's thread.

Here's a rough implementation, based on the NoContainerExample code:

new SlackServiceBuilder()
    .UseApiToken("<your api token>")
    .UseAppLevelToken("<your app level token>")
    .RegisterEventHandler<MessageEvent>(ctx => new AudioTranscriber(ctx.ServiceProvider.GetApiClient()));

var client = slackServices.GetSocketModeClient();
await client.Connect();

class AudioTranscriber : IEventHandler<MessageEvent>
{
    private readonly ISlackApiClient _slack;
    public AudioTranscriber(ISlackApiClient slack) => _slack = slack;

    public async Task Handle(MessageEvent message)
    {
        // Slack's audio clips are webm, but you could handle other uploaded files as well
        foreach (var file in message.Files.Where(f => f.Mimetype == "audio/webm"))
        {
            using var httpClient = new HttpClient();
            // Note that the download URL requires authentication
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "<your api token>");
            var fileBytes = await httpClient.GetByteArrayAsync(file.UrlPrivateDownload);
            var transcription = await TranscribeWithWhisper(fileBytes);
            await _slack.Chat.PostMessage(new Message
                {
                    Channel = message.Channel,
                    ThreadTs = message.Ts,
                    Text = transcription
                });
        }
    }
}

It looks like SlackNet is missing a handful of audio- and video-specific file properties that Slack returns (including their transcription), which I'll try to add soon, but hopefully this works well enough for now.

stale[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

mousliiim commented 5 months ago

Hello !

Nothing new for slack's automatic transcription ? :D

soxtoby commented 5 months ago

Hi @mousliiim. Slack hasn't made any changes around video transcription APIs that I'm aware of. You can check the latest changes to the API on their changelog page, but given that they don't mention video transcription anywhere on their docs site, I wouldn't hold your breath.