-
Notifications
You must be signed in to change notification settings - Fork 0
/
telego.go
129 lines (103 loc) · 2.62 KB
/
telego.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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/iambighead/goutils/logger"
"github.com/iambighead/goutils/utils"
"github.com/iambighead/telego/internal/config"
"github.com/mymmrac/telego"
tu "github.com/mymmrac/telego/telegoutil"
)
const VERSION = "v0.0.3"
// --------------------------------
var main_logger logger.Logger
var master_config config.MasterConfig
// --------------------------
func init() {
logger.Init("telego.log", "TELEGO_LOG_LEVEL")
main_logger = logger.NewLogger("main")
ex, err := os.Executable()
if err != nil {
main_logger.Error("unable to get executable path")
os.Exit(1)
}
{
var err error
config_path := filepath.Join(filepath.Dir(ex), "config.yaml")
master_config, err = config.ReadConfig(config_path)
if err != nil {
main_logger.Error(fmt.Sprintf("failed to read config: %v", err))
}
}
}
func processFile(file string, token string, chat_id int64) error {
// Create bot and enable debugging info
// Note: Please keep in mind that default logger may expose sensitive information,
// use in development only
// (more on configuration in examples/configuration/main.go)
bot, err := telego.NewBot(token)
if err != nil {
return err
}
msg, err := os.ReadFile(file)
if err != nil {
return err
}
// Call method sendMessage.
// Send a message to sender with the same text (echo bot).
// (https://core.telegram.org/bots/api#sendmessage)
bot.SendMessage(
tu.Message(
tu.ID(chat_id),
string(msg),
),
)
return nil
}
func cleanupFile(file string) error {
err := os.Remove(file)
if err != nil {
return err
}
return nil
}
func monitorFolder(folder string, tele config.TeleConfig) {
// chatID := master_config.TeleConfigs[0].ChatId
for {
main_logger.Debug(fmt.Sprintf("checking folder %s", folder))
filelist, err := utils.ReadFilelist(folder)
if err != nil {
main_logger.Error(err.Error())
continue
}
for _, f := range filelist {
{
err := processFile(f, tele.Token, tele.ChatId)
if err != nil {
main_logger.Error(fmt.Sprintf("failed to processe %s: %s", f, err.Error()))
} else {
main_logger.Info(fmt.Sprintf("processed %s", f))
}
}
{
err := cleanupFile(f)
if err != nil {
main_logger.Error(fmt.Sprintf("failed to remove %s: %s", f, err.Error()))
}
}
}
time.Sleep(2 * time.Duration(time.Second))
}
}
// --------------------------
func main() {
main_logger.Info(fmt.Sprintf("Telego started. Version %s", VERSION))
for _, sender := range master_config.Senders {
go monitorFolder(sender.Folder, *sender.TeleConfig)
}
for {
time.Sleep(60 * time.Duration(time.Second))
}
}