首页 > 解决方案 > 如何在不输入手机(或机器人令牌)的情况下使用 Telethon 连接到 Telegram?

问题描述

我编写了以下脚本来监听电报公共频道上的新消息。问题是每次在新机器上使用脚本时,电报都会向链接到开发帐户的电话号码发送代码。您必须输入此代码才能授权新机器。有没有办法避免这种情况?

import configparser
from telethon.errors import SessionPasswordNeededError
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
PeerChannel
)

api_id = 'xxxxxx'
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxx'

# Here you define the target channel that you want to listen to:
input_channels = ('https://t.me/xxxxxx','https://t.me/xxxx','https://t.me/xxxxxxx')

print(input_channels)

#create a client
client = TelegramClient('anon', api_id, api_hash)

# Listen to messages from target channel 
@client.on(events.NewMessage(chats=input_channels)) 
async def newMessageListener(event):
    # Get message text 
    newMessage = event.message.message 

    print(newMessage)


with client: 
    client.run_until_disconnected() 

标签: pythontelegramtelethon

解决方案


推荐阅读