disable_user_daily_push.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:fenc=utf-8
  4. """
  5. Agent在和用户对话过程中可能识别到用户不想再收消息,Agent会做识别,但需将信息同步至Growth Manager
  6. """
  7. import json
  8. import pymysql.cursors
  9. from pqai_agent import configs
  10. from pqai_agent.user_manager import MySQLUserManager, MySQLUserRelationManager
  11. if __name__ == '__main__':
  12. config = configs.get()
  13. user_db_config = config['storage']['user']
  14. staff_db_config = config['storage']['staff']
  15. user_manager = MySQLUserManager(user_db_config['mysql'], user_db_config['table'], staff_db_config['table'])
  16. wecom_db_config = config['storage']['user_relation']
  17. user_relation_manager = MySQLUserRelationManager(
  18. user_db_config['mysql'], wecom_db_config['mysql'],
  19. config['storage']['staff']['table'],
  20. user_db_config['table'],
  21. wecom_db_config['table']['staff'],
  22. wecom_db_config['table']['relation'],
  23. wecom_db_config['table']['user']
  24. )
  25. sql = f"SELECT wxid, name, profile_data_v1 FROM {user_manager.table_name} WHERE profile_data_v1 IS NOT NULL"
  26. users = user_manager.db.select(sql, pymysql.cursors.DictCursor)
  27. for user in users:
  28. profile = json.loads(user['profile_data_v1'])
  29. if profile.get('interaction_frequency', None) == 'stopped':
  30. user_id = user['wxid']
  31. user_name = user['name']
  32. print(f'update user[{user_id}] {user_name}: stop group message')
  33. sql = f"UPDATE we_com_user SET group_msg_disabled = 1 WHERE union_id = %s"
  34. rows = user_relation_manager.wecom_db.execute(sql, (user_id, ))
  35. print(f'affected rows: {rows}')