├── .gitignore ├── LICENSE ├── README.md ├── bot.py ├── config.yml └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | *~ 106 | *.pkl -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 潘伟洲 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wukong-itchat 2 | 3 | 用于微信控制 [wukong-robot](http://github.com/wzpan/wukong-robot.git) 。 4 | 5 | ## 安装 6 | 7 | 1. 先安装 wukong-robot 并运行,确保 wukong-robot 的版本 >= 1.5; 8 | 2. 安装依赖: 9 | 10 | ``` bash 11 | pip3 install -r requirements.txt 12 | ``` 13 | 14 | ## 运行 15 | 16 | ``` bash 17 | python3 bot.py 18 | ``` 19 | 20 | ## 配置 21 | 22 | 详见 config.yml 。尤其注意如果 itchat 和 wukong-robot 并不在同一台服务器上运行,务必修改 `host` 配置。 23 | 24 | ## 使用 25 | 26 | 与文件传输助手聊天,文本或者语音都将会作为指令转发给 wukong-robot 的后台管理端。 27 | 28 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import itchat 4 | import os 5 | import requests 6 | import logging 7 | import time 8 | import yaml 9 | import json 10 | import base64 11 | from pydub import AudioSegment 12 | from itchat.content import * 13 | 14 | LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" 15 | logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) 16 | 17 | config = {} 18 | with open('config.yml', 'r') as f: 19 | config = yaml.safe_load(f) 20 | if any (key not in config for key in ['host', 'port', 'validate']): 21 | logging.error("配置文件格式有误,程序将退出!") 22 | exit(1) 23 | url = '{}:{}/chat'.format(config['host'], config['port']) 24 | 25 | def convert_mp3_to_wav(mp3_path): 26 | """ 27 | 将 mp3 文件转成 wav 28 | 29 | :param mp3_path: mp3 文件路径 30 | :returns: wav 文件路径 31 | """ 32 | target = mp3_path.replace(".mp3", ".wav") 33 | if not os.path.exists(mp3_path): 34 | logging.critical("文件错误 {}".format(mp3_path)) 35 | return None 36 | AudioSegment.from_mp3(mp3_path).export(target, format="wav") 37 | return target 38 | 39 | @itchat.msg_register([RECORDING]) 40 | def download_files(msg): 41 | if msg.toUserName == 'filehelper': 42 | logging.info('received voice {}'.format(msg)) 43 | msg.download(msg.fileName) 44 | wav = convert_mp3_to_wav(msg.fileName) 45 | with open(wav, 'rb') as f: 46 | data = base64.b64encode(f.read()) 47 | param = { 48 | 'validate': config['validate'], 49 | 'type': 'voice', 50 | 'voice': data, 51 | 'uuid': str(int(time.time())) 52 | } 53 | r = requests.post(url, data=param) 54 | r.encoding = 'utf-8' 55 | try: 56 | resp = r.json()['resp'] 57 | msg.user.send('wukong: %s' % (resp)) 58 | except Exception as e: 59 | logging.error(e) 60 | finally: 61 | if os.path.exists(msg.fileName): 62 | os.remove(msg.fileName) 63 | if os.path.exists(wav): 64 | os.remove(wav) 65 | 66 | @itchat.msg_register(TEXT) 67 | def text_reply(msg): 68 | if msg.toUserName == 'filehelper' and not msg.text.startswith('wukong: '): 69 | logging.info('received text {}'.format(msg.text)) 70 | param = { 71 | 'validate': config['validate'], 72 | 'type': 'text', 73 | 'query': msg.text, 74 | 'uuid': str(int(time.time())) 75 | } 76 | r = requests.post(url, data=param) 77 | r.encoding = 'utf-8' 78 | try: 79 | resp = r.json()['resp'] 80 | msg.user.send('wukong: %s' % (resp)) 81 | except Exception as e: 82 | logging.error(e) 83 | 84 | itchat.auto_login(enableCmdQR=2, hotReload=True) 85 | itchat.run(True) 86 | -------------------------------------------------------------------------------- /config.yml: -------------------------------------------------------------------------------- 1 | # 后台管理端的地址 2 | host: 'http://localhost' 3 | 4 | # 后台管理端的端口 5 | port: 5000 6 | 7 | # 后台管理端的验证码 8 | # 需与 config.yml 里的设置相同 9 | validate: 'f4bde2a342c7c75aa276f78b26cfbd8a' 10 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | itchat==1.3.10 2 | requests==2.21.0 3 | PyYAML==5.1 4 | pydub==0.23.1 5 | --------------------------------------------------------------------------------