0

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

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");
    }
});
  • Are you getting a particular error message? Do you see the messages coming back from the pythonshell callback (the console.log)? Is the bot.on('message') callback being fired? – Erty Seidohl Jul 15 '21 at 19:32
  • no there is no error message , I just want to sent the output from python file to the user when he asks "Randome Insight For Me or SendMe" – Pankaj Bhadane Jul 15 '21 at 20:10
  • here is my git repo with all the code - https://github.com/pankaj-kb/taskbot – Pankaj Bhadane Jul 15 '21 at 20:12
  • msg.text.toString().toLowerCase() will never include "Random" since the "R" is capital. Try lowercasing the sendme text as well? – Erty Seidohl Jul 15 '21 at 21:28

1 Answers1

0

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