Telegram CLI unread messages - raspberry-pi

I want to display unread messages with telegram cli running on my raspberry pi, and save them to some file which will be processed later with php, and displayed on a small TFT screen.
I think it may be possible with get_dialog_list () but I can't find a way to make it work.
Any help/advice is welcome! :-)

If you choose php as a laguage to write your customized client or just pull messages from Telegram, you need to connect to the tg client and then send or receive messages.
You can easily wirte a Bash script and use it inside your php code.
#!/bin/bash
now=$(date)
from=$1
subject=$2
body=$3
tgpath=/home/telpath/tg
LOGFILE="/home/logpath/tglog.log"
cd ${tgpath}
${tgpath}/telegram -k ${tgpath}/tg-server.pub -W <<EOF
msg $to $subject
safe_quit
EOF
echo "$now Recipient=$from " >> ${LOGFILE}
echo "Finished" >> ${LOGFILE}
So we will have:
<?php
while (TRUE) {
$output = shell_exec('tg.sh', '#user');
echo "<pre>$output</pre>";
}
?>
You can easily iterate over your bash script (tg.sh) to pull messages from any contact.
Note that these codes are trivial like a sudo code and needs more development.

Related

command to email file as an attachment

I'm using the following command to send an email with a specific file attached from the web server to me
mail -A /home/root/BACKUPS/backup.sql -s "$(date +%F) sql Backup" me#domain.com < /dev/null
Resource: https://tecadmin.net/ways-to-send-email-from-linux-command-line/
Problem is that although it is sending me an email, it's not attaching the item (backup.sql).
Does anyone see the problem with this code? Or is there something else that needs to be done to make the code work?

Script response if md5sum returns FAILED

Say I had a script that checked honeypot locations using md5sum.
#!/bin/bash
#cryptocheck.sh
#Designed to check md5 CRC's of honeypot files located throughout the filesystem.
#Must develop file with specific hashes and create crypto.chk using following command:
#/opt/bin/md5sum * > crypto.chk
#After creating file, copy honeypot folder out to specific folders
locations=("/share/ConfData" "/share/ConfData/Archive" "/share/ConfData/Application"
"/share/ConfData/Graphics")
for i in "${locations[#]}"
do
cd "$i/aaaCryptoAudit"
/opt/bin/md5sum -c /share/homes/admin/crypto.chk
done
And the output looked like this:
http://pastebin.com/b4AU4s6k
Where would you start to try and recognize the output and perhaps trigger some sort of response by the system if there is a 'FAILED'?
I've worked a bit with PERL trying to parse log files before but my attempts typically failed miserably for one reason or another.
This may not be the proper way to go about this, but I'd want to be putting this script into a cronjob that would run every minute. I had some guys telling me that an inotify job or script (I'm not familiar with this) would be better than doing it this way.
Any suggestions?
--- edit
I made another script to call the script above and send the output to a file. The new script then runs a grep -q on 'FAILED' and if it picks anything up, it sounds the alarm (tbd what the alarm will be).
#!/bin/bash
#cryptocheckinit.sh
#
#rm /share/homes/admin/cryptoalert.warn
/share/homes/admin/cryptocheck.sh > /share/homes/admin/cryptoalert.warn
grep -q "FAILED" /share/homes/admin/cryptoalert.warn && echo "LIGHT THE SIGNAL FIRES"
Use:
if ! /opt/bin/md5sum -c /share/homes/admin/crypto.chk
then
# Do something
fi
Or pipe the output of the loop:
for i in "${locations[#]}"
do
cd "$i/aaaCryptoAudit"
/opt/bin/md5sum -c /share/homes/admin/crypto.chk
done | grep -q FAILED && echo "LIGHT THE SIGNAL FIRES"

Curl command uploading document fails when run from Perl

