I’m testing this by sending an sms from my phone with delivery report enabled. If the autopi is off, I don’t get the delivery report but with it on the delivery report is delivered pretty quickly … so looks like the message is getting to the autopi.
If I put the sim into a spare phone I don’t see the old messages but new messages come through.
I’m wondering if the autopi is reading / deleting all messages ?
Afterwards, you should be able to retrieve the text messages from the modem.
We have a startup task that executes the above command that enables us to retrieve the messages, but the value is not remembered when the modem is powered off, so it needs to be executed each time it starts.
But… It is currently disabled because it fails if there is no sim card present, a fix is on our roadmap, but in the meantime, you can run the command manually, or maybe set up a job that does it on startup.
How exactly the text messages work, like where they are stored, I’m not entirely sure, but I think it’s on the modem, so that’s probably why you don’t see the previous messages when you move the SIM card to a spare phone.
Here is my custom code to read SMS’s on autopi, forward to telegram and delete once forwarded. My main reason for doing this is to get any “out of credit” messages from the operator.
import logging
import requests
import os
import re
log = logging.getLogger(__name__)
# Telegram tokens - see https://www.mariansauter.de/2018/01/send-telegram-notifications-to-your-mobile-from-python-opensesame/
#
bot_token = 'xxx'
bot_chatID = 'xxx'
def forward():
"""
Poll for sms and forward to telegram
Once sent, delete from autopi
"""
__salt__['ec2x.query']('AT+CMGF=1')
args = ['AT+CMGL="ALL"']
res = __salt__['ec2x.query']('AT+CMGL="ALL"')
if 'data' in res:
allmessages = parse(res['data'])
for message in allmessages:
log.info ("SMS: "+message['message'])
bot_sendtext('SMS From: '+message['from']+'\nDate: '+message['date']+'\n\n'+message['message'])
__salt__['ec2x.query']('AT+CMGD='+message['index'])
return {"msg": "Forward finished"}
# parse output to an array of dictionary
#
def parse(x):
res = []
i = 0
while i < len(x):
m = re.match('\+CMGL: (\d+),\"(.+)\",\"(.+)\",(.*),\"(.+)\"', x[i])
if m:
res.append({'index':m.group(1),'from':m.group(3),'date':m.group(5),'message':x[i+1]})
else:
log.info ("SMS: No match "+x[i])
i += 2
return res
# send message to telegram
#
def bot_sendtext(bot_message):
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
requests.get(send_text)