We have moved to a new Sailfish OS Forum. Please start new discussions there.
33

Sending an SMS via the command line

asked 2014-01-21 16:23:15 +0300

rolandw gravatar image

updated 2014-01-22 00:37:51 +0300

CsTom gravatar image

I would like to be able to send an SMS via the command line. I'd also like to be able to dial a number and hang up a call but perhaps that's a step too far for now. I could do both via some application magic on the N900 (http://wiki.maemo.org/SMSCON) and it seems logical to be able to continue to do so on the Jolla.

Has anyone worked out how to do it?

TIA.

edit retag flag offensive close delete

Comments

2

Sending SMS per ssh is kinda cool (in a very nerdy way) and porting SMSCON would be nice too.

PyroDevil ( 2014-01-22 00:36:32 +0300 )edit

5 Answers

Sort by » oldest newest most voted
38

answered 2014-01-22 00:32:37 +0300

CsTom gravatar image

updated 2014-01-24 13:17:52 +0300

Yep, the key is that the modem is called /ril_0 (not isimodem as on N900), otherwise the dbus commands work nicely.

For example sending and SMS to +358500000000:

dbus-send --system --print-reply --dest=org.ofono /ril_0 org.ofono.MessageManager.SendMessage string:"+358500000000" string:"test sms" (okay, the --print-reply you don't need for sending, but for other queries it's good to have it on the line :-)

UPDATE: dbus-send --type=method_call --dest=org.nemomobile.qmlmessages / org.nemomobile.qmlmessages.startSMS array:string:"+358123456" string:"Hello world" would open the message editor on the phone and you have to press the "Send" button to actually send it. Might be useful for some, eg. just to proof-read the SMS before sending. (and also, this is the allowed method for applications wana be on Harbour)

edit flag offensive delete publish link more

Comments

Nice one:) Very useful. Richard

richardski ( 2014-01-22 10:44:41 +0300 )edit
4

So nice that it's possible to do that without any hacking. That's why I love Jolla :)

Thanks @CsTom.

Sthocs ( 2014-01-22 15:17:09 +0300 )edit
3

Is it possible to monitor an incoming SMS ?

Amelot ( 2014-01-23 20:28:11 +0300 )edit
1

Please keep in mind that sending messages without opening the message editor is not allowed in Harbour.

rainisto ( 2014-01-24 11:54:20 +0300 )edit

@Amelot you can read SMS and even save them when sent from command line, see my answer. @CsTom After typing dbus-monitor, I saw some calls to Telepathy when using Jolla Messages app. Does Jolla use oFono or Telepathy? It looks like these applications can do the same thing: https://gitorious.org/meego-cellular/ofono/source/a0f4dd266aab85e198b2be4735abf81e4b7bb718:doc/message-api.txthttp://telepathy.freedesktop.org/spec/Channel_Interface_Messages.html

baptx ( 2014-09-24 20:40:04 +0300 )edit
17

answered 2014-01-22 14:09:32 +0300

rolandw gravatar image

Many thanks to Richard for confirming that the dbus-send should still work. I produced a bash script which, if placed in your /usr/local/bin directory will do the job for you. I've called the script qSMS.

You can get this from github at https://github.com/qururoland/qsms

Alternatively copy and paste the following:

#! /usr/bin/env bash

# Send an SMS message to one or more recipients via the commandline 
# via SailfishOS 
# Usage: qsms "The message" "+4412345678" "0207160 2888"...
# newfile is optional and will always have .jpg as a file type

