Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0.0 fixes and improvements #15

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
81f0a29
add a v2 of hanu using the nlopes/slack client
penguinpowernz Oct 25, 2019
5b1f9ee
make things that check the message always work
penguinpowernz Oct 25, 2019
dfe2154
add channel object for shortcut around always using channel in bot.Say
penguinpowernz Oct 25, 2019
fafaa98
update README.md with v2 usage details
penguinpowernz Oct 25, 2019
8ee4282
add unknown command printer
penguinpowernz Oct 25, 2019
9d5a19f
ensure to manage the RTM connection, update README
penguinpowernz Oct 25, 2019
66c1b6d
the bot does NOT like getting his own identity
penguinpowernz May 30, 2020
edb40ea
allow for realtime messages using the actual slack object
penguinpowernz May 30, 2020
1e36f16
add gotest make task
penguinpowernz May 30, 2020
1c6164b
update dep lockfile
penguinpowernz May 30, 2020
a5279be
update readme
penguinpowernz May 30, 2020
38362da
stop using nlopes
penguinpowernz May 31, 2021
dafd4b2
switch to go modules
penguinpowernz May 31, 2021
5c9dda3
add WaitForConnection function
penguinpowernz May 31, 2021
deb61dc
only call the connected event when we see HelloEvent
penguinpowernz May 31, 2021
ea999c9
fixed bugs found via tests
penguinpowernz May 31, 2021
89f983b
get bot ID automatically
penguinpowernz May 31, 2021
0e1da4e
fix ability to @ users
penguinpowernz May 31, 2021
ba6b7b0
only dump slack messages when HANU_DEBUG envvar is set
penguinpowernz Jun 16, 2021
20bad02
Merge branch 'v2'
penguinpowernz Jun 16, 2021
c0898e0
move the v2 code into the root directory
penguinpowernz Jun 16, 2021
cff40bd
update go mod files
penguinpowernz Jun 16, 2021
6ba1213
update readme
penguinpowernz Jun 16, 2021
dfb1dea
port to the latest slack lib that has the SocketMode support
penguinpowernz Apr 21, 2023
f6d33e1
Merge branch 'v3'
penguinpowernz Apr 21, 2023
51703ad
fix messages and README
penguinpowernz Apr 21, 2023
afd1732
Merge branch 'master' into v3
penguinpowernz Apr 21, 2023
69643b2
move the v3 folder into main dir to work with go mod better
penguinpowernz Apr 21, 2023
7d26557
Merge branch 'v3'
penguinpowernz Apr 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions Gopkg.lock

This file was deleted.

38 changes: 0 additions & 38 deletions Gopkg.toml

This file was deleted.

3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
COVERAGE_FILE ?= c.out

gotest:
go test -v ./... -race

test:
@ ginkgo -cover -coverprofile=$(COVERAGE_FILE) $(RACE) ./...

