-
Notifications
You must be signed in to change notification settings - Fork 1
/
BotApiService.go
67 lines (57 loc) · 1.54 KB
/
BotApiService.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
package main
import (
"golang.org/x/net/proxy"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
)
const TelegramUrl = "https://api.telegram.org/"
const JiraUrl = "https://jira.proitr.ru/browse/"
const TelegramUrlApiGetMe = "/getMe"
const TelegramUrlApiGetUpdates = "/getUpdates"
type BotApiService struct {
botId string
proxyUser string
proxyPass string
proxyIp string
chatId int
}
func (settings BotApiService) sendMessageToChat(message string) string {
sendUrl := "/sendMessage?chat_id=" + strconv.Itoa(settings.chatId) + "&text=" + message
return sendRequest(settings, sendUrl)
}
func (settings BotApiService) getMe() string {
return sendRequest(settings, TelegramUrlApiGetMe)
}
func (settings BotApiService) getUpdates() string {
return sendRequest(settings, TelegramUrlApiGetUpdates)
}
func sendRequest(settings BotApiService, method string) string {
var auth = &proxy.Auth{User: settings.proxyUser, Password: settings.proxyPass}
dialSocksProxy, err := proxy.SOCKS5("tcp", settings.proxyIp, auth, proxy.Direct)
if err != nil {
log.Println("Error connecting to proxy:", err)
}
tr := &http.Transport{
Dial: dialSocksProxy.Dial,
MaxIdleConns: 20,
MaxIdleConnsPerHost: 20,
}
// Create client
client := &http.Client{
Transport: tr,
Timeout: 60 * time.Minute,
}
resp, err := client.Get(TelegramUrl + "bot" + settings.botId + method)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
return string(body)
}