How can I delete a post from a supergroup in telegram with telegram-cli?

旧时模样 提交于 2020-01-06 20:05:50

问题


we have a group in telegram and we have a rule says no one must leave a message in group between 23 to 7 am , I wanna delete messages comes to group between these times automatically . could anyone tell me how I can do that with telegram cli or any other telegram client?


回答1:


Use new version of telegram-cli. It's not fully open source, but you can download a binary from its site. Also you can find some examples there.

I hope the following snippet in JavaScript will help you to achieve your goal.

var spawn = require('child_process').spawn;
var readline = require('readline');

// delay between restarts of the client in case of failure
const RESTARTING_DELAY = 1000;

// the main object for a process of telegram-cli
var tg;

function launchTelegram() {
  tg = spawn('./telegram-cli', ['--json', '-DCR'],
             { stdio: ['ipc', 'pipe', process.stderr] });
  readline.createInterface({ input: tg.stdout }).on('line', function(data) {
    try {
      var obj = JSON.parse(data);
    } catch (err) {
      if (err.name == 'SyntaxError') {
        // sometimes client sends not only json, plain text process is not
        // necessary, just output for easy debugging
        console.log(data.toString());
      } else {
        throw err;
      }
    }
    if (obj) {
      processUpdate(obj);
    }
  });
  tg.on('close', function(code) {
    // sometimes telegram-cli fails due to bugs, then try to restart it
    // skipping problematic messages
    setTimeout(function(tg) {
      tg.kill(); // the program terminates by sending double SIGINT
      tg.kill();
      tg.on('close', launchTelegram); // start again for updates
                                      // as soon as it is finished
    }, RESTARTING_DELAY, spawn('./telegram-cli', { stdio: 'inherit' }));
  });
}

function processUpdate(upd) {
  var currentHour = Date.now().getHours();
  if (23 <= currentHour && currentHour < 7 &&
      upd.ID='UpdateNewMessage' && upd.message_.can_be_deleted_) {
    // if the message meets certain criteria, send a command to telegram-cli to
    // delete it
    tg.send({
      'ID': 'DeleteMessages',
      'chat_id_': upd.message_.chat_id_,
      'message_ids_': [ upd.message_.id_ ]
    });
  }
}

launchTelegram(); // just launch these gizmos

We activate JSON mode passing --json key. telegram-cli appends underscore to all fields in objects. See all available methods in full schema.



来源:https://stackoverflow.com/questions/42140214/how-can-i-delete-a-post-from-a-supergroup-in-telegram-with-telegram-cli

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!