1.发送微信消息

直接使用第三方库 itchat,其文档中有详细使用方式;https://itchat.readthedocs.io/zh/latest/

如下实例为 发送群聊信息

# -*- coding: utf-8 -*-
# (C) Guangcai Ren, 2019
# All rights reserved
import logging import itchat log = logging.getLogger(__name__) # itchat 微信官方教程:https://itchat.readthedocs.io/zh/latest/
# 微信登录
# 登录时如果断网,则此程序直接停止
# 启动热登录,并且生成 命令行 登录二维码
itchat.auto_login(hotReload=True, enableCmdQR=2)
# 保持心跳状态,防止自动退出登录
itchat.start_receiving() # 获取群聊,注意群 必须保持到通讯录,否则可能会找不到群
itchat.get_chatrooms(update=True)
room = itchat.search_chatrooms('python')
if len(room) == 0:
log.error('没有找到群信息')
else:
try:
iRoom = room[0]['UserName']
# 发送消息
result = itchat.send('send message', iRoom)
try:
if result['BaseResponse']['ErrMsg'] == '请求成功':
log.info('send wechat success')
except Exception as e:
print('resolve wechat result fail,result is :{},error is {}'.format(result, e))
except Exception as e:
print('wechat send message fail,reason is :{} '.format(e))

2.发送企业微信 信息

企业微信 官方有 相关文档,直接按照文档开发即可。

注意点:

  • 先开通企业微信
  • 登录网页版 企业微信 https://work.weixin.qq.com/  从中查找相关 id(在获取访问token时需要)
  • 接口一般 流程为 先 获取 token,再用 token访问其他接口
  • 发送群聊信息时,群id 只能通过 接口创建群聊的才有群id
  • 创建群聊时的 获取token的参数 corpsecret必须 从 应用的 部门一定要选根目录,否则报错 86006;
  • 如果开发过程中有任何问题(错误提示有一定的误导性) 可以通过 企业微信客服 进行沟通解决,他们非常有耐心,谢谢他们。

如下代码 做到了 获取token,创建群聊,发送群聊信息,发送个人信息

# -*- coding: utf-8 -*-
# (C) Guangcai Ren <renguangcai@jiaaocap.com>
# All rights reserved
# create time '2019/6/13 17:17'
import json import requests result = requests.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken",
params={'corpid': 'fg',
'corpsecret': ''})
access_token = None
if result.status_code != 200:
print('连接到服务器失败')
else:
result_json = json.loads(result.text)
if result_json['errcode'] != 0:
print('响应结果不正确')
else:
access_token = result_json['access_token']
print(access_token) # 创建群聊
result = requests.post('https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token={}'.format(access_token),
data=json.dumps({
"name": "通知群",
"owner": "user_name",
"userlist": ["user_name", "user_name1", "user_name2"],
"chatid": "secid"
}))
print(result.text) # 推送群聊信息
result = requests.post('https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token={}'.format(access_token),
data=json.dumps({
"chatid": "secid",
"msgtype": "text",
"text": {
"content": "测试:你的快递已到\n请携带工卡前往邮件中心领取"
},
"safe": 0
}))
print(result.text) # 发送个人消息
result = requests.post('https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}'.format(access_token),
data=json.dumps({
"touser": "user_name",
"msgtype": "text",
"agentid": 23,
"text": {
"content": "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
},
"safe": 0
}
))
print(result.text)

