python - 如何在不输入手机(或机器人令牌)的情况下使用 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()
解决方案
推荐阅读
- javascript - setTimeout 在 for 循环中的 Promise
- php - 相等的字符串在php上不相等
- ios - 如何使用 Inspector 在 Xcode 中缩放 3D 对象?
- mysql - MySql-5.1.40 全文搜索在 MyISAM 表中不起作用
- android - Cordova 构建 android“没有 android 构建工具”
- reactjs - 表单提交后如何将 react-select 组件重置为 defaultValue 状态
- powershell - 在 PowerShell 中编辑后如何存储文件?
- html - 有没有办法使用 box-shadow 有一个圆形的嵌入边框?
- aws-lambda - 解析 AWS Connect 中的对象列表
- sql - 传递要在存储过程中使用的项目字符串(varchar)