func (s *Search) Run() { if !s.isAuthed() { s.sendQuestion() return } if len(s.Args) > 1 { result := []string{} for _, v := range conf.Groups { arg := strings.ToLower(s.Args[1]) lower := strings.ToLower(v.GroupName) if strings.Contains(lower, arg) { if v.GroupURL != "" { result = append(result, helper.ToMarkdownLink(v.GroupName, v.GroupURL)) } else { result = append(result, v.GroupName) } } } if len(result) != 0 { s.NewMessage(s.Message.From.ID, strings.Join(result, "\n")). MarkdownMode().DisableWebPagePreview().Send() } else { s.NewMessage(s.Message.From.ID, "搜索大失败喵(/ ̄ˇ ̄)/").Send() } } }
func (g *Google) G(query string) string { query = url.QueryEscape(query) retry := 0 Req: res, err := goreq.Request{ Uri: fmt.Sprintf("http://ajax.googleapis.com/"+ "ajax/services/search/web?v=1.0&rsz=7&q=%s", query), Timeout: 17 * time.Second, }.Do() if err != nil { if retry < 2 { retry++ goto Req } else { log.Println("Google Timeout!") return "群组娘连接母舰失败,请稍后重试" } } var google struct { ResponseData struct { Results []struct { URL string TitleNoFormatting string } } } err = res.Body.FromJsonTo(&google) if err != nil { return "转换失败,母舰大概是快没油了Orz" } var buf bytes.Buffer for _, item := range google.ResponseData.Results { u, _ := url.QueryUnescape(item.URL) t, _ := url.QueryUnescape(item.TitleNoFormatting) buf.WriteString(helper.ToMarkdownLink(t, u) + "\n") } return buf.String() }
func (d *Default) Run() { if d.FromPrivate { switch d.getStatus() { case "auth": d.auth(d.Message.Text) case "broadcast": d.bc(d.Message.Text) d.setStatus("") case "trans": result := d.translator(d.Message.Text) d.NewMessage(d.ChatID, result).Send() d.setStatus("") default: if conf.CategoriesSet.Has(d.Message.Text) { // custom keyboard reply if !d.isAuthed() { d.sendQuestion() return } groups := conf.List2SliceInConf(d.Message.Text) result := make([]string, len(groups)) for k, v := range groups { reg := regexp.MustCompile("^(.+) (http(s)?://(.*))$") strs := reg.FindAllStringSubmatch(v, -1) if !reg.MatchString(v) { result[k] = v } if len(strs) > 0 { result[k] = helper.ToMarkdownLink(strs[0][1], strs[0][2]) } } msgContent := strings.Join(result, "\n") msgContent = strings.Replace(msgContent, "\\n", "", -1) d.NewMessage(d.ChatID, msgContent). MarkdownMode().DisableWebPagePreview().Send() } else { if len(d.Args) > 0 { d.turing(d.Message.Text) return } photo := d.Message.Photo if len(photo) > 0 { go d.NewChatAction(d.ChatID).UploadPhoto().Send() fileID := photo[len(photo)-1].FileID link, _ := d.GetLink(fileID) path := helper.Downloader(link, fileID) mime := helper.FileMime(path) size := helper.FileSize(path) bar := helper.BarCode(path) vcn := helper.Vim_cn_Uploader(path) os.Remove(path) s := fmt.Sprintf("%s %s\n%s\n%s", mime, size, vcn, bar) d.NewMessage(d.ChatID, s). DisableWebPagePreview(). ReplyToMessageID(d.Message.MessageID).Send() return } } } } }