Bài viết này mình sẽ chia sẻ 1 script chạy bằng bash shell dùng để check các thư mới của gmail sau đó gửi thông báo qua telegram nhé.
Nội dung script như sau:
#!/bin/bash
gmailtotelegram(){
while :
do
for ((j=1; j<21; j++))
do
timeloop=1
server='https://mail.google.com/mail/feed/atom'
domain='gmail.com'
gmail="$username@$domain"
unset email title summary message stttelegram
username=''
password=''
token=''
chatid=''
file_email=/root/$username/email_$username
file_title=/root/$username/title_$username
file_summary=/root/$username/summary_$username
file_new=/root/$username/new_hexdump_hoanghd_$username
file_hexdump=/root/$username/data_hexdump_hoanghd_$username
file_message=/root/$username/message_$username
check_new_hexdump_hoanghdfile=$((ls $file_new >> /dev/null 2>&1 && echo yes) || echo no)
if [[ $check_new_hexdump_hoanghdfile == 'no' ]];then touch $file_new; fi
check_data_hexdump_hoanghdfile=$((ls $file_hexdump >> /dev/null 2>&1 && echo yes) || echo no)
if [[ $check_data_hexdump_hoanghdfile == 'no' ]];then touch $file_hexdump; fi
touch $file_email $file_title $file_summary
curl -u "$gmail":"$password" --silent "$server" | grep -oP "(?<=<email>)[^<]+" | awk "FNR>=$j && FNR<=$j" > $file_email
curl -u "$gmail":"$password" --silent "$server" | grep -oPm1 "(?<=<title>)[^<]+" | sed '1d' | awk "FNR>=$j && FNR<=$j" > $file_title
curl -u "$gmail":"$password" --silent "$server" | grep -oPm1 "(?<=<summary>)[^<]+" | awk "FNR>=$j && FNR<=$j" > $file_summary
# curl -u "hoangbp562@gmail.com":"HadanghoangHathiyen19901993" --silent "https://mail.google.com/mail/feed/atom" | grep -oPm1 "(?<=<summary>)[^<]+" | head -n 2
email=$(cat $file_email)
title=$(cat $file_title)
summary=$(cat $file_summary)
if [[ $email == '' || $title == '' || $summary == '' ]];then status=0; else status=1; fi
rm -rf $file_email $file_title $file_summary
echo -e "\nEmail: $email\nTitle: $title\nSummary: $summary" > $file_message
message=$(cat $file_message)
hexdump -C $file_message | grep -Eo '[0-9]' | tr -d '\n' > $file_new
new_hexdump_hoanghd=$(cat $file_new)
check_data_hexdump=$(cat $file_hexdump | grep "$new_hexdump_hoanghd")
if [[ $check_data_hexdump == '' ]];then
hexdump_value=0
echo "$new_hexdump_hoanghd" >> $file_hexdump
else
hexdump_value=1
fi
if [[ $status == 1 && $hexdump_value == 0 ]]; then
URL="https://api.telegram.org/bot$token/sendMessage"
curl -s -X POST $URL -d chat_id=$chatid -d text="$message" >> /dev/null
stttelegram='Yes'
sleep $timeloop
else
sleep $timeloop
stttelegram='No'
fi
echo -e "\n---------------------------------------------------\nCheck new email status number: $j $status $hexdump_value\nSend telegram: $stttelegram\nEmail: $email\nTitle: $title\nSummary: $summary\n---------------------------------------------------"
done
done
}
gmailtotelegram
Cách sử dụng đơn giản, các bạn thay tài khoản đăng nhập của gmail vào username, mật khẩu gmail vào password, token botid của telegram vào token và chatid của telegram vào chatid. Ví dụ
username=’hoanghd164′
password=’passgmail’
token=1740152898:AAHtP7a6mmKcEc-GcBpsqyfGh3xamvSiXHk
chatid=-502481549
Sau đó chạy file script là xong.