Sending messages as a bot

I’m trying to send a message as my bot, but I’m running into some trouble.
I’m making the following request:

post("https://api.twitch.tv/helix/chat/messages")
        .header("Authorization", format!("Bearer {}", token))
        .header("Client-ID", client_id)
        .header("Content-Type", "application/json")
        .json(&json!({
            "broadcaster_id": user_id,
            "sender_id": bot_id,
            "message": content
        }));

Where token is the token I got from curl -X POST -d client_id=REDACTED -d client_secret=REDACTED -d grant_type=client_credentials "https://id.twitch.tv/oauth2/token", client_id is the client id of my bot, user_id is the id of the user whose chat I want to send a message to, and bot_id is the id of my bot user.

However, when making this request, I get a 401 stating ‘The sender must have authorized the app with the user:write:chat and user:bot scopes.’
What am I doing wrong?

NOTE: When sending a request with bot_id the same as user_id and having token as the user token, it does work, but then I send a message as the user instead of as my bot, which is not what I want.

You need the account that you wish to send messages as connect to your app, with the user:bot scope. Additionally the channel you wish to send messages to either needs to connect to your app with the channel:bot scope, or your bot needs to be a moderator in that channel.

This is mentioned in the Authorization section of the Send Chat Message docs.

I finally figured out what was wrong.
I come from discord bot development, where bots are their own users, but this is not the case here.
I did not realize I have to create a new twitch account for my bot in order to send and receive messages using it.
Thank you for the suggestion though!

Just curious:

If you didn’t

There where/how did you

Get the user ID of the bot for this request?
Without the user you wanted to send as pre-exsiting?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.