diff --git a/README.md b/README.md index ab895ad..f930250 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,14 @@

- License + + + - + - lines + lines Version @@ -22,6 +24,9 @@ lastcommit + + License +

## Introduction @@ -66,6 +71,7 @@ Special thanks go to all [ contributors ](https://github.com/TechProber/pro-bot/ - [https://stackoverflow.com/questions/52765833/why-i-cant-send-sticker-by-its-id](https://stackoverflow.com/questions/52765833/why-i-cant-send-sticker-by-its-id) - [Telegram, getting file_id for existing sticker](https://stackoverflow.com/questions/34355648/telegram-getting-file-id-for-existing-sticker) - [How to set up push notifications in your Telegram bot](https://www.freecodecamp.org/news/telegram-push-notifications-58477e71b2c2/) +- [https://lornajane.net/posts/2020/a-first-netlify-function-in-golang](https://lornajane.net/posts/2020/a-first-netlify-function-in-golang) ## Development Notes @@ -78,6 +84,7 @@ POST `https://api.telegram.org/bot/sendMessage?chat_id=&text=/sendMessage?chat_id=&text= ``` + ### Send Sticker POST `https://api.telegram.org/bot/sendSticker?chat_id=&sticker=` diff --git a/go.mod b/go.mod index d6a6b2e..dea9444 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/TechProber/pro-bot go 1.17 + +require github.com/aws/aws-lambda-go v1.28.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4f6d652 --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/aws/aws-lambda-go v1.28.0 h1:fZiik1PZqW2IyAN4rj+Y0UBaO1IDFlsNo9Zz/XnArK4= +github.com/aws/aws-lambda-go v1.28.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/handler.go b/handler.go index e924c1b..4ca6041 100644 --- a/handler.go +++ b/handler.go @@ -1,34 +1,56 @@ package main import ( - "encoding/json" - "log" - "net/http" - "strings" + // "encoding/json" + // "log" - "github.com/TechProber/pro-bot/method" - "github.com/TechProber/pro-bot/model" + // "strings" + + // "github.com/TechProber/pro-bot/method" + // "github.com/TechProber/pro-bot/model" + "context" + "fmt" + + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" ) -func Handler(res http.ResponseWriter, req *http.Request) { - body := &model.WebhookReqBody{} // decode the JSON response body - if err := json.NewDecoder(req.Body).Decode(body); err != nil { - log.Println("could not decode request body", err) - return - } +// func handler(res http.ResponseWriter, req *http.Request) { - if !strings.Contains(strings.ToLower(body.Message.Text), "morning") { - return - } +// body := &model.WebhookReqBody{} // decode the JSON response body +// if err := json.NewDecoder(req.Body).Decode(body); err != nil { +// log.Println("could not decode request body", err) +// return +// } - if err := method.Hello(body.Message.Chat.ID, body.Message.Text); err != nil { - log.Println("error in sending reply:", err) - return +// if !strings.Contains(strings.ToLower(body.Message.Text), "morning") { +// return +// } + +// if err := method.Hello(body.Message.Chat.ID, body.Message.Text); err != nil { +// log.Println("error in sending reply:", err) +// return +// } + +// log.Println("reply sent") +// } +func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) { + params := request.QueryStringParameters + fmt.Println(request.HTTPMethod, request.Path) + + name := "World" + if params["name"] != "" { + name = params["name"] } - log.Println("reply sent") + return &events.APIGatewayProxyResponse{ + StatusCode: 200, + Body: "Hi " + name, + }, nil + } func main() { - http.ListenAndServe(":3000", http.HandlerFunc(Handler)) + // http.ListenAndServe(":3000", http.HandlerFunc(handler)) + lambda.Start(handler) } diff --git a/makefile b/makefile index dc0427f..c3037f4 100644 --- a/makefile +++ b/makefile @@ -3,6 +3,7 @@ TELEGRAM_TOKEN := $(TELEGRAM_TOKEN) URL := "https://7260-206-190-232-194.ngrok.io" GHCR_URL := ghcr.io +APP_NAME := pro-bot GHCR_USERNAME := TechProber VERSION := latest @@ -17,31 +18,11 @@ set-webhook: @echo "==> Update webhook endpoint" @curl -F "url=$(URL)" https://api.telegram.org/bot$(TELEGRAM_TOKEN)/setWebhook -ghcr-login: - @echo "==> Login to GHCR" - @echo $(GHCR_TOKEN) | docker login $(GHCR_URL) -u $(GHCR_USERNAME) --password-stdin - .PHONY: build build: - @echo "==> Build application image with tag $(IMAGE_TAG)" - @DOCKER_BUILDKIT=1 docker build \ - --platform=linux/amd64 \ - -t $(IMAGE_NAME):$(IMAGE_TAG) \ - . - -.PHONY: nerd-build -nerd-build: - @echo "==> Build application image with tag $(IMAGE_TAG)" - @sudo nerdctl build \ - --platform=linux/amd64 \ - -t $(IMAGE_NAME):$(IMAGE_TAG) \ - . - -.PHONY: tag -tag: - @echo "==> Tag the local image as GHCR image" - @docker tag $(IMAGE_NAME):$(IMAGE_TAG) $(GHCR_URL):$(IMAGE_TAG) - @docker tag $(IMAGE_NAME):$(IMAGE_TAG) $(GHCR_URL):latest + @mkdir -p functions + @go get ./... + @GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o functions/app ./handler.go .PHONY: help help: @@ -55,10 +36,9 @@ Usage: $ make [TARGETS] TARGETS help Show the help menu - build Build the application image + build Build the application artifact run Run the application container locally (VERSION optional) set-webhook Update the bound webhook url - publish Build the application image, tag it with a custom version tag, and push it to GHCR (Version required) EXAMPLE USAGE diff --git a/method/hello.go b/method/hello.go deleted file mode 100644 index 8f81df3..0000000 --- a/method/hello.go +++ /dev/null @@ -1,42 +0,0 @@ -package method - -import ( - "bytes" - "encoding/json" - "errors" - "log" - "net/http" - - "github.com/TechProber/pro-bot/model" -) - -func Hello(chatID int64, message string) error { - log.Println(message) - // reqBody := &model.MessageReqBody{ - // ChatID: chatID, - // Text: "Nihao!!", - // } - reqBody := &model.StickerReqBody{ - ChatID: chatID, - Sticker: model.GoodMorningStickerRef, - } - - reqBytes, err := json.Marshal(reqBody) - if err != nil { - return err - } - - res, err := http.Post( - model.TelegramApi, - "application/json", - bytes.NewBuffer(reqBytes), - ) - if err != nil { - return err - } - if res.StatusCode != http.StatusOK { - return errors.New("unexpected status" + res.Status) - } - - return nil -} diff --git a/netlify.toml b/netlify.toml new file mode 100644 index 0000000..e387652 --- /dev/null +++ b/netlify.toml @@ -0,0 +1,4 @@ +[build] + command = "make build" + functions = "functions" + publish = "./"