简体   繁体   English

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

[英]Take Input From Python Script To Node.js Telegram Bot

I want to take input from the python script in the Telegram Bot When User asks for it I Have written the code to take input from Python Script But I Can't Figure out how to Use it in Telegram Bot to SendMessage当用户要求时,我想从 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");
    }
});

finally this helped me -最后这帮助了我 -

`// 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 **/
}`

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

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