SMS forward with 4G dongle

4G dongle

The dongle used is: Alcatel IK42UC . In lsusb, it should show:

1
1bbb:00b6 T & A Mobile Phones Mobilebroadband

Find the ttyUSB port

It should create 3 usb devices under ls /dev/ttyUSB*. Alcatel dongles have 3 slots/ports, and one of them will accpet at commands. Can try one by one to find out which one is accepting the command.

  • config file

    1
    2
    3
    4
    5
    6
    sudo nano /etc/gammurc

    [gammu]
    device = /dev/ttyUSB0 #Will start with 0
    connection = at115200
    logformat = textall
  • status command

    1
    sudo gammu networkinfo

Run the command and see if it returns anything. If not, modify the config to another ttyUSB port and try again.

Force to Tmobile

ATT has a whitelist, so I have to force the dongle to use Tmobile network for my roamming sim card.
Connect to dongle console:

1
sudo screen /dev/ttyUSBX 115200 #Replace with the port found above.

And run the commond in console to force Tmobile:

1
AT+COPS=1,2,"310260"

Note: 310260 instructs the hardware to seek out T-Mobile US

In my case, after the command to force TMO, I got

1
2
3
4
+CREG: 5,"3AFD","2CAD104", 7
+CGREG: 5,"3AFD","2CAD104",7
+CREG: 5,"3AFD","1422A01", 7
+CGREG: 5,"3AFD","1422A01",7

The 5 means registered and roaming.

To safely close interactive serial terminal without turning off the modem:

  1. Ctrl + A
  2. Press K (Kill)
  3. Confirm by pressing Y

We only need to do this once as it is written to the dongle’s internal disk and will remain after reboot.

Run

1
udevadm info --query=property --name=/dev/ttyUSBX | grep -E "ID_USB_INTERFACE_NUM|DEVNAME"

On the ttyUSB device that worked above. Note its ID_USB_INTERFACE_NUM.

Create a mapping rule by:

1
sudo nano /etc/udev/rules.d/99-alcatel-modem.rules

Enter the content:

1
SUBSYSTEM=="tty", ATTRS{idVendor}=="1bbb", ATTRS{idProduct}=="00b6", ENV{ID_USB_INTERFACE_NUM}=="00", SYMLINK+="unicom_modem"
  • idVendor and idProduct are from the lsusb info
  • ID_USB_INTERFACE_NUM is from the udevadm info
  • SYMLINK is your picked name for this symbolic link. In my case, a new device mapping name /dev/unicom_modem will be created pointing to the actual ttyUSB port.

Save file and apply the change by:

1
2
sudo udevadm control --reload-rules
sudo udevadm trigger

Modify the gammu config to:

1
2
3
4
5
6
sudo nano /etc/gammurc

[gammu]
device = /dev/unicom_modem
connection = at115200
logformat = textall

and try sudo gammu networkinfo again. It should work the same with the new link.

use gammu-smsd to auto receive sms

Install gammu-smsd:

1
sudo apt install gammu-smsd

Config sudo nano /etc/gammu-smsdrc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[gammu]
port = /dev/unicom_modem
connection = at115200

[smsd]
service = files
logfile = syslog
debuglevel = 0

MultipartTimeout = 20

# Paths where messages are stored
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/

RunOnReceive = /usr/local/bin/telegram-forward.sh
InitCommand = AT+COPS=1,2,"310260"

A few things:

  • MultipartTimeout is a delay in case a long message is cut into pieces. We wait until all message are received to process
  • RunOnReceive is the script to run when receiving a sms
  • InitCommand is the command to force TMO in case the dongle is reset for some reason.

Script to parse and forward sms to telegram

File located at: /usr/local/bin/telegram-forward.sh. Can change to other location if needed. Remember to add execute permision.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash

# Telegram Credentials
TOKEN= "xxxxx"
CHAT_ID= "yyyy"

# Gammu passes the sender number and text variables automatically
SENDER="$SMS_1_NUMBER"

FULL_TEXT=""
i=1
while true; do
var_name="SMS_${i}_TEXT"
if [ -z "${!var_name}" ]; then
break
fi
FULL_TEXT="${FULL_TEXT}${!var_name}"
i=$((i + 1))
done

# If the text is still empty, fallback to the standard part 1
if [ -z "$FULL_TEXT" ]; then
FULL_TEXT="$SMS_1_TEXT"
fi

CLEAN_TEXT=$(echo "$FULL_TEXT" | sed 's/[_*`\[]/\\&/g')

# Format the message nicely for Telegram
MESSAGE="%0A*From:* $SENDER%0A*Message:* $CLEAN_TEXT"

# Send to Telegram via curl using Markdown formatting
curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" \
-d "chat_id=$CHAT_ID" \
-d "text=$MESSAGE" \
-d "parse_mode=Markdown"

exit 0

Enable gammu-smsd service

1
2
3
sudo systemctl stop gammu-smsd
sudo systemctl start gammu-smsd
sudo systemctl enable gammu-smsd