简体   繁体   English

Telegram Bot 从外部运行命令

[英]Telegram Bot run commands from outside

Ok i'm quite new to working with telegram bots but i managed well so far with some basic bot.好的,我对使用电报机器人很陌生,但到目前为止我用一些基本的机器人管理得很好。 Now i want to improove a bit things and let my site "feed" the bot.现在我想改进一些东西,让我的网站“喂养”机器人。

This is the scenario这是场景

I have a google spreadsheet that make some calculation and then send a message to the bot with the classic url.我有一个谷歌电子表格,可以进行一些计算,然后使用经典 url 向机器人发送消息。 Something like this...像这样的东西...

var optionsUG = {
   'method' : 'post',
   'payload' : formDataUG,
   'muteHttpExceptions':true
 };
 var optionsLG = {
   'method' : 'post',
   'payload' : formDataLG
 };
  //SpreadsheetApp.getUi().alert('UrlFetchApp options ['+options+"]");
 //UrlFetchApp.fetch('https://api.telegram.org/bot'+token+'/sendMessage?chat_id='+channelNumber+'&text='+text);
  var result = UrlFetchApp.fetch('https://api.telegram.org/bot'+token+'/sendMessage',optionsUG);
  Utilities.sleep(5 * 1000);
  result = UrlFetchApp.fetch('https://api.telegram.org/bot'+token+'/sendMessage',optionsLG);

now i would like to make something like but, instead of sendMessage i vould like to call a method of my bot i use javascript Telegraf framework ATM but i can change is not a problem.现在我想做一些类似的事情,但是我想调用我的机器人的方法而不是 sendMessage 我使用 javascript Telegraf 框架 ATM 但我可以改变不是问题。 i want to achieve something like我想实现类似的目标

var result = UrlFetchApp.fetch('https://api.telegram.org/bot'+token+'/register',optionsUG);

here is the bot currently configured这是当前配置的机器人

const serverPath = "/home/bots/PlatoonAdvisor/telegram";

const commands = require(serverPath+'/package/modules/commands.js');
const config = require(serverPath+'/config.json');

var helpText = require(serverPath+'/package/help.txt');

const token = config.TELEGRAM_BOT_SECRET;

const Telegraf = require('telegraf');
const bot = new Telegraf(token);
const REGISTER_COM = 'register';
const HELP_COM = 'help';
const REQUIREMENTS_COM = 'requirements';
const CAHT_ID_COM = 'chatid';


const getCommandParameters = function (text, command) {
  var reg = "/\/"+command+" (.*)/g";
  var arr = text.match(reg);
  return arr;
}

/*
bot.on('text', message=> {
  return message.reply('I am Grooth');
})
*/
bot.command(HELP_COM, ctx=> {
  return ctx.reply(helpText);
});
bot.command(REGISTER_COM, ctx=> {
  var replyMsg;
  var param = getCommandParameters(ctx.message.text, REGISTER_COM);
  var player_name, allycode;
  if (param != null) {
    try {
      var params = param.split(",");
      if (params.length < 2) {
        replyMsg = "Missing parameters, try /help if you need help :)";
        throw replyMsg;
      }
      player_name = params[1];
      allycode = params[0];
      var channel = ctx.chat.id;
      commands.registerTPlayer(player_name, allycode, channel);
      replyMsg = "Successfully registered player ${player_name} with allycode ${allycode}!"
    } catch (ex) {
      console.log (ex);
    }
  }
  return ctx.reply(replyMsg);
});
bot.command(REQUIREMENTS_COM, ctx=> {
  var param = getCommandParameters(ctx.message.text, REQUIREMENTS_COM);
  var params = param.split(",");
  var json = ctx.chat.id;
  return ctx.reply(json);
});
bot.command(CAHT_ID_COM, ctx=> {
  var id = ctx.chat.id;
  var msg = "The chat id requested is ${id}";
  return ctx.reply(msg);
});
bot.startPolling();

is that even possible?这甚至可能吗? i'm looking over the internet for a while now and was not able to find any clue about.我现在在互联网上搜索了一段时间,但找不到任何线索。

Any help appreciated tnx任何帮助表示赞赏 tnx

EDIT: Doing some more digging i found webhooks to send content to a web server when something happen in the bot but not viceversa ... i'm getting frustrated编辑:做一些更多的挖掘,我发现当机器人发生某些事情时,webhooks 可以将内容发送到网络服务器,反之亦然......我很沮丧

My goal is to update the local database with informations the spreadsheet have but the bot still don't so users can later ask to the bot to retrieve those informations.我的目标是使用电子表格中的信息更新本地数据库,但机器人仍然没有,以便用户稍后可以要求机器人检索这些信息。

i mean i could make an ajax call if it were a real web server but it is just a spreadsheet wich don't act as a server ... :(我的意思是,如果它是真正的 Web 服务器,我可以进行 ajax 调用,但它只是一个电子表格,不能充当服务器...... :(

Ok i forgot to answer this question with the solution i found.好吧,我忘了用我找到的解决方案来回答这个问题。

there is no way indeed to call a specific function of the bot from the outside because it is not a real function, it is a parsed string that a user type and the bot interpret as a command.确实没有办法从外部调用机器人的特定函数,因为它不是真正的函数,它是用户键入的解析字符串,机器人将其解释为命令。

So i had to be creative and expose a RestServer from the bot itself (the nodejs express library did the trick) wich i was then able to call from the script.所以我必须要有创意并从机器人本身公开一个 RestServer(nodejs express 库做到了这一点),然后我就可以从脚本中调用了。

Here an handy guide for Express.js这是 Express.js 的便捷指南

This is my solution wich is working great now.这是我的解决方案,现在效果很好。 Tnx :) tnx :)

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

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