Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A simple server to receive and handle Slack Commands for Golang

License

NotificationsYou must be signed in to change notification settings

ngdinhtoan/slackcmd

Repository files navigation

A simple server to receive and handle Slack Commands written in GO (also known as Golang)

Build StatusCoverage StatusGo Report CardGoDoc

Install and import SlackCmd

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"

How to use SlackCmd

Checkout packagegithub.com/ngdinhtoan/slackcmd/example to see how to use it

How to write new commander

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

Use packagewebhook to send rich message to Slack

import "github.com/ngdinhtoan/slackcmd/webhook"

GoDoc

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.

Contribution

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 chatGitter.

License

SlackCmd is licensed under theMIT License.

About

A simple server to receive and handle Slack Commands for Golang

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp