12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package discourse
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "net/http"
- "strconv"
- "strings"
- )
- type Session struct {
- apiKey string
- apiUser string
- apiEndpoint string
- }
- func (session Session) SendPM(usernames []string, title string, body string) (string, error) {
- targetUsernames := strings.Join(usernames, ",")
- messageJSON := map[string]interface{}{
- "title": title,
- "target_usernames": targetUsernames,
- "archetype": "private_message",
- "raw": body,
- }
- payload, err := json.Marshal(messageJSON)
- if err != nil {
- return "", err
- }
- url := fmt.Sprintf("%s/posts.json?api_key=%s&api_username=%s", session.apiEndpoint, session.apiKey, session.apiUser)
- response, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
- if err != nil {
- return "", err
- }
- var result map[string]interface{}
- json.NewDecoder(response.Body).Decode(&result)
- topicIDDigit := result["topic_id"].(float64)
- topicID := strconv.FormatFloat(topicIDDigit, 'f', -1, 64)
- return topicID, nil
- }
|