Exemplo n.º 1
0
def sendnews(news_title=[], news_url=[], type=1):

    # author: Fangwen Li time: 2020/06/15
    """
    input:
    new_title: the list of the title of the news
    news_url:  the list of the link of the news
    type: 1 for positive news and 0 for negative news

    """
    from dingtalkchatbot.chatbot import DingtalkChatbot, CardItem
    new_webhook = 'https://oapi.dingtalk.com/robot/send?access_token=bb1fda685c28f7f70c36f114715488b70d9e4e427e9690e3702439958e7f7d0c'
    secret = 'SEC750b06eda22414656e9983c44538812c0d970a9a4d8c43cf8b4e25c9b1ee4963'  # 创建机器人时钉钉设置页面有提供
    dingding = DingtalkChatbot(new_webhook, secret=secret)
    positive_pic = 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3792737622,1884031801&fm=15&gp=0.jpg'
    negative_pic = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592241074187&di=fd70398ba4da7c7ce27634c1a1b7d8af&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170615%2F69f8cc42c3514842bce29182ef5beeb7_th.jpg'
    dingding.send_text(msg='现在推送今天新闻!')

    if type == 1:
        dingding.send_text(msg='以下是积极类型新闻:')
        cards = []
        for title, url in zip(news_title, news_url):
            card = CardItem(title=title, url=url, pic_url=positive_pic)
            cards = cards + [card]
        dingding.send_feed_card(cards)

    if type == 0:
        dingding.send_text(msg='以下是消极类型新闻:')
        cards = []
        for title, url in zip(news_title, news_url):
            card = CardItem(title=title, url=url, pic_url=negative_pic)
            cards = cards + [card]
        dingding.send_feed_card(cards)
Exemplo n.º 2
0
 def test_send_actioncard(self):
     """测试发送整体跳转ActionCard消息功能(CardItem新API)"""
     btns1 = [CardItem(title="查看详情", url="https://www.dingtalk.com/")]
     actioncard1 = ActionCard(
         title='万万没想到,竟然...',
         text=
         '![markdown](http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) \n### 故事是这样子的...',
         btns=btns1,
         btn_orientation=1,
         hide_avatar=1)
     result = self.xiaoding.send_action_card(actioncard1)
     self.assertEqual(result['errcode'], 0)
     """测试发送单独跳转ActionCard消息功能"""
     btns2 = [
         CardItem(title="支持", url="https://www.dingtalk.com/"),
         CardItem(
             title="反对",
             url="http://www.back china.com/news/2018/01/11/537468.html")
     ]
     actioncard2 = ActionCard(
         title='万万没想到,竟然...',
         text=
         '![markdown](http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) \n### 故事是这样子的...',
         btns=btns2,
         btn_orientation=1,
         hide_avatar=1)
     result = self.xiaoding.send_action_card(actioncard2)
     self.assertEqual(result['errcode'], 0)
Exemplo n.º 3
0
def send_msg(webhook, secret, msg):
    """
    :param webhook: 钉钉机器人webhook
    :param secret: 钉钉机器人secret
    :msg 消息对象
    """
    
    # 初始化机器人小丁
    xiaoding = DingtalkChatbot(webhook, secret=secret)

    # 构建消息体
    btns = [
        CardItem(
            title=msg["btn_label"][0]['title'],
            url=msg["btn_label"][0]['url'],
        ),
        CardItem(
            title=msg["btn_label"][1]['title'],
            url=msg["btn_label"][1]['url'],
        )
    ]
    actioncard = ActionCard(
        title=msg["title"],
        text=msg["content"],
        btns=btns,
        btn_orientation=1,
        hide_avatar=0,
    )

    # 发送卡片消息
    xiaoding.send_action_card(actioncard)
