国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

封裝Python腳本:使用企業(yè)微信機器人發(fā)送消息至企業(yè)微信

這篇具有很好參考價值的文章主要介紹了封裝Python腳本:使用企業(yè)微信機器人發(fā)送消息至企業(yè)微信。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

官方文檔地址:https://developer.work.weixin.qq.com/document/path/91770#%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E7%BE%A4%E6%9C%BA%E5%99%A8%E4%BA%BA

一、獲取自定義機器人webhook

可以通過如下步驟設(shè)置企業(yè)微信機器人:文章來源地址http://www.zghlxwxcb.cn/news/detail-700577.html

  1. 首先建立或者進入某個群聊
  2. 進入群聊設(shè)置頁面, 點擊“群機器人>添加”可添加一個機器人成功
  3. 添加成功后,復(fù)制并保留其webhook地址。

二、python封裝腳本

# -*- coding: utf-8 -*-
# @Time    : 2023/5/11 15:01
# @Author  : chenyinhua
# @File    : webchat_handle.py
# @Software: PyCharm
# @Desc: 企業(yè)微信機器人
import os

from requests import request
from loguru import logger
import base64
import hashlib
import re


class WechatBot:
    """
    企業(yè)微信機器人
    當(dāng)前自定義機器人支持文本(text)、markdown(markdown)、圖片(image)、圖文(news), 文件(file)五種消息類型。
    機器人的text/markdown類型消息支持在content中使用<@userid>擴展語法來@群成員
    """

    def __init__(self, webhook_url):
        """
        :param webhook_url: 機器人的WebHook_url
        """
        self.webhook_url = webhook_url
        self.headers = {
            "Content-Type": "application/json",
            "Charset": "UTF-8"
        }

    def send_text(self, content, mentioned_list=[], mentioned_mobile_list=[]):
        """
        發(fā)送文本消息
        :param content: 文本內(nèi)容,最長不超過2048個字節(jié),必須是utf8編碼
        :param mentioned_list: userid的列表,提醒群中的指定成員(@某個成員),@all表示提醒所有人,如果開發(fā)者獲取不到userid,可以使用mentioned_mobile_list
        :param mentioned_mobile_list: 手機號列表,提醒手機號對應(yīng)的群成員(@某個成員),@all表示提醒所有人
        """
        payload = {
            "msgtype": "text",
            "text": {
                "content": content,
                "mentioned_list": mentioned_list,
                "mentioned_mobile_list": mentioned_mobile_list
            }
        }
        response = request(url=self.webhook_url, method="POST", json=payload, headers=self.headers)
        if response.json().get("errcode") == 0:
            logger.debug(f"通過企業(yè)微信發(fā)送文本消息成功:{response.json()}")
            return True
        else:
            logger.error(f"通過企業(yè)微信發(fā)送文本消息失敗:{response.text}")
            return False

    def send_markdown(self, content):
        """
        發(fā)送markdown消息
        目前支持的markdown語法是如下的子集:
            1. 標題 (支持1至6級標題,注意#與文字中間要有空格)
            2. 加粗
            3. 鏈接
            4. 行內(nèi)代碼段(暫不支持跨行)
            5. 引用
            6. 字體顏色(只支持3種內(nèi)置顏色), 綠色(color="info"),灰色(color="comment"),橙紅色(color="warning")
        :param content: markdown內(nèi)容,最長不超過4096個字節(jié),必須是utf8編碼
        """
        payload = {
            "msgtype": "markdown",
            "markdown": {
                "content": content
            }
        }
        response = request(url=self.webhook_url, method="POST", json=payload, headers=self.headers)
        if response.json().get("errcode") == 0:
            logger.debug(f"通過企業(yè)微信發(fā)送md消息成功:{response.json()}")
            return True
        else:
            logger.error(f"通過企業(yè)微信發(fā)送md消息失?。?/span>{response.text}")
            return False

    def send_picture(self, image_path):
        """
        發(fā)送圖片消息
        :param image_path: 圖片的絕對路徑
        """
        with open(image_path, "rb") as f:
            image_data = f.read()
        payload = {
            "msgtype": "image",
            "image": {
                "base64": base64.b64encode(image_data).decode("utf-8"),  # # 將圖片數(shù)據(jù)轉(zhuǎn)換成Base64編碼格式
                "md5": hashlib.md5(image_data).hexdigest()  # # 計算圖片的MD5值
            }
        }
        response = request(url=self.webhook_url, method="POST", json=payload, headers=self.headers)
        if response.json().get("errcode") == 0:
            logger.debug(f"通過企業(yè)微信發(fā)送圖片消息成功:{response.json()}")
            return True
        else:
            logger.error(f"通過企業(yè)微信發(fā)送圖片失敗:{response.text}")
            return False

    def send_text_picture(self, articles: list):
        """
        發(fā)送圖文消息
        :param articles: 圖文消息,一個圖文消息支持1到8條圖文, 包括如下字段
            1. title: 標題,不超過128個字節(jié),超過會自動截斷
            2. description: 非必填,描述,不超過512個字節(jié),超過會自動截斷
            3. url: 點擊后跳轉(zhuǎn)的鏈接。
            4. picurl: 非必填,圖文消息的圖片鏈接,支持JPG、PNG格式,較好的效果為大圖 1068*455,小圖150*150。
        """
        payload = {
            "msgtype": "news",
            "news": {
                "articles": [
                ]
            }
        }
        for article in articles:
            payload["news"]["articles"].append(
                {
                    "title": article.get("title"),
                    "description": article.get("description", ""),
                    "url": article.get("url"),
                    "picurl": article.get("picurl", "")
                }
            )
        response = request(url=self.webhook_url, method="POST", json=payload, headers=self.headers)
        if response.json().get("errcode") == 0:
            logger.debug(f"通過企業(yè)微信發(fā)送圖文消息成功:{response.json()}")
            return True
        else:
            logger.error(f"通過企業(yè)微信發(fā)送圖文失敗:{response.text}")
            return False

    def upload_file(self, file_path):
        """
        上傳文件到企業(yè)微信服務(wù)器(要求文件大小在5B~20M之間)
        注意:素材上傳得到media_id,該media_id僅三天內(nèi)有效;media_id只能是對應(yīng)上傳文件的機器人可以使用
        :param file_path: 文件絕對路徑
        """
        token_regex = r"key=([\w-]+)"
        match = re.search(token_regex, self.webhook_url)
        token = match.group(1)
        url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={token}&type=file"
        headers = {
            "Content-Type": "multipart/form-data;"
        }
        with open(file_path, "rb") as f:
            files = {"media": (os.path.basename(file_path), f.read())}
        response = request(url=url, method="POST", files=files, headers=headers)
        if response.json().get("errcode") == 0:
            media_id = response.json().get("media_id")
            logger.debug(f"上傳文件成功,media_id= {media_id}")
            return media_id
        else:
            logger.error(f"上傳文件失?。?/span>{response.text}")
            return False

    def send_file(self, media_id):
        """
        發(fā)送文件
        :param media_id: 文件id,通過下文的文件上傳接口獲取
        """
        payload = {
            "msgtype": "file",
            "file": {
                "media_id": media_id,
            }
        }
        response = request(url=self.webhook_url, method="POST", json=payload, headers=self.headers)
        if response.json().get("errcode") == 0:
            logger.debug(f"通過企業(yè)微信發(fā)送文件消息成功:{response.json()}")
            return True
        else:
            logger.error(f"通過企業(yè)微信發(fā)送文件消息失?。?/span>{response.text}")
            return False


