首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Python脚本中的输入转换为Node.js电报机器人

将Python脚本中的输入转换为Node.js电报机器人
EN

Stack Overflow用户
提问于 2021-07-16 03:27:59
回答 1查看 52关注 0票数 0

当用户请求时,我想从电报机器人中的python脚本中获取输入我已经编写了从Python脚本中获取输入的代码,但我不知道如何在电报机器人中将其用于SendMessage

代码语言:javascript
复制
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");
    }
});
EN

回答 1

Stack Overflow用户

发布于 2021-07-16 19:17:00

最终,这帮助了我-

代码语言:javascript
复制
`// 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 **/
}`
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68399789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档