本文介绍了如何使用 Telethon 在电报中搜索组和频道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用
解决方案
用你的秘密创建文件:config.ini
[电报]# 不需要引号# 您可以在电报 API 开发工具中获取电报开发凭证api_id = 1234api_hash = 1234# 使用完整的电话号码,包括 + 和国家代码电话 = +4411111111111用户名 = session_user
一个解决方案是,do_search.py
#!/usr/bin/env python# -*- 编码:utf-8 -*-# 使用 Telethon 1.14.0 版测试导入配置解析器从 Telethon 导入 TelegramClient从 Telethon.errors 导入 SessionPasswordNeededError从 Telethon.sync 导入 TelegramClient从 Telethon 导入函数,类型# 读取配置config = configparser.ConfigParser()config.read("config.ini")# 这些示例值不起作用.您必须获得自己的 api_id 和# api_hash 来自 https://my.telegram.org,在 API Development 下.# (1) 在这里使用您自己的值api_id = config['Telegram']['api_id']api_hash = config['Telegram']['api_hash']# (2) 创建客户端并连接电话 = 配置['电报']['电话']用户名 = config['电报']['用户名']客户端 = TelegramClient(用户名,api_id,api_hash)异步定义主():等待 client.start()# 确保您已获得授权如果不等待 client.is_user_authorized():等待 client.send_code_request(电话)尝试:await client.sign_in(phone, input('输入代码:'))除了 SessionPasswordNeededError:等待 client.sign_in(password=input('密码:'))搜索 = 'linux'结果 = 等待客户端(functions.contacts.SearchRequest(q=搜索,限制=100))打印(result.stringify())与客户:client.loop.run_until_complete(main())
并且不要忘记始终感谢 @lonami 创建这个很棒的库 Telethon :)
I use telethon for sending messages to telegram using python script.
I did not find anything in telethon to search for groups and channels I like used to search on telegram app. Please see iamge. How can I get such list using telethon?
解决方案
Create file with your secrets: 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
One solution is this, 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())
And don't forget to always thank @lonami for creating this awesome library Telethon :)
这篇关于如何使用 Telethon 在电报中搜索组和频道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!