Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question of Telebot
#1
Hello,

I've created a Telegram bot (set on webhook) to demonstrate the issue I'd like to address:
The program currently returns the user's actions with a 3-second delay. However, I want to modify it to only process the user's initial action. I want the bot to receive the first action, ignore any subsequent actions FOREVER (it should delete if from bot's cache or elsewhere it is saved), response to that first action, then wait for the new first action from the user.

In its current state (the following code), the program responds to all user actions in the order they are received and there is no any error in log saved. I'm looking for a solution to make the bot work as follows:

- Receive the user's first action.
- Respond to the first action.
- Wait for the user's NEW action (not previous and past user's actions).
- If the user has sent additional actions before the bot responds to the first action, ignore and discard them from memory without any reaction or acknowledgement forever.

I do not know how to do it, especially deleting those non-first actions so that the bot forget them forever and never use them.
I hope the explanation is clear enough.
Can anyone provide a solution to achieve this behavior?

from flask import Flask, request
import telebot
import time
import threading

secret = "***********************************"
app = Flask(__name__)
bot = telebot.TeleBot('***************************************************', threaded=False)

processing = False
processing_lock = threading.Lock()

@app.route('/webhook', methods=['POST'])
def webhook():
    if request.headers.get('content-type') == 'application/json':
        json_str = request.get_data().decode('UTF-8')
        update = telebot.types.Update.de_json(json_str)
        bot.process_new_updates([update])
        return '', 200
    else:
        return '', 403

@bot.message_handler(func=lambda message: True)
def echo_all(message):
    global processing
    
    with processing_lock:
        if processing:
            print(f"Ignored: {message.text}")
            return  

        processing = True
    
    try:
        time.sleep(3) 
        bot.reply_to(message, message.text)
    finally:
        with processing_lock:
            processing = False

if __name__ == "__main__":
    bot.remove_webhook()
    bot.set_webhook(url='*****************************************/webhook')

Copy Snippet
Edit Snippet
 Wordwrap
from flask import Flask, request
import telebot
import time
import threading
​
secret = "***********************************"
app = Flask(__name__)
bot = telebot.TeleBot('***************************************************', threaded=False)
​
processing = False
processing_lock = threading.Lock()
​
@app.route('/webhook', methods=['POST'])
def webhook():
    if request.headers.get('content-type') == 'application/json':
        json_str = request.get_data().decode('UTF-8')
        update = telebot.types.Update.de_json(json_str)
        bot.process_new_updates([update])
        return '', 200
    else:
        return '', 403
​
@bot.message_handler(func=lambda message: True)
def echo_all(message):
    global processing
    
    with processing_lock:
        if processing:
            print(f"Ignored: {message.text}")
            return  
​
        processing = True
    
    try:
        time.sleep(3) 
        bot.reply_to(message, message.text)
    finally:
        with processing_lock:
            processing = False
​
if __name__ == "__main__":
    bot.remove_webhook()
    bot.set_webhook(url='*****************************************/webhook')
Larz60+ write Aug-14-2024, 07:50 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Handling receiving updates from user in Telebot mohsenamiri 0 1,453 Aug-26-2024, 09:25 AM
Last Post: mohsenamiri
  call query telebot lolita7777 1 2,464 May-13-2024, 06:49 PM
Last Post: david_jr_br

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020