Exemplo n.º 4
0
    def parse_rss(self):
        # self.remove_old_history()
        rss_list = Rss.select()
        rss_card_dict = {}
        post_url_list = [
            rss_history.url for rss_history in History.select().where(
                History.publish_at == datetime.today().strftime("%Y-%m-%d"))
        ]
        for rss in rss_list:
            rss_history_list = []
            card_list = [
                CardItem(title=rss.title, url=rss.url, pic_url=rss.cover)
            ]
            feed = feedparser.parse(rss.feed)
            for entry in feed.entries:
                if entry.link not in post_url_list and self.is_today(entry):
                    card_list.append(
                        CardItem(
                            title=f'{entry.title}',
                            url=entry.link,
                            pic_url=
                            'https://ftp.bmp.ovh/imgs/2020/07/6cdb9f606677c9e3.jpg'
                        ))
                    rss_history_list.append(History(url=entry.link))

            if len(card_list) > 1:
                rss_card_dict[rss.title] = card_list
                with db.atomic():
                    History.bulk_create(rss_history_list, batch_size=10)

        return rss_card_dict
Exemplo n.º 5
0
 def test_send_feedcard(self):
     """测试发送FeedCard类型消息功能(CardItem新API)"""
     carditem1 = CardItem(title="氧气美女", url="https://www.dingtalk.com/", pic_url="https://unzippedtv.com/wp-content/uploads/sites/28/2016/02/asian.jpg")
     carditem2 = CardItem(title="氧眼美女", url="https://www.dingtalk.com/", pic_url="https://unzippedtv.com/wp-content/uploads/sites/28/2016/02/asian.jpg")
     carditem3 = CardItem(title="氧神美女", url="https://www.dingtalk.com/", pic_url="https://unzippedtv.com/wp-content/uploads/sites/28/2016/02/asian.jpg")
     cards = [carditem1, carditem2, carditem3]
     result = self.xiaoding.send_feed_card(cards)
     self.assertEqual(result['errcode'], 0)
Exemplo n.º 6
0
 def test_send_feedcard(self):
     """测试发送FeedCard类型消息功能(CardItem新API)"""
     carditem1 = CardItem(
         title="氧气美女",
         url="https://www.dingtalk.com/",
         pic_url=
         "http://pic1.win4000.com/wallpaper/2020-03-11/5e68b0557f3a6.jpg")
     carditem2 = CardItem(
         title="氧眼美女",
         url="https://www.dingtalk.com/",
         pic_url=
         "http://pic1.win4000.com/wallpaper/2020-03-11/5e68b0557f3a6.jpg")
     carditem3 = CardItem(
         title="氧神美女",
         url="https://www.dingtalk.com/",
         pic_url=
         "http://pic1.win4000.com/wallpaper/2020-03-11/5e68b0557f3a6.jpg")
     cards = [carditem1, carditem2, carditem3]
     result = self.xiaoding.send_feed_card(cards)
     self.assertEqual(result['errcode'], 0)
Exemplo n.º 7
0
def sendnews(news_title=[], news_url=[], type=1):

    # author: Fangwen Li time: 2020/06/15
    """
    input:
    new_title: the list of the title of the news
    news_url:  the list of the link of the news
    type: 1 for positive news and 0 for negative news

    """

    from dingtalkchatbot.chatbot import DingtalkChatbot, CardItem


    new_webhook = 'https://oapi.dingtalk.com/robot/send?access_token=fdc8c721927a1d6a640d7050d318afa880558c69bb63888ae6d989db213cc3b1'
    secret = 'SECed9cb888287a1a6462378752c4e3a33a9d638a322e872fb989dc113a3072e83d'  # 创建机器人时钉钉设置页面有提供
    dingding = DingtalkChatbot(new_webhook, secret=secret)
    positive_pic = 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3792737622,1884031801&fm=15&gp=0.jpg'
    negative_pic = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592241074187&di=fd70398ba4da7c7ce27634c1a1b7d8af&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170615%2F69f8cc42c3514842bce29182ef5beeb7_th.jpg'
    dingding.send_text(msg='现在推送今天新闻!')

    if type == 1:
        dingding.send_text(msg='以下是积极类型新闻:')
        cards = []
        for title, url in zip(news_title, news_url):
            card = CardItem(title=title, url=url, pic_url=positive_pic)
            cards = cards + [card]
        dingding.send_feed_card(cards)

    if type == 2:
        dingding.send_text(msg='以下是消极类型新闻:')
        cards = []
        for title, url in zip(news_title, news_url):
            card = CardItem(title=title, url=url, pic_url=negative_pic)
            cards = cards + [card]
        dingding.send_feed_card(cards)
