0

The main problem in my code is that the root.mainloop() block all the other functions, mainly the functions that communicates to the arduino and takes the elements from the queue. I've been looking on the internet about that but the only things I've found were about creating a new thread that unfortunately doesn't work. Anyone knows the answer? Anyway the code has been cleaned from Api and others useless commands I hope you may help me

    import time
    import telepot
    from telepot.loop import MessageLoop
    from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, 
    InlineKeyboardButton
    from queuelib import FifoDiskQueue
    from serial import Serial
    import serial
    from tkinter import  *
    import tkinter.font as font
    import threading
    
    arduino = serial.Serial(port='COM4', baudrate=115200, timeout= .1)
    
    q = FifoDiskQueue("NewOrdFile1")
    s = FifoDiskQueue("Names")
    
    keyboard = ReplyKeyboardMarkup(keyboard=[['Order Drink', '⚙️help'], ['....', '....'],['️about me', '️Ping']])
    inline_Key = InlineKeyboardMarkup(inline_keyboard=[
                                        [InlineKeyboardButton(text="Margarita",callback_data='/Margarita'), InlineKeyboardButton(text="CocaCola",callback_data='/CocaCola')],
                                        [InlineKeyboardButton(text="Coca-Buton",callback_data='/Coca-Buton') ,InlineKeyboardButton(text="Fanta",callback_data='/Fanta')]
                                    ]
                                )
    keyboardDrinks = ReplyKeyboardMarkup(keyboard=[['Margarita', 'Buton'], ['....', '....'],['CocaCola', 'Fanta']])
    
    root = Tk()
    root.geometry("1920x1080")   
    #set window color
    root.configure(bg='grey')
    myFont = font.Font(family='Helvetica', size=20, weight='bold')
    
    
    
    def myClick():
        
        label= Label(root, text= "YOU ORDERED A DRINK: Redbull-Vodka", bg= "white",padx= 20, pady=20)
        
        label["font"]  = myFont
        label.grid(row = 7, column = 540)
    
    def myClick1():
        label= Label(root, text= "YOU ORDERED A DRINK:       Margarita       ", bg="white",padx= 25, pady=20)
        label["font"]  = myFont
        label.grid(row = 7, column = 540)
    
    def myClick2():
        label= Label(root, text= "YOU ORDERED A DRINK:  coca-buton  ", bg= "white",padx= 30, pady=20)
        label["font"]  = myFont
        label.grid(row = 7, column = 540)
    
    def handle(msg):
        chat_id = msg['chat']['id'] # Receiving the message from telegram
        command = msg['text']   # Getting text from the message
        username = msg['from']['username']
        
        b = bytes(username, 'utf-8')
        
       
    
        
        
       
        Welcome = "Hi " +"@"+ username 
        print ('Received:')
        print(command)
        
    
        if chat_id== or chat_id== -:
    
        # Comparing the incoming message to send a reply according to it
            if command == '️about me':
                bot.sendMessage (chat_id, str(Welcome)) 
                
       
            if command == "ping":
                bot.sendMessage(chat_id, str("pong"))
            if command == "ding":
                time.sleep(2)
                bot.sendMessage(chat_id, str("dong"))
            if command == "️Ping":
                bot.sendMessage(chat_id, str("Pong"))
            if command == "Ding":
                time.sleep(2)
                bot.sendMessage(chat_id, str("Dong"))
            if command == "⚙️help":
                time.sleep(2)
               
    
            if command == "/Start":
                bot.sendMessage(chat_id, 'Hello', reply_markup=keyboard)
    
            if command == "Order Drink":
                bot.sendMessage(chat_id, "Scegli drink qui sotto, usando la  keyboard", reply_markup=keyboardDrinks)
            
           
            if command == "Margarita":
    
                bot.sendMessage(chat_id, "Your Margarita has been processed, Wait")
                q.push(b"a")
                #print(b("a"))
                s.push(b)
                print("Successfully pushed")
                bot.sendMessage(chat_id, 'Redirecting', reply_markup=keyboard)
            
            
         
    mylabel = Label(root, text= "SELF BARTENDER ROBOT", bg= "grey", padx= 540, pady= 100) 
    mylabel["font"]  = myFont
    mylabel2= Label(root, text= "name client", bg="grey", padx= 540, pady= 1)
    mylabel2["font"]  = myFont
    mylabel3 =Label(root, text= "Choose your cocktail", bg="grey", padx= 540, pady= 20)  
    mylabel3["font"]  = myFont
    mybutton= Button(root, text = "Redbull-Vodka",  command=  myClick , padx= 100, pady= 20 ,bg= "red") 
    mybutton1= Button(root, text = "Margarita", command=  myClick1,padx= 100, pady= 20 , bg= "blue")
    #mybutton.pack()
    mybutton2= Button(root, text = "coca-buton",  command=myClick2,padx= 100, pady= 20 , bg= "yellow") 
    #showing it on the screen
    
    mylabel.grid(row = 1, column = 540 )
    mylabel2.grid(row = 2, column = 540)
    mylabel3.grid(row = 3, column = 540)
    mybutton.grid(row = 4, column = 540)
    mybutton1.grid(row =5, column = 540)
    mybutton2.grid(row = 6, column = 540)
          
    
    bot = telepot.Bot('')
    print (bot.getMe())
    
    
    
    # Start listening to the telegram bot and whenever a message is  received, the handle function will be called.
    
    
    MessageLoop(bot, {'chat': handle}).run_as_thread()
    
    
    
    def ReadList1():
    
        #print(arduino.readline())
        if arduino.readline()==b'1': 
    
            print("ho inviato info!")
            c= q.pop()
            k = s.pop()
            print(c)
            print(k)
            if c != None:
    
                arduino.write(c)
            
    
    
    
    
    root.mainloop()
    
    
    
    
    print ('Listening....')
    
    while 1:
    
         ReadList1()
         
         time.sleep(2)
        
        ```
Giuseppe
  • 3
  • 8

1 Answers1

0

I've been having the same problem too. I'm not sure what it is that's causing the error. Maybe the GUI is running something that interferes with the telegram bot because closing the GUI allows it to work? I'm not sure though.

Charlie
  • 11
  • 1
  • So, you are right. In this case Tkinter was blocking my bot. According to @TheLIzzard i used ".after" function that doesn't refresh the gui every time – Giuseppe May 05 '21 at 11:07