本文整理匯總了Golang中github.com/nlopes/slack.Slack類的典型用法代碼示例。如果您正苦於以下問題:Golang Slack類的具體用法?Golang Slack怎麽用?Golang Slack使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Slack類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PipeSlackToIRC
func PipeSlackToIRC(slackAPI *slack.Slack, ircLink *irc.Connection) {
//sender := make(chan slack.OutgoingMessage)
receiver := make(chan slack.SlackEvent)
wsAPI, err := slackAPI.StartRTM("", "http://example.com")
if err != nil {
log.Fatalf("StartRTM() error: %s", err)
}
go wsAPI.HandleIncomingEvents(receiver)
go wsAPI.Keepalive(10 * time.Second)
for {
msg := <-receiver
switch msg.Data.(type) {
case *slack.MessageEvent:
msgEvent := msg.Data.(*slack.MessageEvent)
// Ignore bot messages, including our own
if msgEvent.BotId != "" {
break
}
fmt.Printf("Message: %s\n", msgEvent)
user, err := slackAPI.GetUserInfo(msgEvent.UserId)
if err != nil {
log.Printf("GetUserInfo(): %s\n", err)
break
}
msg := fmt.Sprintf("(Slack) <%s> %s", user.Profile.RealName, unescapeMessage(msgEvent.Text))
ircLink.Privmsg(IRCChannel, msg)
fmt.Println("Slack -> IRC:", msg)
}
}
}
示例2: 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()
}
示例3: dumpGroups
func dumpGroups(api *slack.Slack, dir string, rooms []string) []slack.Group {
groups, err := api.GetGroups(false)
check(err)
if len(rooms) > 0 {
groups = FilterGroups(groups, func(group slack.Group) bool {
for _, room := range rooms {
if room == group.Name {
return true
}
}
return false
})
}
if len(groups) == 0 {
var groups []slack.Group
return groups
}
for _, group := range groups {
dumpChannel(api, dir, group.Id, group.Name, "group")
}
return groups
}
示例4: fetchChannelHistory
func fetchChannelHistory(api *slack.Slack, ID string) []slack.Message {
historyParams := slack.NewHistoryParameters()
historyParams.Count = 1000
// Fetch History
history, err := api.GetChannelHistory(ID, historyParams)
check(err)
messages := history.Messages
latest := messages[len(messages)-1].Timestamp
for {
if history.HasMore != true {
break
}
historyParams.Latest = latest
history, err = api.GetChannelHistory(ID, historyParams)
check(err)
length := len(history.Messages)
if length > 0 {
latest = history.Messages[length-1].Timestamp
messages = append(messages, history.Messages...)
}
}
return messages
}
示例5: lastMessageTimestamp
func lastMessageTimestamp(api *slack.Slack, channel slack.Channel) (int64, error) {
var latest string
for {
historyParams := slack.HistoryParameters{Count: 5}
if latest != "" {
historyParams.Latest = latest
}
history, err := api.GetChannelHistory(channel.Id, historyParams)
if err != nil {
return -1, err
}
if len(history.Messages) == 0 {
return -1, nil
}
for _, msg := range history.Messages {
latest = msg.Msg.Timestamp
if msg.SubType != "channel_join" && msg.SubType != "channel_leave" {
msgStamp := strings.Split(msg.Msg.Timestamp, ".")
if timestamp, err := strconv.ParseInt(msgStamp[0], 10, 32); err == nil {
return timestamp, nil
}
}
}
}
}
示例6: dumpChannels
func dumpChannels(api *slack.Slack, dir string, rooms []string) []slack.Channel {
channels, err := api.GetChannels(false)
check(err)
if len(rooms) > 0 {
channels = FilterChannels(channels, func(channel slack.Channel) bool {
for _, room := range rooms {
if room == channel.Name {
return true
}
}
return false
})
}
if len(channels) == 0 {
var channels []slack.Channel
return channels
}
for _, channel := range channels {
dumpChannel(api, dir, channel.Id, channel.Name, "channel")
}
return channels
}
示例7: addChannelHistory
func addChannelHistory(s *slack.Slack, db *sql.DB, channel, oldest string) (string, error) {
params := slack.NewHistoryParameters()
params.Oldest = oldest
params.Count = 1000
history, err := s.GetChannelHistory(channel, params)
if err != nil {
return "", err
}
for _, msg := range history.Messages {
if msg.SubType != "" {
continue
}
fmt.Printf("%s: %s\n", msg.User, msg.Text)
ts := timestampToTime(msg.Timestamp)
_, err := db.Exec(
`INSERT INTO messages(channel_id, user_id, text, timestamp, created_at, updated_at)
VALUES(?, ?, ?, ?, NOW(), NOW())
`,
channel, msg.User, msg.Text, ts.Format("2006-01-02 15:04:05"))
if err != nil {
return "", err
}
}
if len(history.Messages) > 0 {
return history.Messages[0].Timestamp, nil
}
return "", nil
}
示例8: dumpUsers
func dumpUsers(api *slack.Slack, dir string) {
users, err := api.GetUsers()
check(err)
data, err := MarshalIndent(users, "", " ")
check(err)
err = ioutil.WriteFile(path.Join(dir, "users.json"), data, 0644)
check(err)
}
示例9: 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())
}
}
示例10: 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,
})
}
示例11: setupRTM
func setupRTM(api *slack.Slack) {
chSender := make(chan slack.OutgoingMessage)
chReceiver := make(chan slack.SlackEvent)
wsAPI, err := api.StartRTM("", "http://example.com")
if err != nil {
fmt.Errorf("%s\n", err)
}
go wsAPI.HandleIncomingEvents(chReceiver)
go wsAPI.Keepalive(20 * time.Second)
go func(wsAPI *slack.SlackWS, chSender chan slack.OutgoingMessage) {
for {
select {
case msg := <-chSender:
wsAPI.SendMessage(&msg)
}
}
}(wsAPI, chSender)
for {
select {
case msg := <-chReceiver:
fmt.Print("Event Received: ")
switch msg.Data.(type) {
case slack.HelloEvent:
// Ignore hello
case *slack.MessageEvent:
a := msg.Data.(*slack.MessageEvent)
fmt.Printf("Message: %v\n", a)
fmt.Printf("ChanID:", a.ChannelId)
if strings.Contains(a.Text, "U085QG8D7") {
createFormForEverybody(a, api)
}
case *slack.PresenceChangeEvent:
a := msg.Data.(*slack.PresenceChangeEvent)
fmt.Printf("Presence Change: %v\n", a)
case slack.LatencyReport:
a := msg.Data.(slack.LatencyReport)
fmt.Printf("Current latency: %v\n", a.Value)
case *slack.SlackWSError:
error := msg.Data.(*slack.SlackWSError)
fmt.Printf("Error: %d - %s\n", error.Code, error.Msg)
default:
fmt.Printf("Unexpected: %v\n", msg.Data)
}
}
}
}
示例12: 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 {
log.Printf("Error archiving #%s (%s): %s\n", c.Name, c.Id, err)
}
}(channel)
}
wg.Wait()
}
示例13: startStream
func startStream(c *slack.Slack, flows []string, responders []*MessageResponder) {
chSender := make(chan slack.OutgoingMessage)
chReceiver := make(chan slack.SlackEvent)
wsAPI, err := c.StartRTM("", "https://slack.com/api")
if err != nil {
fmt.Errorf("%s\n", err)
}
go wsAPI.HandleIncomingEvents(chReceiver)
go wsAPI.Keepalive(20 * time.Second)
go func(wsAPI *slack.WS, chSender chan slack.OutgoingMessage) {
for {
select {
case msg := <-chSender:
wsAPI.SendMessage(&msg)
}
}
}(wsAPI, chSender)
for {
select {
case msg := <-chReceiver:
fmt.Print("Event Received: ")
switch msg.Data.(type) {
case slack.HelloEvent:
// Ignore hello
case *slack.MessageEvent:
a := msg.Data.(*slack.MessageEvent)
handleMessage(c, a, responders)
fmt.Printf("Message: %v\n", a)
case *slack.PresenceChangeEvent:
a := msg.Data.(*slack.PresenceChangeEvent)
fmt.Printf("Presence Change: %v\n", a)
case slack.LatencyReport:
a := msg.Data.(slack.LatencyReport)
fmt.Printf("Current latency: %v\n", a.Value)
case *slack.WSError:
error := msg.Data.(*slack.WSError)
fmt.Printf("Error: %d - %s\n", error.Code, error.Msg)
default:
fmt.Printf("Unexpected: %v\n", msg.Data)
}
}
}
}
示例14: updateChannels
func updateChannels(s *slack.Slack, db *sql.DB) error {
channels, err := s.GetChannels(false)
if err != nil {
return err
}
for _, c := range channels {
fmt.Printf("%s : %s\n", c.Name, c.ID)
_, err := db.Exec(
`INSERT INTO channels(id, name, created_at, updated_at)
VALUES(?, ?, NOW(), NOW())
ON DUPLICATE KEY UPDATE name = VALUES(name), updated_at = NOW()
`,
c.ID, c.Name)
if err != nil {
return err
}
}
return nil
}
示例15: 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,
})
}
}