如何在telethon python的联系人中保存一个号码?
from telethon import TelegramClient
from telethon.tl.functions.contacts import GetContactsRequest
from telethon.tl.types import InputPeerUser
client = TelegramClient('arta0', api_id, api_hash)
client.connect()
#number=+19133704541
#name='ali karimi'
Run Code Online (Sandbox Code Playgroud)
添加联系人需要什么模块?
我阅读了 telethon 文档来检索用户名并加入频道。经过一番尝试我写了这段代码:
result = client.invoke(ResolveUsernameRequest('XXXX'))
channel = InputChannel(result.peer.channel_id, result.chats[0].access_hash)
client.invoke(JoinChannelRequest(channel))
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,但我想知道是否有更好或更简单的方法?
通过下面的代码,我已经使用 telethon 连接到电报大约一年了!
client = TelegramClient(
SESSION,
API_ID,
API_HASH
)
client.connect()
# the rest of my code
Run Code Online (Sandbox Code Playgroud)
昨天,当我想使用代码连接到我的会话时,我收到此错误:
telethon.errors.rpcerrorlist.AuthKeyDuplicatedError: An auth key with the same ID was already generated
Run Code Online (Sandbox Code Playgroud)
该错误不再说明,并且在文档中没有关于此错误消息的更多描述。我如何检索我的会话?
您需要获取有关频道、聊天、群组的信息。\n由于每个帐户对接收信息都有限制,因此我将使用多个帐户。\n我尝试获取信息,但收到错误:
\n\n Traceback (most recent call last):\n File "D:/Git/telegram/telegram_new.py", line 50, in parse_entity\n channel=entity\nTypeError: \'coroutine\' object is not callable\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File "D:/Git/telegram/telegram_new.py", line 56, in parse_entity\n id=entity\nTypeError: \'coroutine\' object is not callable\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File "D:/Git/telegram/telegram_new.py", line 60, in parse_entity\n chat_id=entity\nTypeError: \'coroutine\' object is not callable\nRun Code Online (Sandbox Code Playgroud)\n\n帮助修复错误
\n\n代码:
\n\nimport logging\nfrom telethon import TelegramClient,sync, utils, errors\nfrom telethon.tl.types …Run Code Online (Sandbox Code Playgroud) 看起来同步版本的客户端不会抛出任何错误?使用 telethon.sync 时处理错误的正确方法是什么?
下面的代码导致客户端进入“睡眠”状态,但不会出现任何错误。
我尝试对 的显式异常执行相同操作FloodWaitError,但它不能解决问题。
from telethon.sync import TelegramClient
from telethon.tl.functions.channels import GetFullChannelRequest
if __name__ == '__main__':
setup_logging(level=logging.INFO)
tg = TelegramClient(
'anon',
api_id=config.API_ID,
api_hash=config.API_HASH,
)
with tg as client:
try:
result = client(GetFullChannelRequest(-1001100118939))
except ValueError as e:
print(e)
break;
# print('Flood wait for ', e.seconds)
# time.sleep(e.seconds)
print(result)
Run Code Online (Sandbox Code Playgroud) 我想“阅读”来自私人 Telegram 频道的消息,我已经通过手机加入,但不知道如何在代码中指定它。
我已经编写了一些代码,在其中创建了一个 Telegram 客户端(使用我的电话号码登录),每次消息包含关键字(关键字:升序/降序 - 下面的代码)时,它都会“执行某些操作”。
问题:
仅当我向我的机器人、我保存的消息或我的联系人之一写入消息(使用与 API 相同的电话号码)时,它才会触发。
但是,如果我将消息发送到我的测试通道(我是管理员),则什么也不会发生。主频道中的消息也没有任何作用(我不是管理员)。
我不确定如何使用:
@client.on(events.NewMessage(chats=('插入一些东西')))
因为我不知道我需要插入什么。
已经尝试过将组名称显示为每条消息的标题,但没有任何反应/打印。
from telethon import TelegramClient, events, utils
# Get eventupdates on messages
# Here I get stuck and something needs to be changed:
@client.on(events.NewMessage)
async def handler(event):
#If keyword is in message -> do something
if "Descending" in event.raw_text:
print( 'Alert with ', event.text, '!')
if "Ascending" in event.raw_text:
print( 'Alert with ', event.text, '!')
Run Code Online (Sandbox Code Playgroud)
我预计该事件仅由特定组触发,但目前它由发送给我的机器人/联系人/已保存消息的消息触发,但不会通过频道消息执行任何操作。
如何获取今天发布的消息TELETHON
我正在使用下面的代码
date_of_post = datetime.datetime(2019, 12, 24)
with TelegramClient(name, api_id, api_hash) as client:
for message in client.iter_messages(chat , offset_date = date_of_post):
print(message.sender_id, ':', message.text)
Run Code Online (Sandbox Code Playgroud) 我看过所有询问有关电视马拉松的条目,但没有一个回答我的问题。
我想要实现什么?我想将图像从 telegram 频道下载到我的电脑中
到目前为止我做了什么?我能够创建一个客户端并读取包含图像的消息。但是,在 download_media 之后,我不知道文件在哪里或如何继续。
注意:我不想使用任何 BOT。
这是我的代码:
import configparser #read API credentials from a config file
import json # dump data into JSON formatted files.
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from telethon.tl.types import (PeerChannel)
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon import utils
#**************** CREATE TELEGRAM CLIENT ******************************
# Reading Configs
config = configparser.ConfigParser()
config.read("E:\TelegramBOT\config.ini")
# Setting configuration values
api_id = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']
api_hash = …Run Code Online (Sandbox Code Playgroud) 我正在使用 Python 的热图库来登录多个帐户。我需要创建一个函数,仅将验证码发送到帐户,然后从其他用户输入(不是默认的热解图登录提示)中读取它。
当我使用 send_code 时,它会发送代码并等待来自控制台的用户输入,而这是我不希望它执行的操作。我只需要一个以电话号码作为参数并向其发送确认码的函数,然后使用该确认码登录的函数(从其他地方的用户输入获得,例如:从电报消息到链接的机器人或......
该代码似乎运行完美,在我最终再次注销之前,我什至有时间用机器人对其进行了短暂的测试。此时我已经尝试了大约 10 次,但 Telethon 似乎出于某种原因将我从手机上的 Telegram 中删除了。我不知道为什么会发生这种情况,而且我也没有看到其他人遇到过这个问题。
负责登录的代码片段:
async def get_channel_messages(chat_id, msg_id):
# Telegram API ID and Hash (you can get it from my.telegram.org)
api_id = '12345678'
api_hash = '1234'
session_hash='hash'
# Identify your bot with his ID number, can be found using this link
my_bot_id = '12345'
# Storing max 3 past messages
max_memory_message = 3
chat_id = int(chat_id)
data = {}
# Create a Telegram client with the given session string
async with TelegramClient(StringSession(session_hash), api_id, api_hash) as client: …Run Code Online (Sandbox Code Playgroud)