当节点 js 运行 时在 python 脚本中提交数据

submit a data in a python script when it run by node js

首先我很抱歉我的英语不好
我有一个 python 代码来为电报创建一个 cli 帐户,如下所示:

from pyrogram import Client
from pyrogram.raw import functions

api_id = someNumber
api_hash = "someHash"

with Client("my_account", api_id, api_hash) as app:
    print('Bot is online...')

我将 node.js 代码用于 运行 python 脚本,如下所示:
if(command == 'run cli'){
     var spawn = require("child_process").spawn;
     var process = spawn('python', ["python/hello.py"]);
     process.stdout.on('data', function(data) {
           console.log(data.toString());
     });
}

输出为: Enter phone number or bot token:

我怎样才能给它我从 node.js 得到的 phone 号码?
实际上,当我通过“python hello.py”使用 cmd 运行 python 脚本时,它需要我 phone 编号,然后我编写它然后按回车键并完成。太简单。
但是在这种情况下我不知道该怎么办。

我认为最简单的方法(如果可能)是通过 sys.argv.

修改您的 python 脚本以接受命令行参数

因此,您可以将 python 程序修改为:

from pyrogram import Client
from pyrogram.raw import functions
import sys # we need the sys module to access these arguments I'm talking about

phone_number = sys.argv[1] # the phone number will be the first (and only, I'm assuming) argument. You can pass multiple arguments, and access them with `sys.argv[2]`, `sys.argv[3]`...etc.
api_id = someNumber
api_hash = "someHash"

with Client("my_account", api_id, api_hash, phone_number=phone_number) as app: # pass the phone number to Client
    print('Bot is online...')

现在您将 phone 数字存储在变量 phone_number 中并将其传递给 Client,因此您应该在 Python 端做好准备。

不过,在您的节点脚本中,您实际上必须 传递 phone 数字作为参数,您可以通过添加参数作为"python/hello.py" 所属的数组,如下所示:

if(command == 'run cli'){
     var spawn = require("child_process").spawn;
     var process = spawn('python', ["python/hello.py", "PHONE NUMBER GOES HERE"]);
     process.stdout.on('data', function(data) {
           console.log(data.toString());
     });
}

...你应该很好。

请注意,我尚未测试此代码。