0

I have a Python script that leverages lists in a Google Sheet and sends bulk SMS text messages using Twilio.

I'm fairly new to this and have struggled to get this far - any Python script I've created in the past, I've been able to just run off my local computer in VS Code.

I am trying to share this with a family member - I've read into tkinter and gui's a bit, but because the rest of this workflow is already in a Google Sheet, it would be perfect if any user could just run the Python script right from the spreadsheet itself.

I found this, but I don't really understand how to create a webservice in GAE. I've googled it all over but struggling to put into action -- Trigger python code from Google spreadsheets?

Is there a simple way to just tie my Python script into this spreadsheet so that anyone can run it? Or another way to go about this?

ChatGPT response says this, but I feel it is inaccurate (or I just can't get it working):

ChatGPT Response

My code is here, if it helps:

from twilio.rest import Client
import gspread

# Your Account SID and Auth Token from twilio.com/console
account_sid = 'AC868ea4e1a779ff0816b466a13f201b02'
auth_token = '85469ae1eb492ffc814c095b5c6e0889'
client = Client(account_sid, auth_token)


gc = gspread.service_account(filename='creds.json')

# Open a spreadsheet by ID
sh = gc.open_by_key('1KRYITQ_O_-7exPZp8zj1VvAUPPutqtO4SrTgloCx8x4')

# Get the sheets
wk = sh.worksheet("Numbers to Send")

# E.G. the URLs are listed on Sheet 1 on Column A
numbers = wk.batch_get(('f3:f',))[0]
names = wk.batch_get(('g3:g',))[0]

# names = ['John', 'Jane', 'Jim']
# numbers = ['+16099725052', '+16099725052', '+16099725052']

# Loop through the names and numbers and send a text message to each phone number
for i in range(len(names)):

    message = client.messages.create(
        to=numbers[i],
        from_='+18442251378',
        body=f"Hello {names[i][0]}, this is a test message from Twilio.")
    print(f"Message sent to {names[i]} at {numbers[i]}")
5
  • I have to apologize for my poor English skill. About How to run a Python Script from a Google Sheet, when I saw your question, I cannot understand when you want to execute the python script from Google Spreadsheet. Can I ask you about the detailed situation for executing your python script from Google Spreadsheet? And, can I ask you about the detailed reason that you are required to use python script? Commented Feb 9, 2023 at 6:32
  • Thanks @Tanaike - ideally, I could have a button on my Google Sheet that triggers the Python script, similar to as if I were triggering an Apps Script file. I'm not required to use Python at all, it just took me a long time to figure out how to get my Python script working (above).. didn't want to try to recreate it if I didn't have to. Doing the same function in Apps Script would work - I'll start looking into how I might be able to do that. I was hoping I could just share this script with someone else so they can run it via Google Sheets Commented Feb 9, 2023 at 13:45
  • Thank you for replying. If you want to run a script by some actions in Google Spreadsheet, I thought that Google Apps Script is suitable. But from I was hoping I could just share this script with someone else so they can run it via Google Sheets, if you want to run a script except for Google Apps Script by some actions in Google Spreadsheet, I thought that the HTTP request might be required to be run by Google Apps Script. If I misunderstood your expected result, I apologize. Commented Feb 10, 2023 at 1:40
  • @Tanaike this is very helpful - I'm not entirely understanding how I could set this up though? I essentially have a Python script on my local computer that I want to make available to any Google Sheets user (they can run it there). Does that make sense at all? Sorry for the confusion Commented Feb 10, 2023 at 2:21
  • Thank you for replying. If you want to run the script in your local PC from Google Apps Script, I think that it is required to prepare a server in your local PC and it is required to be able to be accessed from the HTTP request by Google Apps Script. I think that in this case, it might be a bit complicated. If you can use Google Apps Script instead of another language, I would like to recommend using it. In that case, other users can also use the Google Apps Script. Commented Feb 10, 2023 at 2:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.