Initial commit

This commit is contained in:
Guillaume Ouint 2024-03-20 16:43:03 +01:00
commit 35c462f0a0
7 changed files with 156 additions and 0 deletions

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# Telegram Discord Bridge
Bridge a telegram channel topic to a discord channel in both direction

43
discord/bot.go Normal file
View file

@ -0,0 +1,43 @@
package discord
import (
"botbridge/types"
"os"
"github.com/bwmarrin/discordgo"
)
type Bot struct {
bot *discordgo.Session
Events chan types.Message
}
func NewBot() *Bot {
token := os.Getenv("DISCORD_TOKEN")
dg, err := discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
return &Bot{bot: dg, Events: make(chan types.Message)}
}
func (b *Bot) Start() {
b.bot.AddHandler(b.messageCreate)
err := b.bot.Open()
if err != nil {
panic(err)
}
}
func (b *Bot) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
// handle the message
//s.ChannelMessageSend(m.ChannelID, m.Content)
}
func (b *Bot) SendMessage(message types.Message) {
//msg := tgbotapi.NewMessage(chat, message)
//b.bot.Send(msg)
}

15
go.mod Normal file
View file

@ -0,0 +1,15 @@
module botbridge
go 1.22.1
require (
github.com/Lakhtiste/telegram-bot-api v0.0.0-20240320054535-151f5192fbb4
github.com/joho/godotenv v1.5.1
)
require (
github.com/bwmarrin/discordgo v0.27.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
)

16
go.sum Normal file
View file

@ -0,0 +1,16 @@
github.com/Lakhtiste/telegram-bot-api v0.0.0-20240320054535-151f5192fbb4 h1:jYiQL6oS/0VSxz70tjohzlWFeWYPY+Y31pRKwIeE/VI=
github.com/Lakhtiste/telegram-bot-api v0.0.0-20240320054535-151f5192fbb4/go.mod h1:xaBlbmDjk6InLqc2QhPA0Ib/z9s31K9br87nwUo4Z5E=
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

28
main.go Normal file
View file

@ -0,0 +1,28 @@
package main
import (
"botbridge/discord"
"botbridge/telegram"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
telegramBot := telegram.NewBot()
telegramBot.Start()
discordBot := discord.NewBot()
discordBot.Start()
discordMessages := telegramBot.Events
telegramMessages := telegramBot.Events
for {
select {
case message := <-discordMessages:
go telegramBot.SendMessage(message)
case message := <-telegramMessages:
go discordBot.SendMessage(message)
}
}
}

44
telegram/bot.go Normal file
View file

@ -0,0 +1,44 @@
package telegram
import (
"botbridge/types"
"fmt"
"os"
tgbotapi "github.com/Lakhtiste/telegram-bot-api"
)
// Bot is the main struct for the telegram bot
type Bot struct {
bot *tgbotapi.BotAPI
Events chan types.Message
}
// NewBot creates a new bot
func NewBot() *Bot {
token := os.Getenv("TELEGRAM_TOKEN")
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
panic(err)
}
bot.Debug = true
fmt.Printf("Authorized on account %s\n", bot.Self.UserName)
return &Bot{bot: bot, Events: make(chan types.Message)}
}
func (b *Bot) Start() {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := b.bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
// handle the message
}
}
func (b *Bot) SendMessage(message types.Message) {
//msg := tgbotapi.NewMessage(chat, message)
//b.bot.Send(msg)
}

7
types/message.go Normal file
View file

@ -0,0 +1,7 @@
package types
type Message struct {
Username string
Content string
Channel int
}