简体   繁体   English

从另一个 Python 文件调用热解图中的 function

[英]Calling a function in pyrogram from another Python file

In Pyrogram package, how can I call a function from another Python script?在 Pyrogram package 中,如何从另一个 Python 脚本调用 function?

For example, suppose that the Pyrogram function takes two arguments chat_id and text as input and then send text to given chat_id .例如,假设 Pyrogram function 将两个 arguments chat_idtext作为输入,然后将text发送到给定chat_id

We know that app.send_message(chat_id = given_chat_id,text = given_text) method is send given_text to given_chat_id ,我们知道app.send_message(chat_id = given_chat_id,text = given_text)方法是将given_text发送到given_chat_id

Suppose there are two files named run.py and tel.py in the same folder.假设在同一个文件夹中有两个名为run.pytel.py的文件。

File run.py should contain this script:文件run.py应该包含这个脚本:

from tel import my_func
my_func(given_chat_id, given_text)

and tel.py should look like this code:tel.py应该看起来像这样的代码:

from pyrogram import Client
app = Client("my_account")
async def my_func(chat_id, text):
    print("Hello world!")
    await app.send_message(chat_id, text)
app.run()

We know it doesn't work.我们知道这行不通。 when I run run.py file Even the phrase Hello world!当我运行run.py文件时,即使是短语Hello world! is not printed.不打印。

I don't know how to call it.我不知道怎么称呼它。

You should Use decorator before your function您应该在 function 之前使用装饰器

from pyrogram import Client

app = Client("my_account")


@app.on_message()
def log(client, message):
    print(message)
app.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM