企业微信自动回复软件,企业微信自动回复机器人,python框架分享

简介: 企业微信机器人包含完整的消息处理流程,支持文本消息自动回复、事件处理、消息加密解密等功能

下载地址:https://www.pan38.com/yun/share.php?code=JCnzE 提取密码:1131
企业微信机器人包含完整的消息处理流程,支持文本消息自动回复、事件处理、消息加密解密等功能。使用时需要替换配置文件中的企业微信相关参数,并部署到服务器上。

import json
import time
import requests
from flask import Flask, request, jsonify

app = Flask(name)

class WeChatWorkRobot:
def init(self, corp_id, corp_secret, agent_id):
self.corp_id = corp_id
self.corp_secret = corp_secret
self.agent_id = agent_id
self.access_token = None
self.token_expire_time = 0

def get_access_token(self):
    if time.time() < self.token_expire_time and self.access_token:
        return self.access_token

    url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={self.corp_id}&corpsecret={self.corp_secret}"
    response = requests.get(url)
    data = response.json()

    if data.get('errcode') == 0:
        self.access_token = data['access_token']
        self.token_expire_time = time.time() + data['expires_in'] - 300
        return self.access_token
    else:
        raise Exception(f"获取access_token失败: {data}")

def send_message(self, user_id, content):
    token = self.get_access_token()
    url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}"

    payload = {
        "touser": user_id,
        "msgtype": "text",
        "agentid": self.agent_id,
        "text": {"content": content},
        "safe": 0
    }

    response = requests.post(url, json=payload)
    return response.json()

def handle_message(self, msg_data):
    msg_type = msg_data.get('MsgType')
    user_id = msg_data.get('FromUserName')

    if msg_type == 'text':
        content = msg_data.get('Content', '').strip()
        if content == '帮助':
            return self.send_message(user_id, "输入关键词获取帮助信息")
        else:
            return self.send_message(user_id, f"已收到您的消息: {content}")
    return None
AI 代码解读

robot = WeChatWorkRobot(
corp_id="YOUR_CORP_ID",
corp_secret="YOUR_CORP_SECRET",
agent_id=YOUR_AGENT_ID
)

@app.route('/callback', methods=['POST'])
def callback():
data = request.json
if data.get('Event') == 'subscribe':
user_id = data.get('FromUserName')
robot.send_message(user_id, "欢迎关注企业微信机器人")
else:
robot.handle_message(data)
return jsonify({"status": "success"})

if name == 'main':
app.run(host='0.0.0.0', port=5000)

企业微信配置
WECHAT_CONFIG = {
"corp_id": "YOUR_CORP_ID",
"corp_secret": "YOUR_CORP_SECRET",
"agent_id": 1000002,
"token": "YOUR_TOKEN",
"encoding_aes_key": "YOUR_ENCODING_AES_KEY"
}

自动回复规则

REPLY_RULES = {
"帮助": "输入以下关键词获取帮助:\n1. 天气\n2. 新闻\n3. 会议",
"天气": "今日天气晴朗,气温25-32℃",
"新闻": "最新新闻请访问公司内网",
"会议": "今日会议安排:\n10:00 部门例会\n15:00 项目评审"
}

import re
from config import REPLY_RULES

class MessageHandler:
@staticmethod
def process_text_message(content):
content = content.strip().lower()

    # 精确匹配
    if content in REPLY_RULES:
        return REPLY_RULES[content]

    # 模糊匹配
    for keyword, reply in REPLY_RULES.items():
        if keyword in content:
            return reply

    # 正则匹配
    if re.search(r'时间|几点', content):
        from datetime import datetime
        return f"当前时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"

    return "抱歉,我不理解您的请求。输入'帮助'获取支持信息。"

@staticmethod
def process_event_message(event):
    if event == 'subscribe':
        return "欢迎关注企业微信机器人!"
    elif event == 'unsubscribe':
        return ""
    return None
AI 代码解读

hashlib
import time
import random
import string
from Crypto.Cipher import AES
import base64
from config import WECHAT_CONFIG

class WeChatUtils:
@staticmethod
def verify_signature(signature, timestamp, nonce):
token = WECHAT_CONFIG['token']
tmp_list = sorted([token, timestamp, nonce])
tmp_str = ''.join(tmp_list).encode('utf-8')
hashcode = hashlib.sha1(tmp_str).hexdigest()
return hashcode == signature

