// 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() }
// 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 }
// 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() }
func (r *Robot) Stop() { r.cron.Stop() if r.db != nil { r.db.Session.Close() } logger.Info("Robot stopped!") }
func (r *Robot) Start() { r.cron.Start() // Setup r.LoadCronJobs() r.LoadTasks() r.SetupSignal() logger.Info("Robot started!") for { sign := <-r.Signal if r.HandleSignal(sign) { logger.Info("Graceful down robot...") break } } }
func Test_SetupLogger(t *testing.T) { buffer := bytes.NewBuffer(make([]byte, 0)) LogFile = buffer RobotName = "Test Robot" setupLogger() logger.Info("Test Message") // Validate logger wirte into buffer and conatains spacify robot name assert.Contains(t, buffer.String(), RobotName) }
// Load cron jobs from database func (r *Robot) LoadCronJobs() { jobsIt := r.db.C("jobs").Find(nil).Iter() job := db.Job{} // Read all jobs and throw into motto vm to run script for jobsIt.Next(&job) { jobName := GenerateJobScript(job.Name, job.Script) logger.Info("Add new job: %s", job.Name) r.cron.AddFunc(job.Schedule, func() { motto.Run(jobName) }) } }
// Get Plurks using "Polling" way func (p *Polling) GetPlurks(offset *Time, limit int) (result *GetPlurkResponse, err error) { params := make(url.Values) params.Add("offset", offset.PollingOffset()) params.Add("limit", fmt.Sprintf("%d", limit)) logger.Info("Polling from %s", offset.PollingOffset()) data, err := p.Get("Polling/getPlurks", params) if err != nil { return nil, err } result = &GetPlurkResponse{} err = json.Unmarshal(data, result) if err != nil { return nil, err } return result, nil }
func (t *Timeline) GetPlurks(offset int, limit int, filter string) (result *GetPlurkResponse, err error) { params := make(url.Values) params.Add("offset", fmt.Sprintf("%d", offset)) params.Add("limit", fmt.Sprintf("%d", limit)) if len(filter) > 0 { params.Add("filter", fmt.Sprintf("%d", filter)) } data, err := t.Get("Timeline/getPlurks", params) if err != nil { return nil, err } logger.Info("Plruks JSON: %s", string(data)) result = &GetPlurkResponse{} err = json.Unmarshal(data, result) if err != nil { return nil, err } return result, err }