Skip to content

Commit

Permalink
convert to go module and add github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Muzafarov committed Feb 3, 2022
1 parent c566a03 commit 7a88826
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 37 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
34 changes: 34 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Go

on:
push:
branches: [ main ]
tags: [ 'v*' ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Build
run: go build -o tg-webhook-emulator -v ./...

- name: Build release files
if: ${{ github.ref == 'refs/heads/main' }} || startsWith(github.ref, 'refs/tags/')
run: |
GOOS=linux GOARCH=amd64 go build -o tg-webhook-emulator
GOOS=linux CGO_ENABLED=0 GOARCH=arm go build -o tg-webhook-emulator-arm
GOOS=darwin go build -o tg-webhook-emulator-mac
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: tg-webhook-emulator*
34 changes: 34 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- main
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
only-new-issues: true

# Optional: if set to true then the action will use pre-installed Go.
# skip-go-installation: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/tg-webhook-emulator
/tg-webhook-emulator-arm
/tg-webhook-emulator-mac
67 changes: 30 additions & 37 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,42 @@ import (
"bytes"
"encoding/json"
"flag"
"github.com/go-telegram-bot-api/telegram-bot-api"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
log "github.com/sirupsen/logrus"
)

func parseWebhookAnswer(resp *http.Response) (string, url.Values) {
v := url.Values{}
func parseWebhookAnswer(resp *http.Response) (string, tgbotapi.Params) {
var method string
for i := 0; i < 3; i++ {
if i == 0 {
var parsed map[string]interface{}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
continue
}
err = json.Unmarshal(bytes, &parsed)
if err != nil {
continue
}
for key, value := range parsed {
if key == "method" {
method = value.(string)
} else {
switch value.(type) {
case int64:
v.Add(key, strconv.FormatInt(value.(int64), 10))
case float64:
v.Add(key, strconv.FormatFloat(value.(float64), 'f', 0, 64))
case string:
v.Add(key, value.(string))
}
}
}
return method, v
params := make(tgbotapi.Params)

var parsed map[string]interface{}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return method, params
}
err = json.Unmarshal(respBytes, &parsed)
if err != nil {
return method, params
}
for key, value := range parsed {
if key == "method" {
method = value.(string)
} else {
// Implement other values methods here
switch value := value.(type) {
case int64:
params.AddNonZero64(key, value)
case float64:
params.AddNonZeroFloat(key, value)
case string:
params.AddNonEmpty(key, value)
}
}
}
return method, v
return method, params
}

func main() {
Expand All @@ -67,7 +60,7 @@ func main() {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60

updates, err := bot.GetUpdatesChan(u)
updates := bot.GetUpdatesChan(u)

for update := range updates {
jsonValue, _ := json.Marshal(update)
Expand All @@ -77,10 +70,10 @@ func main() {
continue
}
method, params := parseWebhookAnswer(resp)
_, err = bot.MakeRequest(method, params)
apiResp, err := bot.MakeRequest(method, params)

if err != nil {
log.Error(err)
log.Errorf("error: %s, resp: %s", err, apiResp.Result)
continue
}
}
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/m-messiah/tg-webhook-emulator

go 1.17

require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/sirupsen/logrus v1.8.1
)

require golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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/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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Binary file removed tg-webhook-emulator
Binary file not shown.

0 comments on commit 7a88826

Please sign in to comment.