Exemple #1
0
// Add Response API
func plurk_AddResponse(call otto.FunctionCall) otto.Value {

	plurkID, _ := call.Argument(0).ToInteger()
	message, _ := call.Argument(1).ToString()
	qualifier, _ := call.Argument(2).ToString()

	if plurkID == 0 {
		logger.Error("Plurk ID not specify, add response failed!")
		return otto.FalseValue()
	}

	if len(message) <= 0 || message == "undefined" {
		logger.Error("No plurk content specify, add response failed!")
		return otto.FalseValue()
	}

	if qualifier == "undefined" {
		qualifier = ":"
	}

	responses := client.GetResponses()
	res, err := responses.ResponseAdd(int(plurkID), message, qualifier)

	if err != nil {
		logger.Error("Add response failed, because %s", err.Error())
	}

	logger.Info("Add response success, content is %s", res.RawContent)

	return otto.TrueValue()
}
Exemple #2
0
// Send GET Request to PlurkClient API
func (plurk *PlurkClient) Get(endpoint string, params url.Values) ([]byte, error) {

	requestUri := fmt.Sprintf("%s/%s", plurk.ApiBase, endpoint)
	uri, err := url.Parse(requestUri)
	// TODO(elct9620): Imrpove error handle
	if err != nil {
		return nil, err
	}
	params = signParams(&plurk.credential, "GET", uri, params)
	requestUri = fmt.Sprint(requestUri, "?", params.Encode())
	res, err := http.Get(requestUri)
	logger.Info("GET %s", uri.String())
	logger.Debug("Params %s", params.Encode())
	if err != nil {
		return nil, err
	}

	defer res.Body.Close()
	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	if res.StatusCode != 200 {
		var responseError Error
		json.Unmarshal(data, &responseError)
		logger.Error(responseError.ErrorText)
		return nil, errors.New(responseError.ErrorText)
	}

	return data, nil
}
Exemple #3
0
// Add Plurk API
func plurk_AddPlurk(call otto.FunctionCall) otto.Value {
	message, _ := call.Argument(0).ToString()
	qualifier, _ := call.Argument(1).ToString()
	lang, _ := call.Argument(2).ToString()

	// No message specify, return error
	if len(message) <= 0 {
		return otto.FalseValue()
	}

	// Default qualifier
	if qualifier == "undefined" {
		qualifier = ":"
	}

	if lang == "undefined" {
		lang = "en"
	}

	timeline := client.GetTimeline()
	res, err := timeline.PlurkAdd(message, qualifier, make([]int, 0), false, lang, true)

	if err != nil {
		logger.Error("Add Plurk failed, because %s", err.Error())
		return otto.FalseValue()
	}

	logger.Info("New plurk added, content is %s", res.RawContent)

	return otto.TrueValue()
}
Exemple #4
0
// Create a new robot instance
func New() *Robot {
	session, err := db.OpenSession("")
	if err != nil {
		logger.Error("Initialize robot failed, because %s", err.Error())
		return nil
	}

	return &Robot{cron: cron.New(), Signal: make(chan os.Signal, 1), db: session.DB("")}
}