ChatWebhook receives incoming updates from a chat pipe. The URL format is: /api/chat-webhook/:pipeType/:pipeName This endpoint does not require authentication because it is called by chat platform servers. @router /api/chat-webhook/:pipeType/:pipeName [post]
()
| 99 | // This endpoint does not require authentication because it is called by chat platform servers. |
| 100 | // @router /api/chat-webhook/:pipeType/:pipeName [post] |
| 101 | func (c *ApiController) ChatWebhook() { |
| 102 | pipeType := c.Ctx.Input.Param(":pipeType") |
| 103 | pipeName := c.Ctx.Input.Param(":pipeName") |
| 104 | host := c.Ctx.Request.Host |
| 105 | lang := c.GetAcceptLanguage() |
| 106 | |
| 107 | pipeObj, err := object.GetPipeByName("admin", pipeName) |
| 108 | if err != nil { |
| 109 | c.Ctx.ResponseWriter.WriteHeader(http.StatusInternalServerError) |
| 110 | return |
| 111 | } |
| 112 | if pipeObj == nil || pipepkg.NormalizeType(pipeObj.Type) != pipeType { |
| 113 | c.Ctx.ResponseWriter.WriteHeader(http.StatusNotFound) |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | provider, err := pipeObj.GetProvider(lang) |
| 118 | if err != nil { |
| 119 | c.Ctx.ResponseWriter.WriteHeader(http.StatusInternalServerError) |
| 120 | return |
| 121 | } |
| 122 | |
| 123 | body, err := io.ReadAll(c.Ctx.Request.Body) |
| 124 | if err != nil { |
| 125 | c.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest) |
| 126 | return |
| 127 | } |
| 128 | |
| 129 | immediateResponse, err := getImmediatePipeResponse(provider, body, c.Ctx.Request.Header) |
| 130 | if err != nil { |
| 131 | c.Ctx.ResponseWriter.WriteHeader(http.StatusBadRequest) |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | incoming, err := provider.ParseWebhookRequest(body) |
| 136 | if err != nil { |
| 137 | // Acknowledge malformed updates so chat platforms do not keep retrying them. |
| 138 | c.Ctx.ResponseWriter.WriteHeader(http.StatusOK) |
| 139 | return |
| 140 | } |
| 141 | if incoming == nil { |
| 142 | writePipeWebhookResponse(c, immediateResponse) |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | if immediateResponse != nil { |
| 147 | writePipeWebhookResponse(c, immediateResponse) |
| 148 | go sendPipeAnswer(provider, pipeObj, incoming, host, lang) |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | sendPipeAnswer(provider, pipeObj, incoming, host, lang) |
| 153 | c.Ctx.ResponseWriter.WriteHeader(http.StatusOK) |
| 154 | } |
| 155 | |
| 156 | func getImmediatePipeResponse(provider pipepkg.Pipe, body []byte, header http.Header) (*pipepkg.WebhookResponse, error) { |
| 157 | responder, ok := provider.(pipepkg.ImmediateWebhookResponder) |
nothing calls this directly
no test coverage detected