如何断开电报客户端?
How to disconnect telegram client?
我正在尝试在 python 中编写一个脚本来监听机器人的“第一次回复”然后退出。因此,我创建了一个客户端实例,然后向 Bot 发送了一条消息,现在我只想记录 bot 的第一个回复(可以忽略即将到来的回复),并将 bot 回复保存到 Reply 变量。现在如何退出侦听器模式,以便在收到回复后我可以做其他事情。我尝试了 client.disconnect() 和 client.disconnected() 但现在可以使用或者我可能不知道正确使用这些方法。我是电视节目 API 的新手。
When I run this script, a msg from my telegram is sent to
bot(BotFather) and then bot send a reply
机器人爸爸的回复
I can help you create and manage Telegram bots. If you're new to the
Bot API, please see the manual (https://core.telegram.org/bots).
You can control me by sending these commands:
/newbot - create a new bot /mybots - edit your bots [beta]
Edit Bots /setname - change a bot's name /setdescription - change bot
description /setabouttext - change bot about info /setuserpic - change
bot profile photo /setcommands - change the list of commands
/deletebot - delete a bot
Bot Settings /token - generate authorization token /revoke - revoke
bot access token /setinline - toggle inline mode
(https://core.telegram.org/bots/inline) /setinlinegeo - toggle inline
location requests
(https://core.telegram.org/bots/inline#location-based-results)
/setinlinefeedback - change inline feedback
(https://core.telegram.org/bots/inline#collecting-feedback) settings
/setjoingroups - can your bot be added to groups? /setprivacy - toggle
privacy mode (https://core.telegram.org/bots#privacy-mode) in groups
Games /mygames - edit your games
(https://core.telegram.org/bots/games) [beta] /newgame - create a new
game (https://core.telegram.org/bots/games) /listgames - get a list of
your games /editgame - edit a game /deletegame - delete an existing
game
并且此回复已分配到回复变量中
but my scripts
still listening for other upcoming events. is there any method from
which I can close this connection.
import random
import traceback
import configparser
from telethon import TelegramClient, events, sync
from telethon.errors import SessionPasswordNeededError
from telethon.errors.rpcerrorlist import PeerFloodError
from telethon.tl.functions.channels import InviteToChannelRequest
from telethon.tl.functions.messages import GetDialogsRequest,GetHistoryRequest
from telethon.tl.types import InputPeerEmpty, InputPeerChannel, InputPeerUser, PeerChannel
api_id = #Api_ID
api_hash = #Api_Hash
phone = #session
client = TelegramClient(phone, api_id, api_hash)
Reply = ' '
@client.on(events.NewMessage(chats='https://t.me/BotFather'))
async def NewMessageListener(event):
Reply = event.message.message
with client:
client.send_message("https://t.me/BotFather", "/start")
client.run_until_disconnected()
# Disconnect client to stop run_until_disconnected()
# Do other stuff!!!
我不明白你想在这里实现什么,但你可以使用 disconnect
方法断开客户端连接
from telethon import TelegramClient, events
api_id = #Api_ID
api_hash = #Api_Hash
phone = #session
client = TelegramClient(phone, api_id, api_hash)
Reply = ' '
@client.on(events.NewMessage(chats='https://t.me/BotFather'))
async def newMessageListener(event):
reply = event.message.message
# do stuff with reply then close the client
await client.disconnect()
async def main():
async with client:
await client.send_message("https://t.me/BotFather", "/start")
await client.run_until_disconnected()
我正在尝试在 python 中编写一个脚本来监听机器人的“第一次回复”然后退出。因此,我创建了一个客户端实例,然后向 Bot 发送了一条消息,现在我只想记录 bot 的第一个回复(可以忽略即将到来的回复),并将 bot 回复保存到 Reply 变量。现在如何退出侦听器模式,以便在收到回复后我可以做其他事情。我尝试了 client.disconnect() 和 client.disconnected() 但现在可以使用或者我可能不知道正确使用这些方法。我是电视节目 API 的新手。
When I run this script, a msg from my telegram is sent to bot(BotFather) and then bot send a reply
机器人爸爸的回复
I can help you create and manage Telegram bots. If you're new to the Bot API, please see the manual (https://core.telegram.org/bots).
You can control me by sending these commands:
/newbot - create a new bot /mybots - edit your bots [beta]
Edit Bots /setname - change a bot's name /setdescription - change bot description /setabouttext - change bot about info /setuserpic - change bot profile photo /setcommands - change the list of commands /deletebot - delete a bot
Bot Settings /token - generate authorization token /revoke - revoke bot access token /setinline - toggle inline mode (https://core.telegram.org/bots/inline) /setinlinegeo - toggle inline location requests (https://core.telegram.org/bots/inline#location-based-results) /setinlinefeedback - change inline feedback (https://core.telegram.org/bots/inline#collecting-feedback) settings /setjoingroups - can your bot be added to groups? /setprivacy - toggle privacy mode (https://core.telegram.org/bots#privacy-mode) in groups
Games /mygames - edit your games (https://core.telegram.org/bots/games) [beta] /newgame - create a new game (https://core.telegram.org/bots/games) /listgames - get a list of your games /editgame - edit a game /deletegame - delete an existing game
并且此回复已分配到回复变量中
but my scripts still listening for other upcoming events. is there any method from which I can close this connection.
import random
import traceback
import configparser
from telethon import TelegramClient, events, sync
from telethon.errors import SessionPasswordNeededError
from telethon.errors.rpcerrorlist import PeerFloodError
from telethon.tl.functions.channels import InviteToChannelRequest
from telethon.tl.functions.messages import GetDialogsRequest,GetHistoryRequest
from telethon.tl.types import InputPeerEmpty, InputPeerChannel, InputPeerUser, PeerChannel
api_id = #Api_ID
api_hash = #Api_Hash
phone = #session
client = TelegramClient(phone, api_id, api_hash)
Reply = ' '
@client.on(events.NewMessage(chats='https://t.me/BotFather'))
async def NewMessageListener(event):
Reply = event.message.message
with client:
client.send_message("https://t.me/BotFather", "/start")
client.run_until_disconnected()
# Disconnect client to stop run_until_disconnected()
# Do other stuff!!!
我不明白你想在这里实现什么,但你可以使用 disconnect
方法断开客户端连接
from telethon import TelegramClient, events
api_id = #Api_ID
api_hash = #Api_Hash
phone = #session
client = TelegramClient(phone, api_id, api_hash)
Reply = ' '
@client.on(events.NewMessage(chats='https://t.me/BotFather'))
async def newMessageListener(event):
reply = event.message.message
# do stuff with reply then close the client
await client.disconnect()
async def main():
async with client:
await client.send_message("https://t.me/BotFather", "/start")
await client.run_until_disconnected()