Пример #1
0
// Actual webhook handler, receive events and post it to connected services.
func hooks(writer http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)
	handler := "w" + vars["handler"]
	context := appengine.NewContext(request)
	webhook := getWebhookFromHandler(context, handler)
	if webhook != nil {
		event, desc := services.GetEventData(request)
		context.Infof("%s: %s \n %s", webhook.Type, event, desc)
		if event != "" {
			if webhook.Type == "Trello" {
				services.PushToTrello(
					context, webhook.ListId,
					getAccessToken(context, webhook.User), event, desc)
			} else if webhook.Type == "Telegram" {
				event = strings.Replace(event, "_", "\\_", -1)
				desc = strings.Replace(desc, "_", "\\_", -1)
				services.SendTeleMessage(
					context, event+"\n"+desc, webhook.TeleChatId)
			} else if webhook.Type == "Pushover" {
				services.SendPushoverMessage(
					context, event+"\n"+desc, webhook.POUserKey)
			} else if webhook.Type == "Hipchat" {
				color := "red"
				if strings.Index(event, " success ") > -1 ||
					strings.Index(event, " merged ") > -1 ||
					strings.Index(event, " up") > -1 ||
					strings.Index(event, "Ping!") > -1 {
					color = "green"
				} else if strings.Index(event, " pull ") > -1 {
					color = "yellow"
				}
				services.SendHipchatMessage(
					context, event+"\n"+desc, webhook.HCRoomId,
					webhook.HCToken, color)
			}
		}
		fmt.Fprintf(writer, "OK")
	}
}
Пример #2
0
// Save new hook from web.
func save(writer http.ResponseWriter, request *http.Request) {
	context := appengine.NewContext(request)
	appUser := user.Current(context)
	response := Response{
		Success: true,
		Reason:  "",
	}
	handler := "w" + services.GetAlphaNumberic(7)
	webhook := Webhook{
		User:    appUser.Email,
		Handler: handler,
		Date:    time.Now(),
		Count:   0,
	}
	if request.FormValue("service") == "trello" {
		webhook.Type = "Trello"
		webhook.BoardId = request.FormValue("boardId")
		webhook.BoardName = request.FormValue("boardName")
		webhook.ListId = request.FormValue("listId")
		webhook.ListName = request.FormValue("listName")
		services.PushToTrello(
			context, webhook.ListId,
			getAccessToken(context, webhook.User), "You are connected!", "")
	} else if request.FormValue("service") == "telegram" {
		webhook.Type = "Telegram"
		webhook.TeleChatId, webhook.TeleChatName = services.GetChatIdFromCode(
			context, request.FormValue("teleCode"))
		if webhook.TeleChatId == 0 {
			response.Success = false
			response.Reason = "Invalid code."
		} else {
			services.SendTeleMessage(
				context, "You are connected!", webhook.TeleChatId)
		}
	} else if request.FormValue("service") == "pushover" {
		webhook.Type = "Pushover"
		webhook.POUserKey = request.FormValue("poUserkey")
		status := services.SendPushoverMessage(
			context, "You are connected!", webhook.POUserKey)
		if status == 0 {
			response.Success = false
			response.Reason = "Invalid key."
		}
	} else if request.FormValue("service") == "hipchat" {
		webhook.Type = "Hipchat"
		webhook.HCToken = request.FormValue("hcToken")
		webhook.HCRoomId = request.FormValue("hcRoomid")
		status := services.SendHipchatMessage(
			context, "You are connected!", webhook.HCRoomId,
			webhook.HCToken, "green")
		if !status {
			response.Success = false
			response.Reason = "Invalid room id or token."
		}
	}
	if response.Success {
		key := datastore.NewIncompleteKey(
			context, "Webhook", webhookKey(context, handler))
		_, err := datastore.Put(context, key, &webhook)
		if err != nil {
			context.Infof("%v", err.Error())
			return
		}
		response.Handler = handler
	}
	writer.Header().Set("Content-Type", "application/json")
	resp, _ := json.Marshal(response)
	fmt.Fprintf(writer, string(resp))
}