From b7e6ee862daa41f31f05a54c660abdff20d67c69 Mon Sep 17 00:00:00 2001 From: morooi <19279567+morooi@users.noreply.github.com> Date: Thu, 20 Jul 2023 16:07:53 +0800 Subject: [PATCH] actions --- .github/workflows/Dockerfile | 6 +-- .github/workflows/build.yaml | 81 ++++++++++++++++++++++++++++++++++++ handler.go | 18 ++++---- main.go | 5 ++- 4 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/build.yaml diff --git a/.github/workflows/Dockerfile b/.github/workflows/Dockerfile index a0c060d..22c6bb3 100644 --- a/.github/workflows/Dockerfile +++ b/.github/workflows/Dockerfile @@ -1,11 +1,11 @@ -FROM alpine:latest +FROM --platform=$TARGETPLATFORM alpine:latest MAINTAINER SJ Zhou WORKDIR /app -COPY morooi-telegram-bot-go /app/morooi-telegram-bot-go +COPY bot /app/bot # 设置时区 ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone -ENTRYPOINT ["/app/morooi-telegram-bot-go"] \ No newline at end of file +ENTRYPOINT ["/app/bot"] \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..3b34884 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,81 @@ +name: Docker Build & Publish + +on: + workflow_dispatch: + push: + branches: + - main + +env: + ASSET_NAME: bot + IMAGE_NAME: morooi/morooi-telegram-bot-go + +jobs: + build-and-publish: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - goos: linux + goarch: amd64 + platform: linux/amd64 + - goos: linux + goarch: arm64 + platform: linux/arm64 + - goos: darwin + goarch: amd64 + platform: darwin/amd64 + - goos: darwin + goarch: arm64 + platform: darwin/arm64 + + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + CGO_ENABLED: 0 + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + check-latest: true + + - name: Get project dependencies + run: go mod download + + - name: Build Code + run: go build -v -o ${{ env.ASSET_NAME }} + + - name: Upload files to Artifacts + uses: actions/upload-artifact@v3 + with: + name: ${{ env.ASSET_NAME }} + path: ${{ env.ASSET_NAME }} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Copy Dockerfile + run: copy .github/workflows/Dockerfile ./ + + - name: Build and push + uses: docker/build-push-action@v4 + with: + context: . + push: true + platforms: ${{ matrix.platform }} + tags: ${{ env.IMAGE_NAME }}:latest \ No newline at end of file diff --git a/handler.go b/handler.go index f9f910e..d2ddb31 100644 --- a/handler.go +++ b/handler.go @@ -44,7 +44,7 @@ func StartHandler(c tele.Context) error { lastName := message.Sender.LastName reply := fmt.Sprintf("%s %s,欢迎使用 morooi's Bot", firstName, lastName) - return c.Send(reply, tele.ModeMarkdownV2) + return c.Send(reply) } func InfoHandler(c tele.Context) error { @@ -54,14 +54,14 @@ func InfoHandler(c tele.Context) error { lastName := message.Sender.LastName reply := fmt.Sprintf("*INFO*\nfirstName: %s\nlastName: %s\nuserId: %d", firstName, lastName, id) - return c.Send(reply, tele.ModeMarkdownV2) + return c.Send(reply) } func BwgBindHandler(c tele.Context) error { message := c.Message() args := c.Args() if len(args) != 2 { - return c.Send("请在命令后指定您的 VEID 和 API KEY,用空格分隔\n如:`/bwg_bind VEID API_KEY`", tele.ModeMarkdownV2) + return c.Send("请在命令后指定您的 VEID 和 API KEY,用空格分隔\n如:`/bwg_bind VEID API_KEY`") } userId := message.Sender.ID @@ -71,16 +71,16 @@ func BwgBindHandler(c tele.Context) error { if err != nil { insertErr := Insert(bwgApiKey) if insertErr != nil { - return c.Send("*绑定失败*!\n请稍后再试", tele.ModeMarkdownV2) + return c.Send("*绑定失败*!\n请稍后再试") } } else { updateErr := UpdateByUserId(bwgApiKey) if updateErr != nil { - return c.Send("*绑定失败*!\n请稍后再试", tele.ModeMarkdownV2) + return c.Send("*绑定失败*!\n请稍后再试") } } - return c.Send("*绑定成功*!\n请使用 /bwg\\_info 命令获取信息", tele.ModeMarkdownV2) + return c.Send("*绑定成功*!\n请使用 /bwg\\_info 命令获取信息") } func BwgInfoHandler(c tele.Context) error { @@ -89,12 +89,12 @@ func BwgInfoHandler(c tele.Context) error { bwgApiKey, err := SelectByUserId(userId) if err != nil { - return c.Send("请先使用 /bwg\\_bind 命令绑定 VEID 和 API KEY", tele.ModeMarkdownV2) + return c.Send("请先使用 /bwg\\_bind 命令绑定 VEID 和 API KEY") } info, err := GetBwgServerInfo(bwgApiKey.Veid, bwgApiKey.ApiKey) if err != nil || info == nil || info.Error != 0 { - return c.Send("获取服务器信息失败,请确认 VEID 和 API KEY 是否正确\n确认后重新使用 /bwg\\_bind 命令更新信息", tele.ModeMarkdownV2) + return c.Send("获取服务器信息失败,请确认 VEID 和 API KEY 是否正确\n确认后重新使用 /bwg\\_bind 命令更新信息") } hostname := ReplaceForMarkdownV2(info.HostName) @@ -110,7 +110,7 @@ func BwgInfoHandler(c tele.Context) error { reply := fmt.Sprintf("*主机名*:%s\n*IP*:`%s`\n*数据中心*:%s\n*流量使用情况*:%s GB / %s GB \\(%s %%\\)\n*流量重置时间*:%s\n*距离重置还有*:%s", hostname, ipAddresses, nodeDatacenter, dataCounter, planMonthlyData, dataPercent, dataNextReset, duration) - return c.Send(reply, tele.ModeMarkdownV2) + return c.Send(reply) } func TextHandler(c tele.Context) error { diff --git a/main.go b/main.go index 8e0c3d4..2df9e51 100644 --- a/main.go +++ b/main.go @@ -28,8 +28,9 @@ func main() { func InitBot() { pref := tele.Settings{ - Token: os.Getenv("TOKEN"), - Poller: &tele.LongPoller{Timeout: 10 * time.Second}, + Token: os.Getenv("TOKEN"), + Poller: &tele.LongPoller{Timeout: 10 * time.Second}, + ParseMode: tele.ModeMarkdownV2, } b, err := tele.NewBot(pref)