Example #1
0
func slash(w http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		slashCommand := slack.NewSlashCommandModel(req)
		gomol.Infof("slash command received: %f", slashCommand)

		w.Header().Add("Content Type", "text/plain")

		if slashCommand.Text == "categories" {
			cats := images.Images.GetCategories()
			var catBuffer bytes.Buffer
			for key, count := range cats {
				catBuffer.WriteString(key +
					" has " +
					strconv.Itoa(count) +
					" shipit squirrels.\n")
			}

			catBuffer.WriteTo(w)
		} else if slashCommand.Text == "" {
			image, err := images.Images.Take()
			if err != nil {
				gomol.Err("Failed to .Take() image. " + err.Error())
			}

			incomingWebhook := slack.NewIncomingWebhook(
				slashCommand.ChannelName,
				slack.GetImageMarkup(context.GetSiteRootPath()+image))

			_, err = slack.SendIncomingWebhook(incomingWebhook)
			if err != nil {
				gomol.Err(err.Error())
			}
		} else {
			image, err := images.Images.TakeFromCategory(slashCommand.Text)
			if err != nil {
				b := bytes.NewBufferString("Command not recognized.")
				b.WriteTo(w)
			} else {
				incomingWebhook := slack.NewIncomingWebhook(
					slashCommand.ChannelName,
					slack.GetImageMarkup(context.GetSiteRootPath()+image))

				_, err = slack.SendIncomingWebhook(incomingWebhook)
				if err != nil {
					gomol.Err(err.Error())
				}

			}
		}
	}
}
Example #2
0
func makeRequest(url string, payload *bytes.Buffer) (*http.Request, error) {
	req, err := http.NewRequest("POST", url, payload)
	if err != nil {
		gomol.Err("Could not create create post request. " + err.Error())
		return nil, err
	}

	req.Header.Add("Content-Type", "application/json")
	return req, nil
}
Example #3
0
func makePostPayload(model *IncomingWebhook) (*bytes.Buffer, error) {
	modelJson, err := json.Marshal(model)
	if err != nil {
		gomol.Err("Could not marshal json from IncomingWebhook model. " +
			err.Error())
		return nil, err
	}

	payload := bytes.NewBuffer(modelJson)
	return payload, nil
}
Example #4
0
func sendRequest(req *http.Request) (int, error) {
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		gomol.Err("Could not send IncomingWebhook. " + err.Error())
		return 0, err
	}
	defer resp.Body.Close()

	_, err = ioutil.ReadAll(resp.Body) // must read to close?
	if err != nil {
		return 0, err
	}

	return resp.StatusCode, nil
}