-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
145 lines (119 loc) · 3.31 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"github.com/fatih/color"
"golang.org/x/net/proxy"
"github.com/MakeGolangGreat/archive-go"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
)
var botToken string
var telegraphToken string
var socks5 string
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func errHandler(msg string, err error) {
if err != nil {
fmt.Printf("%s - %s\n", msg, err)
os.Exit(1)
}
}
func main() {
if IsHeroku() {
botToken = os.Getenv("ARCHIVE_BOT_TOKEN")
socks5 = os.Getenv("SOCKS5")
telegraphToken = os.Getenv("ARCHIVE_TELEGRAPH_TOKEN")
}
fmt.Println("len(botToken) ", len(botToken))
// 如果没有从参数重获取到botToken,说明程序运行在本地,那么从配置文件中读取即可。
if botToken == "" {
readConfig()
}
start()
}
// 从配置中读取配置
func readConfig() {
file, err := os.OpenFile("./config.json", os.O_RDWR|os.O_CREATE, 0766) //打开或创建文件,设置默认权限
errHandler("读取配置失败", err)
defer file.Close()
var conf config
err2 := json.NewDecoder(file).Decode(&conf)
errHandler("解码配置失败", err2)
botToken = conf.BotToken
telegraphToken = conf.TelegraphToken
socks5 = conf.Socks5
color.Green("读取配置成功,botToken is: %s\n telegraphToken is %s\nsocks5 is %s", botToken, telegraphToken, socks5)
}
// 启动Telegram Bot
func start() {
var bot *tgbot.BotAPI
var err error
// 如果不需要代理(比如跑在Github Action上)
if socks5 == "" {
bot, err = tgbot.NewBotAPI(botToken)
fmt.Println("没有经过代理")
} else {
// 跑在本地就需要代理
client := createProxyClient()
bot, err = tgbot.NewBotAPIWithClient(botToken, client)
}
errHandler("初始化bot失败", err)
log.Printf("Authorized on account %s", bot.Self.UserName)
bot.Debug = false
u := tgbot.NewUpdate(0)
u.Timeout = 60
// 被取回的新消息数量(1-100)
u.Limit = 100
updates, err := bot.GetUpdatesChan(u)
replyMessage := ""
// 持续监测Bot收到的消息
for updateObj := range updates {
// 收到消息后,并行处理
go func(update tgbot.Update) {
if update.Message == nil {
return
}
for _, entity := range *update.Message.Entities {
if entity.Type == "text_link" && entity.URL != "" {
update.Message.Text += " " + entity.URL + " "
}
}
updateText := update.Message.Text
link, saveError := archive.Save(updateText, telegraphToken, attachInfo)
if saveError != nil {
replyMessage = "文章保存出错:" + saveError.Error()
} else {
// 不知道Telegraph的换行符是什么,所以这里处理了先。
replyMessage = link + `
文章由 @beifenbot 备份
代码开源:` + projectLink
}
msg := tgbot.NewMessage(update.Message.Chat.ID, replyMessage)
bot.Send(msg)
}(updateObj)
}
}
// 配置翻墙用的Client
func createProxyClient() *http.Client {
client := &http.Client{}
tgProxyURL, err := url.Parse(socks5)
errHandler("解析socks5失败", err)
tgDialer, err := proxy.FromURL(tgProxyURL, proxy.Direct)
if err != nil {
log.Printf("Failed to obtain proxy dialer: %s\n", err)
}
tgTransport := &http.Transport{
Dial: tgDialer.Dial,
}
client.Transport = tgTransport
return client
}
// IsHeroku func
func IsHeroku() bool {
return os.Getenv("WHERE") == "heroku"
}