I use telethon用于使用 python 脚本向 telegram 发送消息。
我在电视马拉松中没有找到任何东西来搜索我喜欢用来在电报应用程序上搜索的群组和频道。请看图片。我如何使用电视马拉松获得这样的列表?
![enter image description here](https://i.stack.imgur.com/N81ox.png)
使用您的秘密创建文件:config.ini
[Telegram]
# no need for quotes
# you can get telegram development credentials in telegram API Development Tools
api_id = 1234
api_hash = 1234
# use full phone number including + and country code
phone = +4411111111111
username = session_user
一种解决方案是这样的,do_search.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Tested with Telethon version 1.14.0
import configparser
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.sync import TelegramClient
from telethon import functions, types
# Reading Configs
config = configparser.ConfigParser()
config.read("config.ini")
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
# (1) Use your own values here
api_id = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']
# (2) Create the client and connect
phone = config['Telegram']['phone']
username = config['Telegram']['username']
client = TelegramClient(username, api_id, api_hash)
async def main():
await client.start()
# Ensure you're authorized
if not await client.is_user_authorized():
await client.send_code_request(phone)
try:
await client.sign_in(phone, input('Enter the code: '))
except SessionPasswordNeededError:
await client.sign_in(password=input('Password: '))
search = 'linux'
result = await client(functions.contacts.SearchRequest(
q=search,
limit=100
))
print(result.stringify())
with client:
client.loop.run_until_complete(main())
并且不要忘记始终感谢@lonami创建这个很棒的库Telethon :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)