簡體   English   中英

從 Python 腳本獲取輸入到 Node.js Telegram Bot

[英]Take Input From Python Script To 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 **/
}`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM