Skip to content

Commit

Permalink
actions
Browse files Browse the repository at this point in the history
  • Loading branch information
morooi committed Jul 20, 2023
1 parent 857a6ad commit b7e6ee8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM alpine:latest
FROM --platform=$TARGETPLATFORM alpine:latest
MAINTAINER SJ Zhou <[email protected]>

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"]
ENTRYPOINT ["/app/bot"]
81 changes: 81 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 9 additions & 9 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b7e6ee8

Please sign in to comment.