Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持多channel #2438

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ def func(_signo, _stack_frame):
conf().save_user_datas()
if callable(old_handler): # check old_handler
return old_handler(_signo, _stack_frame)
stop_event.set()
sys.exit(0)

signal.signal(_signo, func)


def start_channel(channel_name: str):
channel = channel_factory.create_channel(channel_name)
if channel_name in ["wx", "wxy", "terminal", "wechatmp","web", "wechatmp_service", "wechatcom_app", "wework",
const.FEISHU, const.DINGTALK]:
PluginManager().load_plugins()

if conf().get("use_linkai"):
try:
Expand All @@ -40,6 +38,7 @@ def start_channel(channel_name: str):
channel.startup()


stop_event = threading.Event()
def run():
try:
# load config
Expand All @@ -49,19 +48,20 @@ def run():
# kill signal
sigterm_handler_wrap(signal.SIGTERM)

# create channel
channel_name = conf().get("channel_type", "wx")

if "--cmd" in sys.argv:
channel_name = "terminal"

if channel_name == "wxy":
os.environ["WECHATY_LOG"] = "warn"

start_channel(channel_name)

while True:
time.sleep(1)
PluginManager().load_plugins()

channel_name = "terminal" if "--cmd" in sys.argv else conf().get("channel_type", "wx")
channel_names = conf().get("channel_types", [])
if channel_name not in channel_names:
channel_names.append(channel_name)
channel_names=list(set(channel_names))

for name in channel_names:
if name == "wxy":
os.environ["WECHATY_LOG"] = "warn"
threading.Thread(target=start_channel, args=(name,)).start()

stop_event.wait()
except Exception as e:
logger.error("App startup failed!")
logger.exception(e)
Expand Down
6 changes: 3 additions & 3 deletions channel/chat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
class ChatChannel(Channel):
name = None # 登录的用户名
user_id = None # 登录的用户id
futures = {} # 记录每个session_id提交到线程池的future对象, 用于重置会话时把没执行的future取消掉,正在执行的不会被取消
sessions = {} # 用于控制并发,每个session_id同时只能有一个context在处理
lock = threading.Lock() # 用于控制对sessions的访问

def __init__(self):
self.futures = {} # 记录每个session_id提交到线程池的future对象, 用于重置会话时把没执行的future取消掉,正在执行的不会被取消
self.sessions = {} # 用于控制并发,每个session_id同时只能有一个context在处理
self.lock = threading.Lock() # 用于控制对sessions的访问
_thread = threading.Thread(target=self.consume)
_thread.setDaemon(True)
_thread.start()
Expand Down
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
"clear_memory_commands": ["#清除记忆"], # 重置会话指令,必须以#开头
# channel配置
"channel_type": "", # 通道类型,支持:{wx,wxy,terminal,wechatmp,wechatmp_service,wechatcom_app,dingtalk}
"channel_types": [], # 支持多通道,同时会合并 channel_type
"subscribe_msg": "", # 订阅消息, 支持: wechatmp, wechatmp_service, wechatcom_app
"debug": False, # 是否开启debug模式,开启后会打印更多日志
"appdata_dir": "", # 数据目录
Expand Down