python发送微信及企业微信消息的更多相关文章

  1. CabloyJS一站式助力微信、企业微信、钉钉开发 - 钉钉篇

    前言 现在软件开发不仅要面对前端碎片化,还要面对后端碎片化.针对前端碎片化,CabloyJS提供了pc=mobile+pad的跨端自适应方案,参见:自适应布局:pc = mobile + pad 在这 ...

  2. CabloyJS一站式助力微信、企业微信、钉钉开发 - 企业微信篇

    前言 现在软件开发不仅要面对前端碎片化,还要面对后端碎片化.针对前端碎片化,CabloyJS提供了pc=mobile+pad的跨端自适应方案,参见:自适应布局:pc = mobile + pad 在这 ...

  3. CabloyJS一站式助力微信、企业微信、钉钉开发 - 微信篇

    前言 现在软件开发不仅要面对前端碎片化,还要面对后端碎片化.针对前端碎片化,CabloyJS提供了pc=mobile+pad的跨端自适应方案,参见:自适应布局:pc = mobile + pad 在这 ...

  4. Elemetary OS deepin-wine 版 微信 和 企业微信 输入框字体显示异常解决

    Elemetary OS deepin-wine 版 微信 和 企业微信 输入框字体显示异常解决 最近体验了Elementary OS 这个系统, 话说我之前都是用的Manjora 的各个桌面版, 突 ...

  5. 配置zabbix通过微信报警企业微信报警

    如今势态: 报警的方式可谓是八仙过海各显神通,如电话报警,短信报警,邮件报警,QQ报警,微信报警等等. 电话报警:一般都是使用别的平台的工具,平台给你提供一个接口供你使用,大多数为限量收费款 短信报警 ...

  6. elk使用微信ElartAlert企业微信告警,自定义告警内容

    第一种方式 alert: - "elastalert_modules.wechat_qiye_alert.WeChatAlerter" alert_text: " === ...

  7. 微信、企业微信和支付窗 SDK 三合一,JeeWx-api 1.2.0 版本发布

    摘要: JEEWX-API 是第一款JAVA版微信极速SDK,同时集成企业微信SDK,支付窗SDK,可以快速的基于她进行微信公众号.企业微信.支付窗应用开发.基于 jeewx-api 开发可以立即拥有 ...

  8. Elementary OS常见软件(TIM、微信、企业微信)安装(二)

    前言 最终没忍住还是把开发环境迁移到了Elementary OS上来,这其中也没少折腾,试过Ubuntu 20.04 LTS和deepin V20可以(),deepin真的很不错可能是我的电脑兼容性不 ...

  9. Mint-Linux【最佳】【快速】安装微信、企业微信、TIM、QQ等软件

    废话不多说 直接上教程 注意看 方式一.在线安装 在本地目录下.如 /home/root/Document 直接使用在线安装脚本,安装最新的Release版本: wget -qO- https://r ...

随机推荐

  1. 调试Xamarin.Android时出现缺少"Mono.Posix 2.0.0"的错误

    1.在http://originaldll.com/file/mono.posix.dll/31191.html中下载mono.posix 2.0.0 dll 2.以管理员权限运行Visual Stu ...

  2. spring3.0+struts2+ibatis整合

    User.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLI ...

  3. Vue内敛模板

    在学习<Vue实战>一书时,学习到组件高级应用-内联模板这一小节时,照着书上的例子敲了一遍,发现未达到预期,切报错. 书上源代码如下: <!DOCTYPE html> < ...

  4. 【Kickstart】2017 Round (Practice ~ G)

    Practice Round Problem A Country Leader (4pt/7pt) Problem B Vote (5pt/8pt) Problem C Sherlock and Pa ...

  5. [转]C# CancellationTokenSource 终止线程

    我们在多线程中通常使用一个bool IsExit类似的代码来控制是否线程的运行与终止,其实使用CancellationTokenSource来进行控制更为好用,下面我们将介绍CancellationT ...

  6. 【leetcode】1003. Check If Word Is Valid After Substitutions

    题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...

  7. 【leetcode】940. Distinct Subsequences II

    题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...

  8. loadRunner之参数关联

    录制脚本,对用户名和密码进行参数化: Action() { web_url("WebTours", "URL=http://127.0.0.1:1080/WebTours ...

  9. sql的分页

    public static string GetPageSql(string sql, int start, int end)        {            return string.Fo ...

  10. bzoj 2013

    http://www.lydsy.com/JudgeOnline/problem.php?id=2013 最初看这个题的时候,以为神题不可做,然后去找yzjc..然后做法过于简单了(' '       ...