if __name__ == '__main__':
    webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=***********"
    bot = WechatBot(webhook_url)
    bot.send_text(content="hello1", mentioned_list=["@all"])
    bot.send_text(content="hello2", mentioned_list=["@all"], mentioned_mobile_list=["18774970063"])
    md = "實時新增用戶反饋<font color=\"warning\">132例</font>,請相關(guān)同事注意。\n>類型:<font color=\"comment\">用戶反饋</font>>普通用戶反饋:<font color=\"comment\">117例</font>>VIP用戶反饋:<font color=\"comment\">15例</font>"
    bot.send_markdown(content=md)
    bot.send_picture(image_path=r"xxxxxx.png")
    articles = [
        {
            "title": "中秋節(jié)禮品領(lǐng)取",
            "description": "今年中秋節(jié)公司有豪禮相送",
            "url": "www.qq.com",
            "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
        }
    ]
    bot.send_text_picture(articles=articles)
    filepath = r"xxxxxxx\apiautotest-report-2023-05-11 14_57_18.html"
    bot.send_file(media_id=bot.upload_file(filepath))

到了這里,關(guān)于封裝Python腳本:使用企業(yè)微信機器人發(fā)送消息至企業(yè)微信的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • C#使用企業(yè)微信群機器人推送生產(chǎn)數(shù)據(jù)

    C#使用企業(yè)微信群機器人推送生產(chǎn)數(shù)據(jù)

    ? ? 在日常的工作生產(chǎn)中,經(jīng)常會有將將生產(chǎn)數(shù)據(jù)或者一些信息主動推送給相關(guān)的管理人員,我們公司在開發(fā)WMS系統(tǒng)時,為了倉庫的儲存安全,需要在危廢品庫存達到一定的儲量時,自動通知倉管員去處理危廢品,所以就需要程序自動的通過企業(yè)微信告知倉管員,這個時候就

    2024年02月10日
    瀏覽(35)
  • 【Python微信機器人】第六七篇: 封裝32位和64位Python hook框架實戰(zhàn)打印微信日志

    【Python微信機器人】第六七篇: 封裝32位和64位Python hook框架實戰(zhàn)打印微信日志

    目前的系列目錄(后面會根據(jù)實際情況變動): 在windows11上編譯python 將python注入到其他進程并運行 注入Python并使用ctypes主動調(diào)用進程內(nèi)的函數(shù)和讀取內(nèi)存結(jié)構(gòu)體 調(diào)用匯編引擎實戰(zhàn)發(fā)送文本和圖片消息(支持32位和64位微信) 允許Python加載運行py腳本且支持熱加載 利用匯編和反匯編

    2024年02月04日
    瀏覽(19)
  • 零代碼,使用 Dify 和 Laf 兩分鐘接入企業(yè)微信 AI 機器人

    零代碼,使用 Dify 和 Laf 兩分鐘接入企業(yè)微信 AI 機器人

    原文鏈接:https://docs.dify.ai/v/zh-hans/use-cases/integrate-with-wecom-using-dify Dify 允許創(chuàng)建 AI 應(yīng)用,并提供二次開發(fā)的能力。這里我將演示創(chuàng)建一個法律問答助手的 AI 應(yīng)用,稱作“知法”。在本篇教程中,我將指導(dǎo)你為“知法”接入企業(yè)微信。 企業(yè)微信的管理員權(quán)限 一個 Dify 的帳號

    2024年02月11日
    瀏覽(25)
  • 企業(yè)微信創(chuàng)建群機器人步驟

    企業(yè)微信創(chuàng)建群機器人步驟

    1.選擇群,右鍵點擊“管理聊天信息“? ?2.添加機器人的信息 ? ?3.創(chuàng)建好的機器人都有一個唯一的Webhook地址,點擊Webhook地址就可以看到文檔說明,自動推送消息需要自行開發(fā)。 ? ? 開發(fā)者中心地址:https://developer.work.weixin.qq.com/

    2024年02月13日
    瀏覽(20)
  • 企業(yè)微信群:機器人實現(xiàn)定時提醒功能

    企業(yè)微信群:機器人實現(xiàn)定時提醒功能

    如果每天都需要,或者經(jīng)常需要提醒企業(yè)微信群里面的人做某一件事情的話,靠人力去實現(xiàn)比較費力,而且偶爾忘記。 正好,企業(yè)微信群有一個機器人,正可以實現(xiàn)這一功能。 1、首先,在企業(yè)微信群,添加一個機器人。 2、根據(jù)企業(yè)微信機器人的配置說明,編寫程序。這里

    2024年02月16日
    瀏覽(36)
  • 基于ChatGPT的企業(yè)微信機器人

    基于ChatGPT的企業(yè)微信機器人

    登錄OpenAI的賬號后,再點擊右上角的“Personal”圖標,然后點擊“view API keys”進入API頁面。 點擊“create new secret key”按鈕。 生成秘鑰之后,把秘鑰復(fù)制下來。 根目錄下的config-template.json文件是配置文件的模板,復(fù)制該模板,修改復(fù)制的文件名為:config.json 打開剛才復(fù)制的c

    2024年02月13日
    瀏覽(27)
  • Zabbix配置企業(yè)微信報警機器人

    Zabbix配置企業(yè)微信報警機器人

    微信告警機器人是一種可以將Zabbix告警通知發(fā)送到微信群或個人微信號的工具。 1、申請企業(yè)微信 自己到企業(yè)微信官網(wǎng)申請一個賬號 2、配置微信企業(yè)號 1、創(chuàng)建機器人 在電腦企業(yè)微信群創(chuàng)建機器人 在企業(yè)微信上創(chuàng)建一個群聊,并添加需要接收告警通知的成員。 在群管理創(chuàng)建

    2024年02月06日
    瀏覽(24)
  • 使用Python做一個微信機器人

    使用Python做一個微信機器人

    實現(xiàn)代碼和pip安裝: https://blog.csdn.net/Qwertyuiop2016/article/details/135076957 簡介 該程序?qū)⑽⑿诺膬?nèi)部功能提取出來,然后在程序里加載Python,接著將這些功能導(dǎo)出成庫函數(shù),就可以在Python里使用這些函數(shù) 程序啟動的時候會執(zhí)行py_code目錄下的main.py,類似于你在命令行使用 python main

    2024年02月07日
    瀏覽(25)
  • SQL企業(yè)微信群機器人消息推送

    ?--參考資料地址 ?? ??? ?--微軟官方地址: https://learn.microsoft.com/zh-cn/sql/relational-databases/system-stored-procedures/ole-automation-stored-procedures-transact-sql?view=sql-server-ver16 ?? ??? ?--騰訊官方地址:https://developer.work.weixin.qq.com/ ?? ??? ?--使用教程: ?? ??? ?--1.開啟數(shù)據(jù)庫的功能, \\\"只

    2024年02月08日
    瀏覽(18)
  • PowerShell 實現(xiàn)企業(yè)微信機器人推送消息

    PowerShell 實現(xiàn)企業(yè)微信機器人推送消息

    在ARMS告警管理中創(chuàng)建企業(yè)微信機器人后,您可以在通知策略中指定對應(yīng)的企業(yè)微信群用于接收告警。當(dāng)通知策略的匹配規(guī)則被觸發(fā)時,系統(tǒng)會自動向您指定的企業(yè)微信群發(fā)送告警通知。企業(yè)微信群收到通知后,您可以在企業(yè)微信群中對告警進行管理。 通過接口實現(xiàn)在群里發(fā)

    2024年02月06日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包