Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
raed-shomali committed May 20, 2017
0 parents commit cd33d12
Show file tree
Hide file tree
Showing 70 changed files with 7,036 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Raed Shomali

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.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# slacker
Slack Bot Library
21 changes: 21 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package slacker

import "github.com/shomali11/slacker/expression"

func NewCommand(cmd string, description string, handler func(request *Request, response *Response)) *Command {
return &Command{cmd: cmd, description: description, handler: handler}
}

type Command struct {
cmd string
description string
handler func(request *Request, response *Response)
}

func (c *Command) Match(text string) (bool, map[string]string) {
return expression.Match(c.cmd, text)
}

func (c *Command) Execute(request *Request, response *Response) {
c.handler(request, response)
}
19 changes: 19 additions & 0 deletions examples/example1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"github.com/shomali11/slacker"
"log"
)

func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")

bot.Command("ping", "Ping!", func(request *slacker.Request, response *slacker.Response) {
response.Reply("Pong")
})

err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
23 changes: 23 additions & 0 deletions examples/example2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"github.com/shomali11/slacker"
"log"
)

func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")

bot.Init(func() {
log.Println("Connected!")
})

bot.Err(func(err string) {
log.Println(err)
})

err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
20 changes: 20 additions & 0 deletions examples/example3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/shomali11/slacker"
"log"
)

func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")

bot.Command("echo <word>", "Echo words!", func(request *slacker.Request, response *slacker.Response) {
word := request.Param("word")
response.Reply(word)
})

err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
23 changes: 23 additions & 0 deletions examples/example4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"github.com/shomali11/slacker"
"log"
)

func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")

bot.Command("repeat <word> <number>", "Repeat a word a number of times!", func(request *slacker.Request, response *slacker.Response) {
word := request.StringParam("word", "Hello!")
number := request.IntegerParam("number", 1)
for i := 0; i < number; i++ {
response.Reply(word)
}
})

err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
22 changes: 22 additions & 0 deletions examples/example5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/nlopes/slack"
"github.com/shomali11/slacker"
"log"
)

func main() {
bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>")

bot.Command("upload <word>", "Upload a word!", func(request *slacker.Request, response *slacker.Response) {
word := request.Param("word")
channel := request.Event.Channel
bot.Client.UploadFile(slack.FileUploadParameters{Content: word, Channels: []string{channel}})
})

err := bot.Listen()
if err != nil {
log.Fatal(err)
}
}
73 changes: 73 additions & 0 deletions expression/expression.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package expression

import (
"regexp"
"strings"
)

const (
empty = ""
space = " "
ignoreCase = "(?i)"
parameterPattern = "<\\S+>"
spacePattern = "\\s*"
wordPattern = "(\\S+)?"
)

func Match(command string, text string) (bool, map[string]string) {
parameters := make(map[string]string)
pattern := extractPattern(command)
if len(pattern) == 0 {
return false, parameters
}

compiledExpression, err := regexp.Compile(pattern)
if err != nil {
return false, parameters
}

result := strings.TrimSpace(compiledExpression.FindString(text))
if len(result) == 0 {
return false, parameters
}

commandTokens := strings.Split(command, space)
resultTokens := strings.Split(result, space)

valueRegex := regexp.MustCompile(parameterPattern)
for i, resultToken := range resultTokens {
commandToken := commandTokens[i]
isValue := valueRegex.MatchString(commandToken)
if !isValue {
continue
}

parameters[commandToken[1:len(commandToken)-1]] = resultToken
}
return true, parameters
}

func IsParameter(text string) bool {
valueRegex := regexp.MustCompile(parameterPattern)
return valueRegex.MatchString(text)
}

func extractPattern(command string) string {
command = strings.TrimSpace(command)
tokens := strings.Split(command, space)
if len(tokens) == 0 {
return empty
}

pattern := empty
for _, token := range tokens {
isMatch := IsParameter(token)
if isMatch {
pattern += wordPattern
} else {
pattern += token
}
pattern += spacePattern
}
return ignoreCase + pattern
}
50 changes: 50 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package parser

import "strconv"

func StringParam(key string, parameters map[string]string, defaultValue string) string {
value, ok := parameters[key]
if !ok {
return defaultValue
}
return value
}

func BooleanParam(key string, parameters map[string]string, defaultValue bool) bool {
value, ok := parameters[key]
if !ok {
return defaultValue
}

integerValue, err := strconv.ParseBool(value)
if err != nil {
return defaultValue
}
return integerValue
}

func IntegerParam(key string, parameters map[string]string, defaultValue int) int {
value, ok := parameters[key]
if !ok {
return defaultValue
}

integerValue, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}
return integerValue
}

func FloatParam(key string, parameters map[string]string, defaultValue float64) float64 {
value, ok := parameters[key]
if !ok {
return defaultValue
}

integerValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return defaultValue
}
return integerValue
}
39 changes: 39 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package slacker

import (
"github.com/nlopes/slack"
"github.com/shomali11/slacker/parser"
)

const (
empty = ""
)

func NewRequest(event *slack.MessageEvent, parameters map[string]string) *Request {
return &Request{Event: event, parameters: parameters}
}

type Request struct {
Event *slack.MessageEvent
parameters map[string]string
}

func (r *Request) Param(key string) string {
return r.StringParam(key, empty)
}

func (r *Request) StringParam(key string, defaultValue string) string {
return parser.StringParam(key, r.parameters, defaultValue)
}

func (r *Request) BooleanParam(key string, defaultValue bool) bool {
return parser.BooleanParam(key, r.parameters, defaultValue)
}

func (r *Request) IntegerParam(key string, defaultValue int) int {
return parser.IntegerParam(key, r.parameters, defaultValue)
}

func (r *Request) FloatParam(key string, defaultValue float64) float64 {
return parser.FloatParam(key, r.parameters, defaultValue)
}
22 changes: 22 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package slacker

import (
"github.com/nlopes/slack"
)

func NewResponse(channel string, rtm *slack.RTM) *Response {
return &Response{channel: channel, rtm: rtm}
}

type Response struct {
channel string
rtm *slack.RTM
}

func (r *Response) Reply(text string) {
r.rtm.SendMessage(r.rtm.NewOutgoingMessage(text, r.channel))
}

func (r *Response) Typing() {
r.rtm.SendMessage(r.rtm.NewTypingMessage(r.channel))
}
Loading

0 comments on commit cd33d12

Please sign in to comment.