前引

在接收到短信之后会将内容较大的短信内容拆分到不同文件中保存,在之前的自动转发脚本转发短信文本内容不全

image-20240723105748080

解决

最近在阅读gammu官方文档的时候发现官方有个Processing message text in Python 的示例:

Following script (if used as RunOnReceive handler) written in Python will concatenate all text from received message:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python

import os

numparts = int(os.environ["DECODED_PARTS"])

text = ""
# Are there any decoded parts?
if numparts == 0:
text = os.environ["SMS_1_TEXT"]
# Get all text parts
else:
for i in range(1, numparts + 1):
varname = "DECODED_%d_TEXT" % i
if varname in os.environ:
text = text + os.environ[varname]

# Do something with the text
print("Number {} have sent text: {}".format(os.environ["SMS_1_NUMBER"], text))

这个python脚本能将在不同文件中的短信内容拼接,刚好能够实现我们的需求,将这个python代码转写为shell代码,和之前的短信转发代码组合后的新代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
numparts=${DECODED_PARTS}
text=""
if [ "$numparts" -eq 0 ]; then
text=${SMS_1_TEXT}
else
for i in $(seq 1 $numparts); do
varname="DECODED_${i}_TEXT"
if [ ! -z "${!varname}" ]; then
text="${text}${!varname}"
fi
done
fi

echo "${text}" |mutt -s "树莓派转发${SMS_1_NUMBER}短信" mstzf@foxmail.com

就能够完美实现短信转发功能

gammu官网链接:

ec20模块使用: