Sending an SMS to a Cellphone using Django

46

Solution 1

Hi my name is Jarod and I work for Twilio.com so I am a little biased. But with that said, it is super easy to send an SMS from your Python web application using the Twilio Rest Api. Here is a simple example:

# Download the Python helper library from twilio.com/docs/python/install 
from twilio.rest import TwilioRestClient

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "{{ account_sid }}"
auth_token  = "{{ auth_token }}"
client = TwilioRestClient(account_sid, auth_token)

message = client.messages.create(
    body="Jenny please?! I love you <3",
    to="+15558675309",
    from_="+14158141829",
    media_url="http://www.example.com/hearts.png")
print message.sid

Solution 2

From a technical standpoint, the easiest way to accomplish SMS sending with any web-app is through e-mails. Most cell providers usually give out email accounts to their users, and sending a mail to said account will more likely than not redirect the mail to their cell via SMS. However, not all carriers do this and some charge extra for this type of service. In this case, you could handle this checking out the following Django documentation page

However, as mentioned, this isn't a really complete solution, so the easiest way would be to use a SMS-gateway. Mostly, they provide simple REST based API's for sending text messages to cell phones. The API would vary obviously from carrier to carrier. I would recommend checking out Kannel in case you're looking for a free and open source solution (that is assuming you want to install the actual gateway on your server).

Anyway, I would start out trying to get it to work with the e-mail scenario, and then moving on to using a carrier if you actually require it. Hopefully this helps somewhat.

Solution 3

I answered a similar question, a bit late to the game, in another post. Here it is for additional information. Hope it helps:

I was struggling with this for some time and really liked the Twilio option. But then I dug deeper and found that there is a Google Voice API called pygooglevoice that works. Clean, easy... No carrier lookup... For example, set up a virtualenv and install with pip:

pip install pygooglevoice

Then use something like this:

from googlevoice import Voice
from googlevoice.util import input

def send(number, message):
    user = '[email protected]'
    password = 'password'

    voice = Voice()
    voice.login(user, password)

    #number = input('Number to send message to: ') # use these for command method
    #message = input('Message text: ')

    voice.send_sms(number, message)

Please note that I have done limited testing with this so I'm not sure all the pros and cons. It's quite possible that there are limitations I haven't discovered yet. But in the time I've played around with it, I've been happy.

Solution 4

take a look at django-sms

Share:
46
Gusto
Author by

Gusto

Updated on July 09, 2022

Comments

  • Gusto
    Gusto almost 2 years

    What am I doing wrong here: Trying to access a defined method but getting an error saying: "There is no argument given that corresponds to the required formal parameter 'bag' of 'Program.PutInTheBag(ref List)' ... "

    My method ( PutInTheBag() ) doesn't require any argument, how can I told my program to ignore that? If that is not possible, how do I access my bag list from all my methods (including Main() as well)? P.S. Plz don't judge too hard, I'm just a beginner...

     class Program
    {
        List<string> bag = new List<String>();
        static void Main(string[] args)
        {
            Console.WriteLine("Välkommen till ryggsäcken!");
    
            while (true)
            {
                MainMenu();
                int userChoice;
                int.TryParse(Console.ReadLine(), out userChoice);
    
                if (userChoice == 1)                
                    PutInTheBag();                                   
    
                else if 
                // some code
    
                else
                {
                    //some code
                }
            }
    
        }        
    
        private static void PutInTheBag(ref List<string> bag)
        {
            Console.Write("Lägg till ett föremål: \t");
            bag.Add(Console.ReadLine());
            Console.Clear();
            Console.WriteLine("Innehållet sparat! \nTryck en valfri knapp för att komma till huvudmenu");
            Console.ReadKey();
            Console.Clear();
        }
    
    }
    
    • BradleyDotNET
      BradleyDotNET about 7 years
      but it does require an argument... so either make it not do that or pass it an argument
  • Nanda Kishore
    Nanda Kishore over 15 years
    I tried this, but I couldn't post messages back from my mobile to my server.
  • Nanda Kishore
    Nanda Kishore over 15 years
    Thankyou Dave, Let me have a try with Kannel
  • nicorellius
    nicorellius over 10 years
    this service is pretty cool. i just signed up and played around with it... but it costs money, eh? $1 a month and per message fees?
  • nicorellius
    nicorellius over 10 years
    i think my main concern with this kind of app is the carrier info. if you only know the number, can you find out the carrier code email to actually send smtp messages? i'm reading it now, so if this is answered, i'm sure i'll find it. thanks.
  • teewuane
    teewuane over 10 years
    before I sign up, do you have any stackoverflow special promo codes? :) :)
  • Saher Ahwal
    Saher Ahwal over 9 years
    Is this free? what's the catch
  • Gusto
    Gusto about 7 years
    This is exactly what I wanted! Thanks a lot!
  • Olfredos6
    Olfredos6 about 5 years
    I have been trying to use pygooglevoice but keeps getting errors no matter which version of the module or python's. can you please share your environment settings or a requirements.txt file if using a virtual env?
  • Olfredos6
    Olfredos6 about 5 years
    Looks simple to use but doesn't cover many countries. Twilio has the widest coverage so far
  • nicorellius
    nicorellius about 5 years
    @Olfredos6 You should be able to test this with a module similar to the one I added above. If you install pygooglevoice from PyPi, you should get its dependencies, which are the important ones here.. You could also try googlevoice which is based on the former.
  • Olfredos6
    Olfredos6 almost 5 years
    unfortunately, Google's Voice is only available in the U.S
  • Olfredos6
    Olfredos6 almost 5 years
    and Voice for work is only available in a few countries. support.google.com/voice/answer/9310722
  • cảnh nguyễn
    cảnh nguyễn almost 4 years
    I am also trying to use twillo but I have to wait a long time for twillo to confirm my phone number, and currently, the twillo has not sent me a confirmation message. Very bad library
  • Shmack
    Shmack over 2 years
    When I saw the body of the message, the first thing I checked was the phone number... I was born in '98... One day no one will know this reference.