需要电报机器人来编辑另一个文件的函数

发布于 2025-02-10 23:19:06 字数 1346 浏览 2 评论 0原文

如标题所说。我需要我的电报机器人来获取用户输入,并使用它来从另一个文件上更改另一个函数的值。我已经让文件从机器人成功运行,但是我不知道如何首先更改值。我正在使用python-telegram-bot。

这是我需要编辑的代码,该代码在一个单独的文件(call.py)中,

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

我需要在上面的代码中编辑“ to”和“ from”“ from”“ from”字段。 我用来从机器人运行此操作的代码如下:

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

我对所有这些都是新的,所以我知道代码根本不好。我读到我应该使用对话手或指挥手,但老实说,我不确定如何实施它。

我根据Alexey建议的内容编辑了代码,现在AM卡在类似问题上。

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

Telegram Bot仅一次运行所有代码,它不会停止并等待数字字段中的任何输入。我该如何实现MessageHandler以使Bot停止并接受输入以传递到Call_State,然后在末尾执行CALL_STATE?

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. 18004585478\n')
    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. 18004585478\n'
                             'Number Must begin with 1')
   from_number = update.message.text
   update.message.reply_text('Enter number:\n'
                             'e.g. 18004585478\n'
                             '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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风起 2025-02-17 23:19:06

您无需更改代码,您需要使用参数传递想要的数据。

call.py中,您可以

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

更新功能中制作一个funciton

import call
def update(update, context):
    update.message.reply_text('Enter number :\n'
                              'e.g. 18004585478\n')
    update.message.reply_text('Calling...')
    call_state = call.make_call(to_number='0123456789', from_number='9876543210')
    # use call_state ...

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. 18004585478\n')
    update.message.reply_text('Calling...')
    call_state = call.make_call(to_number='0123456789', from_number='9876543210')
    # use call_state ...
乖乖公主 2025-02-17 23:19:06

Alexey所说的最终进行了非常轻微的修改。

我采用了Alexey发布的内容并删除了数字,并将它们变成了我可以从其他脚本中编辑的变量。

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)

然后,在另一个文件中,我通过使用user_data [from] = update.message.text.text 和user_data [to] = update来导入所需编辑的文件来定义变量并执行它们。 Message.Text

然后打电话给funciton。

call_state = call.make_call({user_data [to]},{user_data [from]})

不要忘记在代码顶部添加user_data = {}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文