Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Sep 19, 2023
1 parent c53a75e commit 0068e27
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
15 changes: 15 additions & 0 deletions api/telegram/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package telegram

import (
"encoding/json"
"gopkg.in/telebot.v3"
"log/slog"
)

func LoggingHandlerFunc(next telebot.HandlerFunc, log *slog.Logger) telebot.HandlerFunc {
return func(ctx telebot.Context) error {
data, _ := json.MarshalIndent(ctx.Update(), "", " ")
log.Debug(string(data))
return next(ctx)
}
}
43 changes: 43 additions & 0 deletions api/telegram/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package telegram

import (
"errors"
"fmt"
"gopkg.in/telebot.v3"
)

var ErrChatType = errors.New("unsupported chat type (supported options: \"group\", \"private\")")

var setSub = &telebot.ReplyMarkup{
ReplyKeyboard: [][]telebot.ReplyButton{
{
telebot.ReplyButton{
WebApp: &telebot.WebApp{
URL: "https://google.com",
},
},
},
},
}

func Start(ctx telebot.Context) (err error) {
chat := ctx.Chat()
switch chat.Type {
case telebot.ChatGroup:
err = startSubscription(ctx, chat.ID)
case telebot.ChatPrivate:
err = startMessage(ctx, chat.ID)
default:
err = fmt.Errorf("%w: %s", ErrChatType, chat.Type)
}
return
}

func startSubscription(ctx telebot.Context, chatId int64) (err error) {
err = ctx.Send("Setup New Subscription", setSub)
return
}
func startMessage(ctx telebot.Context, chatId int64) (err error) {
fmt.Printf("TODO: start message")
return
}
11 changes: 11 additions & 0 deletions api/telegram/user_left.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package telegram

import (
"fmt"
"gopkg.in/telebot.v3"
)

func UserLeft(ctx telebot.Context) (err error) {
fmt.Printf("User id=%d left, TODO: delete the subscription if owner\n", ctx.Sender().ID)
return
}
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"crypto/tls"
"fmt"
"github.com/awakari/bot-telegram/api/telegram"
"github.com/awakari/bot-telegram/config"
"gopkg.in/telebot.v3"
"log/slog"
Expand Down Expand Up @@ -37,16 +38,16 @@ func main() {
Cert: "/etc/server-cert/tls.crt",
},
},
Verbose: true,
}
var b *telebot.Bot
b, err = telebot.NewBot(s)
if err != nil {
panic(err)
}
b.Handle(telebot.OnText, func(ctx telebot.Context) error {
log.Info(fmt.Sprintf("update: %+v", ctx.Update()))
return ctx.Send(ctx.Text())
b.Use(func(next telebot.HandlerFunc) telebot.HandlerFunc {
return telegram.LoggingHandlerFunc(next, log)
})
b.Handle("/start", telegram.Start)
b.Handle(telebot.OnUserLeft, telegram.UserLeft)
b.Start()
}

0 comments on commit 0068e27

Please sign in to comment.