feat(go): add some forward logic (wip)

This commit is contained in:
tiennm99 committed 2025-11-29 17:28:34 +07:00
1 parent 16cf1c7b75
commit bc79252661
4 files changed
+70 -3

No files matched your search

+3 -2
View File
@@ -6,5 +6,6 @@ TG_PHONE=your_telegram_phone_number
APP_ID=your_app_id
APP_HASH=your_app_hash
SOURCE_GROUP_ID=-123456789
TARGET_GROUP_ID=-123456789
# Group chat id is without -100
SOURCE_CHAT_ID=-123456789
TARGET_CHAT_ID=-123456789
+2
View File
@@ -1,4 +1,6 @@
.claude
.idea
session
+1 -1
View File
@@ -59,4 +59,4 @@ The application will connect to Telegram and start forwarding messages from the
## The Original Python Version
The original Python version of this project can be found at the `feature/python` branch.
*In 2025, I rewrote this project using Go. The original Python version of this project can be found at the `feature/python` branch.*
+64
View File
@@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
"path/filepath"
@@ -71,6 +72,18 @@ func run(ctx context.Context) error {
return errors.New("no app hash")
}
sourceChatId, err := strconv.ParseInt(os.Getenv("SOURCE_CHAT_ID"), 10, 0)
if err != nil {
return errors.New("no source chat id")
}
targetChatId, err := strconv.ParseInt(os.Getenv("TARGET_CHAT_ID"), 10, 0)
if err != nil {
return errors.New("no target chat id")
}
fmt.Printf("forward from %d to %d\n", sourceChatId, targetChatId)
// Setting up session storage.
// This is needed to reuse session and not login every time.
sessionDir := filepath.Join("session", sessionFolder(phone))
@@ -181,6 +194,57 @@ func run(ctx context.Context) error {
}
fmt.Printf("%s: %s\n", p, msg.Message)
api := client.API()
targetPeer, err := storage.FindPeer(ctx, peerDB, &tg.PeerChat{ChatID: targetChatId})
_, err = api.MessagesForwardMessages(ctx, &tg.MessagesForwardMessagesRequest{
ToPeer: targetPeer.AsInputPeer(),
FromPeer: p.AsInputPeer(),
ID: []int{msg.ID},
RandomID: []int64{rand.Int63()},
})
if err != nil {
fmt.Printf("%s\n", err)
}
return err
})
dispatcher.OnNewChannelMessage(func(ctx context.Context, e tg.Entities, u *tg.UpdateNewChannelMessage) error {
msg, ok := u.Message.(*tg.Message)
if !ok {
return nil
}
if msg.Out {
// Outgoing message.
return nil
}
// Use PeerID to find peer because *Short updates does not contain any entities, so it necessary to
// store some entities.
//
// Storage can be filled using PeerCollector (i.e. fetching all dialogs first).
p, err := storage.FindPeer(ctx, peerDB, msg.GetPeerID())
if err != nil {
return err
}
fmt.Printf("%s: %s\n", p, msg.Message)
api := client.API()
targetPeer, err := storage.FindPeer(ctx, peerDB, &tg.PeerChat{ChatID: targetChatId})
_, err = api.MessagesForwardMessages(ctx, &tg.MessagesForwardMessagesRequest{
ToPeer: targetPeer.AsInputPeer(),
FromPeer: p.AsInputPeer(),
ID: []int{msg.ID},
RandomID: []int64{rand.Int63()},
})
if err != nil {
fmt.Printf("%s\n", err)
}
return nil
})