from celery.task.control import revoke from wechat.celery import app from wxpy.exceptions import ResponseError from itchat.signals import logged_out def restart_listener(sender, **kw): task_id = r.get(LISTENER_TASK_KEY) if task_id: revoke(str(task_id, 'utf-8')) task_id = app.send_task('wechat.tasks.listener') r.set(LISTENER_TASK_KEY, task_id) logged_out.connect(restart_listener) from wxpy.signals import stopped from libs.wx import gen_avatar_path, get_bot from views.api import json_api from models.redis import db as r, LISTENER_TASK_KEY from app import app as sse_api from ext import db, sse from models.core import User, Group, MP # noqa from models.messaging import Notification stopped.connect(restart_listener) MP_FIELD = ['nick_name', 'signature', 'province', 'city'] USER_FIELD = MP_FIELD + ['sex'] bot = get_bot()
} def publish(uuid, **kwargs): data = { 'uuid': uuid, 'extra': kwargs.pop('extra', None), 'type': kwargs.pop('type', None) } data.update(kwargs) send_event(event_name='login', data=json.dumps(data), channel='wechat') scan_qr_code.connect(publish) confirm_login.connect(publish) logged_out.connect(publish) def clean_msg_notify(receiver_id): """ Delete new message notification key in redis for logged-in user after logout :param receiver_id: user PUID retrieved from wxpy.bot """ Notification.clean_by_receiver_id(receiver_id) def new_msg_notify(receiver_id, clean=False): """ Publish new message notification to redis for the user, then FE listener could get new message count
MP_FIELD = ['sex', 'nick_name', 'signature', 'province', 'city'] USER_FIELD = MP_FIELD + ['sex'] def publish(uuid, **kw): from app import app with app.app_context(): params = {'uuid': uuid, 'extra': kw.pop('extra', None), 'type': kw.pop('type', None)} params.update(kw) sse.publish(params, type='login') scan_qr_code.connect(publish) confirm_login.connect(publish) logged_out.connect(publish) from wxpy import * # noqa def get_bot(): bot = Bot('bot.pkl', qr_path=os.path.join( here, '../static/img/qr_code.png')) bot.enable_puid() bot.messages.max_history = 0 return bot from models.core import User, Group, MP # noqa
def process_qr_code(uuid, **kw): from app import app with app.app_context(): params = { 'uuid': uuid, 'extra': kw.pop('extra', None), 'type': kw.pop('type', None) } params.update(kw) sse.publish(params, type='login') scan_qr_code.connect(publish) confirm_login.connect(publish) logged_out.connect(logout_publish) from wxpy import * # noqa def get_bot(bot_id): bot = Bot('bot_{}.pkl'.format(bot_id), qr_path=os.path.join( here, '../static/img/qr_code_{}.png'.format(bot_id)), console_qr=None) bot.enable_puid() bot.messages.max_history = 0 return bot def gen_avatar_path(puid, force=False):