基础环境搭建

  • pip3 install telethon
  1. #coding:utf-8
  2. from telethon import TelegramClient, sync,events
  3. import logging
  4. import random
  5. import asyncio
  6. import telethon
  7. from telethon.tl.types import PeerUser, PeerChat, PeerChannel,UpdateNewChannelMessage
  8. from telethon.tl.functions.messages import SendMessageRequest
  9. from telethon.tl import types, functions
  10. from telethon import utils
  11. import re
  12. api_id = {你的id}
  13. api_hash = "{你的api_hash}"
  14. client = TelegramClient('随便一个名字', api_id, api_hash).start()

获取chat_id

  • 其中onegroups是群组nick
    1. messages = client.get_messages('onegroups', 100)
    2. for line in messages:
    3. print(line)

拉人进群

  • 不需要别人同意,即可拉入群。
  • 可用于发广告,SEO。
    1. from telethon.tl.functions.messages import AddChatUserRequest
    2. client(AddChatUserRequest(
    3. chat_id = -269442445 , #chat_id,从上面获取
    4. user_id = 585015279 , #被邀请人id
    5. fwd_limit=10 # Allow the user to see the 10 last messages
    6. ))

参考资料