基于 chatGPT和WeChat的微信机器人
1、 灵活使用chatGPT
首先注册 openapi 账号是重中之重,网址:https://chat.openai.com/chat
拿到apikey
其次这个有一个使用和收费政策,根据个人情况来试用,是按照词汇量来计算的
2、接入到微信
2.1、使用wxauto来直接操作微信窗口
前面尝试过ITchat 和 pyautogui(快捷键)pyperclip(剪切板) 效果不是很好
目前可实现自动回复的完整代码:(还需继续优化,望各路大神指点)
# -*- coding:utf-8 -*- #!/usr/bin/python3 ##################################################################### #Author : zhoujt #Tel : 917644912053 #Date : 2022-12-20 #FileName : main.py #Description: The combination of AI and wechat ##################################################################### import openai from wxauto import * from datetime import date, datetime import time wx = WeChat() all_word = [] # Author's wechat whoami = 'Anonymous' # Answer the last piece of information def answer_meg(question): # api needs to register on the official website openai.api_key = "skeiqwytsnavcsdvuyetrqwebfnmfdsbfgasnLGTMwMZ" completion = openai.Completion.create( engine="text-davinci-003", prompt=question, max_tokens=1024, temperature=1 ) an_info = completion.choices[0].text print("Answer: %s" % an_info) return an_info # Get the last message def get_endmegs(): lastmsgs = wx.GetLastMessage print("Question: %s" % lastmsgs[1]) return lastmsgs[1] # Sending a Solution def send_megs(meg_info): msg = meg_info # The "who" is chat sender, which can also be a group name who = 'About me' wx.ChatWith(who) wx.SendMsg(msg) def diff_info(): lastmsgs = wx.GetLastMessage print("The last piece of data: %s" % lastmsgs[2]) last_word = lastmsgs[2] last_sender = lastmsgs[0] f = open('allmessage.txt', 'r') for line in f.readlines(): line = line[-17:] line = line.replace("')", "") line = line.replace("\n", "") all_word.append(line) if last_word in all_word: print("No last message") elif last_sender in whoami: print("Robots speak without reply") else: print("I have a new message to respond") info = answer_meg(question=get_endmegs()) send_megs(meg_info=info) if __name__ == '__main__': while True: diff_info() # update the all message log lastinfo = wx.GetAllMessage with open('allmessage.txt', 'w') as f: for ilog in lastinfo: f.write(str(ilog) + '\n') f.close() print(datetime.now()) time.sleep(10)
结尾 end.....