# Check to see that at least one parameter has been passed
if [ $# -lt 2 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] 
then
    echo ""
    echo "qSMS - a command line SMS tool"
    echo "------------------------------"
    echo ""
    echo "Usage: qsms \"Your message\" \"tel no\" [\"another tel no\"]"
    echo "This will send your message to each of the telephone numbers"
    echo ""
    echo "The message must be a single string. Use \n for new lines."
    echo "Telephone numbers can be local (ie \"07768345678\") or"
    echo "internationally formatted (ie \"+447768345678\")."
    echo ""
    echo "Roland Whitehead, http://quru.com, January 2014"
    echo ""
    echo ""
    exit 1
fi

declare -i itsGood=1

function checkPattern {
    # check the number $1 ($0 is alays the function name)
    if [[ "$1" =~ "^\+?[0-9\ ]*$" ]]
    then
    return 0
    else
    return 1
    fi
}

# Get the message
theMessage="$1"
# Clear out the first parameter
shift

# Loop through the rest of the parameters, checking each and sending it out
declare -i countSent=0
declare -i countFailed=0
while [ -n "$1" ]
  do
    theNumber="$1"
    if checkPattern $theNumber
    then
    #echo "Sending $theMessage to $theNumber"
    dbus-send --system --print-reply --dest=org.ofono /ril_0 org.ofono.MessageManager.SendMessage string:"$theNumber" string:"$theMessage"
    countSent=$countSent+1
     else
    echo "The number $theNumber is incorrectly formatted."
    countFailed=$countFailed+1
     fi
    shift
done

# Give feedback
if [ $countSent -gt 1 ]
then
    goodMsg="$countSent messages sent"
else
    if [ $countSent = 0 ]
then
    goodMsg="No messages sent"
    else
    goodMsg="1 message sent"
    fi
fi

if [ $countFailed = 1 ]
then
    baddMsg="1 message failed"
else
    badMsg="$countSent messages failed"
fi


if [ $countFailed -ne 0 ]
then
    if [ $countSent -ne 0 ]
then
    echo "$goodMsg. $badMsg."
    exit 0
    else
    echo "$badMsg"
    exit 0
    fi
else
    echo "$goodMsg."
    exit 1
fi

Please don't be nasty - don't use it for spamming!

edit flag offensive delete publish link more

Comments

Thanks a lot for your great work! Would it be possible to use contact names instead of writing exact numbers? The few numbers I remember, I never send sms to them. For instance, using something like qsms "Blah blah blah" "John Doe" /Home? This would imply proper identification of first names, last names, and phone types (home, office, mobile, etc.).

Kabouik ( 2014-01-22 14:23:04 +0300 )edit

Note that I get a "No such file or directory" when trying qsms, either as nemo or as root. ls -l on qsms returns the following: [nemo@localhost ~]$ ls -l /usr/local/bin/qsms -rwxrwxr-x 1 nemo nemo 2294 Jan 22 13:34 /usr/local/bin/qsms

Kabouik ( 2014-01-22 14:41:41 +0300 )edit

Kabouik: I've already started a road map on this which will include using contact names and nicknames, logging the sms in the sms database and other excitements.

Don't know why you are getting a now such file or directory as the script doesn't do anything with files or directories at all. I suspect that the script itself isn't in the right place or isn't marked as executable. Try running which qsms

and you should get something like:

/usr/local/bin/qsms

If you don't then it's not in the right place (ie your shell won't find it) or it's not marked as an executable (devel-su; chmod +x /usr/local/bin/qsms)

rolandw ( 2014-01-22 14:55:04 +0300 )edit

Great news, thanks a lot.

As for the error, I've double checked and triple checked after your reply. "which qsms" does return the qsms file in /usr/local/bin, and it is already executable as I had already run "chmod +x" on it. I did it again to be sure, but still the same. "ls -l" confirms that it is executable anyway. Should the file have any extension? Or should I install a package to run it?

Kabouik ( 2014-01-22 15:00:44 +0300 )edit

@Kabouik: Until rolandw implements the contactdb queries: It is also possible to start the "proper" SMS sender app on the phone from command line, with a predefined message text. That would help you so you can write the text via ssh, then just pick the proper contacts on the phone and press send.

I don't have my Jolla with me at the moment, but the above thing is also a dbus-send command, perhaps you'll find it on the interwebs :-) (I'll get back with it later tonight)

@rolandw: "Many thanks to Richard" ?!? :-)

CsTom ( 2014-01-22 15:48:30 +0300 )edit
7

answered 2014-01-23 12:04:23 +0300

marsch gravatar image

updated 2014-01-23 17:49:22 +0300

Please keep in mind that bash scripts are a pain to port. Though sh is usally linked to bash on linux, this is not set in stone. I've wrote this code for a single recipient before the qsms script was posted, so it basically accomplishes the same task not using bash specific stuff.

Using quotes is not necessary for the single-shot approach, as long as the number is a string without spaces. It might however make sense to introduce quoted strings for a more robust multi-shot implementation.

Here you go:

#!/bin/sh

# Nail down path
PATH=/bin:/usr/bin

# Do we have enough parameters?
if [ $# -ge 2 ]; then

  # Check if second param looks like a phone number
  if [ $(echo "$1" | awk '/^\+?[0-9]+$/') ]; then
    # Store phone number
    PHONE=$1
    shift

    # Convert message to UTF-8
    MESSAGE=$(echo $* | iconv -f iso-8859-1 -t utf-8)

    # Try to send the message
    dbus-send --system --dest=org.ofono /ril_0 org.ofono.MessageManager.SendMessage string:"${PHONE}" string:"${MESSAGE}"

    # Check return value
    if [ $? -eq 0 ]; then
      echo "SMS to ${PHONE} successfully sent."
      exit 0
    else
      echo "Sending SMS to ${PHONE} failed."
      exit 1
    fi
  fi
fi

# Print usage
echo "Usage: $0 [+]NUMBER MESSAGE"
exit 1
edit flag offensive delete publish link more

Comments

Doesn't work for me, I get the "message sent" confirmation but my recipient does not actually receive the message. Perhaps I did something wrong. What I did is make a "termsms" file with your script, moved it to /usr/bin, made it executable, and run "termsms +33123456789 "Hello world."

Note that if I use "!" inside my message, then the "termsms" command messes up for some reason and adds several command lines I previously typed in the SSH terminal into the content of the message to be sent.

Kabouik ( 2014-01-24 11:27:33 +0300 )edit
1

I already noticed that dbus-send returns with 0 even when there's no SIM in the phone. There has to be an additional check. It would also return true if the phone number doesn't exist.

marsch ( 2014-01-24 11:36:42 +0300 )edit

If unmasked, ! expands (it's a shell builtin), showing commands you've been issuing before. Numbers following the ! are interpreted chronologically backwards, letters show the last command beginning with them. This should only be true though if a number/letter directly follows the exclamation mark.

marsch ( 2014-01-24 11:44:24 +0300 )edit

If you want to use special characters, mask them with a backslash and use no ticks at all. Alternatively don't mask anything and use single ticks. Stuff in double ticks gets expanded.

marsch ( 2014-01-24 11:50:45 +0300 )edit

Got no possibility to test now, but try to change the dbus-send line to

dbus-send --system --print-reply --dest=org.ofono /ril_0 org.ofono.MessageManager.SendMessage string:"${PHONE}" string:"${MESSAGE}" >/dev/null 2>&1

I'd call it side-effect error handling, but it might give you a clue.

marsch ( 2014-01-24 12:01:38 +0300 )edit
2

answered 2014-09-24 20:27:04 +0300

baptx gravatar image

updated 2014-09-25 21:20:39 +0300

Thanks @CsTom, so you can send SMS using command line with dbus-send and you can read SMS with commhistory-tool (with list -group "group_number" option). Here's a minimal script for saving SMS sent via command line so they can appear in the Messages application (you can call the script sms_send.sh). It takes 3 arguments: message, phone number and the conversation group number you want from commhistory-tool listgroups.

if [ $# -ne 3 ]; then
    echo "Usage: $0 message phone_number conversation_group (see commhistory-tool)"
    exit
fi
commhistory-tool add -group "$3" -text "$1" -out -sms "" "$2"
dbus-send --system --print-reply --dest=org.ofono /ril_0 org.ofono.MessageManager.SendMessage string:"$2" string:"$1"

By the way, if someone knows how to receive SMS delivery reports (with dbus-send maybe?), it would be great.

edit flag offensive delete publish link more
0

answered 2019-07-29 16:38:07 +0300

sedric gravatar image

Hello,

Since 3.1.0 this does not work, raising a org.ofono.Error.AccessDenied (Probably due to access control in ofono ?)

Does anyone know how the access are given ? I tried to add a policy for nemo in /etc/dbus-1/system.d/ofono.conf (by copying the radio rights) but it doesnt seems to work...

edit flag offensive delete publish link more

Comments

Good catch... I'm curious to know what's wrong now.

ced117 ( 2019-07-29 22:09:13 +0300 )edit
1

Found the reason : the user should be in the sailfish-radio group.

sedric ( 2019-11-18 14:44:23 +0300 )edit
Login/Signup to Answer

Question tools

Follow
29 followers

Stats

Asked: 2014-01-21 16:23:15 +0300

Seen: 8,779 times

Last updated: Jul 29 '19