Python实现通过微信企业号发送文本消息的Class

简介:

前文《Python实现获取微信企业号access_token的Class》提供了获取微信企业号的access_token,本文中的代码做实际发送文本消息。

编程要点和调用方法:

  1. 支持发送中文,核心语句“payload = json.dumps(self.data, encoding='utf-8', ensure_ascii=False)”,关键字“python json 中文”

  2. 这个Class只有一个公共方法send()。

  3. 使用方法:import这个class,然后调用send方法即可,方法参数只需要两个,给谁(多UserID用"|"隔开),内容是什么,例如:

1
2
3
import  odbp_sendMessage
msg  =  odbp_sendMessage.WeiXinSendMsgClass()
msg.send( "dingguodong" "Python 大魔王!" )

运行效果图如下:

wKiom1etlGGiyHoBAAfIStLK3o0028.jpg

手机端效果:

IMG_8148

Python脚本内容可以从github上获取:https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/projects/WeChatOps/OpsDevBestPractice/odbp_sendMessage.py

脚本内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
"""
Created by PyCharm.
File:               LinuxBashShellScriptForOps:odbp_sendMessage.py
User:               Guodong
Create Date:        2016/8/12
Create Time:        14:49
  """
 
import  odbp_getToken
 
 
class  WeiXinSendMsgClass( object ):
     def  __init__( self ):
         self .access_token  =  odbp_getToken.WeiXinTokenClass().get()
         self .to_user  =  ""
         self .to_party  =  ""
         self .to_tag  =  ""
         self .msg_type  =  "text"
         self .agent_id  =  2
         self .content  =  ""
         self .safe  =  0
 
         self .data  =  {
             "touser" self .to_user,
             "toparty" self .to_party,
             "totag" self .to_tag,
             "msgtype" self .msg_type,
             "agentid" self .agent_id,
             "text" : {
                 "content" self .content
             },
             "safe" self .safe
         }
 
     def  send( self , to_user, content):
         if  to_user  is  not  None  and  content  is  not  None :
             self .data[ 'touser' =  to_user
             self .data[ 'text' ][ 'content' =  content
         else :
             print
             raise  RuntimeError
         import  requests
         import  json
 
         url  =  "https://qyapi.weixin.qq.com/cgi-bin/message/send"
 
         querystring  =  { "access_token" self .access_token}
 
         payload  =  json.dumps( self .data, encoding = 'utf-8' , ensure_ascii = False )
 
         headers  =  {
             'content-type' "application/json" ,
             'cache-control' "no-cache" ,
         }
 
         response  =  requests.request( "POST" , url, data = payload, headers = headers, params = querystring)
 
         return_content  =  json.loads(response.content)
         if  return_content[ "errcode" = =  0  and  return_content[ "errmsg" = =  "ok" :
             print  "Send successfully! %s "  %  return_content
         else :
             print  "Send failed! %s "  %  return_content

tag:python,微信企业号发送报警,微信企业号发送文本消息

--end--




本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1837376,如需转载请自行联系原作者


相关文章
|
5天前
|
机器学习/深度学习 自然语言处理 数据可视化
数据代码分享|PYTHON用NLP自然语言处理LSTM神经网络TWITTER推特灾难文本数据、词云可视化
数据代码分享|PYTHON用NLP自然语言处理LSTM神经网络TWITTER推特灾难文本数据、词云可视化
|
5天前
|
机器学习/深度学习 自然语言处理 算法
Gensim详细介绍和使用:一个Python文本建模库
Gensim详细介绍和使用:一个Python文本建模库
22 1
|
5天前
|
机器学习/深度学习 自然语言处理 算法
【Python机器学习专栏】文本数据的特征提取与表示
【4月更文挑战第30天】本文探讨了文本特征提取与表示在机器学习和NLP中的重要性。介绍了词袋模型、TF-IDF和n-gram等特征提取方法,以及稀疏向量和词嵌入等表示方式。Python中可利用sklearn和gensim库实现这些技术。有效的特征提取与表示有助于将文本数据转化为可处理的数值形式,推动NLP和机器学习领域的进步。
|
5天前
|
数据采集 机器学习/深度学习 人工智能
Python实现文本情感分析
Python实现文本情感分析
24 1
|
5天前
|
数据采集 存储 人工智能
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
43 0
|
5天前
|
人工智能 机器人 API
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
34 0
|
5天前
|
缓存 人工智能 API
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
23 0
|
5天前
|
XML 人工智能 数据安全/隐私保护
【Python+微信】【企业微信开发入坑指北】1. 数据链路打通:接收用户消息处理并回复
【Python+微信】【企业微信开发入坑指北】1. 数据链路打通:接收用户消息处理并回复
17 0
|
5天前
|
机器学习/深度学习 人工智能 算法
【AI大模型应用开发】【补充知识】文本向量化与向量相似度(含Python代码)
【AI大模型应用开发】【补充知识】文本向量化与向量相似度(含Python代码)
24 0
|
5天前
|
Linux 网络安全 开发工具
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
【超详细!超多图!】【代码管理】Python微信公众号开发(3)- 服务器代码上传Github
35 0

热门文章

最新文章