-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (70 loc) · 1.8 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/CyberDefenseEd/ChatProx/handlers"
"github.com/CyberDefenseEd/ChatProx/util"
"github.com/bwmarrin/discordgo"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type HandlerArgs struct {
TGbot *tgbotapi.BotAPI
Config *util.Config
Session *discordgo.Session
}
func loadConfig() (*util.Config, error) {
file, err := os.Open("config.json")
if err != nil {
return nil, err
}
defer file.Close()
config := &util.Config{}
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
if err != nil {
return nil, err
}
return config, nil
}
func main() {
config, err := loadConfig()
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
tgBot, err := tgbotapi.NewBotAPI(config.TelegramBotToken)
if err != nil {
log.Panic(err)
}
tgBot.Debug = true
dg, err := discordgo.New("Bot " + config.DiscordBotToken)
if err != nil {
log.Panic(err)
}
dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
handlers.ListenToMessageCreation(tgBot, config, s, m)
})
u := tgbotapi.NewUpdate(0)
u.Timeout = config.TelegramTimeout
updates := make(chan *tgbotapi.Update)
go func() {
for update := range tgBot.GetUpdatesChan(u) {
updates <- &update
}
}()
go handlers.GetTelegramUpdates(updates, config, tgBot, dg)
// Open Discord websocket connection
err = dg.Open()
if err != nil {
log.Panic(err)
}
defer dg.Close()
tgMsg := tgbotapi.NewMessage(config.TelegramChatID, "Discord -> Telegram proxy is now online!")
_, err = tgBot.Send(tgMsg)
if err != nil {
log.Printf("Failed to send message to Telegram: %v", err)
}
fmt.Println("Bot is now running. Press CTRL+C to exit.")
select {}
}