postPowerAutomateMessage sends a webhook message to a Power Automate Workflow URL using the Adaptive Card format wrapped in a message envelope.
(context webhook.Context)
| 117 | // postPowerAutomateMessage sends a webhook message to a Power Automate Workflow URL |
| 118 | // using the Adaptive Card format wrapped in a message envelope. |
| 119 | func postPowerAutomateMessage(context webhook.Context) error { |
| 120 | card := getAdaptiveCard(context) |
| 121 | msg := activity{ |
| 122 | Type: "message", |
| 123 | Attachments: []attachment{ |
| 124 | { |
| 125 | ContentType: "application/vnd.microsoft.card.adaptive", |
| 126 | Content: card, |
| 127 | }, |
| 128 | }, |
| 129 | } |
| 130 | |
| 131 | body, err := json.Marshal(msg) |
| 132 | if err != nil { |
| 133 | return errors.Wrapf(err, "failed to marshal webhook POST request to %s", context.URL) |
| 134 | } |
| 135 | req, err := http.NewRequest("POST", context.URL, bytes.NewBuffer(body)) |
| 136 | if err != nil { |
| 137 | return errors.Wrapf(err, "failed to construct webhook POST request to %s", context.URL) |
| 138 | } |
| 139 | |
| 140 | req.Header.Set("Content-Type", "application/json") |
| 141 | client := &http.Client{ |
| 142 | Timeout: webhook.Timeout, |
| 143 | } |
| 144 | resp, err := client.Do(req) |
| 145 | if err != nil { |
| 146 | return errors.Wrapf(err, "failed to POST webhook to %s", context.URL) |
| 147 | } |
| 148 | |
| 149 | b, err := io.ReadAll(resp.Body) |
| 150 | if err != nil { |
| 151 | return errors.Wrapf(err, "failed to read POST webhook response from %s", context.URL) |
| 152 | } |
| 153 | defer resp.Body.Close() |
| 154 | |
| 155 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 156 | return errors.Errorf("failed to POST webhook %s, status code: %d, response body: %s", context.URL, resp.StatusCode, b) |
| 157 | } |
| 158 | |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | func postDirectMessage(webhookCtx webhook.Context) bool { |
| 163 | teams := getTeamsConfig(webhookCtx.IMSetting) |