Home > other >  python-telegram-bot get user input and pass it to another function
python-telegram-bot get user input and pass it to another function

Time:12-24

Im trying to get an integer number from user in my tel bot and pass it to a function called Locator; This functions sends a query to a .db file and returns a list. I want to send that list to the user (So to simplify above statements the user will search for something and the reply will be the result of an sql query stored as a list, I also want to set filters so this function only trigger and work if the user sends Integer values). The locator function is stored in another file called Database.py and its called in the main code (Bot.py) by import. Bot.py :

import logging
from telegram import *
from telegram.ext import *
import Database
from re import *

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)

async def start(update: Update, context: CallbackContext.DEFAULT_TYPE):
    
    buttons = [[KeyboardButton ("Stats")]]
    await context.bot.send_message(chat_id=update.effective_chat.id, text="Hello; Please enter your ID",
    reply_markup = ReplyKeyboardMarkup(buttons))

async def Catcher(user_input):
    data = Locator(user_input)
    return data
async def Acc_Stat(update : Update , context):
    user_input = update.message.text
    update.message.reply.text(Catcher(user_input))
    
    


if __name__ == '__main__':
    application = ApplicationBuilder().token('xxxx').build()
    
    start_handler = CommandHandler('start', start)
    application.add_handler(start_handler)
    Stat_Handler = MessageHandler(filters.Regex(r'\d '), Acc_Stat)
    application.add_handler(Stat_Handler)
    
    application.run_polling()

Database.py :

#This file is made to Explore and search the database
from datetime import date
import sqlite3
#Pass SQLFile name/Location in this sentence
conn = sqlite3.connect('mydb.db')
#A cursor to call sql functions
c = conn.cursor()
#Locator (account id is passed as an argument, Returns account Enable , upload , download and expiry date as a list)
def Locator(*account):
    temp = list()(c.execute('SELECT enable FROM client_traffics WHERE inbound_id= ?',(account)))
    c.execute('SELECT up FROM client_traffics WHERE inbound_id= ?',(account))
    temp.append(c.fetchone())
    c.execute('SELECT down FROM client_traffics WHERE inbound_id= ?',(account))
    temp.append(c.fetchone())
    c.execute('SELECT expiry_time FROM client_traffics WHERE inbound_id= ?',(account))
    temp.append(c.fetchone())
    #Query Result
    return temp

How ever I pass a Number in my chat with the bot; Nothing happens. Also Can someone explain to me how to work with the user_input in ptb? (I checked the Locator function with print and it seems its working fine, I also removed my API so dont see that as en error)

CodePudding user response:

I think there is a misspelling in Acc_Stat function.

Rewrite this:

 update.message.reply.text(Catcher(user_input))

To this:

update.message.reply_text(Catcher(user_input))
  • Related