| 1 |
package telegram |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"os/signal" |
| 9 |
"slices" |
| 10 |
"strconv" |
| 11 |
|
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 14 |
"github.com/nicholas-fedor/shoutrrr/pkg/util/generator" |
| 15 |
) |
| 16 |
|
| 17 |
// Generator facilitates Telegram-specific URL generation via user interaction. |
| 18 |
type Generator struct { |
| 19 |
userDialog *generator.UserDialog |
| 20 |
client *Client |
| 21 |
chats []string |
| 22 |
chatNames []string |
| 23 |
chatTypes []string |
| 24 |
done bool |
| 25 |
botName string |
| 26 |
Reader io.Reader |
| 27 |
Writer io.Writer |
| 28 |
} |
| 29 |
|
| 30 |
// UpdatesLimit defines the number of updates to retrieve per API call. |
| 31 |
const ( |
| 32 |
UpdatesLimit = 10 // Number of updates to retrieve per call |
| 33 |
UpdatesTimeout = 120 // Timeout in seconds for long polling |
| 34 |
) |
| 35 |
|
| 36 |
// ErrNoChatsSelected indicates that no chats were selected during generation. |
| 37 |
var ( |
| 38 |
ErrNoChatsSelected = errors.New("no chats were selected") |
| 39 |
) |
| 40 |
|
| 41 |
// Generate creates a Telegram Shoutrrr configuration from user dialog input. |
| 42 |
func (g *Generator) Generate( |
| 43 |
_ types.Service, |
| 44 |
props map[string]string, |
| 45 |
_ []string, |
| 46 |
) (types.ServiceConfig, error) { |
| 47 |
var config Config |
| 48 |
|
| 49 |
if g.Reader == nil { |
| 50 |
g.Reader = os.Stdin |
| 51 |
} |
| 52 |
|
| 53 |
if g.Writer == nil { |
| 54 |
g.Writer = os.Stdout |
| 55 |
} |
| 56 |
|
| 57 |
g.userDialog = generator.NewUserDialog(g.Reader, g.Writer, props) |
| 58 |
userDialog := g.userDialog |
| 59 |
|
| 60 |
userDialog.Writelnf( |
| 61 |
"To start we need your bot token. If you haven't created a bot yet, you can use this link:", |
| 62 |
) |
| 63 |
userDialog.Writelnf(" %v", format.ColorizeLink("https://t.me/botfather?start")) |
| 64 |
userDialog.Writelnf("") |
| 65 |
|
| 66 |
token := userDialog.QueryString( |
| 67 |
"Enter your bot token:", |
| 68 |
generator.ValidateFormat(IsTokenValid), |
| 69 |
"token", |
| 70 |
) |
| 71 |
|
| 72 |
userDialog.Writelnf("Fetching bot info...") |
| 73 |
|
| 74 |
g.client = &Client{token: token} |
| 75 |
|
| 76 |
botInfo, err := g.client.GetBotInfo() |
| 77 |
if err != nil { |
| 78 |
return &Config{}, err |
| 79 |
} |
| 80 |
|
| 81 |
g.botName = botInfo.Username |
| 82 |
|
| 83 |
userDialog.Writelnf("") |
| 84 |
userDialog.Writelnf( |
| 85 |
"Okay! %v will listen for any messages in PMs and group chats it is invited to.", |
| 86 |
format.ColorizeString("@", g.botName), |
| 87 |
) |
| 88 |
|
| 89 |
g.done = false |
| 90 |
lastUpdate := 0 |
| 91 |
|
| 92 |
signals := make(chan os.Signal, 1) |
| 93 |
|
| 94 |
// Subscribe to system signals for graceful shutdown |
| 95 |
signal.Notify(signals, os.Interrupt) |
| 96 |
|
| 97 |
for !g.done { |
| 98 |
userDialog.Writelnf("Waiting for messages to arrive...") |
| 99 |
|
| 100 |
updates, err := g.client.GetUpdates( |
| 101 |
lastUpdate, |
| 102 |
UpdatesLimit, |
| 103 |
UpdatesTimeout, |
| 104 |
nil, |
| 105 |
) |
| 106 |
if err != nil { |
| 107 |
panic(err) |
| 108 |
} |
| 109 |
|
| 110 |
// If no updates were retrieved, prompt user to continue |
| 111 |
promptDone := len(updates) == 0 |
| 112 |
|
| 113 |
for _, update := range updates { |
| 114 |
lastUpdate = update.UpdateID + 1 |
| 115 |
|
| 116 |
switch { |
| 117 |
case update.Message != nil || update.ChannelPost != nil: |
| 118 |
message := update.Message |
| 119 |
if update.ChannelPost != nil { |
| 120 |
message = update.ChannelPost |
| 121 |
} |
| 122 |
|
| 123 |
chat := message.Chat |
| 124 |
|
| 125 |
source := message.Chat.Name() |
| 126 |
if message.From != nil { |
| 127 |
source = "@" + message.From.Username |
| 128 |
} |
| 129 |
|
| 130 |
userDialog.Writelnf("Got Message '%v' from %v in %v chat %v", |
| 131 |
format.ColorizeString(message.Text), |
| 132 |
format.ColorizeProp(source), |
| 133 |
format.ColorizeEnum(chat.Type), |
| 134 |
format.ColorizeNumber(chat.ID)) |
| 135 |
userDialog.Writelnf(g.addChat(chat)) |
| 136 |
// Another chat was added, prompt user to continue |
| 137 |
promptDone = true |
| 138 |
|
| 139 |
case update.ChatMemberUpdate != nil: |
| 140 |
cmu := update.ChatMemberUpdate |
| 141 |
oldStatus := cmu.OldChatMember.Status |
| 142 |
newStatus := cmu.NewChatMember.Status |
| 143 |
userDialog.Writelnf( |
| 144 |
"Got a bot chat member update for %v, status was changed from %v to %v", |
| 145 |
format.ColorizeProp(cmu.Chat.Name()), |
| 146 |
format.ColorizeEnum(oldStatus), |
| 147 |
format.ColorizeEnum(newStatus), |
| 148 |
) |
| 149 |
|
| 150 |
default: |
| 151 |
userDialog.Writelnf("Got unknown Update. Ignored!") |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if promptDone { |
| 156 |
userDialog.Writelnf("") |
| 157 |
|
| 158 |
g.done = !userDialog.QueryBool( |
| 159 |
fmt.Sprintf("Got %v chat ID(s) so far. Want to add some more?", |
| 160 |
format.ColorizeNumber(len(g.chats))), |
| 161 |
"", |
| 162 |
) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
userDialog.Writelnf("") |
| 167 |
userDialog.Writelnf("Cleaning up the bot session...") |
| 168 |
|
| 169 |
// Notify API that we got the updates |
| 170 |
if _, err = g.client.GetUpdates(lastUpdate, 0, 0, nil); err != nil { |
| 171 |
g.userDialog.Writelnf( |
| 172 |
"Failed to mark last updates as received: %v", |
| 173 |
format.ColorizeError(err), |
| 174 |
) |
| 175 |
} |
| 176 |
|
| 177 |
if len(g.chats) < 1 { |
| 178 |
return nil, ErrNoChatsSelected |
| 179 |
} |
| 180 |
|
| 181 |
userDialog.Writelnf("Selected chats:") |
| 182 |
|
| 183 |
for i, id := range g.chats { |
| 184 |
name := g.chatNames[i] |
| 185 |
chatType := g.chatTypes[i] |
| 186 |
userDialog.Writelnf( |
| 187 |
" %v (%v) %v", |
| 188 |
format.ColorizeNumber(id), |
| 189 |
format.ColorizeEnum(chatType), |
| 190 |
format.ColorizeString(name), |
| 191 |
) |
| 192 |
} |
| 193 |
|
| 194 |
userDialog.Writelnf("") |
| 195 |
|
| 196 |
config = Config{ |
| 197 |
Notification: true, |
| 198 |
Token: token, |
| 199 |
Chats: g.chats, |
| 200 |
} |
| 201 |
|
| 202 |
return &config, nil |
| 203 |
} |
| 204 |
|
| 205 |
// addChat adds a chat to the generator's list if it’s not already present. |
| 206 |
func (g *Generator) addChat(chat *Chat) string { |
| 207 |
chatID := strconv.FormatInt(chat.ID, 10) |
| 208 |
name := chat.Name() |
| 209 |
|
| 210 |
if slices.Contains(g.chats, chatID) { |
| 211 |
return fmt.Sprintf("chat %v is already selected!", format.ColorizeString(name)) |
| 212 |
} |
| 213 |
|
| 214 |
g.chats = append(g.chats, chatID) |
| 215 |
g.chatNames = append(g.chatNames, name) |
| 216 |
g.chatTypes = append(g.chatTypes, chat.Type) |
| 217 |
|
| 218 |
return fmt.Sprintf("Added new chat %v!", format.ColorizeString(name)) |
| 219 |
} |