1. 程式人生 > >微信自動回覆和群聊訊息完善

微信自動回覆和群聊訊息完善

將資料新增到伺服器mysql中,增加群聊訊息,增加圖靈機器人回覆訊息

import os
import json
import itchat
import requests
import pprint
import time
import random
from pymysql import *
from itchat.content import *

# 2.支援的微信訊息型別:TEXT文字, PICTURE圖片, MAP地點, CARD名片, NOTE通知, SHARING分享, RECORDING語音, ATTACHMENT附件, VIDEO視訊
"""一系列初始化功能"""
conn = connect(host
='localhost', port=3306, database='wechat', user='root', password='******', charset='utf8') cs1 = conn.cursor() def tuling(text): url = "http://www.tuling123.com/openapi/api?" data = { "key": "********************", "info": text, } response = requests.post(url, data=json.dumps(data)) content = response.text data = json.loads(content) answer = data.get('text'
, "你說的是什麼?") # print(answer) if data.get("list"): answer1 = data['list'] for an in answer1: answer += an["source"] + an["article"] + an["icon"] + an["detailurl"] # print(an["source"]) # print(an["article"]) # print(an["icon"]) # print(an["detailurl"])
return answer # 設定接收請求 @itchat.msg_register([TEXT, PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO]) def run(msg): """執行程式""" global TDlist global fpath global cs1 global conn # print(msg['Type']) td_name = msg["User"]["NickName"] # print(td_name) who = itchat.search_friends() text = msg["Text"] if who.get("UserName") != msg.get("FromUserName"): # print(msg['Type']) # 查詢資料庫,看看改使用者有沒有退訂服務 # 1.查詢操作 # 編寫sql 查詢語句 user 對應我的表名 sql = "select * from user_name where name=%s;" cs1.execute(sql, td_name) # 執行sql語句 results = cs1.fetchall() # 獲取查詢的所有記錄 # print(results,"ces") if msg['Type'] == 'Text': """文字訊息""" # friend = itchat.search_friends(userName=msg['FromUserName']) # 獲取傳送過來的訊息 if text == "退訂": sql = """insert into user_name values(0,%s);""" cs1.execute(sql, td_name) # 執行sql語句 conn.commit() results = cs1.fetchall() # 獲取查詢的所有記錄 itchat.send_msg(msg="小可愛,人家會想你的,記得有時間傳送訂閱開啟我哦!", toUserName=msg["FromUserName"]) elif text == "訂閱": count = cs1.execute("delete from user_name where name=%s;", (td_name,)) print(count) conn.commit() itchat.send_msg(msg="歡迎訂閱,回覆'美女圖片'有驚喜哦!" + "----(可回覆'退訂'和'訂閱')---來自壯壯的小助手", toUserName=msg["FromUserName"]) sql = "select * from user_name where name=%s;" cs1.execute(sql, td_name) # 執行sql語句 results = cs1.fetchall() # 獲取查詢的所有記錄 if text == "訂閱": results = 1 if not results: if msg['Type'] == 'Text': """文字訊息""" # print(who.get("UserName")) # print(msg.get("FromUserName") if text =="美女圖片": # 獲取當前資料夾下所有的檔名 data = itchat.search_friends(userName=msg["FromUserName"]) user_name = data["RemarkName"] if data["RemarkName"] != "" else data["NickName"] # print(user_name) msg_content = msg['FileName'] list_dir = next(os.walk("./images"))[1] # 獲取當前資料夾下所有檔案 img_ur = random.choice(list_dir) img_list = os.listdir("./images/" + img_ur) img = random.choice(img_list) # print(img) # msg['Text'](".\\images\\" + str(user_name) + "-" + str(int(time.time())) + "-" + str(msg_content)) # itchat.send_msg(msg="在圖片庫中隨機選擇一張圖片", toUserName=msg["FromUserName"]) ur = "./images/" + img_ur +"/"+ str(img) # print(ur) itchat.send_image(ur, toUserName=msg["FromUserName"]) else: result = tuling(text) # 請求圖靈機器人 # 獲取好友發過來的訊息 itchat.send_msg(msg=result + "----(可回覆'退訂'和'訂閱')---來自壯壯的小助手", toUserName=msg["FromUserName"]) elif msg['Type'] == 'Recording': """語言訊息""" itchat.send_msg(msg="我可以回覆語音訊息,但是主人沒有錄製~~", toUserName=msg["FromUserName"]) elif msg['Type'] == 'Picture': """圖片訊息""" data = itchat.search_friends(userName=msg["FromUserName"]) user_name = data["RemarkName"] if data["RemarkName"] != "" else data["NickName"] # print(user_name) msg_content = msg['FileName'] # 獲取當前資料夾下所有的檔名 list_dir = next(os.walk("./images"))[1] # 獲取當前資料夾下所有檔案 aa = random.choice(list_dir) img_list = os.listdir("./images/" + aa) img = random.choice(img_list) # print(img) msg['Text']("./images/照片/" + str(user_name) + "-" + str(int(time.time())) + "-" + str(msg_content)) # itchat.send_msg(msg="在圖片庫中隨機選擇一張圖片", toUserName=msg["FromUserName"]) itchat.send_image("./images/" + aa +"/"+ img, toUserName=msg["FromUserName"]) @itchat.msg_register([TEXT, NOTE], isGroupChat=True) def text_reply(msg): # print(msg) if (msg.isAt): text = msg["Text"] result = tuling(text) # 請求圖靈機器人 # 獲取好友發過來的訊息 itchat.send_msg(msg=result + "----(可回覆'退訂'和'訂閱')---來自壯壯的小助手", toUserName=msg["FromUserName"]) if __name__ == '__main__': # 登陸微信 itchat.auto_login(hotReload=True, enableCmdQR=True) itchat.run()