cleverbot.go
func (s *Session) Ask(question string) (string, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.Values.Set("input", question)
	// Clear previous json, just in case
	s.clear()
	// Prepare the request.
	req, err := http.NewRequest("GET", apiURL+s.Values.Encode(), nil)
	if err != nil {
		return "", err
	}

	// Headers.
	req.Header.Set("User-Agent", "cleverbot-go https://github.com/ugjka/cleverbot-go")

	// Make the request
	resp, err := s.Client.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	// Check for errors.
	switch resp.StatusCode {
	case http.StatusUnauthorized:
		ErrKeyNotValid.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrKeyNotValid
	case http.StatusNotFound:
		ErrAPINotFound.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrAPINotFound
	case http.StatusRequestEntityTooLarge:
		ErrRequestTooLarge.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrRequestTooLarge
	case http.StatusBadGateway:
		ErrNoReply.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrNoReply
	case http.StatusGatewayTimeout:
		ErrNoReply.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrNoReply
	case http.StatusServiceUnavailable:
		ErrTooManyRequests.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrTooManyRequests
	default:
		if resp.StatusCode != http.StatusOK {
			ErrStatusNotOK.origin = fmt.Sprintf("status code %d", resp.StatusCode)
			return "", ErrStatusNotOK
		}
	}

	if err := json.NewDecoder(resp.Body).Decode(&s.Decoded); err != nil {
		ErrInvalidJSON.origin = err.Error()
		return "", ErrInvalidJSON
	}
	if _, ok := s.Decoded["output"].(string); !ok {
		return "", fmt.Errorf("Cleverbot API: 'output' does not exist or is not a string")
	}
	if _, ok := s.Decoded["cs"].(string); !ok {
		return "", fmt.Errorf("Cleverbot API: 'cs' does not exist or is not a string")
	}
	// Set session context id.
	s.Values.Set("cs", s.Decoded["cs"].(string))

	return s.Decoded["output"].(string), nil
}
func (s *Session) Ask(question string) (string, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.Values.Set("input", question)
	// Clear previous json, just in case
	s.clear()
	// Prepare the request.
	req, err := http.NewRequest("GET", apiURL+s.Values.Encode(), nil)
	if err != nil {
		return "", err
	}

	// Headers.
	req.Header.Set("User-Agent", "cleverbot-go https://github.com/ugjka/cleverbot-go")

	// Make the request
	resp, err := s.Client.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	// Check for errors.
	switch resp.StatusCode {
	case http.StatusUnauthorized:
		ErrKeyNotValid.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrKeyNotValid
	case http.StatusNotFound:
		ErrAPINotFound.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrAPINotFound
	case http.StatusRequestEntityTooLarge:
		ErrRequestTooLarge.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrRequestTooLarge
	case http.StatusBadGateway:
		ErrNoReply.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrNoReply
	case http.StatusGatewayTimeout:
		ErrNoReply.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrNoReply
	case http.StatusServiceUnavailable:
		ErrTooManyRequests.origin = fmt.Sprintf("status code %d", resp.StatusCode)
		return "", ErrTooManyRequests
	default:
		if resp.StatusCode != http.StatusOK {
			ErrStatusNotOK.origin = fmt.Sprintf("status code %d", resp.StatusCode)
			return "", ErrStatusNotOK
		}
	}

	if err := json.NewDecoder(resp.Body).Decode(&s.Decoded); err != nil {
		ErrInvalidJSON.origin = err.Error()
		return "", ErrInvalidJSON
	}
	if _, ok := s.Decoded["output"].(string); !ok {
		return "", fmt.Errorf("Cleverbot API: 'output' does not exist or is not a string")
	}
	if _, ok := s.Decoded["cs"].(string); !ok {
		return "", fmt.Errorf("Cleverbot API: 'cs' does not exist or is not a string")
	}
	// Set session context id.
	s.Values.Set("cs", s.Decoded["cs"].(string))

	return s.Decoded["output"].(string), nil
}
func ( s * Session ) Ask ( question string ) ( string , error ) { s . mu . Lock ( ) defer s . mu . Unlock ( ) s . Values . Set ( " " , question ) // Clear previous json, just in case s . clear ( ) // Prepare the request. req , err := http . NewRequest ( " " , apiURL + s . Values . Encode ( ) , nil ) if err != nil { return " " , err } // Headers. req . Header . Set ( " " , " " ) // Make the request resp , err := s . Client . Do ( req ) if err != nil { return " " , err } defer resp . Body . Close ( ) // Check for errors. switch resp . StatusCode { case http . StatusUnauthorized : ErrKeyNotValid . origin = fmt . Sprintf ( " " , resp . StatusCode ) return " " , ErrKeyNotValid case http . StatusNotFound : ErrAPINotFound . origin = fmt . Sprintf ( " " , resp . StatusCode ) return " " , ErrAPINotFound case http . StatusRequestEntityTooLarge : ErrRequestTooLarge . origin = fmt . Sprintf ( " " , resp . StatusCode ) return " " , ErrRequestTooLarge case http . StatusBadGateway : ErrNoReply . origin = fmt . Sprintf ( " " , resp . StatusCode ) return " " , ErrNoReply case http . StatusGatewayTimeout : ErrNoReply . origin = fmt . Sprintf ( " " , resp . StatusCode ) return " " , ErrNoReply case http . StatusServiceUnavailable : ErrTooManyRequests . origin = fmt . Sprintf ( " " , resp . StatusCode ) return " " , ErrTooManyRequests default : if resp . StatusCode != http . StatusOK { ErrStatusNotOK . origin = fmt . Sprintf ( " " , resp . StatusCode ) return " " , ErrStatusNotOK } } if err := json . NewDecoder ( resp . Body ) . Decode ( & s . Decoded ) ; err != nil { ErrInvalidJSON . origin = err . Error ( ) return " " , ErrInvalidJSON } if _ , ok := s . Decoded [ " " ] . ( string ) ; ! ok { return " " , fmt . Errorf ( " " ) } if _ , ok := s . Decoded [ " " ] . ( string ) ; ! ok { return " " , fmt . Errorf ( " " ) } // Set session context id. s . Values . Set ( " " , s . Decoded [ " " ] . ( string ) ) return s . Decoded [ " " ] . ( string ) , nil }
// Ask asks cleverbot a question.
Ask asks cleverbot a question .