-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
executable file
·258 lines (232 loc) · 6.56 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/sqeven/tgbot/tdlib"
"log"
"net/url"
"strconv"
"strings"
"time"
)
var (
client *tdlib.Client
poolOne WorkPool
)
func main() {
//初始化go协程池
poolOne.InitPool(60)
tdlib.SetLogVerbosityLevel(1)
tdlib.SetFilePath("./Logs/errors.txt")
// API
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
// PING请求
router.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "tgbot ok",
})
})
// Create new instance of client 793416-021de84fe4f1ac0361c333b0ba6198b6
client = tdlib.NewClient(tdlib.Config{
APIID: "793416",
APIHash: "021de84fe4f1ac0361c333b0ba6198b6",
SystemLanguageCode: "en",
DeviceModel: "Server",
SystemVersion: "1.0.0",
ApplicationVersion: "1.0.0",
UseMessageDatabase: false,
UseFileDatabase: false,
UseChatInfoDatabase: false,
UseTestDataCenter: false,
DatabaseDirectory: "./tdlib-db",
FileDirectory: "./tdlib-files",
IgnoreFileNames: false,
})
//client.AddProxy("127.0.0.1", 443, true, tdlib.NewProxyTypeMtproto("ee4012999756fd6eb3fafd63fd17cb3c70617a7572652e6d6963726f736f66742e636f6d"))
// Handle Ctrl+C
// ch := make(chan os.Signal, 2)
// signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
// go func() {
// <-ch
// client.DestroyInstance()
// os.Exit(1)
// }()
for {
currentState, _ := client.Authorize()
if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPhoneNumberType {
go GetProxy()
fmt.Print("Enter phone: ")
var number string
fmt.Scanln(&number)
_, err := client.SendPhoneNumber(number)
if err != nil {
fmt.Printf("Error sending phone number: %v\n", err)
}
// p, err := client.CheckAuthenticationBotToken(":AAFomEPDiMQ6hE4dpmDFkKpHrmawsvwA")
// fmt.Println(p, err)
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitCodeType {
fmt.Print("Enter code: ")
var code string
fmt.Scanln(&code)
_, err := client.SendAuthCode(code)
if err != nil {
fmt.Printf("Error sending auth code : %v\n", err)
}
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPasswordType {
fmt.Print("Enter Password: ")
var password string
fmt.Scanln(&password)
_, err := client.SendAuthPassword(password)
if err != nil {
fmt.Printf("Error sending auth password: %v\n", err)
}
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateReadyType {
fmt.Println("Authorization Ready! Let's rock")
break
}
}
go GetMSG()
go CheckProxy()
// 请求API服务
router.POST("/api/send", func(c *gin.Context) {
msg := c.DefaultPostForm("msg", "tg bot pong")
cid := c.DefaultPostForm("cid", "-1001463662639")
ChatID, _ := strconv.ParseInt(cid, 10, 64)
log.Println("ChatID", ChatID)
inputMsgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(msg, nil), true, false)
_, err := client.SendMessage(ChatID, 0, false, true, nil, inputMsgTxt)
if err != nil {
c.JSON(200, gin.H{
"code": 100,
"msg": err,
"data": cid,
})
} else {
c.JSON(200, gin.H{
"code": 0,
"msg": msg,
"data": cid,
})
}
})
go router.Run(":8008")
// rawUpdates gets all updates comming from tdlib
rawUpdates := client.GetRawUpdatesChannel(100)
for update := range rawUpdates {
// Show all updates
log.Printf("raw %s", update.Raw)
//fmt.Print("\n\n")
}
}
func GetProxy() {
// 会一直维护的
body, _, _ := HttpRequest("GET", "http://129.204.103.68:58897/v1/TGProxys", nil, nil, nil)
if body != nil {
var _jArray []interface{}
json.Unmarshal(body, &_jArray)
log.Printf("%v", _jArray)
for _, v := range _jArray {
link := v.(string)
link = strings.ReplaceAll(link, "?", "&")
val, err := url.ParseQuery(link)
if err == nil {
port, _ := strconv.Atoi(val.Get("port"))
_, err := client.AddProxy(val.Get("server"), int32(port), true, tdlib.NewProxyTypeMtproto(val.Get("secret")))
log.Printf(" AddProxy %v", err)
}
}
}
}
func CheckProxy() {
heartbeat := time.NewTicker(30 * time.Second)
pullproxy := time.NewTicker(1 * time.Hour) // 1个小时拉取一次代理列表
proxyFlag := false
pmap := make(map[int32]int)
for {
select {
case <-heartbeat.C:
log.Println("正在检测 活跃代理")
proxys, err := client.GetProxies()
if err == nil {
for _, v := range proxys.Proxies {
t, err := client.PingProxy(v.ID)
coust := 0.0
ChackFlag := false
if err != nil {
ChackFlag = true
} else {
if t.Seconds == 0 {
ChackFlag = true
}
}
if ChackFlag {
count := 0
if _, ok := pmap[v.ID]; ok {
pmap[v.ID] += 1
count = pmap[v.ID]
} else {
pmap[v.ID] = 1
}
log.Printf("Proxy %d Err %v Try %d", v.ID, err, count)
if count == 15 {
if v.IsEnabled {
proxyFlag = true
}
delete(pmap, v.ID)
client.RemoveProxy(v.ID)
}
continue
} else {
if _, ok := pmap[v.ID]; ok {
pmap[v.ID] = 0
}
coust = t.Seconds
}
l, err := client.GetProxyLink(v.ID)
if err == nil {
if proxyFlag {
client.EnableProxy(v.ID)
proxyFlag = false
}
log.Printf("Check Ok Proxy %d Ping %fs Link %s IsEnabled %v", v.ID, coust, l.Text, v.IsEnabled)
}
}
}
break
case <-pullproxy.C:
GetProxy()
break
}
}
}
func GetMSG() {
// Create an filter function which will be used to filter out unwanted tdlib messages
eventFilter := func(msg *tdlib.TdMessage) bool {
// updateMsg := (*msg).(*tdlib.UpdateNewMessage)
// // For example, we want incomming messages from user with below id:
// if updateMsg.Message.SenderUserID == 41507975 {
// return true
// }
return true
}
// Here we can add a receiver to retreive any message type we want
// We like to get UpdateNewMessage events and with a specific FilterFunc
receiver := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 5)
for newMsg := range receiver.Chan {
//fmt.Println(newMsg)
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
// We assume the message content is simple text: (should be more sophisticated for general use)
if msgText, ok := updateMsg.Message.Content.(*tdlib.MessageText); ok {
m := make(map[string]interface{})
m["ChatID"] = updateMsg.Message.ChatID
m["SenderUserID"] = updateMsg.Message.SenderUserID
m["MessageID"] = updateMsg.Message.ID
m["MsgType"] = "MessageText"
m["Content"] = msgText.Text.Text
//协程池执行lua插件
poolOne.Run(TGLuaVMRun, m)
}
}
}