0
Help us understand the problem. What are the problem?

posted at

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

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
}

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
0
Help us understand the problem. What are the problem?