Basic Twitch Bot Example
This app is a fully customizable example Twitch bot that you can add to your
channel and share with others via invite link. After setting up an account
and installing, your bot will:
- Confirm installation with a message to your Twitch bot account's channel.
- Respond when it is tagged in a channel with a friendly, informational message.
- Respond to the prefix command !invite with its invite link.
- Reply to messages containing the words hi, hey, hello, or sup
with a sample message.
You can completely customize your bot's behavior by editing the code in each
relevant handler, and your bot even supports invites to other channels via link!
Here's how it works:
Bot Mention Handler
You can find the bot mention handler under functions/events/twitch/bot_mention.js
.
After linking your bot to Autocode, tag it with @bot_name
in its channel to
see a message like the one below:
!invite Prefix Command Handler
You can find the handler that runs when you type the !invite in your server
under functions/events/twitch/message/create/prefix.js
. If you open the file,
you'll see that it's set to respond to the Twitch message.create.prefix
event
with the subtype !invite
:
This event only triggers the handler when someone sends a message prefixed with
!invite in a channel. You can change the prefix to whatever you wish by
clicking the bar and setting a different prefix in the input.
The code itself contains two API calls: the first retrieves data on the bot, and
the second uses that data to generate an invite link and send it as a chat
message. Here's what it looks like:
let authedUserData = await lib.twitch.users['@1.0.1'].me.retrieve();
await lib.twitch.chat['@1.0.0'].messages.create({
channel_name: context.params.event.channel.name,
content: [
`Hey there! You can invite me to your server by using this link:`,
`https://bot.gg/twitch/invite/${authedUserData.app.client_id}/`
].join(' ')
});
Others can then use the invite link to add the bot to their own Twitch channels.
You can try it for yourself as well!
Message Create Handler
This handler replies with a message when a someone in your bot's channels sends
message containing the words hi, hey, hello, or sup. The sent
reply contains a link where you can edit your project in the Autocode editor:
You can find the code that gets triggered under functions/events/twitch/message/create.js
.
The handler's event source is set to the Twitch message.create
event, which,
unlike the previous message.create.prefix
event, triggers the handler
anytime a user sends a message to a channel your bot is in. To limit the messages
it actually sends replies to, the code contains an if
statement.
Here's what it looks like:
// Only respond to messages containing the word "hi", "hey", "hello", or "sup"
if (context.params.event.content.match(/\bhi\b|\bhey\b|\bhello\b|\bsup\b/i)) {
await lib.twitch.chat['@1.0.0'].messages.create({
channel_name: context.params.event.channel.name,
content: `Hey there! You said "${context.params.event.content}"!`
});
await lib.twitch.chat['@1.0.0'].messages.create({
channel_name: context.params.event.channel.name,
content: `This is an example Autocode handler that responds to messages containing "hi", "hey", "hello", or "sup".`
});
let editorLinkMessageContent;
if (context.service.environment) {
editorLinkMessageContent = [
`If you're the bot creator, you can modify this behavior by editing the Autocode project's code here:`,
`https://autocode.com/p/${context.service.name}/${context.service.environment}/?filename=${encodeURIComponent('functions/' + context.name + '.js')}`
].join(' ');
} else {
editorLinkMessageContent = [
`If you're the bot creator, you can modify this behavior by editing the Autocode project's code from your dashboard:`,
`https://autocode.com/dashboard/`
].join(' ');
}
await lib.twitch.chat['@1.0.0'].messages.create({
channel_name: context.params.event.channel.name,
content: editorLinkMessageContent
});
}
There are a few things happening here. The argument of the .match()
call is
a regular expression (regex for short) that matches the content of the
incoming message against a specific pattern.
The single /
characters start and end the regex, and the \b
character matches word boundary characters like spaces or periods so that messages
like "Ohio" don't trigger a response. The |
characters mean "OR", and make the
defined pattern match any of the specified groups. Putting it all together, the
regex matches any message that contains "hi", "hey", "hello", or "sup" as a distinct word.
Inside the if
statement, the handler sends several messages using the messages.create
endpoint of the twitch/chat API. The app uses
separate messages here to make the reply easier to read, since Twitch does not
allow newlines in messages.
Autocode automatically defines a context
variable containing
information about the incoming request, including the incoming event. To see all the
available fields an event contains, you can view the endpoint's Payload.
Inviting Your Bot to Other Channels
You can allow others to invite your bot to their channels by sharing your bot's
invite link. This link is generally structured as https://bot.gg/twitch/invite/<client id>/
,
and supports an optional scope
query parameter if you'd like your bot to only
use specific scopes. Once someone else navigates to the link, they will see
a screen like this:
From here, they can grant the bot the required scopes and add it to their channel.
Once they do, your bot will start receiving chat events from them and will be
able to call APIs on their behalf!
You can also find your invite link by using the
!invite prefix command in this example app, or by opening an Autocode project
that your bot is connected to and clicking the Twitch icon in the corner.
Useful Links
Thank You!
If you have any questions or feedback, please join our community Discord server
by clicking here. You can also follow us on
Twitter, @Autocode.