forked from shelomentsevd/telegramgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
744 lines (665 loc) · 18.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"path/filepath"
"github.com/moorage/mtproto"
)
const updatePeriod = time.Second * 2
type Command struct {
Name string
Arguments string
}
// Returns user nickname in two formats:
// <id> <First name> @<Username> <Last name> if user has username
// <id> <First name> <Last name> otherwise
func nickname(user mtproto.TL_user) string {
if user.Username == "" {
return fmt.Sprintf("%d %s %s", user.Id, user.First_name, user.Last_name)
}
return fmt.Sprintf("%d %s @%s %s", user.Id, user.First_name, user.Username, user.Last_name)
}
// Returns date in RFC822 format
func formatDate(date int32) string {
unixTime := time.Unix((int64)(date), 0)
return unixTime.Format(time.RFC822)
}
// Reads user input and returns Command pointer
func (cli *TelegramCLI) readCommand() *Command {
fmt.Printf("\nUser input: ")
input, err := cli.reader.ReadString('\n')
if err != nil {
fmt.Println(err)
return nil
}
if input[0] != '\\' {
return nil
}
command := new(Command)
input = strings.TrimSpace(input)
args := strings.SplitN(input, " ", 2)
command.Name = strings.ToLower(strings.Replace(args[0], "\\", "", 1))
if len(args) > 1 {
command.Arguments = args[1]
}
return command
}
// Show help
func help() {
fmt.Println("Available commands:")
fmt.Println("\\me - Shows information about current account")
fmt.Println("\\contacts - Shows contacts list")
fmt.Println("\\chats - Shows chats & channels list")
fmt.Println("\\dialogs - Shows dialogs list")
fmt.Println("\\umsg <id> <message> - Sends message to user with <id>")
fmt.Println("\\cmsg <id> <message> - Sends message to chat with <id>")
fmt.Println("\\uhist <id> <limit> - Gets last <limit> messages with user <id>")
fmt.Println("\\chist <id> <limit> - Gets last <limit> messages in chat with <id>")
fmt.Println("\\help - Shows this message")
fmt.Println("\\quit - Quit")
}
type TelegramCLI struct {
mtproto *mtproto.MTProto
state *mtproto.TL_updates_state
read chan struct{}
stop chan struct{}
connected bool
reader *bufio.Reader
users map[int32]mtproto.TL_user
chats map[int32]mtproto.Chat
channels map[int32]mtproto.Channel
dialogs map[string]map[int32]mtproto.Dialog
contacts map[int32]mtproto.Contact
contactUsers map[int32]mtproto.User
}
func NewTelegramCLI(pMTProto *mtproto.MTProto) (*TelegramCLI, error) {
if pMTProto == nil {
return nil, errors.New("NewTelegramCLI: pMTProto is nil")
}
cli := new(TelegramCLI)
cli.mtproto = pMTProto
cli.read = make(chan struct{}, 1)
cli.stop = make(chan struct{}, 1)
cli.reader = bufio.NewReader(os.Stdin)
cli.users = make(map[int32]mtproto.TL_user)
cli.chats = make(map[int32]mtproto.Chat)
cli.channels = make(map[int32]mtproto.Channel)
cli.dialogs = make(map[string]map[int32]mtproto.Dialog)
cli.contacts = make(map[int32]mtproto.Contact)
cli.contactUsers = make(map[int32]mtproto.User)
return cli, nil
}
func (cli *TelegramCLI) Authorization(phonenumber string) error {
if phonenumber == "" {
return fmt.Errorf("Phone number is empty")
}
phoneCodeHash, err := cli.mtproto.Auth_SendCode(phonenumber)
if err != nil {
return err
}
var code string
fmt.Printf("(phone code hash "+phoneCodeHash+") Enter code: ")
fmt.Scanf("%s", &code)
auth, err := cli.mtproto.Auth_SignIn(phonenumber, phoneCodeHash, code)
if err != nil {
return err
}
userSelf := auth.User.(mtproto.TL_user)
cli.users[userSelf.Id] = userSelf
message := fmt.Sprintf("Signed in: Id %d name <%s @%s %s>\n", userSelf.Id, userSelf.First_name, userSelf.Username, userSelf.Last_name)
fmt.Print(message)
log.Println(message)
log.Println(userSelf)
return nil
}
// Load contacts to users map
func (cli *TelegramCLI) LoadContacts() error {
contacts, users, err := cli.mtproto.Contacts_GetContacts(0)
if err != nil {
return err
}
for _, contact := range contacts {
cli.contacts[contact.UserID] = contact
}
for _, user := range users {
cli.contactUsers[user.ID] = user
cli.users[user.ID] = *user.TlUser
}
fmt.Printf("Loaded %d contacts and %d contactUsers\n", len(contacts), len(users))
return nil
}
func (cli *TelegramCLI) LoadChannels() error {
channelTls := make([]mtproto.TL, len(cli.channels))
i := 0
for channelId := range cli.channels {
channelTls[i] = mtproto.TL_inputChannel{
Channel_id: channelId,
Access_hash: cli.channels[channelId].AccessHash,
}
i++
}
channels, err := cli.mtproto.Channels_GetChannels(channelTls)
if err != nil {
return err
}
for _, channel := range channels {
cli.channels[channel.ID] = channel
}
fmt.Printf("Loaded %d channels\n", len(channels))
return nil
}
func (cli *TelegramCLI) LoadChats() error {
chatIds := make([]int32, len(cli.chats))
i := 0
for k := range cli.chats {
chatIds[i] = k
i++
}
chats, err := cli.mtproto.Messages_GetChats(chatIds)
if err != nil {
return err
}
for _, chat := range chats {
cli.chats[chat.ID] = chat
}
fmt.Printf("Loaded %d chats\n", len(chats))
return nil
}
func (cli *TelegramCLI) LoadDialogs() error {
dialogs, _, err := cli.mtproto.Messages_GetDialogs(0, 0, 10000, mtproto.TL_inputPeerEmpty{})
if err != nil {
return err
}
for _, dialog := range dialogs {
if cli.dialogs[dialog.Type] == nil {
cli.dialogs[dialog.Type] = make(map[int32]mtproto.Dialog)
}
cli.dialogs[dialog.Type][dialog.PeerID] = dialog
switch dialog.Type {
case mtproto.DIALOG_TYPE_CHAT:
cli.chats[dialog.Chat.ID] = *dialog.Chat
case mtproto.DIALOG_TYPE_USER:
cli.users[dialog.User.ID] = *(*dialog.User).TlUser
case mtproto.DIALOG_TYPE_CHANNEL:
cli.channels[dialog.Channel.ID] = *dialog.Channel
}
}
fmt.Printf("Loaded %d dialogs\n", len(dialogs))
return nil
}
// Prints information about current user
func (cli *TelegramCLI) CurrentUser() error {
user, err := cli.mtproto.Users_GetFullSelf()
if err != nil {
return err
}
cli.users[user.TlUser.Id] = *user.TlUser
message := fmt.Sprintf("You are logged in as: %s @%s %s\nId: %d\nPhone: %s\n", user.FirstName, user.Username, user.LastName, user.ID, user.Phone)
fmt.Print(message)
log.Println(message)
return nil
}
// Connects to telegram server
func (cli *TelegramCLI) Connect() error {
if err := cli.mtproto.Connect(); err != nil {
return err
}
cli.connected = true
log.Println("Connected to telegram server")
return nil
}
// Disconnect from telegram server
func (cli *TelegramCLI) Disconnect() error {
if err := cli.mtproto.Disconnect(); err != nil {
return err
}
cli.connected = false
log.Println("Disconnected from telegram server")
return nil
}
// Send signal to stop update cycle
func (cli *TelegramCLI) Stop() {
cli.stop <- struct{}{}
}
// Send signal to read user input
func (cli *TelegramCLI) Read() {
cli.read <- struct{}{}
}
// Run telegram cli
func (cli *TelegramCLI) Run() error {
// Update cycle
log.Println("CLI Update cycle started")
UpdateCycle:
for {
select {
case <-cli.read:
command := cli.readCommand()
log.Println("User input: ")
log.Println(*command)
err := cli.RunCommand(command)
if err != nil {
log.Println(err)
}
case <-cli.stop:
log.Println("Update cycle stoped")
break UpdateCycle
case <-time.After(updatePeriod):
log.Println("Trying to get update from server...")
cli.processUpdates()
}
}
log.Println("CLI Update cycle finished")
return nil
}
// Parse message and print to screen
func (cli *TelegramCLI) parseMessage(message mtproto.Message) {
var senderName string
from := message.From
userFrom, found := cli.users[from]
if !found {
log.Printf("Can't find user with id: %d", from)
senderName = fmt.Sprintf("%d unknow user", from)
}
senderName = nickname(userFrom)
toPeer := message.To
date := formatDate(message.Date)
// Peer type
switch message.To.Type {
case mtproto.PEER_TYPE_USER:
user, found := cli.users[message.To.ID]
if !found {
log.Printf("Can't find user with id: %d", message.To.ID)
// TODO: Get information about user from telegram server
}
peerName := nickname(user)
fmt.Printf("%s %d %s to %s: %s\n", date, message.ID, senderName, peerName, message.Body)
case mtproto.PEER_TYPE_CHAT:
chat, found := cli.chats[message.To.ID]
if !found {
log.Printf("Can't find chat with id: %d", message.To.ID)
}
fmt.Printf("%s %d %s in %s(%d): %s\n", date, message.ID, senderName, chat.Title, chat.ID, message.Body)
case mtproto.PEER_TYPE_CHANNEL:
channel, found := cli.channels[message.To.ID]
if !found {
log.Printf("Can't find channel with id: %d", message.To.ID)
}
fmt.Printf("%s %d %s in %s(%d): %s", date, message.ID, senderName, channel.Title, channel.ID, message.Body)
default:
log.Printf("Message `%v` with Unknown peer type: %v", message, toPeer)
}
}
// Works with mtproto.TL_updates_difference and mtproto.TL_updates_differenceSlice
func (cli *TelegramCLI) parseUpdateDifference(users []mtproto.User, messages []mtproto.Message, chats []mtproto.Chat, channels []mtproto.Channel, updates []mtproto.Update) {
// Process users
for _, user := range users {
cli.users[(*user.TlUser).Id] = *user.TlUser
}
// Process chats
for _, chat := range chats {
cli.chats[chat.ID] = chat
}
// Process Channels
for _, channel := range channels {
cli.channels[channel.ID] = channel
}
// Process messages
for _, message := range messages {
cli.parseMessage(message)
}
// Process updates
for _, update := range updates {
switch update.Type {
case mtproto.UPDATE_TYPE_NEW_MESSAGE:
cli.parseMessage(*update.Message)
case mtproto.UPDATE_TYPE_CHANNEL_NEW_MESSAGE:
cli.parseMessage(*update.Message)
case mtproto.UPDATE_TYPE_EDIT_MESSAGE:
cli.parseMessage(*update.Message)
case mtproto.UPDATE_TYPE_EDIT_CHANNEL_MESSAGE:
cli.parseMessage(*update.Message)
default:
log.Printf("Unhandled update type for update: %v\n", update)
}
}
}
// Parse update
func (cli *TelegramCLI) parseUpdate(update mtproto.UpdateDifference) {
if (update.Type == mtproto.UPDATE_DIFFERENCE_EMPTY) {
cli.state.Date = update.IntermediateState.Date
cli.state.Seq = update.IntermediateState.Seq
return
}
if (update.TlUpdatesDifference != nil) {
cli.state = update.IntermediateState.TlUpdatesState
cli.parseUpdateDifference(update.Users, update.NewMessages, update.Chats, update.Channels, update.OtherUpdates)
return
}
if (update.Type == mtproto.UPDATE_DIFFERENCE_SLICE) {
cli.state = update.IntermediateState.TlUpdatesState
cli.parseUpdateDifference(update.Users, update.NewMessages, update.Chats, update.Channels, update.OtherUpdates)
return
}
if (update.Type == mtproto.UPDATE_DIFFERENCE_TOO_LONG) {
cli.state.Pts = update.IntermediateState.Pts
return
}
}
// Get updates and prints result
func (cli *TelegramCLI) processUpdates() {
if cli.connected {
if cli.state == nil {
log.Println("cli.state is nil. Trying to get actual state...")
tl, err := cli.mtproto.Updates_GetState()
if err != nil {
log.Fatal(err)
}
log.Println("Got something")
log.Println(*tl)
cli.state = tl.TlUpdatesState
return
}
tl, err := cli.mtproto.Updates_GetDifference(cli.state.Pts, cli.state.Qts, cli.state.Date)
if err != nil {
log.Println(err)
return
}
log.Println("Got new update")
log.Println(tl)
cli.parseUpdate(*tl)
return
}
}
// Print contact list
func (cli *TelegramCLI) Contacts() error {
err := cli.LoadContacts()
if err != nil {
return err
}
fmt.Printf(
"\033[33m\033[1m%10s %10s %-30s\033[0m\n",
"id", "mutual", "name",
)
for _, contact := range cli.contacts {
name := ""
if cu, ok := cli.contactUsers[contact.UserID]; ok {
name = fmt.Sprintf("%s %s (@%s)", cu.FirstName, cu.LastName, cu.Username)
}
fmt.Printf(
"%10d %10t %-30s\n",
contact.UserID,
contact.Mutual,
name,
)
}
return nil
}
func (cli *TelegramCLI) Dialogs() error {
err := cli.LoadDialogs()
if err != nil {
return err
}
fmt.Printf("Dialogs:\n")
fmt.Printf(
"\033[33m\033[1m%10s %10s %10s %-30s\033[0m\n",
"type", "unreadCount", "peerId", "topMessage",
)
for _, dialogsOfType := range cli.dialogs {
for _, dialog := range dialogsOfType {
msg := ""
if dialog.TopMessage != nil {
msg = strings.Replace(dialog.TopMessage.Body, "\n", "\\", -1)
}
fmt.Printf(
"%10s %10d %10d %-.30s\n",
dialog.Type,
dialog.UnreadCount,
dialog.PeerID,
msg,
)
}
}
return nil
}
func (cli *TelegramCLI) ChatsAndChannels() error {
err := cli.LoadChannels()
if err != nil {
return err
}
err = cli.LoadChats()
if err != nil {
return err
}
fmt.Printf("%d Chats:\n", len(cli.chats))
fmt.Printf(
"\033[33m\033[1m%10s %10s %10s %-30s\033[0m\n",
"id", "memberCount", "username", "title",
)
for _, chat := range cli.chats {
fmt.Printf(
"%10d %10d %10s %-30s\n",
chat.ID,
len(chat.Members),
chat.Username,
chat.Title,
)
}
fmt.Printf("%d Channels:\n", len(cli.channels))
fmt.Printf(
"\033[33m\033[1m%10s %10s %-30s\033[0m\n",
"id", "username", "title",
)
for _, channel := range cli.channels {
fmt.Printf(
"%10d %10t %-30s\n",
channel.ID,
channel.Username,
channel.Title,
)
}
return nil
}
// Runs command and prints result to console
func (cli *TelegramCLI) RunCommand(command *Command) error {
switch command.Name {
case "me":
if err := cli.CurrentUser(); err != nil {
return err
}
case "contacts":
if err := cli.Contacts(); err != nil {
return err
}
case "chats":
if err := cli.ChatsAndChannels(); err != nil {
return err
}
case "dialogs":
if err := cli.Dialogs(); err != nil {
return err
}
case "umsg":
if command.Arguments == "" {
return errors.New("Not enough arguments: peer id and msg required")
}
args := strings.SplitN(command.Arguments, " ", 2)
if len(args) < 2 {
return errors.New("Not enough arguments: peer id and msg required")
}
id, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("Wrong arguments: %s isn't a number", args[0])
}
user, found := cli.users[int32(id)]
if !found {
info := fmt.Sprintf("Can't find user with id: %d", id)
fmt.Println(info)
return nil
}
userInfo := mtproto.TL_inputPeerUser{User_id: user.Id, Access_hash: user.Access_hash}
// userInfo := mtproto.TL_inputPeerSelf{}
update, err := cli.mtproto.Messages_SendMessage(args[1], userInfo, 0)
fmt.Printf("umsg: SendMessage Returned: %v\n", update)
//cli.parseUpdate(*update)
case "cmsg":
if command.Arguments == "" {
return errors.New("Not enough arguments: peer id and msg required")
}
args := strings.SplitN(command.Arguments, " ", 2)
if len(args) < 2 {
return errors.New("Not enough arguments: peer id and msg required")
}
id, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("Wrong arguments: %s isn't a number", args[0])
}
update, err := cli.mtproto.Messages_SendMessage(args[1], mtproto.TL_inputPeerChat{Chat_id: int32(id)}, 0)
fmt.Printf("cmsg: SendMessage Returned: %v\n", update)
//cli.parseUpdate(*update)
case "uhist":
if command.Arguments == "" {
return errors.New("Not enough arguments: peer id and limit required")
}
args := strings.SplitN(command.Arguments, " ", 2)
if len(args) < 2 {
return errors.New("Not enough arguments: peer id and limit required")
}
id, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("Wrong arguments: %s isn't a number", args[0])
}
user, found := cli.users[int32(id)]
if !found {
info := fmt.Sprintf("Can't find user with id: %d", id)
fmt.Println(info)
return nil
}
limit, err := strconv.Atoi(args[1])
if err != nil {
return fmt.Errorf("Wrong arguments: %s isn't a number", args[1])
}
userInfo := mtproto.TL_inputPeerUser{User_id: user.Id, Access_hash: user.Access_hash}
// userInfo := mtproto.TL_inputPeerSelf{}
messages, count := cli.mtproto.Messages_GetHistory(userInfo, int32(limit), 0, 0)
for _, message := range messages {
cli.parseMessage(message)
}
fmt.Printf("uhist: GetHistory Returned %d messages\n", count)
//cli.parseUpdate(*update)
case "chist":
if command.Arguments == "" {
return errors.New("Not enough arguments: peer id and limit required")
}
args := strings.SplitN(command.Arguments, " ", 2)
if len(args) < 2 {
return errors.New("Not enough arguments: peer id and limit required")
}
id, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("Wrong arguments: %s isn't a number", args[0])
}
limit, err := strconv.Atoi(args[1])
if err != nil {
return fmt.Errorf("Wrong arguments: %s isn't a number", args[1])
}
chatInfo := mtproto.TL_inputPeerChat{Chat_id: int32(id)}
messages, count := cli.mtproto.Messages_GetHistory(chatInfo, int32(limit), 0, 0)
for _, message := range messages {
cli.parseMessage(message)
}
fmt.Printf("chist: GetHistory Returned %d messages\n", count)
//cli.parseUpdate(*update)
case "help":
help()
case "quit":
cli.Stop()
cli.Disconnect()
default:
fmt.Println("Unknow command. Try \\help to see all commands")
return errors.New("Unknow command")
}
return nil
}
func main() {
logfile, err := os.OpenFile("logfile.txt", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer logfile.Close()
appId, _ := strconv.ParseUint(os.Getenv("TELEGRAM_APP_ID"), 10, 32)
if (appId < 1) {
log.Fatalf("couldn't parse appId in $TELEGRAM_APP_ID: `%s`", os.Getenv("TELEGRAM_APP_ID"))
}
apiHash := os.Getenv("TELEGRAM_API_HASH")
if (apiHash == "") {
log.Fatalf("couldn't parse apiHash in $TELEGRAM_API_HASH: `%s`", os.Getenv("TELEGRAM_API_HASH"))
}
dcAddress := os.Getenv("TELEGRAM_DC")
authFile := os.Getenv("TELEGRAM_AUTH_FILE")
if (authFile == "") {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
authFile = dir + "/telegram.auth"
fmt.Println("Using auth file: " + authFile)
}
log.SetOutput(logfile)
log.Println("Program started")
// LoadContacts
mtproto, err := mtproto.NewMTProto(int64(appId), apiHash, authFile, dcAddress, mtproto.DEBUG_LEVEL_NETWORK)
if err != nil {
log.Fatal(err)
}
telegramCLI, err := NewTelegramCLI(mtproto)
if err != nil {
log.Fatal(err)
}
if err = telegramCLI.Connect(); err != nil {
log.Fatal(err)
}
fmt.Println("Welcome to telegram CLI")
if err := telegramCLI.CurrentUser(); err != nil {
var phonenumber string
fmt.Println("Enter phonenumber number below: ")
fmt.Scanln(&phonenumber)
err := telegramCLI.Authorization(phonenumber)
if err != nil {
log.Fatal("Failed authorization", err)
}
}
if err := telegramCLI.LoadContacts(); err != nil {
log.Fatalf("Failed to load contacts: %s", err)
}
// Show help first time
help()
stop := make(chan struct{}, 1)
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
SignalProcessing:
for {
select {
case <-sigc:
telegramCLI.Read()
case <-stop:
break SignalProcessing
}
}
}()
err = telegramCLI.Run()
if err != nil {
log.Println(err)
fmt.Println("Telegram CLI exits with error: ", err)
}
// Stop SignalProcessing goroutine
stop <- struct{}{}
}