2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Azure botで質問をするGoのサンプルコード

Posted at

公式

ソース

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"
)

func main() {
	// Represents the various elements used to create HTTP request URIs
	// for QnA Maker operations.
	// From Publish Page: HOST
	// Example: https://YOUR-RESOURCE-NAME.azurewebsites.net/qnamaker
	var host string = "https://qa-bot-service.azurewebsites.net/qnamaker"

	// Authorization endpoint key
	// From Publish Page
	var endpoint_key string = "ffdbcf11-cxxxxxxxxxxxxxxxxxxxxxxxa"

	// Management APIs postpend the version to the route
	// From Publish Page, value after POST
	// Example: /knowledgebases/xxxxxxxxxxxxxxx/generateAnswer
	var route string = "/knowledgebases/xxxxxxxxxxxxxxxxxxxx/generateAnswer"

	// JSON format for passing question to service
	var question string = "{'question': 'パスワード','top': 3}"

	req, _ := http.NewRequest("POST", host+route, bytes.NewBuffer([]byte(question)))
	req.Header.Add("Authorization", "EndpointKey "+endpoint_key)
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Content-Length", strconv.Itoa(len(question)))
	client := &http.Client{}
	response, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	defer response.Body.Close()
	body, _ := ioutil.ReadAll(response.Body)

	fmt.Printf(string(body) + "\n")
}

実行


go run main.go
2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?