├── pylintrc ├── setup.py ├── docker ├── Dockerfile └── healthcheck.sh ├── girlfriend ├── __init__.py ├── test │ ├── __init__.py │ ├── confirm_friend.py │ └── learn_itchat.py ├── utils │ ├── __init__.py │ └── tencent.py ├── components │ ├── __init__.py │ ├── crontab.py │ ├── mass_texting.py │ ├── ai_robot.py │ ├── ai_picture.py │ ├── ai_ocr.py │ └── ai_youtu.py └── plugins │ ├── __init__.py │ ├── message │ └── __init__.py │ ├── one │ └── __init__.py │ └── weather │ └── __init__.py ├── misc ├── dev-requirements.txt └── prod-requirements.txt ├── images └── love-grilfriend.png ├── .dockerignore ├── weather.sh ├── config ├── config-dev.yml └── config-prod.yml ├── .github └── workflows │ └── pythonapp.yml ├── .gitignore ├── README.md ├── docs └── README_EN.md └── LICENSE /pylintrc: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docker/healthcheck.sh: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /girlfriend/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /girlfriend/test/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /girlfriend/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /girlfriend/components/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /girlfriend/components/crontab.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /girlfriend/plugins/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /girlfriend/plugins/message/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /girlfriend/plugins/one/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /girlfriend/plugins/weather/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /misc/dev-requirements.txt: -------------------------------------------------------------------------------- 1 | -r prod-requirements.txt 2 | ipython 3 | pylint 4 | pycodestyle 5 | -------------------------------------------------------------------------------- /misc/prod-requirements.txt: -------------------------------------------------------------------------------- 1 | itchat==1.3.10 2 | pyyaml 3 | requests 4 | beautifulsoup4 5 | apscheduler 6 | -------------------------------------------------------------------------------- /images/love-grilfriend.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EscapeLife/love-grilfriend/HEAD/images/love-grilfriend.png -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | * 2 | .* 3 | !config/config-prod.yml 4 | !docker/healthcheck.sh 5 | !grilfriend 6 | !misc/requirements-prod.txt 7 | -------------------------------------------------------------------------------- /weather.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -eux 4 | 5 | CITY="Xi'an" 6 | LANGUAGE="zh-CN" 7 | UNIT=m 8 | UA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36" 9 | 10 | curl \ 11 | -H "Accept-Language: $LANGUAGE" \ 12 | -H "User-Agent: $UA" \ 13 | -o result.html \ 14 | wttr.in/$CITY?format=4\&$UNIT 15 | -------------------------------------------------------------------------------- /config/config-dev.yml: -------------------------------------------------------------------------------- 1 | # --------- 常用功能设置 --------- 2 | auto_hot_login: True 3 | 4 | 5 | # --------- 自动回复设置 --------- 6 | auto_replay: True 7 | 8 | auto_rebot: False 9 | rebot_conf: 10 | client_token: 'a8105204604a0b11e916f3879aae3b0b' 11 | 12 | replay_user_conf: 13 | - '文件传输助手' 14 | 15 | replay_group_conf: 16 | - group_name: 'ipython' 17 | at_key: True 18 | replay_key: 'ipython' 19 | 20 | 21 | # --------- 定时任务设置 --------- 22 | auto_alarm: True 23 | alarm_conf: 24 | alarm_time: '12:00' 25 | 26 | gril_friend: 27 | - wechat_alias: '文件传输助手' 28 | nickname: '小助手' 29 | city_name: '北京' 30 | alarm_time: '17:00' 31 | send_type: 32 | - one 33 | - weather 34 | send_message: 35 | - '把今天上传的图片整理成相册' 36 | love_date: '20190214' 37 | 38 | group_friend: 39 | - group_name: 'ipython' 40 | city_name: '西安' 41 | alarm_time: '21:00' 42 | send_type: 43 | - one 44 | - weather 45 | send_message: 46 | - 'I love ipython very much.' 47 | build_date: '20190601' 48 | -------------------------------------------------------------------------------- /config/config-prod.yml: -------------------------------------------------------------------------------- 1 | # --------- 常用功能设置 --------- 2 | 3 | # 是否启用微信热登录 4 | auto_hot_login: False 5 | 6 | 7 | # --------- 自动回复设置 --------- 8 | 9 | # 是否开启女友/群聊自动回复(默认为False) 10 | auto_replay: False 11 | 12 | # 是否开启自动回复智能机器人(默认为False) 13 | auto_rebot: False 14 | rebot_conf: 15 | client_token: 'a8105204604a0b11e916f3879aae3b0b' 16 | 17 | # 自动回复的女友/群聊名单(可为空|可添加多个) 18 | replay_user_conf: 19 | - '一只会飞的小猪' # 女友 20 | - '梦里花落知多少' # 女友 21 | 22 | # 自动回复的女友/群聊名单(可为空|可添加多个) 23 | replay_group_conf: 24 | - group_name: '袁家村狂狼分队' # 群聊 25 | at_key: False # 只有@自己时才会发送对应消息 26 | replay_key: '天气' # 只有触发特定关键词时才会发送对应消息 27 | 28 | 29 | # --------- 定时任务设置 --------- 30 | 31 | # 是否开启定时任务(默认为False) 32 | auto_alarm: False 33 | alarm_conf: 34 | alarm_time: '12:00' 35 | 36 | # 定时回复的女友名单(可为空|可添加多个) 37 | gril_friend: 38 | - wechat_alias: '一只会飞的小猪' # 微信昵称或备注名 39 | nickname: '小猪猪' # 回复消息的昵称 40 | city_name: '北京' # 所在城市 41 | alarm_time: '12:00' # 发送时间 42 | send_type: # 发送消息类型 43 | - one # 散文 44 | - weather # 天气 45 | send_message: # 发送文本消息 46 | - '今天中午我看见了一个美女' # 文本消息内容 47 | love_date: '20190214' # 相爱的日期 48 | - wechat_alias: '梦里花落知多少' 49 | nickname: '小花花' 50 | city_name: '上海' 51 | alarm_time: '17:00' 52 | send_type: 53 | - one 54 | - weather 55 | send_message: 56 | - '今天中午我看见了一个美女' 57 | love_date: '20190214' 58 | 59 | # 定时回复的群聊名单(可为空|可添加多个) 60 | group_friend: 61 | - group_name: '袁家村狂狼分队' 62 | city_name: '深圳' 63 | alarm_time: '21:00' 64 | send_type: 65 | - one 66 | - weather 67 | send_message: 68 | - '今天中午我看见了一个美女' 69 | build_date: '20190601' 70 | -------------------------------------------------------------------------------- /.github/workflows/pythonapp.yml: -------------------------------------------------------------------------------- 1 | # 定义workflow的名称 2 | name: love-grilfriend 3 | 4 | # 指定触发方式 5 | on: 6 | push: 7 | branches: 8 | - master 9 | schedule: 10 | - cron: '0 0 0 */12 0' 11 | 12 | # 定义job任务列表 13 | jobs: 14 | # 项目lint检查 15 | build_job: 16 | # 指定运行所需要的虚拟机环境 17 | runs-on: ubuntu-latest 18 | strategy: 19 | matrix: 20 | python-version: [3.6, 3.7] 21 | 22 | # 指定具体需要执行步骤和命令 23 | steps: 24 | - name: Checkout Codes 25 | uses: actions/checkout@v1 26 | - name: Setup Python 27 | uses: actions/setup-python@v1 28 | with: 29 | python-version: ${{ matrix.python-version }} 30 | architecture: x64 31 | # 步骤名称 32 | - name: Install Dependencies 33 | # 该步骤运行的命令或者action 34 | run: | 35 | python -m pip install --upgrade pip 36 | pip install -r requirements.txt 37 | - name: Python Lint 38 | run: inv lint 39 | - name: Python Test 40 | run: | 41 | pip install pytest 42 | pytest 43 | 44 | # 报错发送邮件 45 | send_mail_job: 46 | runs-on: ubuntu-latest 47 | steps: 48 | - name: Checkout Codes 49 | uses: actions/checkout@v1 50 | - name: Get Weather 51 | run: bash ./weather.sh 52 | - name: Send Mail 53 | uses: dawidd6/action-send-mail@master 54 | with: 55 | server_address: smtp.163.com 56 | server_port: 465 57 | username: ${{ secrets.MAIL_USERNAME }} 58 | password: ${{ secrets.MAIL_PASSWORD }} 59 | subject: Xi'an Weather Report 60 | body: file://result.html 61 | to: escapehack@gmail.com 62 | from: GitHub Actions 63 | content_type: text/html 64 | -------------------------------------------------------------------------------- /girlfriend/components/mass_texting.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | 一个消息群发助手 4 | 5 | Usage: 6 | itchat.auto_login(hotReload=True, enableCmdQR=2) 7 | MASS = MassMSG() 8 | MASS.send_all_friends_msg() 9 | MASS.send_special_group_msg() 10 | """ 11 | 12 | import time 13 | import itchat 14 | 15 | 16 | class MassMSG: 17 | """消息群发助手""" 18 | def __init__(self, greetings='Happy New Year', chatroom_name='mass_group'): 19 | self.greetings = greetings 20 | self.chatroom_name = chatroom_name 21 | 22 | def send_all_friends_msg(self): 23 | """给所有好友发送消息""" 24 | # 获取的好友列表首位是自己 25 | friends_list = itchat.get_friends(update=True)[1:] 26 | for friend in friends_list: 27 | # 如果用于演示则使用print方法 28 | itchat.send(f'{self.greetings} | {friend["NickName"]}', friend['UserName']) 29 | print(f'>>> send {friend["NickName"]} is finished.') 30 | time.sleep(3) 31 | 32 | def send_special_group_msg(self): 33 | """给指定群组内的成员发送消息""" 34 | itchat.get_friends(update=True) 35 | chatrooms = itchat.search_chatrooms(name=self.chatroom_name) 36 | if chatrooms is None: 37 | print(f'The {self.chatroom_name} is not found.') 38 | else: 39 | chatroom = itchat.update_chatroom(chatrooms[0]['UserName']) 40 | for friend in chatroom['MemberList']: 41 | friend = itchat.search_friends(userName=friend['UserName']) 42 | # 如果用于演示则使用print方法 43 | itchat.send(f'{self.greetings} | {friend["NickName"]}', friend['UserName']) 44 | print(f'>>> send {friend["NickName"]} is finished.') 45 | time.sleep(3) 46 | -------------------------------------------------------------------------------- /.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 | # macos 107 | .DS_Store 108 | 109 | # vscode 110 | .vscode/ 111 | -------------------------------------------------------------------------------- /girlfriend/utils/tencent.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """腾讯AI平台请求通用函数""" 3 | 4 | import time 5 | import random 6 | import string 7 | import base64 8 | import hashlib 9 | from urllib import parse 10 | 11 | import requests 12 | 13 | 14 | USERAGENT = { 15 | 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 \ 16 | (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36' 17 | } 18 | 19 | 20 | def get_time_stamp(): 21 | """生成请求时间戳""" 22 | return int(time.time()) 23 | 24 | def get_nonce_str(): 25 | """生成随机字符串""" 26 | return ''.join(random.sample(string.ascii_letters + string.digits, random.randint(10, 16))) 27 | 28 | def md5_encode(app_id): 29 | """生成会话标识 30 | :param app_id => 应用标识 31 | """ 32 | if not isinstance(app_id, str): 33 | app_id = str(app_id) 34 | md5 = hashlib.md5() 35 | md5.update(app_id.encode('utf-8')) 36 | encoded_str = md5.hexdigest().upper() 37 | return encoded_str 38 | 39 | def get_request_sign(parser, app_key): 40 | """获取请求签名 41 | :param app_key => 应用标识 42 | """ 43 | params = sorted(parser.items()) 44 | uri_str = parse.urlencode(params, encoding="UTF-8") 45 | sign_str = '{}&app_key={}'.format(uri_str, app_key) 46 | hash_md5 = hashlib.md5(sign_str.encode("UTF-8")) 47 | return hash_md5.hexdigest().upper() 48 | 49 | def get_base64(pic_param): 50 | """获取媒体的Base64字符串 51 | :param media_param => 媒体URL或者图片文件的BufferedReader对象 52 | """ 53 | if isinstance(pic_param, str): 54 | pic_data = requests.get(pic_param, headers=USERAGENT).content 55 | elif hasattr(pic_param, 'read'): 56 | pic_data = pic_param.read() 57 | else: 58 | raise TypeError('The picture media must be URL or BufferedReader.') 59 | # 将图片内容进行转码存储 60 | image = base64.b64encode(pic_data).decode("utf-8") 61 | return image 62 | -------------------------------------------------------------------------------- /girlfriend/components/ai_robot.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | 利用腾讯AI平台实现一个自动回复的聊天机器人 4 | 5 | Usage: 6 | from girlfriend.components.ai_robot import Chatbot 7 | it = Chatbot('your_app_id', 'your_app_key') 8 | it.run('text') 9 | """ 10 | 11 | from urllib import parse 12 | import requests 13 | 14 | from girlfriend.utils.tencent import get_time_stamp, get_nonce_str 15 | from girlfriend.utils.tencent import md5_encode, get_request_sign 16 | 17 | CONTENTTYPE = { 18 | 'Content-Type': 'application/x-www-form-urlencoded' 19 | } 20 | 21 | class Chatbot: 22 | """腾讯聊天机器人""" 23 | def __init__(self, app_id=None, app_key=None): 24 | self.app_id = app_id 25 | self.app_key = app_key 26 | self.tencent_chat_url = 'https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat' 27 | 28 | def make_params(self, text): 29 | """获取调用接口的参数""" 30 | if self.app_id is None or self.app_key is None: 31 | print('The app_id or app_key is none, please check.') 32 | params = { 33 | 'app_id': self.app_id, # 应用标识 34 | 'time_stamp': get_time_stamp(), # 请求时间戳(秒级) 35 | 'nonce_str': get_nonce_str(), # 随机字符串 36 | 'session': md5_encode(self.app_id), # 会话标识 37 | 'question': text # 用户输入的聊天内容 38 | } 39 | params['sign'] = get_request_sign(params, self.app_key) # 签名信息 40 | return params 41 | 42 | def run(self, text): 43 | """执行方法(可多次执行)""" 44 | params = self.make_params(text) 45 | response = requests.post(self.tencent_chat_url, 46 | data=parse.urlencode(params).encode("utf-8"), 47 | headers=CONTENTTYPE) 48 | if response.status_code == 200: 49 | data_dict = response.json() 50 | if data_dict['ret'] == 0: 51 | print(data_dict['data']['answer']) 52 | -------------------------------------------------------------------------------- /girlfriend/components/ai_picture.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | 利用腾讯AI平台识别图片内容信息并自动生成文字描述 4 | 5 | Usage: 6 | from girlfriend.components.ai_picture import PicToText 7 | it = PicToText('your_app_id', 'your_app_key') 8 | it.run('URL' or 'FileObj') 9 | """ 10 | 11 | from urllib import parse 12 | import requests 13 | 14 | from girlfriend.utils.tencent import get_time_stamp, get_nonce_str 15 | from girlfriend.utils.tencent import get_request_sign, get_base64 16 | 17 | CONTENTTYPE = { 18 | 'Content-Type': 'application/x-www-form-urlencoded' 19 | } 20 | 21 | 22 | class PicToText: 23 | """看图说话""" 24 | def __init__(self, app_id=None, app_key=None): 25 | self.app_id = app_id 26 | self.app_key = app_key 27 | self.tencent_pic_url = 'https://api.ai.qq.com/fcgi-bin/vision/vision_imgtotext' 28 | 29 | def make_params(self, pic_param): 30 | """获取调用接口的参数""" 31 | if self.app_id is None or self.app_key is None: 32 | print('The app_id or app_key is none, please check.') 33 | params = { 34 | 'app_id': self.app_id, # 应用标识 35 | 'time_stamp': get_time_stamp(), # 请求时间戳(秒级) 36 | 'nonce_str': get_nonce_str(), # 随机字符串 37 | 'image': get_base64(pic_param), # 图片内容 38 | 'session_id': get_time_stamp() # 会话标识 39 | } 40 | params['sign'] = get_request_sign(params, self.app_key) # 签名信息 41 | return params 42 | 43 | def run(self, pic_param): 44 | """执行方法(可多次执行)""" 45 | params = self.make_params(pic_param) 46 | response = requests.post(self.tencent_pic_url, 47 | data=parse.urlencode(params).encode("utf-8"), 48 | headers=CONTENTTYPE) 49 | if response.status_code == 200: 50 | data_dict = response.json() 51 | if data_dict['ret'] == 0: 52 | print(data_dict['data']['text']) 53 | -------------------------------------------------------------------------------- /girlfriend/components/ai_ocr.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | 利用腾讯AI平台实现实现图片的识别和提取 4 | Usage: 5 | from girlfriend.components.ai_ocr import PictureOCR 6 | it = PictureOCR('your_app_id', 'your_app_key') 7 | it.run('URL' or 'FileObj') 8 | """ 9 | 10 | from urllib import parse 11 | import requests 12 | 13 | from girlfriend.utils.tencent import get_time_stamp, get_nonce_str 14 | from girlfriend.utils.tencent import get_request_sign, get_base64 15 | 16 | CONTENTTYPE = { 17 | 'Content-Type': 'application/x-www-form-urlencoded' 18 | } 19 | 20 | 21 | class PictureOCR: 22 | """识别图片转文字""" 23 | def __init__(self, app_id=None, app_key=None, image_path=None, image_name=None): 24 | self.app_id = app_id 25 | self.app_key = app_key 26 | self.image_path = image_path 27 | self.image_name = image_name 28 | 29 | def make_params(self, pic_param): 30 | """获取调用接口的参数""" 31 | if self.app_id is None or self.app_key is None: 32 | print('The app_id or app_key is none, please check.') 33 | params = { 34 | 'app_id': self.app_id, # 应用标识 35 | 'image': get_base64(pic_param), # 图片内容 36 | 'card_type': 0, # 仅识别正面照片 37 | 'time_stamp': get_time_stamp(), # 请求时间戳(秒级) 38 | 'nonce_str': get_nonce_str() # 随机字符串 39 | } 40 | params['sign'] = get_request_sign(params, self.app_key) # 签名信息 41 | return params 42 | 43 | def id_card_run(self, pic_param): 44 | """识别身份证图片(正面照片)(可多次执行)""" 45 | id_card_url = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_idcardocr' 46 | params = self.make_params(pic_param) 47 | response = requests.post(id_card_url, 48 | data=parse.urlencode(params).encode("utf-8"), 49 | headers=CONTENTTYPE) 50 | if response.status_code == 200: 51 | data_dict = response.json() 52 | if data_dict['ret'] == 0: 53 | id_card_data = data_dict['data'] 54 | print(', '.join([id_card_data['name'], id_card_data['sex'], 55 | id_card_data['nation'], id_card_data['birth'], 56 | id_card_data['address'], id_card_data['id']])) 57 | -------------------------------------------------------------------------------- /girlfriend/test/confirm_friend.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | 检测是否有删除自己或把自己拉入黑名单的朋友 4 | 原理: 将好友拉入群聊时,非好友和黑名单好友不会被拉入群聊 5 | 隐蔽: 群聊在第一次产生普通消息时才会被除创建者以外的人发现的(系统消息不算普通消息) 6 | 7 | Usage: 8 | itchat.auto_login(hotReload=True, enableCmdQR=2) 9 | CONFIRM = ConfirmFriend() 10 | CONFIRM.get_friend_status() 11 | """ 12 | 13 | import itchat 14 | 15 | 16 | class ConfirmFriend: 17 | """自动检测好友关系""" 18 | def __init__(self, chatroom=None, chatroom_name='friends'): 19 | self.chatroom = chatroom 20 | self.chatroom_name = chatroom_name 21 | self.status_dict = {3: '该好友已经将你加入黑名单', 4: '该好友已经将你删除'} 22 | 23 | def get_chatroom(self): 24 | """获取群组信息""" 25 | if self.chatroom is None: 26 | itchat.get_chatrooms(update=True) 27 | chatrooms = itchat.search_chatrooms(self.chatroom) 28 | if chatrooms: 29 | return chatrooms[0] 30 | room = itchat.create_chatroom(itchat.get_friends()[1:4], topic=self.chatroom_name) 31 | if room['BaseResponse']['ErrMsg'] == '': 32 | self.chatroom = {'UserName': room['ChatRoomName']} 33 | return self.chatroom 34 | return self.chatroom 35 | 36 | def get_friend_status(self): 37 | """获取好友状态""" 38 | own_account = itchat.get_friends(update=True)[0] 39 | friends_total_number = len(itchat.get_friends()) 40 | for num in range(friends_total_number): 41 | friend = itchat.get_friends()[num] 42 | if friend['UserName'] == own_account['UserName']: 43 | return "This account is not checked for my account." 44 | if itchat.search_friends(userName=friend['UserName']) is None: 45 | return "The user is not exist." 46 | 47 | chatroom = self.chatroom or self.get_chatroom() 48 | if chatroom is None: 49 | return '无法自动创建群聊请手动创建 | 创建后请将群聊保存到通讯录' 50 | room = itchat.add_member_into_chatroom(chatroom['UserName'], [friend]) 51 | if room['BaseResponse']['ErrMsg'] == '': 52 | friend_status = room['MemberList'][0]['MemberStatus'] 53 | itchat.delete_member_from_chatroom(chatroom['UserName'], [friend]) 54 | return self.status_dict.get(friend_status, '该好友仍旧与你是好友关系') 55 | return u'无法获取好友状态,预计已经达到接口调用限制。' 56 | -------------------------------------------------------------------------------- /girlfriend/test/learn_itchat.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """简单介绍了关于itchat模块的使用技巧""" 3 | 4 | import time 5 | import json 6 | 7 | import itchat 8 | 9 | 10 | # ------------------------------------------------ 11 | # 发送消息给好友 12 | # ------------------------------------------------ 13 | friend = itchat.search_friends(nickName='Escape')[0] 14 | print(json.dumps(friend, sort_keys=True, indent=4, separators=(',', ':'))) 15 | friend.send('Happy New Year.') 16 | 17 | # ------------------------------------------------ 18 | # 发送消息给群聊 19 | # ------------------------------------------------ 20 | chatroom = itchat.search_chatrooms(name='EscapeGroup')[0] 21 | print(json.dumps(chatroom, sort_keys=True, indent=4, separators=(',', ':'))) 22 | chatroom.send('Happy New Year.') 23 | 24 | 25 | 26 | # ------------------------------------------------ 27 | # 各类型消息的注册 28 | # 就日常的各种信息进行获取与回复 29 | # ------------------------------------------------ 30 | @itchat.msg_register([itchat.content.TEXT, itchat.content.MAP, itchat.content.CARD, itchat.content.NOTE, itchat.content.SHARING]) 31 | def text_friend_reply(msg): 32 | msg.user.send('%s: %s' % (msg.type, msg.text)) 33 | 34 | @itchat.msg_register([itchat.content.PICTURE, itchat.content.RECORDING, itchat.content.ATTACHMENT, itchat.content.VIDEO]) 35 | def download_files(msg): 36 | msg.download(msg.fileName) 37 | typeSymbol = { 38 | itchat.content.PICTURE: 'img', 39 | itchat.content.VIDEO: 'vid', }.get(msg.type, 'fil') 40 | return '@%s@%s' % (typeSymbol, msg.fileName) 41 | 42 | @itchat.msg_register(itchat.content.FRIENDS) 43 | def add_friend(msg): 44 | msg.user.verify() 45 | msg.user.send('Nice to meet you!') 46 | 47 | @itchat.msg_register(itchat.content.TEXT, isGroupChat=True) 48 | def text_chatroom_reply(msg): 49 | if msg.isAt: 50 | msg.user.send(u'@%s\u2005I received: %s' % (msg.actualNickName, msg.text)) 51 | 52 | itchat.auto_login(hotReload=True, enableCmdQR=2) 53 | itchat.run(debug=True, blockThread=True) 54 | 55 | # ------------------------------------------------ 56 | # 实现微信消息的获取 57 | # 文本对应 itchat.content.TEXT 58 | # 图片对应 itchat.content.PICTURE 59 | # 语音对应 itchat.content.RECORDING 60 | # 名片对应 itchat.content.CARD 61 | # ------------------------------------------------ 62 | @itchat.msg_register(itchat.content.TEXT) 63 | def print_content(msg): 64 | print(msg['Text']) 65 | 66 | itchat.auto_login(hotReload=True, enableCmdQR=2) 67 | itchat.run(debug=True, blockThread=True) 68 | 69 | # ------------------------------------------------ 70 | # 实现微信消息的发送 71 | # 微信可以发送各类消息/文本/图片/文件等 72 | # ------------------------------------------------ 73 | itchat.auto_login(hotReload=True, enableCmdQR=2) 74 | itchat.send(u'测试消息发送', 'filehelper') 75 | 76 | 77 | 78 | # ------------------------------------------------ 79 | # 退出及登陆完成后调用特定方法 80 | # ------------------------------------------------ 81 | def lc(): 82 | print('### login wechat ###') 83 | 84 | def ec(): 85 | print('### logout wechat ###') 86 | 87 | itchat.auto_login(hotReload=True, enableCmdQR=2, loginCallback=lc, exitCallback=ec) 88 | time.sleep(3) 89 | itchat.logout() 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 |
8 | love_girlfriend ❤️爱自己女朋友就是爱自己 9 |
10 | 11 | 17 | 18 |  19 | 20 | ## 1. 项目介绍 21 | 22 | > **👊 女朋友 -> 老婆 👰🏻** 23 | 24 | 俗话说得好,爱自己的女朋友就是爱自己。那问题的关键就来了,我们需要怎么爱呢?该项目就是为了关心女友而创建,借鉴 [EverydayWechat](https://github.com/sfyc23/EverydayWechat) 项目,主要包含以下爱护功能: 25 | 26 | - **每日定时给好友发送特定消息** 27 | - 暖心话 28 | - 定时提醒 29 | - **机器人自动回复好友消息** 30 | - 忙碌时自动回复 31 | - 非工作时间勿扰 32 | - **附加扩展功能** 33 | - 天气信息 34 | - 每日一句 35 | - 每日诗歌 36 | - 票房数据 37 | 38 | ## 2. API 接口 39 | 40 | > **介绍项目中主要使用的 API 接口信息** 41 | 42 | - **天气信息** 43 | - [**`SOJSON`**](sojson.com/blog/305.html) 44 | - [**`RollToolsApi`**](https://github.com/MZCretin/RollToolsApi#%E8%8E%B7%E5%8F%96%E7%89%B9%E5%AE%9A%E5%9F%8E%E5%B8%82%E4%BB%8A%E6%97%A5%E5%A4%A9%E6%B0%94) 45 | - **每日一句** 46 | - [**`One`**](http://wufazhuce.com) 47 | - [**`一言`**](hitokoto.cn) 48 | - **智能机器人** 49 | - [**`腾讯智能闲聊`**](ai.qq.com/product/nlpchat.shtml) 50 | - **票房数据** 51 | - [**`猫眼实时票房`**](piaofang.maoyan.com/dashboard) 52 | - **空气质量** 53 | - [**`aqicn`**](http://aqicn.org/here) 54 | 55 | ## 3. 配置文件 56 | 57 | > **介绍配置文件的配置方式和方法** 58 | 59 | ```yaml 60 | 61 | ``` 62 | 63 | ## 4. 效果展示 64 | 65 | > **请看效果截图** 66 | 67 | ![love_girlfriend]() 68 | 69 | ## 5. 计划列表 70 | 71 | > **准备或者预计实现的功能和特性** 72 | 73 | - 将定时发送消息转换成图片形式 74 | - 附加扩展功能支持每日诗歌 75 | - 附加扩展功能支持地图查询([高德地图](https://lbs.amap.com/)|[腾讯地图](https://lbs.qq.com/webservice_v1/index.html)) 76 | - 附加扩展功能支持新闻资讯([feedly](https://developer.feedly.com/)|[HackerNews](https://github.com/HackerNews/API)|[纽约时报](https://developer.nytimes.com/)) 77 | - 附加扩展功能支持图片摄影([px500](https://github.com/500px/legacy-api-documentation)) 78 | 79 | ## 6. 参考项目 80 | 81 | > **下面是该项目参考项目** 82 | 83 | - [**`ItChat` - 微信个人号接口、微信机器人及命令行微信**](https://github.com/littlecodersh/ItChat) 84 | - [**`EverydayWechat` - 基于 Python3 与 Itchat 的微信小工具**](https://github.com/sfyc23/EverydayWechat) 85 | - [**`daily-warm` - 每天定时发邮件给你关心的人**](https://github.com/BarryYan/daily-warm) 86 | 87 | ## 7. 联系方式 88 | 89 | > **以下是我的个人联系方式** 90 | 91 |
92 |
93 |
2 |
3 |
4 |
5 |
8 | love_girlfriend 9 |
10 | 11 | 17 | 18 | ## 1. Introduction 19 | 20 | > **👊 girlfriend - > the wife 👰** 21 | 22 | As the saying goes, love your girlfriend is love yourself. That's the question, how do we need to love? This project is created to care about my girlfriend. It borrows from EverydayWechat project and mainly contains the following functions: 23 | 24 | - **send specific messages to friends at regular times every day** 25 | - warm heart words 26 | - regular reminders 27 | - **the robot automatically replies to friend messages** 28 | - auto reply when busy 29 | - not during non-working hours 30 | - **additional extensions** 31 | - weather information 32 | - sentence of the day 33 | - poetry of the day 34 | - box office data 35 | 36 | ## 2. API 37 | 38 | > **Describes the main API interface information used in the project** 39 | 40 | - **Weather information** 41 | - [**`SOJSON`**](sojson.com/blog/305.html) 42 | - [**`RollToolsApi`**](https://github.com/MZCretin/RollToolsApi#%E8%8E%B7%E5%8F%96%E7%89%B9%E5%AE%9A%E5%9F%8E%E5%B8%82%E4%BB%8A%E6%97%A5%E5%A4%A9%E6%B0%94) 43 | - **Daily sentence** 44 | - [**`One`**](http://wufazhuce.com) 45 | - [**`Hitokoto`**](hitokoto.cn) 46 | - **Intelligent robot** 47 | - [**`TencentChat`**](ai.qq.com/product/nlpchat.shtml ) 48 | - **Sales data** 49 | - [**`Maoyan`**](piaofang.maoyan.com/dashboard) 50 | - **Air quality** 51 | - [**`Aqicn`**](http://aqicn.org/here) 52 | 53 | ## 3. Configuration 54 | 55 | > **Describes the configuration methods and methods of configuration files** 56 | 57 | ```yaml 58 | 59 | ``` 60 | 61 | ## 4. Exhibition 62 | 63 | > **Please see the screenshot.** 64 | 65 | ![exhibition]() 66 | 67 | ## 5. TodoList 68 | 69 | > **Functions and features to be prepared or expected to be implemented** 70 | 71 | - Converts a timed send message into a picture 72 | - Additional extensions support daily poetry 73 | 74 | ## 6. Credits 75 | 76 | > **The following is the project reference project** 77 | 78 | - [**`ItChat`**](https://github.com/littlecodersh/ItChat) 79 | - [**`EverydayWechat`**](https://github.com/sfyc23/EverydayWechat) 80 | - [**`daily-warm`**](https://github.com/BarryYan/daily-warm) 81 | 82 | ## 7. Contact 83 | 84 | > **Below is my personal contact information.** 85 | 86 |
87 |
88 |