Documentation ¶
Overview ¶
Example ¶
package main import ( "os" "github.com/botopolis/bot" "github.com/botopolis/slack" slacker "github.com/nlopes/slack" ) func main() { robot := bot.New( slack.New(os.Getenv("SLACK_TOKEN")), ) robot.Enter(func(r bot.Responder) error { msg := r.Message.Envelope.(*slacker.Message) r.Send(bot.Message{Text: "Any friend of " + msg.Inviter + " is a friend of mine"}) return nil }, ) robot.Run() }
Output:
Index ¶
- type Adapter
- func (a *Adapter) Direct(m bot.Message) error
- func (a *Adapter) Load(r *bot.Robot)
- func (a *Adapter) Messages() <-chan bot.Message
- func (a *Adapter) React(m bot.Message) error
- func (a *Adapter) Reply(m bot.Message) error
- func (a *Adapter) Send(m bot.Message) error
- func (a *Adapter) Topic(m bot.Message) error
- func (a *Adapter) Unload(r *bot.Robot)
- func (a *Adapter) Username() string
- type Store
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter struct { Robot *bot.Robot Client *slack.Client Store Store BotID string Name string // contains filtered or unexported fields }
Adapter is the bot slack adapter it implements bot.Plugin and bot.Chat interfaces
func (*Adapter) Direct ¶
Direct does the same thing as send, but also ensures the message is sent directly to the user
func (*Adapter) React ¶ added in v0.5.0
React adds an emote to the last message sent (requires an Envelope to be set). It relies on the timestamp and channel for a message to be present
Example ¶
package main import ( "os" "github.com/botopolis/bot" "github.com/botopolis/slack" slacker "github.com/nlopes/slack" ) func main() { adapter := slack.New(os.Getenv("SLACK_TOKEN")) fromMessage := slacker.Message{Msg: slacker.Msg{ Timestamp: "2020-01-03T18:23:14Z", Channel: "general", }} adapter.React(bot.Message{ Room: "general", Topic: "General conversation", Envelope: fromMessage, }) }
Output:
func (*Adapter) Reply ¶
Reply does the same thing as send, but prefixes the message with <@userID>, notifying the user of the message.
Example ¶
package main import ( "os" "github.com/botopolis/bot" "github.com/botopolis/slack" ) func main() { adapter := slack.New(os.Getenv("SLACK_TOKEN")) fromMessage := bot.Message{ Text: "Hi bot! How are you?", User: "ali", Room: "general", } adapter.Reply(bot.Message{ Text: "I'm well, thanks!", Envelope: fromMessage, }) }
Output:
func (*Adapter) Send ¶
Send send messages to Slack. If only text is provided, it uses the already open RTM connection. If slack.PostMessageParamters are provided in the message.Params field, it will send a web API request.
Example ¶
package main import ( "os" "github.com/botopolis/bot" "github.com/botopolis/slack" ) func main() { adapter := slack.New(os.Getenv("SLACK_TOKEN")) adapter.Send(bot.Message{Text: "hello!"}) }
Output:
Example (Custom) ¶
package main import ( "os" "github.com/botopolis/bot" "github.com/botopolis/slack" slacker "github.com/nlopes/slack" ) func main() { adapter := slack.New(os.Getenv("SLACK_TOKEN")) adapter.Send(bot.Message{Params: slacker.PostMessageParameters{ Username: "ci", Attachments: []slacker.Attachment{ { Color: "danger", Title: "CI Status", TitleLink: "http://ci.org/123", Fields: []slacker.AttachmentField{ {Title: "Passed", Value: "102"}, {Title: "Failed", Value: "3"}, }, }, }, }}) }
Output:
func (*Adapter) Topic ¶
Topic uses the web API to change the topic. It prefers the message.Room and falls back to message.Extra.Channel to determine what channel's topic should be updated.
Example ¶
package main import ( "os" "github.com/botopolis/bot" "github.com/botopolis/slack" ) func main() { adapter := slack.New(os.Getenv("SLACK_TOKEN")) adapter.Topic(bot.Message{ Room: "general", Topic: "General conversation", }) }
Output:
type Store ¶
type Store interface { // Load takes slack info and adds new users and channels from it Load(*slack.Info) // Update queries Slack's web API for users and channels Update() error // UserByID queries the store for a User by ID UserByID(id string) (slack.User, bool) // UserByName queries the store for a User by Name UserByName(name string) (slack.User, bool) // UserByEmail queries the store for a User by Name UserByEmail(name string) (slack.User, bool) // ChannelByID queries the store for a Channel by ID ChannelByID(id string) (slack.Channel, bool) // ChannelByName queries the store for a Channel by Name ChannelByName(id string) (slack.Channel, bool) // IMByID queries the store for a IM by ID IMByID(id string) (slack.IM, bool) // IMByUserID queries the store for a DM by User ID IMByUserID(userID string) (slack.IM, bool) }
Store is the interface to expect from adapter.Store
Example ¶
package main import ( "fmt" "os" "github.com/botopolis/slack" ) func main() { adapter := slack.New(os.Getenv("SLACK_TOKEN")) // The store is only populated if: // 1. You call adapter.Messages(), which connects it to RTM adapter.Messages() // 2. You call store.Update() adapter.Store.Update() // Gives access to slack.User if u, ok := adapter.Store.UserByName("beardroid"); ok { fmt.Println("Found the bot's real name: " + u.RealName) } // Gives access to slack.Channel if c, ok := adapter.Store.ChannelByName("general"); ok { fmt.Println(len(c.Members), " many people in general") } }
Output: