A
A
andrewMus2020-07-06 13:42:49
Python
andrewMus, 2020-07-06 13:42:49

How to send a message from another telebot thread?

How can I run the Send function that is in the body of the loop function, or just send a message from another thread?
Here is the code that does not work, or rather it works without the Send function, but I need to send a message. Thanks in advance.

import threading
import telebot
from PyQt5 import QtWidgets
from myform import Ui_MainWindow
import sys
a=0
b=0
c=0



class mywindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(mywindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        # подключение клик-сигнал к слоту btnClicked
        self.ui.pushButton.clicked.connect(self.btnClicked)
 
    def btnClicked(self):
        self.ui.label.setText("Вы нажали на кнопку!")
        self.ui.label.adjustSize()
        #send()
        bot.send_message("**************", 'Привет')

def loop():
    def send():
        #bot.send_message(***************", 'Привет')
        pass
        print("55")
    bot = telebot.TeleBot('**************************************************************')

    @bot.message_handler(commands=['start'])
    def start_message(message):
        bot.send_message(message.chat.id, 'Привет, ты написал мне /start')

    @bot.message_handler(content_types=['text'])
    def send_text(message):
        if message.text.lower()=="a":
            bot.send_message(message.chat.id, 'Привет')
        # ответы на вопросы...

    bot.polling()

    
t = threading.Thread(target=loop, args=())
t.start()

app = QtWidgets.QApplication([])
application = mywindow()
application.show()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alekssamos, 2020-07-06
@alekssamos

Try to make the bot variable global, i.e. move to the first level, right after the imports. I know this may not be good practice.

D
Dmitry Link, 2020-07-06
@DmitryLink

Make a function that returns an instance of TeleBot. This is a normal practice, the main thing is not to run a long pull in several threads.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question