本文整理匯總了Golang中github.com/nlopes/slack.Slack.PostMessage方法的典型用法代碼示例。如果您正苦於以下問題:Golang Slack.PostMessage方法的具體用法?Golang Slack.PostMessage怎麽用?Golang Slack.PostMessage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/nlopes/slack.Slack
的用法示例。
在下文中一共展示了Slack.PostMessage方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: archiveChannels
func archiveChannels(api *slack.Slack, c []slack.Channel, reason string) {
var wg sync.WaitGroup
for _, channel := range c {
fmt.Printf("Archiving #%s (%s) due to %s\n", channel.Name, channel.Id, reason)
wg.Add(1)
go func(c slack.Channel) {
defer wg.Done()
if err := api.ArchiveChannel(c.Id); err != nil {
message := fmt.Sprintf(
"Error archiving channel #%s (%s): %s\n", c.Name, c.Id, err)
log.Printf(message)
// send error message in a DM to onErrorNotify user/channel
onErrorNotify := os.Getenv("ARCHIVEBOT_NOTIFY")
if onErrorNotify != "" {
params := slack.PostMessageParameters{}
if _, _, postMessageError := api.PostMessage(
onErrorNotify, message, params); postMessageError != nil {
postMessageErrorMessage := fmt.Sprintf(
"Error posting error message to Slack: %s\n", postMessageError)
log.Printf(postMessageErrorMessage)
}
}
}
}(channel)
}
wg.Wait()
}
示例2: createFormForEverybody
func createFormForEverybody(formMessage *slack.MessageEvent, api *slack.Slack) {
users, err := api.GetUsers()
if err != nil {
fmt.Println("Channel err ", err)
}
for _, user := range users {
//create user form DONE
//send dm to fill out form DONE
formResp := createFormForUser(formMessage, user.Id)
if user.Name == "typebot" || user.Id == "U085QG8D7" {
continue
}
_, _, chanId, err := api.OpenIMChannel(user.Id)
if err != nil {
fmt.Println("IM err ", err)
}
fmt.Println("formResp ", formResp)
formDM := fmt.Sprintf("A form has been created, go fill it out! %v", formResp.Links[1].Href)
api.PostMessage(chanId, formDM, slack.PostMessageParameters{
AsUser: true,
})
}
}
示例3: SendIRCToSlack
func SendIRCToSlack(event *irc.Event, slackAPI *slack.Slack) {
params := slack.PostMessageParameters{
Username: fmt.Sprintf("(IRC) %s", event.Nick),
AsUser: true,
}
_, _, err := slackAPI.PostMessage(SlackChannel, event.Message(), params)
if err != nil {
log.Println("SendIRCToSlack:", err)
} else {
fmt.Println("IRC -> Slack:", event.Message())
}
}
示例4: dmPerson
func dmPerson(user *slack.User, message string, api *slack.Slack) {
if user.Name == "typebot" {
return
}
_, _, chanId, err := api.OpenIMChannel(user.Id)
if err != nil {
fmt.Println("IM err ", err)
}
api.PostMessage(chanId, message, slack.PostMessageParameters{
AsUser: true,
})
}
示例5: dmAll
func dmAll(message string, api *slack.Slack) {
users, err := api.GetUsers()
if err != nil {
fmt.Println("Channel err ", err)
}
for _, user := range users {
if user.Name == "typebot" {
continue
}
_, _, chanId, err := api.OpenIMChannel(user.Id)
if err != nil {
fmt.Println("IM err ", err)
}
api.PostMessage(chanId, message, slack.PostMessageParameters{
AsUser: true,
})
}
}
示例6: handleMessage
// handleMessage receives an event and passes it to the MessageResponders
func handleMessage(c *slack.Slack, e *slack.MessageEvent, responders []*MessageResponder) {
if e.Username == "cbot" {
return
}
content, args, err := parseMessageContent(e)
if err != nil {
log.Printf("Error parsing message: %v", err)
return
}
if len(content) == 0 {
return
}
// determine if this is a direct message (prefixed with bots name)
direct := len(args) > 0 && args[0] == *prefix
// handle 'help' command
if direct && (len(args) == 1 || (len(args) > 1 && args[1] == "help")) {
helpTxt := bytes.NewBufferString("I understand:\n")
for _, r := range responders {
if r.Name[0] == '_' {
continue
}
helpTxt.WriteString(fmt.Sprintf(" %s %s\n", *prefix, r.Name))
}
params := slack.PostMessageParameters{Username: *prefix}
if _, _, err := c.PostMessage(e.Channel, helpTxt.String(), params); err != nil {
log.Println(err)
}
return
}
directHandled := !direct
user, err := c.GetUserInfo(e.Msg.User)
os.Setenv("CURRENT_FLOW", e.Channel)
os.Setenv("CURRENT_USER_AVATAR", user.Profile.ImageOriginal)
os.Setenv("CURRENT_USER_EMAIL", user.Profile.Email)
os.Setenv("CURRENT_USER_NICK", user.Name)
os.Setenv("CURRENT_USER_NAME", user.Profile.RealName)
os.Setenv("CURRENT_USER_ID", string(user.ID))
for _, responder := range responders {
caught, err := responder.Handle(direct, content, args[1:], func(response string) error {
// handle the output of the command by replying to the message
params := slack.PostMessageParameters{Username: *prefix}
_, _, error := c.PostMessage(e.Channel, response, params)
return error
})
if err != nil {
log.Println(err)
continue
}
if caught && direct {
directHandled = true
}
}
// handle case when a direct message wasn't handled
if !directHandled {
log.Printf("Unhandled direct message: %s", content)
resp := "Sorry, didn't recognize that command. Try 'cbot help'"
params := slack.PostMessageParameters{Username: *prefix}
if _, _, err := c.PostMessage(e.Channel, resp, params); err != nil {
log.Println(err)
}
}
}