Skip to content

Commit

Permalink
feat: improve code and support for proxy from environment
Browse files Browse the repository at this point in the history
  • Loading branch information
damonto committed Jun 21, 2024
1 parent e6e4108 commit bb8f039
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 137 deletions.
5 changes: 4 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package config

import "errors"
import (
"errors"
)

type Config struct {
BotToken string
AdminId int64
Dir string
Version string
DontDownload bool
Proxy string
Verbose bool
}

Expand Down
6 changes: 2 additions & 4 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log/slog"

"github.com/damonto/telegram-sms/internal/app/routes"
"github.com/damonto/telegram-sms/internal/pkg/conversation"
"github.com/damonto/telegram-sms/internal/pkg/state"
"gopkg.in/telebot.v3"
"gopkg.in/telebot.v3/middleware"
)
Expand All @@ -27,9 +27,7 @@ func (a *app) setup() error {
a.bot.Use(middleware.Recover())
a.bot.Use(middleware.AutoRespond())

conversation.NewConversation(a.bot)

if err := routes.NewRouter(a.bot).Setup(); err != nil {
if err := routes.NewRouter(a.bot, state.NewState(a.bot)).Setup(); err != nil {
slog.Error("failed to setup router", "error", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/handler/chip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ChipHandler struct {

func HandleChipCommand(c telebot.Context) error {
h := &ChipHandler{}
h.setModem(c)
h.init(c)
return h.handle(c)
}

Expand Down
22 changes: 11 additions & 11 deletions internal/app/handler/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"strings"
"time"

"github.com/damonto/telegram-sms/internal/pkg/conversation"
"github.com/damonto/telegram-sms/internal/pkg/lpac"
"github.com/damonto/telegram-sms/internal/pkg/state"
"github.com/damonto/telegram-sms/internal/pkg/util"
"gopkg.in/telebot.v3"
)

type DownloadHandler struct {
handler
activationCode *lpac.ActivationCode
conversation conversation.Conversation
state state.State
}

const (
Expand All @@ -25,24 +25,24 @@ const (

func HandleDownloadCommand(c telebot.Context) error {
h := &DownloadHandler{}
h.setModem(c)
h.conversation = conversation.New(c)
h.conversation.Flow(map[string]telebot.HandlerFunc{
h.init(c)
h.state = h.stateManager.New(c)
h.state.Stages(map[string]telebot.HandlerFunc{
DownloadAskActivationCode: h.handleActivationCode,
DownloadAskConfirmationCode: h.handleConfirmationCode,
})
return h.handle(c)
}

func (h *DownloadHandler) handle(c telebot.Context) error {
h.conversation.Next(DownloadAskActivationCode)
h.state.Next(DownloadAskActivationCode)
return c.Send("Please send me the activation code")
}

func (h *DownloadHandler) handleActivationCode(c telebot.Context) error {
activationCode := c.Text()
if activationCode == "" || !strings.HasPrefix(activationCode, "LPA:1$") {
h.conversation.Next(DownloadAskActivationCode)
h.state.Next(DownloadAskActivationCode)
return c.Send("Invalid activation code.")
}

Expand All @@ -52,23 +52,23 @@ func (h *DownloadHandler) handleActivationCode(c telebot.Context) error {
MatchingId: parts[2],
}
if len(parts) == 5 && parts[4] == "1" {
h.conversation.Next(DownloadAskConfirmationCode)
h.state.Next(DownloadAskConfirmationCode)
return c.Send("Please send me the confirmation code")
}

h.conversation.Done()
h.stateManager.Done(c)
return h.download(c)
}

func (h *DownloadHandler) handleConfirmationCode(c telebot.Context) error {
confirmationCode := c.Text()
if confirmationCode == "" {
h.conversation.Next(DownloadAskConfirmationCode)
h.state.Next(DownloadAskConfirmationCode)
return c.Send("Invalid confirmation code")
}

h.activationCode.ConfirmationCode = confirmationCode
h.conversation.Done()
h.stateManager.Done(c)
return h.download(c)
}

Expand Down
11 changes: 6 additions & 5 deletions internal/app/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package handler

import (
"github.com/damonto/telegram-sms/internal/pkg/modem"
"github.com/damonto/telegram-sms/internal/pkg/state"
"gopkg.in/telebot.v3"
)

type handler struct {
modem *modem.Modem
modem *modem.Modem
stateManager *state.StateManager
}

func (h *handler) setModem(c telebot.Context) *modem.Modem {
modem := c.Get("modem").(*modem.Modem)
h.modem = modem
return modem
func (h *handler) init(c telebot.Context) {
h.modem = c.Get("modem").(*modem.Modem)
h.stateManager = c.Get("state").(*state.StateManager)
}
44 changes: 30 additions & 14 deletions internal/app/handler/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"log/slog"
"time"

"github.com/damonto/telegram-sms/internal/pkg/conversation"
"github.com/damonto/telegram-sms/internal/pkg/lpac"
"github.com/damonto/telegram-sms/internal/pkg/state"
"github.com/damonto/telegram-sms/internal/pkg/util"
"github.com/google/uuid"
"gopkg.in/telebot.v3"
)

type ProfileHandler struct {
handler
conversation conversation.Conversation
ICCID string
state state.State
ICCID string
}

const (
Expand All @@ -31,9 +31,9 @@ const (

func HandleProfilesCommand(c telebot.Context) error {
h := &ProfileHandler{}
h.setModem(c)
h.conversation = conversation.New(c)
h.conversation.Flow(map[string]telebot.HandlerFunc{
h.init(c)
h.state = h.stateManager.New(c)
h.state.Stages(map[string]telebot.HandlerFunc{
ProfileStateHandleAction: h.handleAction,
ProfileStateActionRename: h.handleActionRename,
ProfileStateActionDelete: h.handleActionDelete,
Expand Down Expand Up @@ -90,7 +90,7 @@ func (h *ProfileHandler) toTextMessage(c telebot.Context, profiles []*lpac.Profi
btn := selector.Data(fmt.Sprintf("%s (%s)", name, p.ICCID[len(p.ICCID)-4:]), uuid.New().String(), p.ICCID)
c.Bot().Handle(&btn, func(c telebot.Context) error {
h.ICCID = c.Data()
h.conversation.Next(ProfileStateHandleAction)
h.state.Next(ProfileStateHandleAction)
return h.handleAskAction(c)
})
buttons = append(buttons, btn)
Expand All @@ -104,10 +104,10 @@ func (h *ProfileHandler) handleAction(c telebot.Context) error {
case ProfileActionEnable:
return h.handleActionEnable(c)
case ProfileActionRename:
h.conversation.Next(ProfileStateActionRename)
h.state.Next(ProfileStateActionRename)
return c.Send("OK. Send me the new name.")
case ProfileActionDelete:
h.conversation.Next(ProfileStateActionDelete)
h.state.Next(ProfileStateActionDelete)
return c.Send("Are you sure you want to delete this profile?", &telebot.ReplyMarkup{
OneTimeKeyboard: true,
ResizeKeyboard: true,
Expand Down Expand Up @@ -156,13 +156,29 @@ func (h *ProfileHandler) handleAskAction(c telebot.Context) error {

template := `
You've selected the profile:
ICCID: %s
%s *%s*
%s
What do you want to do with this profile?
`
return c.Send(util.EscapeText(fmt.Sprintf(template, profile.ICCID)), &telebot.ReplyMarkup{
OneTimeKeyboard: true,
ResizeKeyboard: true,
ReplyKeyboard: [][]telebot.ReplyButton{buttons},
name := fmt.Sprintf("[%s] ", profile.ProviderName)
if profile.Nickname != "" {
name += profile.Nickname
} else {
name += profile.ProfileName
}
var emoji string
if profile.State == lpac.ProfileStateEnabled {
emoji = "✅"
} else {
emoji = "🅾️"
}
return c.Send(util.EscapeText(fmt.Sprintf(template, emoji, name, fmt.Sprintf("`%s`", profile.ICCID))), &telebot.SendOptions{
ParseMode: telebot.ModeMarkdownV2,
ReplyMarkup: &telebot.ReplyMarkup{
OneTimeKeyboard: true,
ResizeKeyboard: true,
ReplyKeyboard: [][]telebot.ReplyButton{buttons},
},
})
}

Expand Down
20 changes: 10 additions & 10 deletions internal/app/handler/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package handler
import (
"fmt"

"github.com/damonto/telegram-sms/internal/pkg/conversation"
"github.com/damonto/telegram-sms/internal/pkg/state"
"gopkg.in/telebot.v3"
)

type SendHandler struct {
handler
phoneNumber string
conversation conversation.Conversation
phoneNumber string
state state.State
}

const (
Expand All @@ -20,17 +20,17 @@ const (

func HandleSendCommand(c telebot.Context) error {
h := &SendHandler{}
h.setModem(c)
h.conversation = conversation.New(c)
h.conversation.Flow(map[string]telebot.HandlerFunc{
h.init(c)
h.state = h.stateManager.New(c)
h.state.Stages(map[string]telebot.HandlerFunc{
SendAskPhoneNumber: h.handlePhoneNumber,
SendAskMessage: h.handleMessage,
})
return h.handle(c)
}

func (h *SendHandler) handle(c telebot.Context) error {
h.conversation.Next(SendAskPhoneNumber)
h.state.Next(SendAskPhoneNumber)
return c.Send("Please send me the phone number you want to send the message to")
}

Expand All @@ -41,7 +41,7 @@ func (h *SendHandler) handlePhoneNumber(c telebot.Context) error {
}
}

h.conversation.Next(SendAskMessage)
h.state.Next(SendAskMessage)
h.phoneNumber = c.Text()
return c.Send("Please send me the message you want to send")
}
Expand All @@ -51,10 +51,10 @@ func (h *SendHandler) handleMessage(c telebot.Context) error {
c.Send(fmt.Sprintf("Failed to send SMS to *%s*", h.phoneNumber), &telebot.SendOptions{
ParseMode: telebot.ModeMarkdownV2,
})
h.conversation.Done()
h.stateManager.Done(c)
return err
}
h.conversation.Done()
h.stateManager.Done(c)
return c.Send(fmt.Sprintf("Your SMS has been sent to *%s*", h.phoneNumber), &telebot.SendOptions{
ParseMode: telebot.ModeMarkdownV2,
})
Expand Down
20 changes: 10 additions & 10 deletions internal/app/handler/ussd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package handler
import (
"fmt"

"github.com/damonto/telegram-sms/internal/pkg/conversation"
"github.com/damonto/telegram-sms/internal/pkg/state"
"gopkg.in/telebot.v3"
)

type USSDHandler struct {
handler
conversation conversation.Conversation
state state.State
}

const (
Expand All @@ -19,38 +19,38 @@ const (

func HandleUSSDCommand(c telebot.Context) error {
h := &USSDHandler{}
h.setModem(c)
h.conversation = conversation.New(c)
h.conversation.Flow(map[string]telebot.HandlerFunc{
h.init(c)
h.state = h.stateManager.New(c)
h.state.Stages(map[string]telebot.HandlerFunc{
USSDExecuteCommand: h.handleExecuteCommand,
USSDRespondCommand: h.handleRespondCommand,
})
return h.handle(c)
}

func (h *USSDHandler) handle(c telebot.Context) error {
h.conversation.Next(USSDExecuteCommand)
h.state.Next(USSDExecuteCommand)
return c.Send("Please send me the USSD command you want to execute")
}

func (h *USSDHandler) handleExecuteCommand(c telebot.Context) error {
response, err := h.modem.RunUSSDCommand(c.Text())
if err != nil {
h.conversation.Done()
h.stateManager.Done(c)
c.Send("Failed to execute USSD command, err: " + err.Error())
return err
}
h.conversation.Next(USSDRespondCommand)
h.state.Next(USSDRespondCommand)
return c.Send(fmt.Sprintf("%s\n%s\nIf you want to respond to this USSD command, please send me the response.", c.Text(), response))
}

func (h *USSDHandler) handleRespondCommand(c telebot.Context) error {
response, err := h.modem.RespondUSSDCommand(c.Text())
if err != nil {
h.conversation.Done()
h.stateManager.Done(c)
c.Send("Failed to respond to USSD command, err: " + err.Error())
return err
}
h.conversation.Next(USSDRespondCommand)
h.state.Next(USSDRespondCommand)
return c.Send(fmt.Sprintf("%s\n%s\nIf you want to respond to this USSD command, please send me the response.", c.Text(), response))
}
1 change: 0 additions & 1 deletion internal/app/middleware/modem.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func SelectModem(requiredEuicc bool) telebot.MiddlewareFunc {
}

func selectModem(c telebot.Context, modems map[string]*modem.Modem, done chan string) error {

selector := telebot.ReplyMarkup{}
btns := make([]telebot.Btn, 0, len(modems))
for k, m := range modems {
Expand Down
15 changes: 15 additions & 0 deletions internal/app/middleware/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package middleware

import (
"github.com/damonto/telegram-sms/internal/pkg/state"
"gopkg.in/telebot.v3"
)

func WrapState(state *state.StateManager) telebot.MiddlewareFunc {
return func(next telebot.HandlerFunc) telebot.HandlerFunc {
return func(c telebot.Context) error {
c.Set("state", state)
return next(c)
}
}
}
Loading

0 comments on commit bb8f039

Please sign in to comment.