Skip to content

Instantly share code, notes, and snippets.

@Eclairemoy
Created August 25, 2020 21:16
Show Gist options
  • Save Eclairemoy/1e2e0324b2fe8c78180fa354765bf10d to your computer and use it in GitHub Desktop.
Save Eclairemoy/1e2e0324b2fe8c78180fa354765bf10d to your computer and use it in GitHub Desktop.
calculate nps
from twilio.rest import Client
import os
def calc_nps(scores):
promoters = 0
neutral = 0
detractors = 0
for score in scores:
if score >= 9:
promoters += 1
elif score >= 7:
neutral += 1
else:
detractors += 1
print("Promoters (9,10):", promoters)
print("Neutral (7,8): ", neutral)
print("Detractors (<=6):", detractors)
nps = (promoters - detractors) / len(scores) * 100
return(nps)
scores = []
feedback = []
client = Client(os.environ.get("ACCOUNT_SID"), os.environ.get("AUTH_TOKEN"))
messages = client.messages.list(to="(309) 220-2172")
print(messages)
twilions = ["+13129526796", "+14844598212"]
for msg in messages:
if msg.from_ not in twilions:
try:
if int(msg.body) in (0,1,2,3,4,5,6,7,8,9,10):
scores.append(int(msg.body))
elif "10. More interesting than I expected." in msg.body:
scores.append(10)
elif "8.5" in msg.body:
scores.append(9)
except:
feedback.append(msg.body)
print("Ratings ", scores)
print("Num Ratings: ", len(scores))
nps = calc_nps(scores)
print("NPS:", nps)
for s in feedback:
print(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment