groupmebot

package module
v0.0.0-...-64073d7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2019 License: MIT Imports: 10 Imported by: 1

README

GroupMeBot Framework

This is a GroupMe Bot Framework that does the basics for you. The bot is configured using the developer API information. Once that file is created, the bot is able to start and respond to incoming messages, assuming your ports are open and your bot is registered to your IP address. To add functionality, simply add functions that generate the appropriate responses and create a hook with whatever trigger you'd like!

Setup

Installing Go

This project uses the programming language Go. Make sure you install it and set your GOPATH environment variable to the location of the directory that you are working.

Creating your first bot
  1. Grab the framework to get started!
go get -u -v github.com/adammohammed/groupmebot
  1. To create the bot first make your folders. As with Go convention, just change "user" to your GitHub username and "mybot" to the name of the bot.
mkdir -p $GOPATH/src/github.com/user/mybot
cd $GOPATH/src/github.com/user/mybot
  1. Create your mybot_cfg.json file shown above with your credentials inserted for that bot_id and group_id

If you haven't already you may need to to the following:

  • Create a bot on the GroupMe Website.
  • Make sure and take note of your bot_id and group_id.
{
  "bot_id": "your_bot_id",
  "group_id": "your_group_id",
  "host": "0.0.0.0",
  "port": "8080"
}

Make sure that the bot you created on the GroupMe Website has the callback URL the same as the External IP and port of the host machine. Also make sure that whichever port you use, is open on your host.

  1. Copy/Create a main file similar to the one in this repositories example folder
cp $GOPATH/src/github.com/adammohammed/groupmebot/example/main.go .
  1. Finally you can run your bot using the go run main.go command
Creating plugins

Defining hooks is simple. The hooks signature needs to be as follows:

func my_hook(msg groupmebot.InboundMessage) (string) {
        resp := fmt.Sprintf("Hi there, %v.", msg.Name)
        return resp
}

This function must accept only an Inbound message as input. The output must be a string. The actual body of the function can do whatever you please but the signature is vital to be able to add it to the list of hooks.

Adding items to the hooks is done as shown below. Assume that these are function names for functions defined with the signature we defined earlier.

bot.AddHook("Hi!$", my_hook)

The first parameter to the AddHook method is the regular expression trigger that the message text must match. In this example, if the message text ends with "Hi!", the bot will send a response. If the bot finds the incoming message matches the trigger expression, the function is executed. When a message is received by the bot, it checks to see if the text matches any available hooks added by the AddHooks method.

Future changes

  • Add hooks by having them satisfy an interface instead of matching function signature

License

Copyright (c) 2017 Adam Mohammed

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSVLogger

type CSVLogger struct {
	LogFile string
}

func (CSVLogger) LogMessage

func (logger CSVLogger) LogMessage(msg InboundMessage)

type CompositeLogger

type CompositeLogger struct {
	Loggers []Logger
}

func (CompositeLogger) LogMessage

func (suite CompositeLogger) LogMessage(msg InboundMessage)

type GroupMeBot

type GroupMeBot struct {
	ID               string `json:"bot_id"`
	GroupID          string `json:"group_id"`
	Host             string `json:"host"`
	Port             string `json:"port"`
	TrackBotMessages bool   `json:"trackbotmessages"`
	Server           string
	Hooks            map[string]func(InboundMessage) string
	Logger
}

func (*GroupMeBot) AddHook

func (b *GroupMeBot) AddHook(trigger string, response func(InboundMessage) string)

func (*GroupMeBot) ConfigureFromJson

func (b *GroupMeBot) ConfigureFromJson(filename string) error

/ This reads a json file containing the keys / See the example bot_cfg.json / Updates existing bot with parameters from JSON filename / Returns err from ioutil if file can not be read

func (*GroupMeBot) HandleMessage

func (b *GroupMeBot) HandleMessage(msg InboundMessage)

func (*GroupMeBot) Handler

func (b *GroupMeBot) Handler() http.HandlerFunc

This is legitimate black magic, this is pretty cool, not usually able to do things like this in other languages. This is a function that takes a list of trigger functions and returns a function that can handle the Server Requests

func (*GroupMeBot) SendMessage

func (b *GroupMeBot) SendMessage(outMessage string) (*http.Response, error)

type InboundMessage

type InboundMessage struct {
	Id           string                   `json:"id"`
	Avatar_url   string                   `json:"avatar_url"`
	Name         string                   `json:"name"`
	Sender_id    string                   `json:"sender_id"`
	Sender_type  string                   `json:"sender_type"`
	System       bool                     `json:"system"`
	Text         string                   `json:"text"`
	Source_guid  string                   `json:"source_guid"`
	Created_at   int                      `json:"created_at"`
	User_id      string                   `json:"user_id"`
	Group_id     string                   `json:"group_id"`
	Favorited_by []string                 `json:"favorited_by"`
	Attachments  []map[string]interface{} `json:"attachments"`
}

type Logger

type Logger interface {
	LogMessage(msg InboundMessage)
}

A CSVLogger comes with the bot, but any logger can be substituted so long as it satisfies this interface

type OutboundMessage

type OutboundMessage struct {
	ID   string `json:"bot_id"`
	Text string `json:"text"`
}

type StdOutLogger

type StdOutLogger struct {
}

func (StdOutLogger) LogMessage

func (logger StdOutLogger) LogMessage(msg InboundMessage)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL