本文整理匯總了Golang中github.com/nlopes/slack.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Fire
func (hook *SlackHook) Fire(sourceEntry *logrus.Entry) error {
hook.client = slack.New(hook.token)
params := slack.PostMessageParameters{
Username: hook.Username,
IconURL: hook.IconURL,
IconEmoji: hook.IconEmoji,
}
var messageFields []slack.AttachmentField
for key, value := range sourceEntry.Data {
message := slack.AttachmentField{
Title: key,
Value: value.(string),
Short: true,
}
messageFields = append(messageFields, message)
}
attachment := slack.Attachment{
Color: getColor(sourceEntry.Level),
AuthorName: hook.AuthorName,
Fields: messageFields,
Text: sourceEntry.Message,
}
params.Attachments = []slack.Attachment{attachment}
_, _, err := hook.client.PostMessage(hook.Channel, "", params)
return err
}
示例2: Publish
// Publish Publishes messages to slack
func (x Slack) Publish(publishMsgs <-chan models.Message, connector models.Connector) {
api := slack.New(connector.Key)
for {
message := <-publishMsgs
msg := ""
params := slack.NewPostMessageParameters()
params.Username = connector.BotName
params.IconEmoji = connector.Image
if message.Out.Detail != "" {
color := slackColorMe(message.Out.Status)
attachment := slack.Attachment{
Title: message.Out.Text,
TitleLink: message.Out.Link,
Text: message.Out.Detail,
Color: color,
MarkdownIn: []string{"text"},
}
params.Attachments = []slack.Attachment{attachment}
} else {
msg = message.Out.Text
}
for _, target := range strings.Split(message.Out.Target, ",") {
if target == "*" {
target = message.In.Target
}
if target != "" && target != "*" {
api.PostMessage(target, msg, params)
}
}
}
}
示例3: main
func main() {
// api := slack.New("xoxb-14316137920-HnYQrHOS7pOtkLKEDRh1i30w")
api := slack.New("xoxp-8035809824-8036215361-17780810228-8a32658b4c")
params := slack.FileUploadParameters{
Title: "New Markdown Example",
Filetype: "post",
Filename: "example.md",
// File: "example.md",
Content: "# hello world!\n*italics*\n**bold**\n`code`\n",
InitialComment: "whoa, programmatic post creation!",
Channels: []string{"#hubotix"},
}
file, err := api.UploadFile(params)
if err != nil {
fmt.Printf("%s\n", err)
return
}
fmt.Printf("%+v\n", file)
/*
err = api.DeleteFile(file.ID)
if err != nil {
fmt.Printf("%s\n", err)
return
}
fmt.Printf("File %s deleted successfully.\n", file.Name)
*/
}
示例4: Run
// Run connects to slack RTM API using the provided token
func Run(token string) {
api = slack.New(token)
rtm = api.NewRTM()
b := bot.New(&bot.Handlers{
Response: responseHandler,
})
b.Disable([]string{"url"})
go rtm.ManageConnection()
Loop:
for {
select {
case msg := <-rtm.IncomingEvents:
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
b.MessageReceived(ev.Channel, ev.Text, extractUser(ev.User))
case *slack.RTMError:
fmt.Printf("Error: %s\n", ev.Error())
case *slack.InvalidAuthEvent:
fmt.Printf("Invalid credentials")
break Loop
}
}
}
}
示例5: main
func main() {
args, err := docopt.Parse(
strings.Replace(usage, "$0", os.Args[0], -1),
nil, true, "slack-topic-setter 1.0", false,
)
if err != nil {
panic(err)
}
var additionalParameters map[string]interface{}
if args["-i"].(bool) {
stdinDecoder := json.NewDecoder(os.Stdin)
err := stdinDecoder.Decode(&additionalParameters)
if err != nil {
log.Printf("error reading stdin: %s", err)
return
}
}
api := &API{
slack.New(args["-k"].(string)),
additionalParameters,
}
switch {
case args["-C"]:
err = api.handleChannelMode(args)
}
if err != nil {
log.Printf("error: %s", err)
}
}
示例6: main
func main() {
slackToken := os.Getenv("ARCHIVEBOT_SLACK_TOKEN")
api := slack.New(slackToken)
//api.SetDebug(true)
channels, err := api.GetChannels(true)
if err != nil {
log.Printf("Error when loading channels: %s\n", err)
return
}
var wg sync.WaitGroup
wg.Add(2)
go func(c []slack.Channel) {
defer wg.Done()
archiveEmptyChannels(api, c)
}(channels)
go func(c []slack.Channel) {
defer wg.Done()
archiveInactiveChannels(api, c)
}(channels)
wg.Wait()
}
示例7: Listen
// Listen Listens to slack messages in channels Jane is present in
func (x Slack) Listen(commandMsgs chan<- models.Message, connector models.Connector) {
defer Recovery(connector)
api := slack.New(connector.Key)
api.SetDebug(connector.Debug)
rtm := api.NewRTM()
if connector.Debug {
log.Print("Starting slack websocket api for " + connector.ID)
}
go rtm.ManageConnection()
for {
select {
case msg := <-rtm.IncomingEvents:
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
if ev.User != "" {
if connector.Debug {
log.Print("Evaluating incoming slack message")
}
var m models.Message
m.In.ConnectorType = connector.Type
m.In.ConnectorID = connector.ID
m.In.Tags = connector.Tags
m.In.Target = ev.Channel
m.In.User = ev.User
m.In.Text = html.UnescapeString(ev.Text)
m.In.Process = true
commandMsgs <- m
}
}
}
}
}
示例8: doSlack
func doSlack(apiToken string) {
api := slack.New(apiToken)
rtm := api.NewRTM()
go rtm.ManageConnection()
mention := ""
for msg := range rtm.IncomingEvents {
switch ev := msg.Data.(type) {
case *slack.ConnectedEvent:
mention = "<@" + ev.Info.User.ID + ">"
if ev.ConnectionCount == 1 {
log.Printf("bot [%s] ready", ev.Info.User.Name)
log.Print("hit ^C to exit")
} else {
log.Printf("bot [%s] reconnected", ev.Info.User.Name)
}
case *slack.MessageEvent:
if strings.HasPrefix(ev.Msg.Text, mention) {
t := strings.TrimPrefix(ev.Msg.Text, mention)
go func(text, ch string) {
r := process(text)
rtm.SendMessage(rtm.NewOutgoingMessage(r, ch))
}(t, ev.Msg.Channel)
}
case *slack.InvalidAuthEvent:
log.Print("bad Slack API token")
os.Exit(1)
}
}
}
示例9: NewSlack
func NewSlack(name, icon, token string) *Slack {
return &Slack{
Name: name,
Icon: icon,
Client: slack.New(token),
}
}
示例10: Notify
// Notify is function to notify slack.
func (s *Slack) Notify(body string, attachments [][]*multipart.FileHeader) {
// auth
api := slack.New(s.Token)
for _, tmp := range attachments {
for _, attachment := range tmp {
fileURL, err := upSlack(api, attachment)
if err != nil {
log.Errorf("upload slack error: %v", err)
} else {
// add url to body
body += fmt.Sprintln(fileURL)
}
}
}
api.PostMessage(s.Channel, body, slack.PostMessageParameters{
Username: s.Username,
AsUser: s.AsUser,
UnfurlLinks: s.UnfurlLinks,
UnfurlMedia: s.UnfurlMedia,
IconURL: s.IconURL,
IconEmoji: s.IconEmoji,
})
}
示例11: StartBot
func StartBot() {
log.Printf("stats-bot: v%v#%v", Version, Commit)
go StartHTTPServer() // Has to run in a Goroutine. Blocks
bot := slack.New(Config.Token)
Census = census.NewCensus("s:maximumtwang", "ps2ps4us:v2")
CensusEU = census.NewCensus("s:maximumtwang", "ps2ps4eu:v2")
StartPopGathering()
t, err := bot.AuthTest()
if err != nil {
log.Printf("Error in auth test: [%v]", err.Error())
return
}
log.Printf("Auth: %v on team %v", t.User, t.Team)
rtm := bot.NewRTM()
go rtm.ManageConnection()
for {
select {
case msg := <-rtm.IncomingEvents:
switch m := msg.Data.(type) {
case *slack.MessageEvent:
Dispatch(&Context{Bot: bot, Ev: m, RTM: rtm})
}
}
}
}
示例12: Start
func (p *slackInput) Start() error {
if len(p.token) == 0 {
return errors.New("missing slack token")
}
p.Lock()
defer p.Unlock()
if p.running {
return nil
}
api := slack.New(p.token)
api.SetDebug(p.debug)
// test auth
_, err := api.AuthTest()
if err != nil {
return err
}
p.api = api
p.exit = make(chan bool)
p.running = true
return nil
}
示例13: GetUsers
func (s *Slack) GetUsers() ([]*SlackUser, error) {
log.WithFields(log.Fields{
"account_id": s.AccountID,
"token": s.Token,
}).Info("Bridges::Slack::GetUsers started")
c := slack.New(s.Token)
svcUsers, err := c.GetUsers()
if err != nil {
log.WithFields(log.Fields{
"account_id": s.AccountID,
"error": err,
}).Error("Bridges::Slack::GetUsers error")
}
var users []*SlackUser
for _, u := range svcUsers {
if u.Deleted || u.IsBot || u.IsRestricted || u.IsUltraRestricted {
continue
}
users = append(users, &SlackUser{
ID: u.Id,
Email: u.Profile.Email,
Name: u.Profile.RealName,
Title: u.Profile.Title,
})
}
return users, err
}
示例14: main
func main() {
var wg sync.WaitGroup
jiraHostURL = os.Getenv(jiraURL)
jiraUserName = os.Getenv(jiraUser)
jiraiUserPassword = os.Getenv(jiraPassword)
slackAPIToken = os.Getenv(slackToken)
url, _ := url.Parse(jiraHostURL)
Client = NewClient(jiraUserName, jiraiUserPassword, url)
Slack = slack.New(slackAPIToken)
Slack.SetDebug(false)
Client.GetProjects()
buildPattern()
rtm := Slack.NewRTM()
go rtm.ManageConnection()
Loop:
for {
select {
case msg := <-rtm.IncomingEvents:
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
if ev.SubType != "bot_message" {
wg.Add(1)
go processEvents(ev.Text, ev.Channel, wg)
}
case *slack.InvalidAuthEvent:
fmt.Printf("Invalid credentials")
break Loop
default:
// Ignore other events..
}
}
}
wg.Wait()
}
示例15: Record
func Record(authorID string, category string, text string, slackToken string) error {
if len(strings.TrimSpace(text)) == 0 {
return errors.New("Empty note given.")
}
var authorName, avatarURL string
api := slack.New(slackToken)
user, err := api.GetUserInfo(authorID)
text, category = effectiveTextAndCategory(text, category)
if err == nil {
authorName = user.Profile.RealName
avatarURL = user.Profile.Image48
} else {
fmt.Printf("Error getting author information from Slack: %s\n", err)
authorName = authorID
avatarURL = ""
}
text = strings.TrimSpace(strings.TrimPrefix(text, category))
note := Note{authorName, avatarURL, category, text}
AddNote(note)
if len(os.Getenv("TESTING")) == 0 {
notifyRoom(api, note)
}
return nil
}