Golang80 Line Code Nail Group robot Subscribe Baidu News

Source: Internet
Author: User
Tags webhook
This is a creation in Article, where the information may have evolved or changed.

1. Information

1.1. Third Party Package

    • Github.com/puerkitobio/goquery
    • Github.com/go-redis/redis
    • Beego Framework Timing Task Pack

1.2 Interface

    • Baidu News: US drama key Words
    • Nail Group Bot Document

2. Initialize Project variables

  Package Mainimport ("FMT" "Log" "Github.com/puerkitobio/goquery" "Github.com/go-redis/redis" "N Et/http "" bytes "" Github.com/astaxie/beego/toolbox ") var (redisclient *redis. Client//redis Cache//Nail swarm robot webhook address Dingdingurl = "Https://oapi.dingtalk.com/robot/send?access_token=dingding_ta Lk_group_bot_webhook_token "//Baidu news search keyword URL 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 "//crawled to the Baidu news Redis key newspost =" News_post "//Sent Baidu news Redis key newslist =" Iot_news "//stored in the Baidu News Redis key)//instantiation of Redis cache func init () {redisclient = Redi S.newclient (&redis.                            options{Addr: "127.0.0.1:6379", Password: "ddfrfgtre4353252",//Redis Password db:0, Redis database ID})}  

On the Robot Management page, select the "Custom" robot, enter the robot name and select the group to send the message to. If necessary, you can set an avatar for the robot. Click "Finish Add".

Click "Copy" button, you can get the Webhook address of this robot, assign value todingdingURl

3func newBot

3.1 Extracting useful information using Goquery and page element selector syntax

func newsBot() error {    // 获取html doc    doc, err := goquery.NewDocument(baiduNewsUrlWithSearchKeyword)    if err != nil {        return nil    }        //使用redis pipelien 减少redis连接数    pipe := redisClient.Pipeline()    // 使用selector xpath 语法获取有用信息        // 储存新闻到redis中 newsList        // 储存新闻ur到redis-set 建newfeed 为以后是用sdiff 找出没有发送的新闻    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)    })        //执行redis pipeline    pipe.Exec()

3.2 Excluded to send the news, stitching markdown strings

        //使用redis sdiff找出没有发送的新闻url    unSendNewsUrls := redisClient.SDiff(newsFeed, newsPost).Val()        //新闻按dingding文档markdonw 规范拼接            content := ""    for _, url := range unSendNewsUrls {        md := redisClient.HGet(newsList, url).Val()        content = content + " \n " + md                //记录已发送新闻的url地址        pipe.SAdd(newsPost, url)    }    pipe.Exec()

3.3 Call Pin Group Robot interface

        //如果有未发送新闻 请求钉钉webhook    if content != "" {        formt := `        {            "msgtype": "markdown",            "markdown": {                "title":"IOT每日新闻",                "text": "%s"            }        }`        body := fmt.Sprintf(formt, content)        jsonValue := []byte(body)                //发送消息到钉钉群使用webhook                //xiang见钉钉文档 https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1        resp, err := http.Post(dingdingURL, "application/json", bytes.NewBuffer(jsonValue))        if (err != nil) {            return err        }        log.Println(resp)    }    return nil}

func newBotfunction complete

4. Set up Scheduled Tasks

func main() {        //销毁redisClient    defer redisClient.Close()    //创建定时任务        //每天 8点 13点 18点 自动执行爬虫和机器人        //     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 {}}
spec format is reference

5 Compiling and running

Final complete codemain.go

go build main.gonohup ./main &

Final effect

6 Last

    • Welcome to star my Golang-base64captcha Open source project
    • If in doubt welcome email:dejavuzhou@qq.com
    • or comment GitHub gist
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.