python 企业微信发送消息

首先管理员登录:企业微信

需要的参数

    1、通讯用户:touser 或 通讯组:toparty

    2、企业ID:corpid

    3、应用ID/密钥:agentId,secret

获取通讯用户/组

通讯录 用户的账号或创建组的部门ID

获取企业ID


  PC企业微信/左上角头像/管理企业/我的企业/企业ID
  手机企业微信/工作台/管理企业/企业信息/企业ID

获取应用ID/密钥

企业微信管理员登录企业微信,

应用管理创建应用

可见范围:消息发给谁

脚本代码

#! /usr/bin/env python
# -*- coding: UTF-8 -*-

import requests, sys


class SendWecom():
    def __init__(self):
        self.CORP_ID = "xxx"  # 企业号的标识
        self.SECRET = "xxx"  # 管理组凭证密钥
        self.AGENT_ID = xxx  # 应用ID
        self.token = self.get_token()

    def get_token(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        data = {
            "corpid": self.CORP_ID,
            "corpsecret": self.SECRET
        }
        req = requests.get(url=url, params=data)
        res = req.json()
        if res['errmsg'] == 'ok':
            return res["access_token"]
        else:
            return res

    def send_message(self, to_user, content):
        url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % self.token
        data = {
            # "touser": to_user,  # 发送个人就填用户账号
            "toparty": to_user,  # 发送组内成员就填部门ID
            "msgtype": "text",
            "agentid": self.AGENT_ID,
            "text": {"content": content},
            "safe": "0"
        }

        req = requests.post(url=url, json=data)
        res = req.json()
        if res['errmsg'] == 'ok':
            print("send message succeed")
            return "send message succeed"
        else:
            print("send message error", res)
            return res


if __name__ == '__main__':
    send_wecom = SendWecom()
    send_wecom.send_message("2", "测试a")

效果

OK.

通过Python企业微信中的成员(再由他们转达至个人微信)发送消息,通常需要借助于企业微信的应用接入能力,并且按照其API接口规范来进行操作。需要注意的是直接从企业微信向非关联的外部个人微信号发信息是受限制的行为。 下面是一个简单的步骤指南: 1. 创建应用并获取相关凭证: 首先你需要拥有一个已经注册的企业账号,在管理后台创建自建应用来获得`corpid`(企业的ID)、 `corpsecret`(应用的密钥),以及设置agentid(应用ID)等必要参数; 2. 获取access_token: 根据官方文档指引,利用上一步得到的信息请求换取用于调用其他接口所需的票据——access_token; 3. 发送消息:确定好接收者userid(即员工标识符),然后构造包含内容的消息体格式JSON字符串,最后发起HTTPS POST请求到指定URL完成发送动作; 对于要将此过程封装成易于使用的Python脚本,则可以参考如下关键代码片段: ```python import requests,json # 第三方用户授权code换openid和session_key (此处仅为示例) def get_access_token(corpid, corpsecret): url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}" response=requests.get(url).json() if 'errcode' in response and not response['errcode']: return response["access_token"] else: raise Exception("Failed to fetch access token") def send_message(access_token,user,message_content): msg_url=f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}' payload={ "touser": user, "msgtype":"text", "agentid": AGENT_ID, "text":{ "content": message_content }, "safe":"0" } headers={'Content-Type': 'application/json'} res=requests.post(msg_url,data=json.dumps(payload),headers=headers).json() if __name__ == '__main__': CORP_ID='your_corp_id' SECRET='your_secret' try: ACCESS_TOKEN=get_access_token(CORP_ID,SECRET) USER="User's ID" # Replace with actual userid MSG_CONTENT="Hello from Python Script!" result=send_message(ACCESS_TOKEN,USER,MSG_CONTENT) print(result) except Exception as e: print(e) ``` 以上就是使用Python配合企业微信提供的开放平台接口实现给定人员发送文本通知的方式简介。请注意实际开发过程中还需考虑安全性等因素。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值