telegram 群组监控 / 群组功能
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.

143 lines
4.7 KiB

8 months ago
  1. from telethon import TelegramClient, functions
  2. from telethon.sessions import StringSession
  3. from config import logger, PROXY, TG_ROBOT_ACCOUNT_TABLE
  4. from tg_utils.tg_api import download_profile_photo
  5. from utils.upload_files import upload_file
  6. class GroupFunc():
  7. """
  8. TODO:
  9. """
  10. @staticmethod
  11. async def login(session_string, api_id, api_hash):
  12. """
  13. session_string
  14. :param session_string:
  15. :param api_id:
  16. :param api_hash:
  17. :return:
  18. """
  19. session = StringSession(session_string) # 通过session字符串来登陆 也可通过本地文件登陆(不推荐)
  20. client = TelegramClient(session, api_id, api_hash, timeout=60, proxy=PROXY)
  21. await client.start()
  22. return client
  23. @staticmethod
  24. async def get_login(api_id, api_hash, client_mysql):
  25. """
  26. session重新登录获得 string_session
  27. :param api_id:
  28. :param api_hash:
  29. :param client_mysql:
  30. :return:
  31. """
  32. # 创建 Telethon 客户端对象并进行身份验证
  33. client = TelegramClient(api_hash, api_id, api_hash, timeout=60, proxy=PROXY)
  34. await client.start()
  35. string_session = StringSession.save(client.session)
  36. logger.info(f"sss: {string_session}")
  37. if string_session: # 更新 session
  38. sql = f"update {TG_ROBOT_ACCOUNT_TABLE} set session_string='{string_session}' where api_hash='{api_hash}'"
  39. client_mysql.getOne(sql)
  40. await client.disconnect()
  41. return string_session
  42. @staticmethod
  43. async def get_related_group(client, group_name):
  44. """
  45. info信息和讨论组
  46. :param client:
  47. :param group_name:
  48. :return:
  49. """
  50. detail = {
  51. "description": "",
  52. "discussion": {}
  53. }
  54. full = await client(functions.channels.GetFullChannelRequest(channel=group_name))
  55. full_channel = full.full_chat
  56. detail["description"] = full_channel.about
  57. discussion_id = full_channel.linked_chat_id
  58. if discussion_id:
  59. detail["discussion"] = {
  60. "group_id": -1000000000000 - discussion_id
  61. }
  62. return detail
  63. @staticmethod
  64. async def get_group(client, channel, file="profile"):
  65. """
  66. :param client:
  67. :param channel:
  68. :param file:
  69. :return:
  70. """
  71. group = {
  72. "group_id": "",
  73. "title": "",
  74. "photo": "",
  75. "description": "",
  76. "participants_count": "",
  77. }
  78. full = await client(functions.channels.GetFullChannelRequest(channel=channel))
  79. full_channel = full.full_chat
  80. group["description"] = full_channel.about
  81. group["group_id"] = -1000000000000 - full_channel.id
  82. group["title"] = full.chats[0].title
  83. group["participants_count"] = full_channel.participants_count
  84. path = await download_profile_photo(client, full_channel, file=file)
  85. group["photo"] = await upload_file(path)
  86. # logger.info(group)
  87. return group
  88. @staticmethod
  89. async def get_group_rights(client, channel):
  90. """
  91. :param client:
  92. :param channel:
  93. :return:
  94. """
  95. _allow_status = {
  96. "send_photos": False,
  97. "send_videos": False,
  98. "send_plain": False,
  99. "send_docs": False,
  100. }
  101. if isinstance(channel, str):
  102. group_entity = await client.get_entity(channel) # 获取群组类型
  103. rights = group_entity.default_banned_rights
  104. else:
  105. rights = channel.default_banned_rights # 群组默认的权限对象
  106. if rights: # 目前发现只有群组存在这个值
  107. logger.info(f"群组权限: {channel.title} => {rights}")
  108. if not rights.send_messages:
  109. _allow_status = {
  110. "send_photos": not rights.send_photos, # 发送图片
  111. "send_videos": not rights.send_videos, # 发送视频
  112. "send_plain": not rights.send_plain, # 发送文本
  113. "send_docs": not rights.send_docs, # 发送文件
  114. }
  115. else: # 群组类型不符合要求
  116. _allow_status = False
  117. return _allow_status
  118. @staticmethod
  119. async def is_banned(client, channel):
  120. """
  121. :param client:
  122. :param channel:
  123. :return:
  124. """
  125. permissions = await client.get_permissions(channel, 'me')
  126. logger.info(f"是否存在权限 ! => {permissions.is_banned}")
  127. return permissions.is_banned