Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI] - CI Construction #7

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
</p>

<p align="center">
<img src="https://img.shields.io/github/license/TechProber/pro-bot?color=critical" alt="License"/>
<a href="https://app.netlify.com/sites/pro-bot/deploys">
<img src="https://api.netlify.com/api/v1/badges/eb4b8a29-181d-4d48-980e-ebce39edafb8/deploy-status">
</a>
<a href="https://hits.seeyoufarm.com">
<img src="https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FTechProber%2Fpro-bot&count_bg=%235322B2&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false"/>
<img src="https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FTechProber%2Fpro-bot&count_bg=%235322B2&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false"/>
</a>
<a href="https://img.shields.io/tokei/lines/github/TechProber/pro-bot?color=orange">
<img src="https://img.shields.io/tokei/lines/github/TechProber/pro-bot?color=orange" alt="lines">
<img src="https://img.shields.io/tokei/lines/github/TechProber/pro-bot?color=orange" alt="lines">
</a>
<a href="https://hub.docker.com/repository/docker/hikariai/">
<img src="https://img.shields.io/badge/docker-v20.10.7-blue" alt="Version">
Expand All @@ -22,6 +24,9 @@
<a href="https://github.com/TechProber/pro-bot">
<img src="https://img.shields.io/github/last-commit/TechProber/pro-bot" alt="lastcommit"/>
</a>
<a href="https://github.com/TechProber/pro-bot/blob/master/LICENSE">
<img src="https://img.shields.io/github/license/TechProber/pro-bot?color=critical" alt="License"/>
</a>
</p>

## Introduction
Expand Down Expand Up @@ -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

Expand All @@ -78,6 +84,7 @@ POST `https://api.telegram.org/bot<token>/sendMessage?chat_id=<id>&text=<text_st
```bash
curl -X POST https://api.telegram.org/bot<token>/sendMessage?chat_id=<id>&text=<text_string>
```

### Send Sticker

POST `https://api.telegram.org/bot<token>/sendSticker?chat_id=<id>&sticker=<sticker_id>`
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/TechProber/pro-bot

go 1.17

require github.com/aws/aws-lambda-go v1.28.0
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
62 changes: 42 additions & 20 deletions handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
30 changes: 5 additions & 25 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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

Expand Down
42 changes: 0 additions & 42 deletions method/hello.go

This file was deleted.

4 changes: 4 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[build]
command = "make build"
functions = "functions"
publish = "./"