Created
February 9, 2018 09:17
-
-
Save mojocn/43b47e8d97abb1e00fd19b2864f053c1 to your computer and use it in GitHub Desktop.
ding-group-bot-webhook main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/PuerkitoBio/goquery" | |
"github.com/go-redis/redis" | |
"net/http" | |
"bytes" | |
"github.com/astaxie/beego/toolbox" | |
) | |
var ( | |
redisClient *redis.Client | |
dingdingURL = "https://oapi.dingtalk.com/robot/send?access_token=tokenkkkkkxxxxxxxxxxxxxx" | |
baiduNewsUrlWithSearchKeyword = "http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=%E7%89%A9%E8%81%94%E7%BD%91" | |
) | |
const ( | |
newsFeed = "news_feed" | |
newsPost = "news_post" | |
newsList = "iot_news" | |
) | |
func init() { | |
redisClient = redis.NewClient(&redis.Options{ | |
Addr: "127.0.0.1:6379", | |
Password: "passssssword", // no password set | |
DB: 2, // use default DB | |
}) | |
} | |
func main() { | |
defer redisClient.Close() | |
//创建定时任务 | |
dingdingNewBot := toolbox.NewTask("dingding-news-bot", "0 0 8,13,18 * * *", newsBot) | |
//dingdingNewBot := toolbox.NewTask("dingding-news-bot", "0 40 */1 * * *", newsBot) | |
//err := dingdingNewBot.Run() | |
//检测定时任务 | |
// if err != nil { | |
// log.Fatal(err) | |
// } | |
//添加定时任务 | |
toolbox.AddTask("dingding-news-bot", dingdingNewBot) | |
//启动定时任务 | |
toolbox.StartTask() | |
defer toolbox.StopTask() | |
select {} | |
} | |
func newsBot() error { | |
//stories := []item{} | |
// Instantiate default collector | |
doc, err := goquery.NewDocument(baiduNewsUrlWithSearchKeyword) | |
if err != nil { | |
return nil | |
} | |
pipe := redisClient.Pipeline() | |
// Find the review items | |
doc.Find("div.result").Each(func(i int, s *goquery.Selection) { | |
// For each item found, get the band and title | |
URL, _ := s.Find("h3 > a").Attr("href") | |
Source := s.Find("p.c-author").Text() | |
Title := s.Find("h3 > a").Text() | |
markdown := fmt.Sprintf("- [%s](%s) _%s_", Title, URL, Source) | |
pipe.HSet(newsList, URL, markdown) | |
pipe.SAdd(newsFeed, URL) | |
}) | |
pipe.Exec() | |
unSendNewsUrls := redisClient.SDiff(newsFeed, newsPost).Val() | |
content := "" | |
for _, url := range unSendNewsUrls { | |
md := redisClient.HGet(newsList, url).Val() | |
content = content + " \n " + md | |
pipe.SAdd(newsPost, url) | |
} | |
pipe.Exec() | |
if content != "" { | |
formt := ` | |
{ | |
"msgtype": "markdown", | |
"markdown": { | |
"title":"IOT每日新闻", | |
"text": "%s" | |
} | |
}` | |
body := fmt.Sprintf(formt, content) | |
jsonValue := []byte(body) | |
resp, err := http.Post(dingdingURL, "application/json", bytes.NewBuffer(jsonValue)) | |
if (err != nil) { | |
return err | |
} | |
log.Println(resp) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment