disable_user_daily_push.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. agent_db_config = config['database']['ai_agent']
  16. growth_db_config = config['database']['growth']
  17. user_manager = MySQLUserManager(agent_db_config, user_db_config['table'], staff_db_config['table'])
  18. wecom_db_config = config['storage']['user_relation']
  19. user_relation_manager = MySQLUserRelationManager(
  20. agent_db_config, growth_db_config,
  21. config['storage']['staff']['table'],
  22. user_db_config['table'],
  23. wecom_db_config['table']['staff'],
  24. wecom_db_config['table']['relation'],
  25. wecom_db_config['table']['user']
  26. )
  27. sql = f"SELECT wxid, name, profile_data_v1 FROM {user_manager.table_name} WHERE profile_data_v1 IS NOT NULL"
  28. users = user_manager.db.select(sql, pymysql.cursors.DictCursor)
  29. for user in users:
  30. profile = json.loads(user['profile_data_v1'])
  31. if profile.get('interaction_frequency', None) == 'stopped':
  32. user_id = user['wxid']
  33. user_name = user['name']
  34. print(f'update user[{user_id}] {user_name}: stop group message')
  35. sql = f"UPDATE we_com_user SET group_msg_disabled = 1 WHERE union_id = %s"
  36. rows = user_relation_manager.wecom_db.execute(sql, (user_id, ))
  37. print(f'affected rows: {rows}')