當前位置: 首頁>>代碼示例>>Golang>>正文


Golang PostMessageParameters.Text方法代碼示例

本文整理匯總了Golang中github.com/nlopes/slack.PostMessageParameters.Text方法的典型用法代碼示例。如果您正苦於以下問題:Golang PostMessageParameters.Text方法的具體用法?Golang PostMessageParameters.Text怎麽用?Golang PostMessageParameters.Text使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/nlopes/slack.PostMessageParameters的用法示例。


在下文中一共展示了PostMessageParameters.Text方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: main

func main() {
	amqpConn, err := amqp.Dial(config.AmqpURI)
	fatalError(err, "couldn't connect to mq server")
	defer amqpConn.Close()

	amqpChan, err := amqpConn.Channel()
	fatalError(err, "couldn't create mq channel")
	defer amqpChan.Close()

	amqpQueue, err := amqpChan.QueueDeclare("", false, true, true, false, nil)
	fatalError(err, "couldn't create mq queue")

	err = amqpChan.QueueBind(amqpQueue.Name, "*.message", config.AmqpExchange, false, nil)
	fatalError(err, "couldn't bind mq queue")

	amqpMessages, err := amqpChan.Consume(amqpQueue.Name, "", true, true, false, false, nil)
	fatalError(err, "couldn't consume mq messages")

	api := slack.New(config.SlackToken)
	rtm := api.NewRTM()
	go rtm.ManageConnection()

	for {
		select {
		case ev := <-rtm.IncomingEvents:
			if msg, ok := ev.Data.(*slack.MessageEvent); ok {
				if msg.SubType == "bot_message" || msg.Channel != config.SlackChannelID {
					break
				}

				user, err := rtm.GetUserInfo(msg.User)
				if err != nil {
					break
				}

				bridgeMsg := BridgeMessage{
					Sender:  user.Name,
					Content: flattenSlackMessage(rtm, msg.Text),
				}

				jsonData, err := json.Marshal(bridgeMsg)
				fatalError(err, "couldn't serialize bridge message")

				amqpMsg := amqp.Publishing{
					Body:         jsonData,
					DeliveryMode: amqp.Persistent,
				}

				fmt.Printf("[<] %s: %s\n", bridgeMsg.Sender, bridgeMsg.Content)

				err = amqpChan.Publish(config.AmqpExchange, "slack.message", false, false, amqpMsg)
				fatalError(err, "couldn't publish mq message")
			}
		case ev := <-amqpMessages:
			if ev.RoutingKey == "slack.message" {
				break
			}

			bridgeMsg := map[string]interface{}{}
			err = json.Unmarshal(ev.Body, &bridgeMsg)
			fatalError(err, "couldn't deserialize bridge message")

			slackMsg := slack.PostMessageParameters{
				Username: bridgeMsg["Sender"].(string),
				Text:     bridgeMsg["Content"].(string),
			}

			if senderAvatar, ok := bridgeMsg["SenderAvatar"]; ok {
				slackMsg.IconURL = senderAvatar.(string)
			}

			for _, interop := range config.BridgeInterop {
				isInteropMessage := true

				for key, value := range interop.MessageMatch {
					if !reflect.DeepEqual(bridgeMsg[key], value) {
						isInteropMessage = false
						break
					}
				}

				if isInteropMessage {
					regex := regexp.MustCompile(interop.MessageRegex)
					match := regex.FindAllStringSubmatch(slackMsg.Text, -1)
					if len(match) == 1 && len(match[0]) == 3 {
						slackMsg.Username = match[0][1]
						slackMsg.Text = match[0][2]
						slackMsg.IconURL = interop.AvatarURLs[slackMsg.Username]
					}
				}
			}

			fmt.Printf("[>] %s: %s\n", slackMsg.Username, slackMsg.Text)

			_, _, err = rtm.PostMessage(config.SlackChannelID, slackMsg.Text, slackMsg)
			fatalError(err, "couldn't post slack message")
		}
	}
}
開發者ID:FacepunchProgrammers,項目名稱:bridge-slack,代碼行數:99,代碼來源:main.go


注:本文中的github.com/nlopes/slack.PostMessageParameters.Text方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。