LoginSignup
0
0

More than 1 year has passed since last update.

goでChatworkにメッセージを送るサンプル

Posted at
package main

import (
	"net/http"
	"net/url"
	"strings"
)

func main() {
	roomID := "xxxxxxxxxx"
	apiUrl := "https://api.chatwork.com/v2/rooms/" + roomID + "/messages"
	apiToken := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	ChatworkMessagePost(apiUrl, apiToken, "testmessage")
}

func ChatworkMessagePost(apiUrl, apiToken, message string) error {
	values := url.Values{}
	values.Set("body", message)

	req, err := http.NewRequest(
		"POST",
		apiUrl,
		strings.NewReader(values.Encode()),
	)

	if err != nil {
		return err
	}

	req.Header.Set("X-ChatWorkToken", apiToken)
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}

	defer resp.Body.Close()

	return err
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0