skip to Main Content

As the title says. I need my telegram bot to take user input, and use that to change some values on another function from another file. I already got the file to be successfully run from the bot, but I can’t figure out how to change values first. I am using Python-Telegram-bot.

here is the code I need to edit that is in a separate file (call.py)

call = client.calls.create(
     machine_detection='Enable',
     url='https://ngrok.io/main',
     to='',
     from_=''
)

I need to edit the "to" and "from" field(s) in this code above.
The code I use to run this from my bot is as follows:

def update(update, context):
    update.message.reply_text('Enter number :n'
                              'e.g. 18004585478n')
    update.message.reply_text('Calling...')
    exec(open("call.py").read())

I am pretty new to all this so I know the code is not good at all. I have read that I should be using ConversationHandler or CommandHandler but I honestly am not sure how to implement it.

I edited the code based on what Alexey suggested and now am stuck on a similar issue.

def update(update, context):
   update.message.reply_text('Enter number:n'
                             'e.g. 18004585478n'
                             'Number Must begin with 1')
   from_number = update.message.text
   update.message.reply_text('Enter number:n'
                             'e.g. 18004585478n'
                             'Number Must begin with 1')
   to_number = update.message.text
   update.message.reply_text('Calling...')
   call_state = call.make_call(to_number, from_number)

The Telegram bot just runs all the code at once, it doesn’t stop and wait for any input from the number fields. How do I go about implementing MessageHandler to make the bot stop and accept input to pass along to call_state, then execute call_state at the end?

2

Answers


  1. Chosen as BEST ANSWER

    What Alexey stated ended up working with very slight modifications.

    I took what Alexey posted and deleted the numbers and turned them into a variable I could edit from my other script.

    def make_call(to_number, from_number):
        call = client.calls.create(
            machine_detection='Enable',
            url='https:snip/main',
            to=to_number,
            from_=from_number
        )
        print(call.sid)
    

    Then in the other file I defined the variables and executed them by importing the file I needed to edit by using user_data[FROM] = update.message.text and user_data[TO] = update.message.text.

    then calling the funciton.

    call_state = call.make_call({user_data[TO]}, {user_data[FROM]})

    Dont forget to add user_data = {} at the top of your code.


  2. You don’t need to change the code, you need to use arguments to pass the data you wanted to.

    In call.py you can make a funciton

    def make_call(to_number, from_number):
        call = client.calls.create(
            machine_detection='Enable',
            url='https://ngrok.io/main',
            to=to_number,
            from=from_number,
        )
        return call
    

    In your update function just use the function by giving it the necessary values

    import call
    def update(update, context):
        update.message.reply_text('Enter number :n'
                                  'e.g. 18004585478n')
        update.message.reply_text('Calling...')
        call_state = call.make_call(to_number='0123456789', from_number='9876543210')
        # use call_state ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search