https://api.slack.com/methods/conversations.open
(ctx context.Context, userID string)
| 162 | |
| 163 | // https://api.slack.com/methods/conversations.open |
| 164 | func (p *provider) openConversation(ctx context.Context, userID string) (string, error) { |
| 165 | data := url.Values{} |
| 166 | data.Set("users", userID) |
| 167 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://slack.com/api/conversations.open", strings.NewReader(data.Encode())) |
| 168 | if err != nil { |
| 169 | return "", errors.Wrapf(err, "failed to new request") |
| 170 | } |
| 171 | req.Header.Add("Authorization", "Bearer "+p.token) |
| 172 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 173 | |
| 174 | resp, err := p.c.Do(req) |
| 175 | if err != nil { |
| 176 | return "", errors.Wrapf(err, "failed to send request") |
| 177 | } |
| 178 | defer resp.Body.Close() |
| 179 | |
| 180 | if resp.StatusCode != http.StatusOK { |
| 181 | return "", errors.Errorf("received non-200 status code %d", resp.StatusCode) |
| 182 | } |
| 183 | |
| 184 | body, err := io.ReadAll(resp.Body) |
| 185 | if err != nil { |
| 186 | return "", errors.Wrapf(err, "failed to read body") |
| 187 | } |
| 188 | var res conversationsOpenResponse |
| 189 | if err := json.Unmarshal(body, &res); err != nil { |
| 190 | return "", errors.Wrapf(err, "failed to unmarshal") |
| 191 | } |
| 192 | if !res.OK { |
| 193 | return "", errors.Errorf("failed to open conversation, error: %v", res.Error) |
| 194 | } |
| 195 | |
| 196 | return res.Channel.ID, nil |
| 197 | } |
| 198 | |
| 199 | // https://api.slack.com/methods/chat.postMessage |
| 200 | func (p *provider) chatPostMessage(ctx context.Context, channelID string, webhookContext webhook.Context) error { |