Skip to content

Instantly share code, notes, and snippets.

@mobileappconsultant
Last active December 3, 2018 14:13
Show Gist options
  • Save mobileappconsultant/df0419dca7e2670a4eb6acf20014735d to your computer and use it in GitHub Desktop.
Save mobileappconsultant/df0419dca7e2670a4eb6acf20014735d to your computer and use it in GitHub Desktop.
A simple python script to send text messages to any valid U.K number
#I want to import a library that manages time
import time
#I want to import a library that can help me define regular expressions
import re
#I want to import a library that tells me about the operating system I'm using and more!
import os
#I want to import a library that creates a SMS Gateway
from sinchsms import SinchSMS
#So I want to enter a uk mobile number but I want to make sure it's in the right format. Regular expression are a way to do this
# Best to read up on but don't wanna detract
regular_expression_matcher="^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$"
#I want to sent a text to this number
number = input("\nPlease enter a phone number here, pls enter the country code:" )
#I want to remove any spaces for instance 000 0000 will not become 000000
number =number.replace(" ", "")
#check the number entered to see if it is a valid uk number if it doesn't match a uk number ask the user to type it again
while(re.match(regular_expression_matcher,number) != True):
os.system('clear')
print("\nYou have entered an invalid UK number, Please try again")
number = input("\nPlease enter a phone number here, pls enter the country code:" )
number =number.replace(" ", "")
#if we get a valid uk number, proceed to the next part of the code
if(re.match(regular_expression_matcher,number)):
break
#I want to send this message to that number
message = input("Enter your message here: ")
#I need to create a SMS gateway variable. It's need my secret key and answer which I created on sinch.com
client = SinchSMS("5af1a15e-d890-4f6f-99d1-397d79873c21", "hUmOGUdmvEK9CYxWuROdSw==")
#Write to the console that the task of sending the text message has begun
print("Sending '%s' to %s" % (message, number))
#When the message has been sent, I want to get the status of the task of sending the text, did it fail or pass
response = client.send_message(number, message)
#The response variable returns a dictionary. I want to get the part of the dictionary that holds the reference number of my message
message_id = response['messageId']
# I want to check the status if it's successful
response = client.check_status(message_id)
# I want to continuously print the status of the message sent till i get status == "Successful"
while response['status'] != 'Successful':
print(response['status'])
# When the app prints a status, it should rest for 2 seconds and continue printing the status.
time.sleep(2)
response = client.check_status(message_id)
print(response['status'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment