从 Python 脚本获取输入到 Node.js Telegram Bot

问题描述

我想在用户要求时从 Telegram Bot 中的 python 脚本中获取输入我已经编写了从 Python 脚本中获取输入的代码,但我不知道如何在 Telegram Bot 中使用它来发送消息

>
const TelegramBot = require('node-telegram-bot-api');
const token = process.env.TELEGRAM_TOKEN;
const bot = new TelegramBot(token,{polling: true});
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json()); 
app.listen(process.env.PORT); 
app.post('/' + bot.token,(req,res) => {
  bot.processUpdate(req.body);
  res.sendStatus(200);
});
const {PythonShell} = require('python-shell');

let pyshell = new PythonShell('insight.py');

  pyshell.on('message',function(message) {
  console.log(message);
})

pyshell.end(function (err) {
  if (err){
    throw err;
  };
});
bot.on('message',(msg) => {
var sendme = "Random Insight For Me";
if (msg.text.toString().toLowerCase().includes(sendme)){
bot.sendMessage(msg.chat.id,"I want To Trigger Output Here");
    }
});

解决方法

最后这对我有帮助 -

`// install python-shell using npm
const {PythonShell} = require('python-shell');
var pyshell = new PythonShell('filename.py');
var test={}; // declaring it as empty array 
pyshell.on('message',function(message) {
test=message; // giving value to test
});
var sendme = "send me";
if (msg.text.toString().toLowerCase().includes(sendme)){
bot.sendMessage(msg.chat.id,test ); /** sending it when user 
triggers the conditon **/
}`

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...