Skip to content

Instantly share code, notes, and snippets.

@rez0n
Forked from codexss/smstools3.md
Last active May 10, 2024 16:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rez0n/6d296017179b62069c64146da18daa56 to your computer and use it in GitHub Desktop.
Save rez0n/6d296017179b62069c64146da18daa56 to your computer and use it in GitHub Desktop.
Linux smstools eventhandel for forward sms to telegram (adapted to Cyrillic alphabet)
sudo apt-get update
sudo apt-get install smstools curl iconv

sudo nano /usr/local/bin/pushsms

#!/bin/bash

chat_id=xxxxxxxx
token=xxxxxxxx

if [ "$1" == "RECEIVED" ]; then
  from=$(grep -a "From:" $2 | awk -F ': ' '{printf $2}')
  sent=$(grep -a "Sent:" $2 | awk -F ': ' '{printf $2}')
  received=$(grep -a "Received:" $2 | awk -F ': ' '{printf $2}')
  alphabet=$(grep -a "Alphabet:" $2 | awk -F ': ' '{printf $2}')

  if [ "$alphabet" = "UCS2" ]; then
    content=$(tail -n +13 $2 | iconv -c -f UCS-2BE -t UTF-8)
  else
    content=$(sed -e '1,/^$/ d' < "$2")
  fi

  text=$(cat <<EOF
From: ${from} (${sent})

${content}
EOF
)

  curl -s -d "chat_id=${chat_id}&text=${text}" -X POST "https://api.telegram.org/bot${token}/sendMessage"
fi

sudo nano /etc/smsd.conf

#
# This is only example for demonstrate eventhandler parameter
#

devices = GSM1
incoming = /var/spool/sms/incoming
outgoing = /var/spool/sms/outgoing
checked = /var/spool/sms/checked
failed = /var/spool/sms/failed
sent = /var/spool/sms/sent
receive_before_send = no
autosplit = 3
loglevel = notice
#delaytime = 0
eventhandler = /usr/local/bin/pushsms

[GSM1]
device = /dev/ttyUSB0
init = AT^CURC=0
incoming = yes
baudrate = 115200

sudo systemctl restart smstools.service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment