-
Notifications
You must be signed in to change notification settings - Fork 15
/
google.go
112 lines (98 loc) · 2.75 KB
/
google.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
package jarvisbot
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/tucnak/telebot"
)
const googleSearchAPI = "https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q="
func (j *JarvisBot) GoogleSearch(msg *message) {
if len(msg.Args) == 0 {
so := &telebot.SendOptions{ReplyTo: *msg.Message, ReplyMarkup: telebot.ReplyMarkup{ForceReply: true, Selective: true}}
j.SendMessage(msg.Chat, "/google: Do a Google search\nHere are some commands to try: \n* best chicken rice\n* mee siam mai hum\n\n\U0001F4A1 You could also use this format for faster results:\n/g mee siam mai hum", so)
return
}
j.bot.SendChatAction(msg.Chat, telebot.Typing)
rawQuery := ""
for _, v := range msg.Args {
rawQuery = rawQuery + v + " "
}
var key, cx string
key = j.keys.CustomSearchAPIKey
if key == "" {
tmp := <-j.googleKeyChan
key, cx = processKeyFromChan(tmp)
if key == "" || cx == "" {
j.log.Printf("error retrieving key from chan")
return
}
} else {
cx = j.keys.CustomSearchID
if cx == "" {
j.log.Printf("error retrieving custom_search_id")
return
}
}
searchURL := fmt.Sprintf(googleSearchAPI, key, cx)
rawQuery = strings.TrimSpace(rawQuery)
q := url.QueryEscape(rawQuery)
resp, err := http.Get(searchURL + q)
if err != nil {
j.log.Printf("failure retrieving search results from Google for query '%s': %s", q, err)
return
}
jsonBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
j.log.Printf("failure reading json results from Google search for query '%s': %s", q, err)
return
}
searchRes := struct {
Items []searchResult `json:"items"`
}{}
err = json.Unmarshal(jsonBody, &searchRes)
if err != nil {
j.log.Printf("failure unmarshalling json for search query '%s': %s", q, err)
return
}
resMsg := ""
if len(searchRes.Items) > 0 {
count := 0
for _, v := range searchRes.Items {
u, err := v.getUrl()
if err == nil {
resMsg = resMsg + fmt.Sprintf("%s - %s\n", u, v.Title)
} else {
continue
}
count++
if count >= 5 {
break
}
}
j.SendMessage(msg.Chat, resMsg, nil)
} else {
var errorRes struct {
Error struct {
Code int `json:"code"`
} `json:"error"`
}
err = json.Unmarshal(jsonBody, &errorRes)
if err == nil && errorRes.Error.Code == 403 {
j.SendMessage(msg.Chat, "Hmm, something went wrong", &telebot.SendOptions{ReplyTo: *msg.Message})
j.log.Printf("[Google search limit] Search limit hit for key %s, with id %s", key, cx)
} else {
j.SendMessage(msg.Chat, "My search returned nothing. \U0001F622", &telebot.SendOptions{ReplyTo: *msg.Message})
}
}
}
type searchResult struct {
Title string `json:"title"`
URL string `json:"link"`
}
func (s *searchResult) getUrl() (*url.URL, error) {
return url.Parse(s.URL)
}