You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # encoding:utf-8
  2. import os
  3. import signal
  4. import sys
  5. import time
  6. from channel import channel_factory
  7. from common import const
  8. from config import load_config
  9. from plugins import *
  10. import threading
  11. def sigterm_handler_wrap(_signo):
  12. old_handler = signal.getsignal(_signo)
  13. def func(_signo, _stack_frame):
  14. logger.info("signal {} received, exiting...".format(_signo))
  15. conf().save_user_datas()
  16. if callable(old_handler): # check old_handler
  17. return old_handler(_signo, _stack_frame)
  18. sys.exit(0)
  19. signal.signal(_signo, func)
  20. def start_channel(channel_name: str):
  21. channel = channel_factory.create_channel(channel_name)
  22. if channel_name in ["wx", "wxy", "terminal", "wechatmp", "wechatmp_service", "wechatcom_app", "wework",
  23. const.FEISHU, const.DINGTALK]:
  24. PluginManager().load_plugins()
  25. if conf().get("use_linkai"):
  26. try:
  27. from common import linkai_client
  28. threading.Thread(target=linkai_client.start, args=(channel,)).start()
  29. except Exception as e:
  30. pass
  31. channel.startup()
  32. def run():
  33. try:
  34. # load config
  35. load_config()
  36. # ctrl + c
  37. sigterm_handler_wrap(signal.SIGINT)
  38. # kill signal
  39. sigterm_handler_wrap(signal.SIGTERM)
  40. # create channel
  41. channel_name = conf().get("channel_type", "wx")
  42. if "--cmd" in sys.argv:
  43. channel_name = "terminal"
  44. if channel_name == "wxy":
  45. os.environ["WECHATY_LOG"] = "warn"
  46. start_channel(channel_name)
  47. while True:
  48. time.sleep(1)
  49. except Exception as e:
  50. logger.error("App startup failed!")
  51. logger.exception(e)
  52. if __name__ == "__main__":
  53. run()