SMS

Sending SMS

We will be sending SMS from the folder projectName, that we created in the Setup and Dependencies Section.

Create a file SMS.py in the folder projectName and copy - paste the code below.

Here are the steps to send an SMS using the python script below.

  1. Import the AfricasTalkingGateway that was installed using pip.
  2. Create an instance of the Gateway class and pass our API credentials to authenticate the API
  3. Call the sendMessage() method provided by the gateway instance, passing in the recipient(s) and the message.

#-> projectName/SMS.py

#import our API wrapper
from africastalking.AfricasTalkingGateway import AfricasTalkingGateway, AfricasTalkingGatewayException

# instantiate the gateway class (optional when using sandbox) add sandbox flag
gateway = AfricasTalkingGateway("some-username", "some-apikey", "sandbox")

# send message using by calling the sendMessage method on our API
gateway.sendMessage('+254701XXXXXX', "Hi, I'm texting")

Fire up the simulator, register the same number as the number in your script ('+254701XXXXXX') and you should see the message appear in the simulator.

Run your code to interact with the simulator:

python SMS.py

results matching ""

    No results matching ""