Automatically clean telegram messages
Posted: Thursday 08 September 2016 9:01
Maybe not directly a script for Domoticz, but lot's of us use Telegram for notifications.
When (on iPhone) you receive the messages from the bot as single user you can easily remove the history from the chat, but when using a channel (so multiple users receive the same message) you just can't. Then you have to select each individual message to remove it. A huge work if you use the Telegram system a lot.
Based on the Telegram CLI program I created a script that removes all messages that are older than x seconds. Freeing the chat history and memory of your phone. Especially handy if you also receive pictures from cameras.
First you need to get the Telegram-cli from https://github.com/vysheng/tg up and running.
Then create a file /home/pi/cleantelegram.sh and make it executable
I am using the bot for technical notifications and a channel for general stuff that my wife is also interested in, that's why I run it twice.
Then create the /home/pi/cleantelegram.php file and make it executable
Add the shell script to cron (I have unlimited internet during the night, so I only run it then)
Enjoy!
When (on iPhone) you receive the messages from the bot as single user you can easily remove the history from the chat, but when using a channel (so multiple users receive the same message) you just can't. Then you have to select each individual message to remove it. A huge work if you use the Telegram system a lot.
Based on the Telegram CLI program I created a script that removes all messages that are older than x seconds. Freeing the chat history and memory of your phone. Especially handy if you also receive pictures from cameras.
First you need to get the Telegram-cli from https://github.com/vysheng/tg up and running.
Then create a file /home/pi/cleantelegram.sh and make it executable
Code: Select all
#!/bin/bash
pgrep telegram-cli && running=1 || running=0
if [ $running == 0 ] ; then
echo $(/home/pi/tg/bin/telegram-cli -k tg-server.pub -W -N -e 'history Egregius 5000' --json -C --permanent-msg-ids) > /var/log/telegram.txt
sleep 1
/home/pi/cleantelegram.php channel 259200
sleep 1
echo $(/home/pi/tg/bin/telegram-cli -k tg-server.pub -W -N -e 'history Egregius#1 5000' --json -C --permanent-msg-ids) > /var/log/telegram.txt
sleep 1
/home/pi/cleantelegram.php bot 259200
else
echo 'already running'
sleep 5
sudo killall telegram-cli
fi
Then create the /home/pi/cleantelegram.php file and make it executable
Code: Select all
#!/usr/bin/php
<?php
error_reporting(E_ALL);ini_set("display_errors","on");
$data=file_get_contents('/var/log/telegram.txt');
$data=strstr($data,"[{");
$data=substr($data,1);
$data = substr($data, 0, strpos($data, "]"));
$datas=explode('}, {',$data);
foreach($datas as $data){
$encoded='{'.$data.'}';
$encoded=str_replace('{{','{',$encoded);
$decoded=json_decode($encoded,false);
$msg='';
if(!empty($decoded->id)&&!empty($decoded->date)) {
//This is the message id from the text 'channel created' that can't be deleted, change it if needed
if($decoded->id!='05000000980ee83e010000000000000044653f1eedca68e6'){
if($decoded->date<(time()-$argv[2])) {
$clean= $decoded->id;$tijd=$decoded->date;
isset($decoded->text)?$tekst=$decoded->text:$tekst='FOTO';
$msg.='-'.strftime("%e-%d %k:%M",$tijd).' '.$tekst;
logwrite($msg,$argv[1]);
shell_exec("/home/pi/tg/bin/telegram-cli -k tg-server.pub -W -N -e 'delete_msg $clean' -C --permanent-msg-ids");
print $msg.PHP_EOL;
sleep(15);
} else {
logwrite('Arrived at recent messages');
die('Arrived at recent messages of '.$argv[1].PHP_EOL);
}
}
}
}
function logwrite($msg,$msg2=NULL){
$time=microtime(true);$dFormat="Y-m-d H:i:s";$mSecs=$time-floor($time);$mSecs=substr(number_format($mSecs,3),1);
$fp=fopen('/files/temp/cleantelegram.txt',"a+");fwrite($fp,sprintf("%s%s %s %s\n",date($dFormat),$mSecs,$msg2,$msg));fclose($fp);
}
Code: Select all
0 1,11 * * * /home/pi/cleantelegram.sh