- Notifications
You must be signed in to change notification settings - Fork2
A simple server to receive and handle Slack Commands for Golang
License
ngdinhtoan/slackcmd
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A simple server to receive and handle Slack Commands written in GO (also known as Golang)
Of course, you have toinstall GO first if you do not have GO on your system.
Get SlackCmd by running command
go get github.com/ngdinhtoan/slackcmd
and import slackcmd package into your project
import"github.com/ngdinhtoan/slackcmd"
If you want to use stable version,don't want the changes in master branch affect to your project,use SlackCmdv1
by
import slackcmd"gopkg.in/ngdinhtoan/slackcmd.v1"
Checkout packagegithub.com/ngdinhtoan/slackcmd/example
to see how to use it
It is quite simple to write a new commander.
Below example will show you how to implement a commander to handle/hello
command
// $GOPATH/src/hello/hello.gopackage helloimport ("io""net/http""github.com/ngdinhtoan/slackcmd")funcinit() {// auto register hellworld commander when import hello packageslackcmd.Register(&helloworld{})}typehelloworldstruct{}// ensure that you do not miss any function of Commander interfacevar_ slackcmd.Commander= (*helloworld)(nil)// GetCommand return hello commandfunc (h*helloworld)GetCommand() []string {return []string{"/hello"}}// Validate payload always return nilfunc (h*helloworld)Validate(payload*slackcmd.Payload)error {returnnil}// Execute will say hello to user, who enter /hello commandfunc (h*helloworld)Execute(payload*slackcmd.Payload,w http.ResponseWriter)error {msg:="Hello "ifpayload.Text!="" {msg+=payload.Text}else {msg+="World"}io.WriteString(w,msg)returnnil}
Now use it in your app
// $GOPATH/src/hello/app/main.gopackage mainimport (_"hello"// just import it, init function will register hello command"github.com/ngdinhtoan/slackcmd")funcmain() {slackcmd.StartServer("localhost","9191","/")}
Run app bygo run
and your server will listen at address loalhost:9191.Send a test request:
curl -X POST -d token=gIkuvaNzQIHg97ATvDxqgjtO \ -d team_id=T0001 \ -d team_domain=example \ -d user_id=U2147483697 \ -d user_name=Steve \ -d channel_id=C2147483705 \ -d channel_name=test \ -d command=/hello \ -d text=SlackCmd \ -- http://localhost:9191
the output should be:
Hello SlackCmd
import "github.com/ngdinhtoan/slackcmd/webhook"
Data that is sent tohttp.ResponseWriter
will only be visible to the user who issued the command.
If the command needs to post to a channel so that all members can see it,you need to use incomming webhook to send message to channel.
You can check packagegithub.com/ngdinhtoan/slackcmd/jira
as an example.
If you have a contribution, new commander or any idea to share, feel free to create a pull request or open a ticket,or join to chat.
SlackCmd is licensed under theMIT License.
About
A simple server to receive and handle Slack Commands for Golang