Exemplo n.º 8
0
    # markdown
    # 1、提醒所有人
    xiaoding.send_markdown(title='氧气文字', text='#### 广州天气\n'
                           '> 9度,西北风1级,空气良89,相对温度73%\n\n'
                           '> ![美景](http://www.sinaimg.cn/dy/slidenews/5_img/2013_28/453_28488_469248.jpg)\n'
                           '> ###### 10点20分发布 [天气](https://www.seniverse.com/) \n',
                           is_at_all=True)
    # 2、提醒指定手机用户,并在text内容中自定义”@用户“的位置
    xiaoding.send_markdown(title='氧气文字', text='#### 广州天气 @18825166XXX\n'
                           '> 9度,西北风1级,空气良89,相对温度73%\n\n'
                           '> ![美景](http://www.sinaimg.cn/dy/slidenews/5_img/2013_28/453_28488_469248.jpg)\n'
                           '> ###### 10点20分发布 [天气信息](https://www.seniverse.com/)\n',
                           at_mobiles=at_mobiles, is_auto_at=False)

    # 整体跳转ActionCard
    btns1 = [CardItem(title="查看详情", url="https://www.dingtalk.com/")]
    actioncard1 = ActionCard(title='万万没想到,竟然...',
                             text='![markdown](http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) \n### 故事是这样子的...',
                             btns=btns1,
                             btn_orientation=1,
                             hide_avatar=1)
    xiaoding.send_action_card(actioncard1)

    # 单独跳转ActionCard
    # 1、两个按钮选择
    btns2 = [CardItem(title="支持", url="https://www.dingtalk.com/"), CardItem(title="反对", url="https://www.dingtalk.com/")]
    actioncard2 = ActionCard(title='万万没想到,竟然...',
                             text='![markdown](http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) \n### 故事是这样子的...',
                             btns=btns2,
                             btn_orientation=1,
                             hide_avatar=1)
Exemplo n.º 9
0
    # markdown
    # 1、提醒所有人
    xiaoding.send_markdown(title='氧气文字', text='#### 广州天气\n'
                           '> 9度,西北风1级,空气良89,相对温度73%\n\n'
                           '> ![美景](http://www.sinaimg.cn/dy/slidenews/5_img/2013_28/453_28488_469248.jpg)\n'
                           '> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n',
                           is_at_all=True)
    # 2、提醒指定手机用户,需要在text参数中@用户
    xiaoding.send_markdown(title='氧气文字', text='#### 广州天气\n'
                           '> 9度,西北风1级,空气良89,相对温度73%\n\n'
                           '> ![美景](http://www.sinaimg.cn/dy/slidenews/5_img/2013_28/453_28488_469248.jpg)\n'
                           '> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n',
                           at_mobiles=at_mobiles)

    # 整体跳转ActionCard
    btns1 = [CardItem(title="查看详情", url="https://www.dingtalk.com/")]
    actioncard1 = ActionCard(title='万万没想到,竟然...',
                             text='![markdown](http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) \n### 故事是这样子的...',
                             btns=btns1,
                             btn_orientation=1,
                             hide_avatar=1)
    xiaoding.send_action_card(actioncard1)

    # 单独跳转ActionCard
    # 1、两个按钮选择
    btns2 = [CardItem(title="支持", url="https://www.dingtalk.com/"), CardItem(title="反对", url="https://www.dingtalk.com/")]
    actioncard2 = ActionCard(title='万万没想到,竟然...',
                             text='![markdown](http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) \n### 故事是这样子的...',
                             btns=btns2,
                             btn_orientation=1,
                             hide_avatar=1)
Exemplo n.º 10
0
def notification(model):
    link = "http://www.qihuofy.com/article?id=%s" % model.article_id
    button = [CardItem(title=model.submit_date, url=link)]
    content = noti_content(model)
    action = ActionCard(title='您有新的文章评论', text= content, btns=button, btn_orientation=1, hide_avatar=1)
    dt_bot.send_action_card(action)