Skip to content

Commit

Permalink
add ignore accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
DesSolo committed Feb 4, 2021
1 parent f463efd commit 3ec3caa
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 18 deletions.
11 changes: 10 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"flag"
"fmt"
"log"
"os"
"time"
)

var configFile string

var version string

// GetTransports ...
func GetTransports(chats map[string]config.Chat) ([]entities.Transport, error) {
var transports []entities.Transport
Expand All @@ -24,7 +27,7 @@ func GetTransports(chats map[string]config.Chat) ([]entities.Transport, error) {

switch chat.Type {
case "telegram":
tg := transport.NewTelegram(chat.Name, chat.Token, chat.ChatID)
tg := transport.NewTelegram(chat.Name, chat.Token, chat.ChatID, chat.IgnoreAccounts)
transports = append(transports, tg)

default:
Expand All @@ -43,8 +46,14 @@ func GetTransports(chats map[string]config.Chat) ([]entities.Transport, error) {

func main() {
flag.StringVar(&configFile, "c", "config.toml", "config file path")
showVer := flag.Bool("v", false, "show current version")
flag.Parse()

if *showVer {
fmt.Printf("chat transport version: %s\n", version)
os.Exit(0)
}

conf, err := config.NewConfig(configFile)
if err != nil {
log.Fatalf("fault read config file err: \"%s\"", err)
Expand Down
2 changes: 2 additions & 0 deletions example/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ update_interval = 10
chat_id = "100"
# token: telegram token
token = "secret"
# ignore_accounts: list bad users
ignore_accounts = ["bad_user_1", "bad_user_2"]

# Destanation chats (target)
[dst]
Expand Down
9 changes: 5 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

// Chat ...
type Chat struct {
Name string
Type string
ChatID string `toml:"chat_id"`
Token string
Name string
Type string
ChatID string `toml:"chat_id"`
Token string
IgnoreAccounts []string `toml:"ignore_accounts"`
}

// Config ...
Expand Down
4 changes: 4 additions & 0 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (d *Daemon) resend(tr entities.Transport) {

for _, dt := range d.Dst {
for _, msg := range messages {
if msg.Chat.ID == dt.GetChatID() {
continue
}

if err := dt.SendMessage(msg); err != nil {
log.Printf("fault send message src_chat: \"%s\" dst_chat: \"%s\" err: \"%s\"", tr.GetName(), dt.GetName(), err)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ type Author struct {
Username string
}

// Chat ...
type Chat struct {
ID string
}

// Message ..
type Message struct {
Chat Chat
Author Author
Text string
}

// Transport ...
type Transport interface {
GetName() string
GetChatID() string
Validate() error
GetNewMessages() ([]*Message, error)
SendMessage(*Message) error
Expand Down
53 changes: 40 additions & 13 deletions internal/transport/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,22 @@ type messageData struct {

// Telegram ...
type Telegram struct {
name string
token string
chatID string
lastUpdateID int
client *http.Client
name string
token string
chatID string
ignoreAccounts []string
lastUpdateID int
client *http.Client
}

// NewTelegram ...
func NewTelegram(name, token, chatID string) *Telegram {
func NewTelegram(name, token, chatID string, ignoreAccounts []string) *Telegram {
return &Telegram{
name: name,
token: token,
chatID: chatID,
client: &http.Client{},
name: name,
token: token,
chatID: chatID,
ignoreAccounts: ignoreAccounts,
client: &http.Client{},
}
}

Expand All @@ -63,6 +65,11 @@ func (t *Telegram) GetName() string {
return t.name
}

// GetChatID ...
func (t *Telegram) GetChatID() string {
return t.chatID
}

// Validate ...
func (t *Telegram) Validate() error {
if t.token == "" {
Expand All @@ -83,7 +90,7 @@ func (t *Telegram) newRequest(method, uri string, body io.Reader) (*http.Request
// https://core.telegram.org/bots/api#getupdates
func (t *Telegram) getUpdates(offset int) ([]Update, error) {
var payload = bytes.NewBufferString(
fmt.Sprintf("{\"offset\": %d}", offset + 1),
fmt.Sprintf("{\"offset\": %d}", offset+1),
)
req, err := t.newRequest("GET", "/getUpdates", payload)
if err != nil {
Expand Down Expand Up @@ -118,6 +125,16 @@ func (t *Telegram) getUpdates(offset int) ([]Update, error) {
return data.Updates, nil
}

func (t *Telegram) isIgnoreUser(username string) bool {
for _, ia := range t.ignoreAccounts {
if ia == username {
return true
}
}

return false
}

// GetNewMessages ...
func (t *Telegram) GetNewMessages() ([]*entities.Message, error) {
updates, err := t.getUpdates(t.lastUpdateID)
Expand All @@ -127,21 +144,31 @@ func (t *Telegram) GetNewMessages() ([]*entities.Message, error) {

var messages []*entities.Message
for _, upd := range updates {

t.lastUpdateID = upd.UpdateID

if upd.Message.Text == "" {
continue
}

if strconv.Itoa(upd.Message.Chat.ID) != t.chatID {
chatID := strconv.Itoa(upd.Message.Chat.ID)

if chatID != t.chatID {
continue
}

t.lastUpdateID = upd.UpdateID
if t.isIgnoreUser(upd.Message.From.Username) {
continue
}

msg := entities.Message{
Author: entities.Author{
Username: upd.Message.From.Username,
},
Text: upd.Message.Text,
Chat: entities.Chat{
ID: chatID,
},
}
messages = append(messages, &msg)
}
Expand Down

0 comments on commit 3ec3caa

Please sign in to comment.