python - 如何使用 Telethon 在电报中搜索群组和频道?
问题描述
我使用Telethon使用 python 脚本向电报发送消息。
我在 Telethon 中没有找到任何东西来搜索我喜欢在电报应用程序上搜索的群组和频道。请看我。如何使用 Telethon 获取此类列表?
解决方案
使用您的秘密创建文件: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())
推荐阅读
- java - 无法将计数器(标题)设置为上下文操作栏
- php - 如何使用 PHP 一次更新多个方括号字符串
- sql - 如何在一组日期和时间之间切换
- javascript - 有没有办法在反应最终形式中禁用嵌套字段名称功能?
- amazon-web-services - 如何更改附加到弹性 beantalk 的负载均衡器
- angular - 错误 TS2554:预期 2 个参数,但使用 @ViewChild 得到 1 个
- visual-studio-code - 创建主题和语法颜色时是否可以在 Visual Studio 代码中使用颜色变量
- java - 错误: throw new UnsupportedOperationException("方法未反编译
- search - 如何修复 Flutter 中的“搜索栏下方显示的内容”?
- javascript - 如何将 highcharts 插件添加到 highcharts-vue(highcharts vue 包装器)