mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 13:28:11 +08:00
init: build minimum viable version
This commit is contained in:
BIN
channel/__pycache__/channel.cpython-36.pyc
Normal file
BIN
channel/__pycache__/channel.cpython-36.pyc
Normal file
Binary file not shown.
BIN
channel/__pycache__/channel_factory.cpython-36.pyc
Normal file
BIN
channel/__pycache__/channel_factory.cpython-36.pyc
Normal file
Binary file not shown.
31
channel/channel.py
Normal file
31
channel/channel.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
Message sending channel abstract class
|
||||
"""
|
||||
|
||||
from bridge.bridge import Bridge
|
||||
|
||||
class Channel(object):
|
||||
def startup(self):
|
||||
"""
|
||||
init channel
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def handle(self, msg):
|
||||
"""
|
||||
process received msg
|
||||
:param msg: message object
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def send(self, msg, receiver):
|
||||
"""
|
||||
send message to user
|
||||
:param msg: message content
|
||||
:param receiver: receiver channel account
|
||||
:return:
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def build_reply_content(self, query):
|
||||
return Bridge().fetch_reply_content(query)
|
||||
15
channel/channel_factory.py
Normal file
15
channel/channel_factory.py
Normal file
@@ -0,0 +1,15 @@
|
||||
"""
|
||||
channel factory
|
||||
"""
|
||||
|
||||
from channel.wechat.wechat_channel import WechatChannel
|
||||
|
||||
def create_channel(channel_type):
|
||||
"""
|
||||
create a channel instance
|
||||
:param channel_type: channel type code
|
||||
:return: channel instance
|
||||
"""
|
||||
if channel_type == 'wx':
|
||||
return WechatChannel()
|
||||
raise RuntimeError
|
||||
BIN
channel/wechat/__pycache__/wechat_channel.cpython-36.pyc
Normal file
BIN
channel/wechat/__pycache__/wechat_channel.cpython-36.pyc
Normal file
Binary file not shown.
39
channel/wechat/wechat_channel.py
Normal file
39
channel/wechat/wechat_channel.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""
|
||||
wechat channel
|
||||
"""
|
||||
import itchat
|
||||
import time
|
||||
import random
|
||||
import json
|
||||
from itchat.content import *
|
||||
from channel.channel import Channel
|
||||
|
||||
|
||||
@itchat.msg_register([TEXT])
|
||||
def handler_receive_msg(msg):
|
||||
WechatChannel().handle(msg)
|
||||
|
||||
|
||||
class WechatChannel(Channel):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def startup(self):
|
||||
# login by scan QRCode
|
||||
itchat.auto_login()
|
||||
|
||||
# start message listener
|
||||
itchat.run()
|
||||
|
||||
def handle(self, msg):
|
||||
print("handle: ", msg)
|
||||
print(json.dumps(msg, ensure_ascii=False))
|
||||
from_user_id = msg['FromUserName']
|
||||
other_user_id = msg['User']['UserName']
|
||||
if from_user_id == other_user_id:
|
||||
self.send(super().build_reply_content(msg['Text']), from_user_id)
|
||||
|
||||
def send(self, msg, receiver):
|
||||
time.sleep(random.randint(1, 3))
|
||||
print(msg, receiver)
|
||||
itchat.send(msg + " [bot]", toUserName=receiver)
|
||||
Reference in New Issue
Block a user