init bot
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
80d6d0736b
commit
36b8707404
|
@ -0,0 +1,29 @@
|
|||
# ---> Go
|
||||
# If you prefer the allow list template instead of the deny list, see community template:
|
||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||
#
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
# Goland
|
||||
.idea
|
||||
bin
|
||||
|
||||
# bot
|
||||
token.txt
|
|
@ -0,0 +1,9 @@
|
|||
package commands
|
||||
|
||||
type Command string
|
||||
|
||||
const (
|
||||
Start = Command("/start")
|
||||
Help = Command("/help")
|
||||
Ping = Command("/ping")
|
||||
)
|
7
go.mod
7
go.mod
|
@ -1,3 +1,10 @@
|
|||
module valera
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
|
||||
github.com/umputun/go-flags v1.5.1
|
||||
)
|
||||
|
||||
require github.com/technoweenie/multipartstreamer v1.0.1 // indirect
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
||||
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
|
||||
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
|
||||
github.com/umputun/go-flags v1.5.1 h1:vRauoXV3Ultt1HrxivSxowbintgZLJE+EcBy5ta3/mY=
|
||||
github.com/umputun/go-flags v1.5.1/go.mod h1:nTbvsO/hKqe7Utri/NoyN18GR3+EWf+9RrmsdwdhrEc=
|
88
main.go
88
main.go
|
@ -1,11 +1,93 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"github.com/umputun/go-flags"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"valera/commands"
|
||||
)
|
||||
|
||||
const (
|
||||
version = "v0.1.1"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, Go!")
|
||||
type Opts struct {
|
||||
Token string `short:"t" long:"token" description:"Telegram api token"`
|
||||
Name string `short:"n" long:"name" description:"Telegram bot name" default:"@body_weight_loss_bot"`
|
||||
}
|
||||
|
||||
var opts Opts
|
||||
|
||||
func readToken() (string, error) {
|
||||
b, err := ioutil.ReadFile("token.txt")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
str := string(b)
|
||||
return str, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
run()
|
||||
}
|
||||
|
||||
func run() {
|
||||
p := flags.NewParser(&opts, flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
|
||||
p.SubcommandsOptional = true
|
||||
if _, err := p.Parse(); err != nil {
|
||||
if err.(*flags.Error).Type != flags.ErrHelp {
|
||||
log.Println(errors.New("[ERROR] cli error: " + err.Error()))
|
||||
}
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
if opts.Token == "" {
|
||||
token, err := readToken()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
opts.Token = strings.ReplaceAll(token, "\n", "")
|
||||
}
|
||||
fmt.Println(opts.Token)
|
||||
|
||||
bot, err := tgbot.NewBotAPI(opts.Token)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
u := tgbot.NewUpdate(0)
|
||||
u.Timeout = 60
|
||||
|
||||
updates, err := bot.GetUpdatesChan(u)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Println("Run", opts.Name)
|
||||
|
||||
for update := range updates {
|
||||
|
||||
if update.Message == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
text := update.Message.Text
|
||||
chatID := update.Message.Chat.ID
|
||||
// username := update.Message.From.UserName
|
||||
|
||||
command := commands.Command(strings.Replace(text, opts.Name, "", 1))
|
||||
switch command {
|
||||
case commands.Start:
|
||||
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))
|
||||
case commands.Help:
|
||||
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Ничего"))
|
||||
case commands.Ping:
|
||||
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue