首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何停止telethon?

如何停止telethon?
EN

Stack Overflow用户
提问于 2020-05-31 09:23:24
回答 1查看 1.4K关注 0票数 0

我在试着阻止机器人执行。但我不能这么做。当我在电报中写机器人命令/stop时,他必须停止。代码:

代码语言:javascript
运行
复制
# -*- coding: utf-8 -*-
import telebot
import asyncio
from telethon import TelegramClient,sync,events,utils
from telethon.events import StopPropagation

api_id = ***
api_hash = '******'
username = '*****'
id_channel = -*** 
token = '*****'

bot = telebot.TeleBot(token)
client = TelegramClient(username, api_id, api_hash)

@bot.message_handler(commands=['start','stop'])
def start(message):
    if '/start' in message.text:
        handler_state(True)
        string = f'<b>Привет {message.from_user.first_name}!</b>\nВведите канал для парсинга и свой канал.\n<i>Пример: <pre>channel_pars,your_channel</pre></i>'
        bot.send_message(message.chat.id, string, parse_mode='html')
    elif '/stop' in message.text:
        string = '<b>Парсер остановлен!</b>'
        bot.send_message(message.chat.id, string, parse_mode='html')
        handler_state(False)

def handler_channel(my_channel,parse_channel,stop_script='start'):
    @client.on(events.NewMessage(chats=(parse_channel)))
    async def normal_handler(event):
        if 'stop' in stop_script:
            await client.disconnect()
        #print(event.message.file.media)  # картинка
        message = event.message.to_dict()['message']
        send_data(message)

    client.start()
    client.run_until_disconnected()


def send_data(message):
    bot = telebot.TeleBot(token)
    bot.send_message(id_channel,message)

def handler_state(state):
    if not state:
        handler_channel('default','default','stop')

    my_channel = None
    parse_channel = None
    @bot.message_handler(content_types=['text'])
    def send_message(message):
        if state:
            if ',' not in message.text:
                return bot.send_message(message.chat.id,'<b>Проверьте правильность ввода!</b>',parse_mode='html')
            channels = message.text.split(',')
            my_channel = channels[-1]
            parse_channel = channels[0]
            if not my_channel or not parse_channel:
                return bot.send_message(message.chat.id,'<b>Проверьте правильность ввода!</b>',parse_mode='html')

            bot.send_message(message.chat.id,f'Введенные данные: <pre>{parse_channel},{my_channel}</pre> \n <b>Парсим</b>',parse_mode='html')


        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        if state:
            handler_channel(my_channel,parse_channel)


bot.polling(none_stop=True)

下面是出现的错误:

代码语言:javascript
运行
复制
p1.py:36: RuntimeWarning: coroutine 'AuthMethods._start' was never awaited
  client.start()
p1.py:37: RuntimeWarning: coroutine 'UpdateMethods._run_until_disconnected' was never awaited
  client.run_until_disconnected()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-31 15:02:46

类似于使用client.connect() (或变体,如start或使用with块)启动Telethon的方式,您可以使用client.disconnect()停止它。

但你的问题似乎不同。

您似乎正在使用telebot,这是一个线程库。同时使用线程和异步需要特别小心,并对这两个库的工作方式有更深入的了解。虽然How to combine python asyncio with threads?可能会有所帮助,但我建议用一个异步bot库(比如Telethon本身,这样您就只需要担心一个库)来替换telebot

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62110516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档