瀏覽代碼

Update agent_service: stop daily push if user asks

StrayWarrior 5 天之前
父節點
當前提交
b0265d56a6
共有 2 個文件被更改,包括 33 次插入4 次删除
  1. 4 0
      agent_service.py
  2. 29 4
      user_manager.py

+ 4 - 0
agent_service.py

@@ -140,6 +140,10 @@ class AgentService:
             logger.debug("user_id: {}, no profile info extracted".format(user_id))
             return
         logger.warning("update user profile: {}".format(profile_to_update))
+        if profile_to_update.get('interaction_frequency', None) == 'stopped':
+            # 和企微日常push联动,减少对用户的干扰
+            if self.user_relation_manager.stop_user_daily_push(user_id):
+                logger.warning(f"user[{user_id}]: daily push set to be stopped")
         merged_profile = self.user_profile_extractor.merge_profile_info(user_profile, profile_to_update)
         self.user_manager.save_user_profile(user_id, merged_profile)
         return merged_profile

+ 29 - 4
user_manager.py

@@ -84,6 +84,10 @@ class UserRelationManager(abc.ABC):
     def get_user_tags(self, user_id: str) -> List[str]:
         pass
 
+    @abc.abstractmethod
+    def stop_user_daily_push(self, user_id: str) -> bool:
+        pass
+
 class LocalUserManager(UserManager):
     def get_user_profile(self, user_id) -> Dict:
         """加载用户个人资料,如不存在则创建默认资料。主要用于本地调试"""
@@ -305,24 +309,45 @@ class MySQLUserRelationManager(UserRelationManager):
             ret.extend(staff_user_pairs)
         return ret
 
-    def get_user_tags(self, user_id: str) -> List[str]:
+    def get_user_union_id(self, user_id: str) -> Optional[str]:
         sql = f"SELECT wxid FROM {self.agent_user_table} WHERE third_party_user_id = '{user_id}' AND wxid is not null"
         user_data = self.agent_db.select(sql, pymysql.cursors.DictCursor)
         if not user_data:
-            logger.error(f"user[{user_id}] has no wxid")
+            logger.error(f"user[{user_id}] has no union id")
+            return None
+        union_id = user_data[0]['wxid']
+        return union_id
+
+    def get_user_tags(self, user_id: str) -> List[str]:
+        union_id = self.get_user_union_id(user_id)
+        if not union_id:
             return []
-        user_wxid = user_data[0]['wxid']
         sql = f"""
             select b.tag_id, c.`tag_name`  from `we_com_user` as a
               join `we_com_user_with_tag` as b
               join `we_com_tag` as c
               on a.`id` = b.`user_id`
               and b.`tag_id` = c.id
-              where a.union_id = '{user_wxid}' """
+              where a.union_id = '{union_id}' """
         tag_data = self.wecom_db.select(sql, pymysql.cursors.DictCursor)
         tag_names = [tag['tag_name'] for tag in tag_data]
         return tag_names
 
+    def stop_user_daily_push(self, user_id: str) -> bool:
+        try:
+            union_id = self.get_user_union_id(user_id)
+            if not union_id:
+                return False
+            sql = f"UPDATE {self.user_table} SET group_msg_disabled = 1 WHERE union_id = %s"
+            rows = self.wecom_db.execute(sql, (union_id, ))
+            if rows > 0:
+                return True
+            else:
+                return False
+        except Exception as e:
+            logger.error(f"stop_user_daily_push failed: {e}")
+            return False
+
 
 if __name__ == '__main__':
     config = configs.get()