Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Openwrt/LEDE smstools3 forward sms to telegram
opkg update
opkg install kmod-usb-serial kmod-usb-serial-wwan kmod-usb-serial-option usb-modeswitch smstools3 curl iconv

vi /usr/local/bin/pushsms

#!/bin/sh

chat_id=xxxxxxxx
token=xxxxxxxx

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

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

    text=$(cat <<EOF
$content
From $from
EOF
)

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

fi

vi /etc/smsd.conf

#
# Description: Main configuration file for the smsd
#

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]
init = AT+CPMS="ME","ME","ME"
device = /dev/ttyUSB1
incoming = yes
#pin = 0000
baudrate = 9600
signal_quality_ber_ignore = yes
detect_unexpected_input = no
memory_start = 0

/etc/init.d/smstools3 restart

@bjo81

This comment has been minimized.

Copy link

@bjo81 bjo81 commented Jul 9, 2017

Thanks for this snippet, I had to change UNICODEBIG to ISO-8859-1, otherwise I got some garbage: 䙲潭㨠灩空愮摥੆牯浟呏䄺⁄〠慬

@codexss

This comment has been minimized.

Copy link
Owner Author

@codexss codexss commented Jul 25, 2017

@bjo81 u can try remove | iconv -f UNICODEBIG -t UTF-8 if ur language is english, this is only chinese needed
here is a better script https://zijung.me/archives/lede-receive-sms-and-forward-to-telegram.html

@shellus

This comment has been minimized.

Copy link

@shellus shellus commented Jun 3, 2019

这很好,我使用它成功的将短信转发到我的微信消息,is good!

@Meroser

This comment has been minimized.

Copy link

@Meroser Meroser commented Sep 7, 2019

哥门 https://zijung.me/archives/lede-receive-sms-and-forward-to-telegram.html这里的
發件人: ${from}
發件時間: ${sent}
收件時間: ${received}
如果这几行存在 收不到消息 ,,有啥办法保留这几个 发出消息的不

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