コード例 #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)
コード例 #2
0
class RssRobot:
    def __init__(self):
        self.robot = DingtalkChatbot(os.environ.get("DD_WEBHOOK"),
                                     pc_slide=True,
                                     secret=os.environ.get("DD_SECRET"))

    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

    def is_today(self, entry):
        return dateparser.parse(
            entry['updated']).date() == datetime.today().date()

    def send_rss(self):
        rss_card_dict = self.parse_rss()
        for key in rss_card_dict:
            self.robot.send_feed_card(rss_card_dict[key])

    def remove_old_history(self):
        history_list = History.delete().where(
            History.publish_at < datetime.today().strftime("%Y-%m-%d"))
        history_list.execute()
コード例 #3
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)
コード例 #4
0
        title="氧气美女",
        message_url="https://www.dingtalk.com/",
        pic_url=
        "https://unzippedtv.com/wp-content/uploads/sites/28/2016/02/asian.jpg")
    feedlink2 = FeedLink(
        title="氧眼美女",
        message_url="https://www.dingtalk.com/",
        pic_url=
        "https://unzippedtv.com/wp-content/uploads/sites/28/2016/02/asian.jpg")
    feedlink3 = FeedLink(
        title="氧神美女",
        message_url="https://www.dingtalk.com/",
        pic_url=
        "https://unzippedtv.com/wp-content/uploads/sites/28/2016/02/asian.jpg")
    links = [feedlink1.get_data(), feedlink2.get_data(), feedlink3.get_data()]
    xiaoding.send_feed_card(links)

# def mini_sample():
#     webhook = 'https://oapi.dingtalk.com/robot/send?access_token=这里填写自己钉钉群自定义机器人的token'
#     at_mobiles = ['这里填写需要提醒的用户的手机号码,字符串或数字都可以']
#     headers = {'Content-Type': 'application/json; charset=utf-8'}
#     post_data = {
#         'msgtype': 'text',
#         'text': {
#             'content': '我就是小丁,小丁就是我!'
#         },
#         'at': {
#             'atMobiles': at_mobiles,
#             'isAtAll': False
#         }
#     }
コード例 #5
0
                             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)
    xiaoding.send_action_card(actioncard2)
    # 2、三个按钮选择
    btns3 = [CardItem(title="支持", url="https://www.dingtalk.com/"), CardItem(title="中立", url="https://www.dingtalk.com/"), CardItem(title="反对", url="https://www.dingtalk.com/")]
    actioncard3 = ActionCard(title='万万没想到,竟然...',
                             text='![markdown](http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) \n### 故事是这样子的...',
                             btns=btns3,
                             btn_orientation=1,
                             hide_avatar=1)
    xiaoding.send_action_card(actioncard3)

    # FeedCard类型
    card1 = CardItem(title="氧气美女", url="https://www.baidu.com/", pic_url="http://pic1.win4000.com/wallpaper/2020-03-11/5e68b0557f3a6.jpg")
    card2 = CardItem(title="氧眼美女", url="https://www.baidu.com/", pic_url="http://pic1.win4000.com/wallpaper/2020-03-11/5e68b0557f3a6.jpg")
    card3 = CardItem(title="氧神美女", url="https://www.baidu.com/", pic_url="http://pic1.win4000.com/wallpaper/2020-03-11/5e68b0557f3a6.jpg")
    cards = [card1, card2, card3]
    xiaoding.send_feed_card(cards)