Jump to content

Newbie programmer - Just wrote first 'real' program

steelo

So, I watched a youtube video where a person used a raspsberry pi to send a text message to themselves. I thought, wouldnt it be cool to write something that texts me on the day of an appointment. So, I created a free twilio account and followed the script to text my phone...that was the easy part. So, I had to figure out how to input an appointment, but also send a text. I figured the easiest way to do this would be to have 2 separate applications, one to enter an appointment and another to automatically send the reminder if todays date==the date of the appointment. I started with the data entry program, which was more challenging than I imagined. I had to read up on how to write to a text file while creating a new line for every entry. After that was completed, I started on the message sending application. I'm embarassed to admit this but I could not for the life of me figure out why when I compared todays date to the date pulled from the text file, python did not see them as equal...how the heck does 5/29/20 not equal 5/29/20???? I tried converting the 2 variables to a string, different formats, etc with zero luck. After quite a bit of frustration, I determined that the text file had a space before and after the date, making the 2 numbers not match (in the eyes of python) So, I ran a strip() function and voila, it sent the message when it determined that the date matched todays date! Now the cool part (at least to me) - I created a cron job to run the texting program every day at 8am.

 

Most of you guys will probably laugh, but this is the first python program I've written that I can actually use! In the past, I kind of lost interest with rpis and python because I couldnt think of how to create something that is useful.

 

I can post the code, if anyone is interested...Im sure it looks very amateurish, but may be useful for someone like myself who is interested in stepping into programming. 🙂

Link to comment
Share on other sites

Link to post
Share on other sites

Nice! You shouldn't be so embarrassed about struggling with the date, time and dates are just notoriously hard. It doesn't matter the language there are so many standards and stupid exceptions because of things like leap years and time zones that just make it and absolute hassle to handle any kind of date.

Also, a lot of people don't know this, but you might actually be able to send a text to your phone via e-mail, depending on your cellular service provider. That might save ya some money!

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, WatermelonLesson said:

Nice! You shouldn't be so embarrassed about struggling with the date, time and dates are just notoriously hard. It doesn't matter the language there are so many standards and stupid exceptions because of things like leap years and time zones that just make it and absolute hassle to handle any kind of date.

Also, a lot of people don't know this, but you might actually be able to send a text to your phone via e-mail, depending on your cellular service provider. That might save ya some money!

Dates suck, i fine it easier to turn dates into epoch and just use them as numbers. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, vorticalbox said:

Dates suck, i fine it easier to turn dates into epoch and just use them as numbers. 

Yeah honestly, the epoch is the best way but it's still absurd how much variation there is in how to represent the epoch.

Link to comment
Share on other sites

Link to post
Share on other sites

39 minutes ago, WatermelonLesson said:

Yeah honestly, the epoch is the best way but it's still absurd how much variation there is in how to represent the epoch.

I think I might make a date npm node package that does things to dates with numbers, at work we use moment and that thing is massive for what it does. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, WatermelonLesson said:

Nice! You shouldn't be so embarrassed about struggling with the date, time and dates are just notoriously hard. It doesn't matter the language there are so many standards and stupid exceptions because of things like leap years and time zones that just make it and absolute hassle to handle any kind of date.

Also, a lot of people don't know this, but you might actually be able to send a text to your phone via e-mail, depending on your cellular service provider. That might save ya some money!

Thanks! I am a sql novice too at work and dates are a HUGE PITA because they are in a really specific format in our tables which have to first be converted in order to do any calculations...its not fun for a newbie

 

As far as texts...I'm using their free version...I think I get so many texts

Link to comment
Share on other sites

Link to post
Share on other sites

from datetime import date
from twilio.rest import Client



class Appointment:
  def __init__(self, date, reminder):
    self.date = date
    self.reminder = reminder

  def SendText(date,message):
        account_sid ="" # Put your Twilio account SID here
        auth_token ="" # Put your auth token here

        client = Client(account_sid, auth_token)

        print("Message sending...")
        message = client.api.account.messages.create(
            to="", # Put your cellphone number here
            from_="", # Put your Twilio number here
            body=message) #message=contents of txt file

        print("Message sent...")

  def ReadFile():
        with open('/home/pi/Desktop/Python/sms.txt') as f:


            while True: #loop until eof
                day=f.readline() #assigns date to date variable
                message=f.readline() #assigns message to message variable
                if not day:
                    break #if no date, break from loop eof


                #print(message)
                present=(date.today().strftime('%m/%d/%y'))


                if day.strip()==present.strip(): #eliminates spaces in order to compare date with todaysdate
                    Appointment.SendText(date,message)



#Appointment.SendText(date,message)
Appointment.ReadFile()
print("DONE")

Here is the code for the application which sends the text - I created a cron job to run this code every morning and send reminders if an appointment falls on today's date. btw, I removed my token key and phone numbers 😃

Link to comment
Share on other sites

Link to post
Share on other sites

from datetime import date
import os




def writefile(date,message):
    with open('/home/pi/Desktop/Python/sms.txt', 'a')as f:


        f.write (date + '\n')
        f.write(message + '\n')


userchoice=0
while userchoice!='3':
    userchoice=input('Please select an option - 1)New appointment, 2)Purge File, 3) Exit: ')
    if userchoice=='1':
        message=input('What is the message you wish to text: ')
        date = input("Enter date in MM/DD/YY format: ")
        writefile(date,message)
        print("Saved")
    elif userchoice=='2':
        verify=input("Are you sure you wish to purge the records? Y/N")
        if verify.upper()=='Y':
            os.remove("/home/pi/Desktop/Python/sms.txt")
            print("/home/pi/Desktop/Python/sms.txt purged")
    else:
        print("Have a great day! =D")

Here is the code for the application to set the appointment. Looking at this now, I need to put some error checking code to ensure the date is in the proper format...maybe verify the len of the date is 8 digits? 😃

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×