support threads, add docker
Marco Andronaco andronacomarco@gmail.com
Sun, 31 Dec 2023 12:59:03 +0100
14 files changed,
139 insertions(+),
14 deletions(-)
M
.env.example
→
.env.example
@@ -1,2 +1,4 @@
BOT_TOKEN=1234567890:abcdefghijklmnopqrstuvwxyzABCDEFGHI -API_KEY=itsme +CHANNEL_ID= +GROUP_ID= +ADMIN_ID=
A
.github/workflows/publish.yaml
@@ -0,0 +1,48 @@
+# +name: Create and publish a Docker image + +# Configures this workflow to run every time a change is pushed to the branch called `main`. +on: + push: + branches: ['main'] + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # + steps: + - name: Checkout repository + uses: actions/checkout@v3 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }}
A
Dockerfile
@@ -0,0 +1,32 @@
+# syntax=docker/dockerfile:1 + +FROM golang:alpine AS builder + +WORKDIR /build + +# Download Go modules +COPY go.mod go.sum ./ +RUN go mod download + +# Transfer source code +COPY telegram ./telegram +COPY webui ./webui +COPY index.html ./ +COPY *.go ./ + +# Build +RUN CGO_ENABLED=0 go build -trimpath -o /dist/app +COPY index.html /dist + +# Test +FROM build-stage AS run-test-stage +RUN go test -v ./... + +FROM scratch AS build-release-stage + +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /dist /app + +WORKDIR /app + +ENTRYPOINT ["/app/app"]
A
README.md
@@ -0,0 +1,28 @@
+# EscarBot +Telegram bot for EarthBound Café! + +## Instructions + +First of all, you should set up some environment variables: +``` +cp .env.example .env +nano .env +``` + +The following variables are required: +* `BOT_TOKEN` +* `CHANNEL_ID` +* `GROUP_ID` +* `ADMIN_ID` + +### Run with Docker +Just run: +``` +docker-compose -f docker-compose.yaml up -d +``` + +## Test and debug locally +``` +go test -v ./... +go run . +```
A
docker-compose.yaml
@@ -0,0 +1,16 @@
+services: + app: + build: . + image: ghcr.io/birabittoh/escarbot:main + container_name: escarbot + restart: unless-stopped + ports: + - 3000:3000 + environment: + - BOT_TOKEN + - CHANNEL_ID + - GROUP_ID + - ADMIN_ID + #- PORT + #volumes: + #- /etc/localtime:/etc/localtime:ro
M
escarbot.go
→
escarbot.go
@@ -12,7 +12,7 @@
func main() { err := godotenv.Load() if err != nil { - log.Fatal("No .env file provided.") + log.Println("No .env file provided.") } botToken := os.Getenv("BOT_TOKEN")@@ -37,8 +37,8 @@ }
port := os.Getenv("PORT") if port == "" { - log.Println("PORT not set in .env! Defaulting to 1111.") - port = "1111" + log.Println("PORT not set in .env! Defaulting to 3000.") + port = "3000" } bot := telegram.NewBot(botToken, channelId, groupId, adminId)
M
go.sum
→
go.sum
@@ -1,4 +1,4 @@
-github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +github.com/BiRabittoh/telegram-bot-api/v5 v5.0.0-20231231112435-13d701ae92be h1:G80Qzl7+TSAjG7xTdG5VbaYlVvK8w5BjSf2IYmjmc7A= +github.com/BiRabittoh/telegram-bot-api/v5 v5.0.0-20231231112435-13d701ae92be/go.mod h1:Hn75O37Cj+Z5L0QnEfXC3pQ3wyBP+h/su5+pclPna5g= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
M
telegram/forward.go
→
telegram/forward.go
@@ -3,7 +3,7 @@
import ( "log" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + tgbotapi "github.com/BiRabittoh/telegram-bot-api/v5" ) func channelPostHandler(escarbot *EscarBot, message *tgbotapi.Message) {@@ -14,7 +14,6 @@ return
} msg := tgbotapi.NewForward(escarbot.GroupID, chatId, message.MessageID) - msg.ReplyToMessageID = message.MessageID _, err := escarbot.Bot.Send(msg) if err != nil { log.Println("Error forwarding message to group:", err)
M
telegram/inline.go
→
telegram/inline.go
@@ -4,7 +4,7 @@ import (
"log" "strconv" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + tgbotapi "github.com/BiRabittoh/telegram-bot-api/v5" ) var emptyKeyboard tgbotapi.InlineKeyboardMarkup
M
telegram/replace.go
→
telegram/replace.go
@@ -4,7 +4,7 @@ import (
"fmt" "regexp" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + tgbotapi "github.com/BiRabittoh/telegram-bot-api/v5" ) type Replacer struct {@@ -92,6 +92,7 @@
for _, link := range links { text := fmt.Sprintf(linkMessage, link, user) msg := tgbotapi.NewMessage(message.Chat.ID, text) + msg.MessageThreadID = message.MessageThreadID msg.ParseMode = parseMode msg.ReplyMarkup = inlineKeyboardFeedback bot.Send(msg)
M
telegram/telegram.go
→
telegram/telegram.go
@@ -4,7 +4,7 @@ import (
"log" "strconv" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + tgbotapi "github.com/BiRabittoh/telegram-bot-api/v5" ) type EscarBot struct {
M
webui/webui.go
→
webui/webui.go
@@ -4,7 +4,6 @@ import (
"bytes" "log" "net/http" - "path" "strconv" "text/template"@@ -18,7 +17,7 @@
port string } -var indexTemplate = template.Must(template.ParseFiles(path.Join("webui", "index.html"))) +var indexTemplate = template.Must(template.ParseFiles("index.html")) const toggleFormName = "toggle"