@staticmethod
def decrypt_message(encrypt_msg):
    aes_key = base64.b64decode(WECHAT_CONFIG['encoding_aes_key'] + "=")
    cipher = AES.new(aes_key, AES.MODE_CBC, aes_key[:16])
    decrypted = cipher.decrypt(base64.b64decode(encrypt_msg))

    pad = ord(decrypted[-1:])
    content = decrypted[16:-pad].decode('utf-8')
    return content

@staticmethod
def encrypt_message(reply_msg):
    aes_key = base64.b64decode(WECHAT_CONFIG['encoding_aes_key'] + "=")
    cipher = AES.new(aes_key, AES.MODE_CBC, aes_key[:16])

    random_str = ''.join(random.choices(string.ascii_letters + string.digits, k=16))
    msg_len = len(reply_msg.encode('utf-8'))
    pad_len = 32 - (msg_len % 32)
    pad_char = chr(pad_len)

    plaintext = random_str + reply_msg + pad_char * pad_len
    encrypted = cipher.encrypt(plaintext.encode('utf-8'))
    return base64.b64encode(encrypted).decode('utf-8')
AI 代码解读
相关文章
拼多多批量下单工具,拼多多买家批量下单软件,低价下单python框架分享
使用Selenium实现自动化操作流程多线程订单处理提升效率
拼多多批量下单软件,拼多多无限账号下单软件,python框架仅供学习参考
完整的拼多多自动化下单框架,包含登录、搜索商品、获取商品列表、下单等功能。
企业微信自动加好友软件,导入手机号批量添加微信好友,python版本源码分享
代码展示了企业微信官方API的合规使用方式,包括获取access_token、查询部门列表和创建用户等功能
自动发短信的软件,批量自动群发短信,手机号电话号生成器【python框架】
这个短信群发系统包含以下核心功能: 随机手机号生成器(支持中国号码) 批量短信发送功能(使用Twilio API)
微信公众号接口:解锁公众号开发的无限可能
微信公众号接口是微信官方提供的API,支持开发者通过编程与公众号交互,实现自动回复、消息管理、用户管理和数据分析等功能。本文深入探讨接口的定义、类型、优势及应用场景,如智能客服、内容分发、电商闭环等,并介绍开发流程和工具,帮助运营者提升用户体验和效率。未来,随着微信生态的发展,公众号接口将带来更多机遇,如小程序融合、AI应用等。
小程序微信分享功能如何开发?开放平台已绑定仍不能使用的问题?-优雅草卓伊凡
小程序微信分享功能如何开发?开放平台已绑定仍不能使用的问题?-优雅草卓伊凡
616 29
小程序微信分享功能如何开发?开放平台已绑定仍不能使用的问题?-优雅草卓伊凡
weixin163基于微信小程序的校园二手交易平台系统设计与开发ssm(文档+源码)_kaic
本文介绍了一款基于微信小程序的校园二手物品交易平台的开发与实现。该平台采用Java语言开发服务端,使用MySQL数据库进行数据存储,前端以微信小程序为载体,支持管理员和学生两种角色操作。管理员可管理用户、商品分类及信息、交易记录等,而学生则能注册登录、发布购买商品、参与交流论坛等。系统设计注重交互性和安全性,通过SSM框架优化开发流程,确保高效稳定运行,满足用户便捷交易的需求,推动校园资源共享与循环利用。
weixin168“返家乡”高校暑期社会实践微信小程序设计与开发ssm(文档+源码)_kaic
本文探讨高校暑期社会实践微信小程序的开发与应用,旨在通过信息化手段提升活动管理效率。借助微信小程序技术、SSM框架及MySQL数据库,实现信息共享、流程规范和操作便捷。系统涵盖需求分析、可行性研究、设计实现等环节,确保技术可行、操作简便且经济合理。最终,该小程序可优化活动发布、学生信息管理和心得交流等功能,降低管理成本并提高工作效率。
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
1449 1
uni-app开发微信小程序的报错[渲染层错误]排查及解决
uni-app开发微信小程序的报错[渲染层错误]排查及解决
2063 7

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问