Expand Down
87 changes: 71 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Read Tutorial](https://badgen.now.sh/badge/Read/Tutorial/orange)](https://sbstjn.com/host-golang-slackbot-on-heroku-with-hanu.html)
[![Code Example](https://badgen.now.sh/badge/Code/Example/cyan)](https://github.com/sbstjn/hanu-example)

The `Go` framework **hanu** is your best friend to create [Slack](https://slackhq.com) bots! **hanu** uses [allot](https://github.com/sbstjn/allot) for easy command and request parsing (e.g. `whisper <word>`) and runs fine as a [Heroku worker](https://devcenter.heroku.com/articles/background-jobs-queueing). All you need is a [Slack API token](https://api.slack.com/bot-users) and you can create your first bot within seconds! Just have a look at the [hanu-example](https://github.com/sbstjn/hanu-example) bot or [read my tutorial](https://sbstjn.com/host-golang-slackbot-on-heroku-with-hanu.html) …
The `Go` framework **hanu** is your best friend to create [Slack](https://slackhq.com) bots! **hanu** uses [allot](https://github.com/sbstjn/allot) for easy command and request parsing (e.g. `whisper <word>`) and runs fine as a [Heroku worker](https://devcenter.heroku.com/articles/background-jobs-queueing). All you need is a [Slack Bot token](https://api.slack.com/authentication/token-types#bot) and [Slack App Token](https://api.slack.com/authentication/token-types#app) for the Slack [Socket Mode](https://api.slack.com/apis/connections/socket-implement) connection. Under the hood it uses [github.com/slack-go/slack](https://github.com/slack-go/slack) by [nlopes](https://github.com/nlopes).

### Features

Expand All @@ -14,7 +14,7 @@ The `Go` framework **hanu** is your best friend to create [Slack](https://slackh
- Auto-Generated command list for `help`
- Works fine as a **worker** on Heroku

## Usage
## V1 Usage

Use the following example code or the [hanu-example](https://github.com/sbstjn/hanu-example) bot to get started.

Expand All @@ -29,33 +29,33 @@ import (
)

func main() {
slack, err := hanu.New("SLACK_BOT_API_TOKEN")
slack, err := hanu.New(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))

if err != nil {
log.Fatal(err)
}

Version := "0.0.1"
version := "0.0.1"

slack.Command("shout <word>", func(conv hanu.ConversationInterface) {
str, _ := conv.String("word")
conv.Reply(strings.ToUpper(str))
slack.Command("shout <word>", func(c hanu.Convo) {
str, _ := c.String("word")
c.Reply(strings.ToUpper(str))
})

slack.Command("whisper <word>", func(conv hanu.ConversationInterface) {
str, _ := conv.String("word")
conv.Reply(strings.ToLower(str))
slack.Command("whisper <word>", func(c hanu.Convo) {
str, _ := c.String("word")
c.Reply(strings.ToLower(str))
})

slack.Command("version", func(conv hanu.ConversationInterface) {
conv.Reply("Thanks for asking! I'm running `%s`", Version)
slack.Command("version", func(c hanu.Convo) {
c.Reply("Thanks for asking! I'm running `%s`", version)
})

slack.Listen()
}
```

The example code above connects to Slack using `SLACK_BOT_API_TOKEN` as the bot's token and can respond to direct messages and mentions for the commands `shout <word>` , `whisper <word>` and `version`.
The example code above connects to Slack using the tokens and can respond to direct messages and mentions for the commands `shout <word>` , `whisper <word>` and `version`.

You don't have to care about `help` requests, **hanu** has it built in and will respond with a list of all defined commands on direct messages like this:

Expand All @@ -69,8 +69,6 @@ Of course this works fine with mentioning you bot's username as well:
@hanu help
```

### Slack

Use direct messages for communication:

```
Expand All @@ -83,12 +81,69 @@ Or use the bot in a public channel:
@hanu version
```

You can set the command prefix, if you like using those:

```go
bot.SetCommandPrefix("!")
bot.SetReplyOnly(false)
```

This will make it so you have to type:

```
!whisper I love turtles
```

The bot can also now talk arbitrarily and has a Channel object that is easy to
interface with since it's one function:

```go
bot.Say("UGHXISDF324", "I like %s", "turtles")

devops := bot.Channel("UGHXISDF324")
devops.Say("Host called %s is not responding to pings", "bobsburgers01")
```

You can print the help message whenever you want:

```go
bot.Say("UGHXISDF324", bot.BuildHelpText())
```

And there is an unknown command handler, but it only works when in reply only mode:

```go
bot.SetReplyOnly(true).UnknownCommand(func(c hanu.Convo) {
c.Reply(slack.BuildHelpText())
})
```

Finally there is the ability to read messages that come into the channel in real time:

```go
devops := bot.Channel("UGHXISDF324")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

for {
select {
case msg := <-devops.Messages():
if msg.IsFrom("bob") {
bot.Say("shutup <@bob>")
}
case <-ctx.Done():
break
}
}
```

## Dependencies

- [github.com/sbstjn/allot](https://github.com/sbstjn/allot) for parsing `cmd <param1:string> <param2:integer>` strings
- [golang.org/x/net/websocket](http://golang.org/x/net/websocket) for websocket communication with Slack
- [github.com/slack-go/slack](http://github.com/slack-go/slack) by nlopes for real time communication with Slack

## Credits

- [Host Go Slackbot on Heroku](https://sbstjn.com/host-golang-slackbot-on-heroku-with-hanu.html)
- [OpsDash article about Slack Bot](https://www.opsdash.com/blog/slack-bot-in-golang.html)
- [A Simple Slack Bot in Go - The Bot](ttps://dev.to/shindakun/a-simple-slack-bot-in-go---the-bot-4olg)
Loading