python - Telethon 没有读取我的电话号码(电报)
问题描述
所以我正在尝试创建一个脚本来读取电报中的消息。基本上我在网上找到了这个脚本,但它不能正常工作
这是代码
import configparser
import json
import asyncio
from datetime import date, datetime
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
PeerChannel
)
# some functions to parse json date
class DateTimeEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, datetime):
return o.isoformat()
if isinstance(o, bytes):
return list(o)
return json.JSONEncoder.default(self, o)
# Reading Configs
config = configparser.ConfigParser()
config.read("config.ini")
# Setting configuration values
api_id = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']
api_hash = str(api_hash)
phone = config['Telegram']['phone']
username = config['Telegram']['username']
# Create the client and connect
client = TelegramClient(username, api_id, api_hash)
async def main(phone):
await client.start()
print("Client Created")
# Ensure you're authorized
if await client.is_user_authorized() == False:
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: '))
me = await client.get_me()
user_input_channel = input('enter entity(telegram URL or entity id):')
if user_input_channel.isdigit():
entity = PeerChannel(int(user_input_channel))
else:
entity = user_input_channel
my_channel = await client.get_entity(entity)
offset_id = 0
limit = 100
all_messages = []
total_messages = 0
total_count_limit = 0
while True:
print("Current Offset ID is:", offset_id, "; Total Messages:", total_messages)
history = await client(GetHistoryRequest(
peer=my_channel,
offset_id=offset_id,
offset_date=None,
add_offset=0,
limit=limit,
max_id=0,
min_id=0,
hash=0
))
if not history.messages:
break
messages = history.messages
for message in messages:
all_messages.append(message.to_dict())
offset_id = messages[len(messages) - 1].id
total_messages = len(all_messages)
if total_count_limit != 0 and total_messages >= total_count_limit:
break
with open('channel_messages.json', 'w') as outfile:
json.dump(all_messages, outfile, cls=DateTimeEncoder)
with client:
client.loop.run_until_complete(main(phone))
所以基本上当我运行这个时,我得到了这个
请输入您的手机(或机器人令牌):
当我以正确的国际格式输入我的电话号码 (+3xxxxxxxx),然后按 Enter 键时,什么也没有发生。所以程序卡在那里。有什么帮助吗?
解决方案
推荐阅读
- javascript - 当条件变为 True 时如何在 Django 模板中自动调用 JavaScript 函数
- typescript - 如何在对象中获取联合类型的类
- azure - ADF Tumbling 窗口触发开始日期格式问题
- typescript - A型不能分配给B型
- automation - 我无法在 cypress 中找到元素,因为 css 选择器每次都在变化
- encryption - 使用 openSSL 1.1.1 和/或 Windows crypt 函数实现 PBEWithHmacSHA256AndAES_256
- css - 用于响应式设计的材料单选按钮组中的材料卡
- c# - 解析时间跨度 1:00 和 01:00
- c# - 从磁盘显示大量图像的最佳方法
- python - 带有 Django 项目和 MS SQL 数据库的 GitLab CI 无法登录