Code Monkey home page Code Monkey logo

hangoutsbot's Introduction

HangoutsBot

Before you get started, I highly recommend you take a look at xmikos's Hangupsbot from which this is a fork of.

Setup

In order to use this, you'll need to setup a GMail account for logging in, and a config.json file (placed either at the folder root or in the Core folder) to give the bot its settings. The config file should look something like:

{
  "admins": ["YOUR-USER-ID-HERE"],
  "autoreplies_enabled": true,
  "autoreplies": [
    [["^@[\\w\\s]+\\++$"],"/karma {}"],
    [["^@[\\w\\s]+-+$"],"/karma {}"],
    [["bot", "robot", "Yo"], "/think {}"]
  ],
  "development_mode": false,
  "commands_admin": ["hangouts", "reload", "quit", "config", "block", "record clear"],
  "commands_conversation_admin": ["leave", "echo", "block"],
  "commands_enabled": true,
  "forwarding_enabled": false,
  "rename_watching_enabled": true,
  "conversations": {
    "CONV-ID-HERE": {
      "conversation_admin": "USER-ID-HERE",
      "autoreplies": [
        [["whistle", "bot", "whistlebot"], "/think {}"],
        [["trash"], "You're trash"]
      ],
      "forward_to": [  
        "CONV1_ID" 
      ]
    }
  }
}

If you neglect to create a config.json file, this exact one will be created for you in the Core folder.

Line by Line breakdown (excluding braces/brackets):

  1. Sets the admins. Only admins can use the admin commands listed in commands_admin.
  2. Sets autoreplies to be enabled for every conversation the bot is in.
  3. Sets the autoreplies for all conversations the bot is in.
  4. Sets "Dev Mode" to default to off for all conversations. Dev Mode will force the bot to print out all of it's replies to the console window instead of replying via Hangouts.
  5. Array of all of the commands that only admins will have access to use. Note: You can block subcommands by explictly stating the command and subcommand (as in "record clear"). You can block all subcommands by using "command *"
  6. Array of all of the commands that only conversation admins and normal admins will have access to use.
  7. Sets commands to be enabled for all chats.
  8. Sets chat forwarding for all chats to disabled. (When enabled, you must have a conversation object with a "forward_to" member that is set to a different conversation id.)
  9. Sets rename watching to enabled. (This is required to have /record record name changes.)
  10. Start of the conversations dictionary.
  11. Start of a conversation specific dictionary. "CONV-ID-HERE" should be replaced with an actual id, which looks something like "Ugxxxxxxxxxxx_xxxxxx4AaABAQ".
  12. Sets the conversation admin for a specific conversation. Unlike the admins array on line 1, there can only be one conversation admin per conversation.
  13. Sets the autoreplies for this specific conversation, which entirely overrides any autoreplies set for all conversations.
  14. Sets an autoreply keyword and reply. In this case, "whistle", "bot", and "whistlebot" are all keywords, and the reply will be the command /think, which will be given the entirety of what the user posted. For example: A user saying "Bot, how are you?" would cause the command "/think Bot, how are you?" to be ran. NOTE: The keywords are case-insensitive.
  15. Sets another autoreply keyword and reply. In this case, if a user says "trash," the Bot will reply with "You're trash." NOTE: The keywords are case-insensitive.
  16. Start of the "forward_to" array. All conversation IDs listed here will have this conversation forwarded to them.
  17. "CONV-ID-HERE" should be replaced with an actual id, which looks something like "Ugxxxxxxxxxxx_xxxxxxxxxxxxx", and then commands will be forwarded to that conversation.

Note: Every top-level argument can be made a conversation level argument, which will overwrite the top level argument for that conversation. For instance, if you put a commands_admin array containing only "quit" and "block" in "CONV-ID-HERE", in the CONV-ID-HERE conversation, all users would be able to run /hangouts, /reload, /config, etc but would still be blocked from running /quit and /block. Conversely, if you put an empty commands_admin array in CONV-ID-HERE, all users in that conversation would be able to run all commands (except any commands in the "commands_conversation_admin" array unless they're a conversation admin.)

A cookies.txt file will also be created, holding the cookies that are valid for your login.

Usage

To actually get the bot up and running, run the Main.py file. If you have Git installed, it will attempt to pull the most recent version from the repo. If you don't want that functionality, simply delete the os.system("git pull") line from Main.py.

On first load, it will ask you for an Email and Password for a Google Account. Input that and the bot will start.

Upon connection, test to make sure that the bot is functioning properly by starting a chat with it and using /ping. If it replies with 'pong', you're in business! If not, manually log into the bot's gmail account and see if it didn't auto-accept the Hangouts invitation.

Adding Functionality

Any function created in the ExtraCommands.py file (or any .py file that imports the DispatcherSingleton and is manually imported in the Handlers.py file or placed in the Core/Commands folder) and decorated with the @DispatcherSingle.register annotation will be automatically picked up by the bot upon next restart. Commands are very simple and should be in the style of:

@DispatcherSingleton.register
def command(bot, event, *args):
    """Command
    Usage: /command <required argument> <optional: optional argument> {for complicated commands, put an example call in curly braces}
    Purpose: Does a thing."""
    
    # Do actual functionality here.

The docstring isn't necessary, but should be used as it will be printed out if a user uses "/help <command name>" or does "/<command_name> ?". Admin-only commands generally don't have help text by default, but that's left up to your discretion.

The bot variable is a reference to the HangoutsBot that is currently running. The event variable is a ConversationEvent that has information about the event (as in, text, the user who sent it, the conversation, and all users in the conversation.) The *args variable is a tuple of all of the text sent along with the command call, split by whitespace so that you can easily check its values. Look at the DefaultCommands.py file for actual examples of how commands work.

It should be frowned upon to force a user to put underscores in to use a command, but that's up to you to decide.

Have fun botting!

Extra

Special credit to:

tdryer for the wonderful Hangups API.
xmikos for a flawless foundation and them some to build off of.
DocJava for support, help, and rubber ducking.

This is always going to be a work in progress, and has been built with a focus on my personal conversations, hence some of the more esoteric features. Regardless, I try to make it as accessible as possible for any one who does wish to use it. Expect a lot of changes and some weirdness.

hangoutsbot's People

Contributors

shaunofthelive avatar wardellbagby avatar mrdjohnson avatar cntrlwiz avatar xinayder avatar dav-s avatar

Stargazers

Archit Jain avatar Corbin Crutchley avatar Yousef Alam avatar  avatar

Watchers

 avatar James Cloos avatar Tom Dryer avatar  avatar Corbin Crutchley avatar

hangoutsbot's Issues

/youtube

Typing '/YouTube [name]' would paste the first YouTube result for that specific search.

add CleverBot session

"You should be able to start a cleverbot session per user

Like I do /thinkstart

And everything I say goes through a separate cleverbot session

Then /thinkend stops it"

add /colour command

generate solid colour images when you type in /color and /colour then follow with a hex value
e.g. /colour #3F51B5

/youtube KeyError

KeyError: 'data-context-item-id'
if the first result is a channel instead of a video, this happens with my parsing

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.