Forums

Class / instance over time

I appreciate the support here, first! :-D

Alright so I got my texting exchange working--I'm working on building a basic from-scratch chat bot to handle responses to our outbound marketing campaigns. But this thing will need to handle multiple conversations at once. Before I get hip-deep in coding something that doesn't work, I wanted to run it by you guys.

My first thought is to build the "bot" (really just a set of if-then rules) as a class, and then have each conversation exist as an instance. So when a text comes in, step 1 is to scan through the list of existing instances--if there's already a conversation, route the message into that instance. If not, create a new instance identified as the incoming phone number. After two hours without response, the instance times out and another incoming text starts the conversation over.

I'm kind of a noob and not terribly familiar with classes. My questions:

  1. Will the instances persist over time within the existing code? Or will I need to create / write to a file to store them over that amount of time?

  2. Is there a better way to handle this?

You might ask why not just use an existing free chat service like Snatchbot. 1) I don't have as much freedom there as in free-styling some python, 2) I would still need software somewhere to make the end-user experience an effortless text conversation, and 3) this is an excellent opportunity to build my coding skills.

Thanks in advance for the pointers!!

what web framework are you using? if you are using say django and created models and store the info into the database, then those would persist.

how much time / effort do you intend to put into this?

how much coding experience do you have?

(what you are describing sounds like it may be fairly complicated, and if your rules are not written well then it may not be up to the standards required for responding to a marketing campaign)

i would almost suggest taking care of anything > 2 hrs by actual humans responding over emails etc

I'm using Flask, as I'm brand-new to the webapp game.

I need to get something functional in the next month, daily work will range from 1 to 3+ hours throughout the day and after shift.

Actual python coding is limited to 4/5 segments on a Udacity course and what I've done here. However, I've been writing complex spreadsheets in Excel for roughly a decade and in Google Sheets for the last couple of years. I'm familiar with concepts of planning, structuring, and turning a big hairy mass of data into reasonable and user-friendly output. I briefly considered just using Google Sheets and their Apps Script as the basis for this, but I wanted to expand my horizons a bit.

I've done a little research into it, and it's looking like flask + python may not be able to dynamically create complex instances and hold them in memory for hours at a time. Alas...beautiful but impractical.

Next best thing--tell me if you think this could work. Today I'm going to research on it.

  • Read/write two CSV files. One with a log of all incoming messages, and one with a log of all outgoing messages. The outgoing message log includes an indicator of the last function called for that specific number.

  • When an incoming text is received, the outgoing message log is searched from the bottom up (to save processing time) for the most recent outgoing message to that number. The "last function called" indicator will determine where (which function) the incoming text is routed for parsing. I'm certain there's a pythonic equivalent for VLOOKUP.

  • Different functions will be written for every point in the conversation to march it down the line. At the beginning of each function will be sub-functions that scrub for "global" responses--call me, send me an email, I want a refund, etc. before that point in the conversation is evaluated.

Do you think that will work? Is it feasible?

It's feasible, but it's way more trouble than you need. All of that would be much more easily implemented in a database. Unless you pay close attention and do tricks with the file pointer, your "search file backwards" is likely to end up being "load entire file, reverse it, search backwards" which would be pretty slow and error prone.

Excellent. Again, thank you guys so much for your help.

1) Can I implement the database solution with flask/python, or would I need to switch to Django? I'm kind of reluctant to do because I'm only barely familiar with flask.

2) If I can do it without Django, is pandas the key to that, and how much of a crash course are we thinking?

3) Is there a file-writing trick to log backwards with newest entries at the top of the file?

--EDIT-- Another thought! I could also use an active-conversations CSV file. When I send an outgoing message, the program tries to find the phone number and overwrites the existing entry with a new message-index., It appends the file if the entry isn't found. Then, once an hour a function goes in and erases entries with a timestamp more than two hours old. Now I'm searching through a much smaller list where each number only exists once, with only the relevant info necessary to move the conversation forward. I would still keep the incoming/outgoing logs, maybe in one file together, for searching and troubleshooting and such, but I wouldn't need to search that log using scripts.

Better?

  1. Sure you can use flask. We have a tutorial for that here: https://blog.pythonanywhere.com/121/

  2. I don't see anything in your descriptions that would require pandas.

  3. Maybe. You'd have to check the Python file io libraries to see if there's something you can use for that. Be sure to pay attention to any warnings because I suspect that writing to the start of a file may involve having to write the whole file out each time you add to it.

You could do something like that, but it really feels like you're trying to write a slow, buggy database in text files instead of just using a database.

lol that's why I keep running things by you guys, I don't want to do a slow buggy anything.

I am just thoroughly unfamiliar with "database" outside of Excel-esque spreadsheets, or how to keep a persistent file outside of writing one. I will research in that direction!

It's also good to know pandas isn't likely needed. I do want to learn it, but I want to nail this project first.

Thanks again! I'll keep you guys posted as I figure it out and hit more roadblocks. :-D