第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

根據(jù)數(shù)據(jù)源在附件中發(fā)送數(shù)據(jù)

根據(jù)數(shù)據(jù)源在附件中發(fā)送數(shù)據(jù)

Go
慕絲7291255 2022-08-15 15:38:30
我正在嘗試創(chuàng)建Slack通知,以發(fā)送通知結(jié)構(gòu)中可用的數(shù)據(jù),即通知結(jié)構(gòu)將是數(shù)據(jù)源。我很難將通知結(jié)構(gòu)正確插入附件。請(qǐng)參閱下面的代碼。有人知道我能做什么嗎?非常感謝您的幫助。我的預(yù)期結(jié)果(即Slack通知)將如下所示:UserID: 99Type: Slack or EmailLevel: WarningReceived: time.Time (now)Originator: Originator NameImmediate:  falseData:  interface{}Recipients: {email@gmail.com; Recipient Name}Customer: {1, Customer Name}package mainimport (    "fmt"    "time"    "github.com/parnurzeal/gorequest")//NotificationType is enum of available notification typestype NotificationType stringconst (    //Email should be sent via email protocols    Email NotificationType = "email")//NotificationLevel is enum of notification levelstype NotificationLevel string//Notification levels represent the importance of the notificationconst (    Error   NotificationLevel = "error"    Warning NotificationLevel = "warning"    Info    NotificationLevel = "info")//Recipient represents the recipient of the notificationtype Recipient struct {    Email string    Name  string}//Customer represents the customer the notification is abouttype Customer struct {    ID   int    Name string}//Notification is struct containing all information about notification//This will be used as Datasource in the Attachmenttype Notification struct {    UserID     int    Type       NotificationType    Level      NotificationLevel    Received   time.Time    Originator string    Immediate  bool    Data       interface{}    // following field are populated by notification service itself    Recipients []*Recipient    Customer   *Customer}//Field defines fields to be used in the notificationtype Field struct {    Title string    Value []*Notification}// Attachment holds data used in payloadtype Attachment struct {    Text   *string  //`json:"text"`    Fields []*Field //`json:"fields"`}//Payload defines the notification structuretype Payload struct {    Text        string       `json:"text,omitempty"`    Attachments []Attachment `json:"attachments,omitempty"`}
查看完整描述

1 回答

?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

以下代碼是此請(qǐng)求的解決方案。


package main


import (

    "bytes"

    "encoding/json"

    "errors"

    "fmt"

    "net/http"

    "strings"

    "time"

    "log"

    "strconv"

    )


//DataFeed contains data for the string concatenation

type DataFeed struct {

    CustomerID    int

    CustomerName  string

    CustomerEmail string

    CustomerPhone string

    Received      time.Time

}

//ConcatenateDate takes data from DataFeed and concatenates them into a msg ('\n' separated)

func ConcatenateData(d DataFeed) (msg string) {

    values := []string{}

    values = append(values, "New notification incoming, see more details below:")

    values = append(values, "ID: " + strconv.Itoa(d.CustomerID))

    values = append(values, "Name: " + d.CustomerName)

    values = append(values, "Email: " + d.CustomerEmail)

    values = append(values, "Phone Number: " + d.CustomerPhone)

    values = append(values, "Received: " + (d.Received).String())


    msg = strings.Join(values, "\n")

    //fmt.Println(values)

    //fmt.Println(msg)


    return msg

}


// SlackRequestBody comment here

type SlackRequestBody struct {

    Text string `json:"text"`

}


func SendSlackNotification(webhookURL string, msg string) error {

    slackBody, _ := json.Marshal(SlackRequestBody{Text: msg})

    req, err := http.NewRequest(http.MethodPost, webhookURL, bytes.NewBuffer(slackBody))

    if err != nil {

        return err

    }


    req.Header.Add("Content-Type", "application/json")

    client := &http.Client{Timeout: 10 * time.Second}

    resp, err := client.Do(req)

    if err != nil {

        return err

    }


    buf := new(bytes.Buffer)

    buf.ReadFrom(resp.Body)

    if buf.String() != "ok" {

        return errors.New("Non-ok response returned from Slack")

    }


    return nil

}


func main() {

    webhookURL := "https://hooks.slack.com/services/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"


    e := DataFeed{

        CustomerID:    767,

        CustomerName:  "Tom",

        CustomerEmail: "tom.nemeth85@gmail.com",

        CustomerPhone: "07479551111",

        Received:      time.Now(),

    }


    fmt.Println(e)

    fmt.Printf("\n")

    fmt.Println(ConcatenateData(e))

    

        err := SendSlackNotification(webhookURL, ConcatenateData(e)) //--> Set "e" as argument here

        if err != nil {

            log.Fatal(err)

        }


查看完整回答
反對(duì) 回復(fù) 2022-08-15
  • 1 回答
  • 0 關(guān)注
  • 100 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)