I've got a Perl script that uploads documents into Alfresco using curl.
Some of the documents have ampersand in the file name and initially this caused curl to fail. I fixed this by placing a carat symbol in front of the ampersand. But now I'm finding some documents are failing to upload when they don't have a space either side of the ampersand. Other documents with spaces in the file name and an ampersand do load successfully.
The snippet of Perl code that is running is:
# Escape & for curl in file name with a ^
my $downloadFileNameEsc = ${downloadfile};
$downloadFileNameEsc =~ s/&/^&/g;
$command = "curl -u admin:admin -F file=\#${downloadFileNameEsc} -F id=\"${docId}\" -F title=\"${docTitle}\" -F tags=\"$catTagStr\" -F abstract=\"${abstract}\" -F published=\"${publishedDate}\" -F pubId=\"${pubId}\" -F pubName=\"${pubName}\" -F modified=\"${modifiedDate}\" -F archived=\"${archived}\" -F expiry=\"${expiryDate}\" -F groupIds=\"${groupIdStr}\" -F groupNames=\"${groupNameStr}\" ${docLoadUrl}";
logmsg(4, $command);
my #cmdOutput = `$command`;
$exitStatus = $?;
my $upload = 0;
logmsg(4, "Alfresco upload status $exitStatus");
if ($exitStatus != 0) {
You can see that I am using backticks to execute the curl command so that I can read the response. The perl script is being run under windows.
What this effectively tries to run is:
curl -u admin:admin -F file=#tmp-download/Multiple%20Trusts%20Gift%20^&%20Loan.pdf -F id="e2ef104d-b4be-4896-8360-7d6f2e7c7b72" ....
This works.
curl -u admin:admin -F file=#tmp-download/Quarterly_Buys^&sells_Q1_2006.doc -F id="78d18634-ee93-4c29-b01d-270aeee3219a" ....
This fails!!
The only difference being as far as I can see is that in the one that works the file name has spaces (%26) in the file name somewhere around the ampersand, not necessarily next to the ampersand.
I can't see why one runs successfully and the other doesn't. Think it must be to do with backticks and ampersands in the file name. I haven't tried using system as I wanted to capture the response.
Any thoughts because I've exhausted all options.
You should learn to use Perl modules. Perl has some great modules to handle the Web requests. If you depend upon operating system commands, you will end up with not only dependencies upon those commands, but shell interactions and whether or not you need to quote special characters.
Perl modules remove a lot of the issues that you can run into. You are no longer dependent upon particular commands or even particular implementation of those commands. (The curl command can vary from system to system, and may not even be on the system you're on). Plus, most of these modules handle the piddling details for you (such as URI escaping strings).
LWP is the standard Perl library for implementing these requests. Take a look at the LWP Cookbook. This is a tutorial on the whole HTTP process. Basically, you need to create an agent which is really just a virtual web browser for you to use. Then, you can configure it (for example, setting the machine, browser type, etc.) you might need.
What is really nice is HTTP::Request::Common that provides a simple interface for using HTTP forms.
my $results = POST "$docLoadUrl"
[ file => '#' . "$downloadFileName",
id => $docId,
title => $docTitle,
tag => $catTagStr,
abstract => $abstract,
published => $publishedDate,
pubId => $pubId,
pubName => $pubName,
...
];
This is a lot easier to read and maintain. Plus, it will handle URI encoding for you.

PowerShell raw FTP

I am looking to use FTP to write a mainframe jobstream. To do that, I can connect to the mainframe via FTP and run these commands:
QUOTE TYPE E
QUOTE SITE FILETYPE=JES
PUT myjob.jcl
So, how would I do this in PowerShell? I have read plenty on standard FTP transfers, but I haven't seen anything about customizing the calls for prep.
Thanks for reading.
EDIT
Here is the solution I came up with, per the answer here and this article:
#echo off
echo user %1> ftpcmd.dat
echo %2>> ftpcmd.dat
echo QUOTE TYPE E>> ftpcmd.dat
echo QUOTE SITE FILETYPE=JES>> ftpcmd.dat
echo put %3>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat %4
del ftpcmd.dat
Usage:
C:\>fileup user123 pa$$word myfile.txt ftp.server.com
Could you use the -s switch for FTP and specify a file that contains your commands to run?
-s:filename Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
Then you can call this in PowerShell as
ftp -s:<myscript> <host>
If you're comfortable with .NET and want to do it programatically, there is a similar question/answer here:
Upload files with FTP using PowerShell
It pretty much goes comes down to using a System.Net.FtpWebRequest for the job. At first glance it seems fairly straight forward.

Cron send email with STDERR but NOT STDOUT?

I have some python scripts that run on a daily basis in cron. How can I have cron send me an email ONLY WHEN THERE IS STDERR OUTPUT from my script? I want to be able to mail multiple recipients, and set the subject line individually for each cron entry.
I tried this:
./prog > /dev/null | mail . . .
but it didn't work -- I still receive blank emails when there is no STDERR. Do I need to do this in the script itself?
Sorry if this seems basic, I have googled a lot but can't seem to find this answered succintly.
For cron you don't need to pipe through mail. The cron daemon will automatically mail any output of your command to you. Your crontab entry should look like:
# every minute
* * * * * ./prog >/dev/null
If there is no STDERR output, you won't get any mail.
You are asking incorrect question. When you are using mail(1) to send the email, it is no longer relevant that its in cron. What you actually need is to pipe stderr to stdin of mail. Normal pipe is from stdout to stdin, so simplest way to solve this is redirect:
{ /prog > /dev/null ; } 2>&1 | mail ...
Or in the less-clear way because of confusing order of redirectings:
/prog 2>&1 > /dev/null | mail ...
mail v1.6 has an option to not send messages with an empty body:
-E Do not send messages with an empty body.
This is useful for piping errors from cron(8) scripts.
This might be what you are looking for.
The -s file test will tell you if a file exists and has size greater than zero.
./prog >/dev/null 2>some/file ; if [ -s some/file ] ; then mail < some/file ; fi
There is a nice tool called cronic that does this. It is part of the moreutils package.
If your SCRIPT has commands that may produce STDERR that you want to be notified on, then you need to use a mail or mailx call within the script itself (if then else or ). The cron job STDOUT and STDERR redirects are ONLY for cron job EXECUTION STDOUT and STDERR. hkmaly had it right on the n