Giter Site home page Giter Site logo

dialogflowtwitchbotbridge's Introduction

PatCat's Dialogflow Twitch Bot Bridge

Been looking for a way to use Dialogflow to make a Twitch bot? Look no further! Here's a quick and easy way to link up a Dialogflow agent to a Twitch account and channel.

The idea is that you should be able to allow your Dialogflow bot to respond to messages in chat, both from direct @ mentions and also just joining in the overall conversation when appropriate. The latter is a bit of a different use that Dialogflow isn't typically used for, so we've got a bit of additional settings for that to make sure the bot doesn't go a bit wild and respond to everything!.

It's still early days, so if you spot issues, definitely raise them here.

Usage

Want to give it a go? Do the following:

  1. Clone this repo somewhere where you'll want to run a Node server (I've typically just run it locally on my PC/Mac). Cloning should be as simple as:
git clone https://github.com/patcat/DialogflowTwitchBotBridge.git
  1. Open up server.js and change projectId to your own access token from Dialogflow (you can get that from https://dialogflow.com/docs/agents#settings).
projectId = "YOURTOKENHERE",
  1. Change dialogflowSessionID to any random series of characters you'd like to use (all one word or separated with - should be fine):
dialogflowSessionID = 'twitchbotbridge-session-id',
  1. Change dialogflowLanguageCode if you're using a different language than 'en-US' (I haven't ever tried an alternative language, so if you try this, let me know how it goes!),
dialogflowLanguageCode = 'en-US',
  1. Change genericIntents to list which intents in your Dialogflow agent should apply to the general chat in the room (e.g. run them even if your bot isn't mentioned directly in the message):
genericIntents = [{
    intent: "smalltalk.greetings.hello",
    freq: 0.5
},
{
    intent: "smalltalk.greetings.bye",
    freq: 0.5
}],

You can change the frequency they're run too. For things like hello and goodbye... the bot will be watching out for those all the time — so if everyone is saying the word "Hello", your bot would theoretically try to respond to that every time. freq: 0.5 adds a bit of randomness to it and sets up a 50% chance it'll actually respond instead. Use this to prevent your bot from constantly triggering generic responses, while also letting it get involved in chat occasionally.

  1. Add in your Twitch channel you'd like the bot to be chatting in:
// Twitch settings
twitchChannel = '#devdiner',
  1. Add in the bot's name (needs to be an actual account you've got access to!):
twitchBotName = 'TheGreatIntelligenceBot',
  1. You'll need a token that'll allow you to post on that account's behalf. Grab one from https://twitchapps.com/tmi/ if you don't already have one. Then put it into the twitchPassword field:
twitchPassword = 'oauth:YOUROAUTHHERE',
  1. When it comes to training up your bot, it can be a bit awkward for the messages in Dialogflow to all have @YourBotUsername constantly. That's not really pronouncable or what Dialogflow expects. So instead, we set a name that isn't the username to replace any instances of the username with (e.g. "Dr" for @DrArnoldBernard, you could use say "Barry" for @BarryAllenRunBarryRun as another example):
genericName = 'Bot',
  1. You could want your bot to respond instantly to anything, but that can be offputting. People don't respond instantly, they take time to read a message. Let's not show just how speedy and superior our bot is to its fellow Twitch humans by adding in a delay to mimic them reading the message first. The delay is in milliseconds:
typicalDelayBeforeResponding = 1000,
  1. Humans also take time to type a message. That time is different depending on how long that message was. The Dialogflow Twitch Bot Bridge can mimic that too! You can decide how many characters per minute your bot can type and we'll delay its responses accordingly:
charactersPerMinute = 300,
  1. Finally, Dialogflow has a fallback for when it doesn't understand something. If someone @ mentions your bot, you can decide whether it should respond or not if it doesn't know what they mean. If your bot isn't quite smart enough yet to respond to everything — and you don't want your chat spammed with "I didn't understand" type messages — you can tell Dialogflow Twitch Bot Bridge to just not respond if it doesn't understand instead.
keepQuietOnFallback = true;
  1. Once all is updated, run the following from root of your cloned folder to install all dependencies:
npm install
  1. You'll also need a Google Cloud Platform project and the assorted steps ready as per the instructions from the (you should have a file in your environment providing Google Application Credentials). The steps from the Dialogflow Node.js Client Library provide the following (from 15 - 19)

  2. Select or create a Cloud Platform project.

    Go to the projects page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Dialogflow API.

    Enable the API

  5. Set up authentication with a service account so you can access the API from your local workstation.

  6. Then, once everything is all good to go Google Cloud Platform wise, run it like so:

node server.js

Needing more help?

I'm releasing an online course on Dialogflow and I'll have a guide that'll explain a lot more of the process of building a Dialogflow bot there! If you're interested, register your interest for the Dialogflow content here!

Note

This API bridge is entirely free to use with a glorious MIT license — but by downloading it, you agree to be responsible with it. With great power comes great responsibility! Go forth and build cool things! Play nice.

Please check Twitch terms and conditions, along with other relevant rules and regulations, before using this Dialogflow Twitch Bot Bridge. You are responsible for anything that goes wrong while you use this, please use it with care!

dialogflowtwitchbotbridge's People

Contributors

patcat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

dialogflowtwitchbotbridge's Issues

API v2 Connects but Invalid Credentials Error

Dialogflow Twitch Bot Bridge is listening on port 55383 (node:11512) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.

Seems like it can't get permission to the Dialogflow agent. Unsure what the sessionID is supposed to be, using the long strip within URL. Can't find anything else that would be like a sessionID.

Connecting and authenticating... Ready to go!
Outputs that right after.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.