Serverless programming model support? #42
-
Hello, I wanted to ask if the library supports serverless model? I plan to write a bot that uses several aws lambda functions with a que. The first function is invoked by AWS api gateway and the URL of the gateway is set up as the Telegram webhook url. In other words I can't listen or poll, instead I must make the Dispatcher somehow receive the and marshal the update objects coming through the lambda service or the AWS SQS. The entrypoint code looks something like this: for sqs func HandleLambda(ctx context.Context, event events.SQSEvent) (events.SQSEventResponse, error) {
log.Printf("Received sqs event with %d records", len(event.Records))
for _, msg := range event.Records {
err := processSqsMessage(ctx, msg)
if err != nil {
return events.SQSEventResponse{}, err
}
}
return events.SQSEventResponse{}, nil
} for api gateway func handleTelgramWebhook(res http.ResponseWriter, req *http.Request) {
log.Printf("got new request %s %s", req.Method, req.URL.Path)
body, err := io.ReadAll(req.Body)
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
io.WriteString(res, "failed to read request body")
return
}
out, err := util.SendMessageWithType(req.Context(), sqsQue, body, c.TMsgTypeTelegram, sqsClient)
if err != nil {
log.Println(err)
res.WriteHeader(http.StatusInternalServerError)
io.WriteString(res, "failed to forward received message")
return
}
res.WriteHeader(http.StatusOK)
io.WriteString(res, fmt.Sprintf("forwarded message with body hash %s", *out.MD5OfMessageBody))
}
func main() {
http.HandleFunc(fmt.Sprintf("/%s/%s", stage, c.RTelegramWebhook), handleTelgramWebhook)
algnhsa.ListenAndServe(http.DefaultServeMux, nil)
} Note that in the server example I am using a wrapper library that simply stubs the normal servemux request and response using the object received from api gateway, so that I can define the server with normal servemux apis, making it more vendor-agnostic. However in reality it is not listening on any server and the actual behavior is very similar to the sqs example, where it just receives the messages from the apigateway, puts them to the req object and passes them down to my handler. How could I hook up this library so that I can let it parse the updates like it normally would when listening on webhook? It would be fine for me if I just manually parse it into the Update object, but it's not yet clear to me how this could be done. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Never mind, I just unmarshaled the webhook payloads into the |
Beta Was this translation helpful? Give feedback.
Never mind, I just unmarshaled the webhook payloads into the
echotron.Update
object and it worked. Nice library!