|
| 1 | +# SMS CHATBOT |
| 2 | + |
| 3 | + |
| 4 | +[ |
| 5 | + |
| 6 | +## PREREQUISITIES |
| 7 | +* A Twilio account - sign up for a free one here - https://www.twilio.com/try-twilio |
| 8 | +* A Twilio phone number with SMS capabilities - learn how to buy a Twilio Phone Number here - https://support.twilio.com/hc/en-us/articles/223135247-How-to-Search-for-and-Buy-a-Twilio-Phone-Number-from-Console |
| 9 | +* OpenAI Account – make an OpenAI Account here - https://openai.com/api/ |
| 10 | +* Python installed - download Python here - https://www.python.org/downloads/ |
| 11 | +* ngrok - https://ngrok.com/download , a handy utility to connect the development version of our Python application running on your machine to a public URL that Twilio can access. This is needed for the development version of the application because your computer is likely behind a router or firewall, so it isn’t directly reachable on the Internet. |
| 12 | + |
| 13 | +## GETTING STARTED |
| 14 | + |
| 15 | +Since you will be installing some Python packages for this project, you will need to make a new project directory and a virtual environment. |
| 16 | + |
| 17 | +If you're using a Unix or macOS system, open a terminal and enter the following commands: |
| 18 | + |
| 19 | +```bash |
| 20 | +mkdir chatgpt-sms-python |
| 21 | +cd chatgpt-sms-python |
| 22 | +python3 -m venv venv |
| 23 | +source venv/bin/activate |
| 24 | +pip install openai twilio flask python-dotenv |
| 25 | +``` |
| 26 | + |
| 27 | +If you're following this tutorial on Windows, enter the following commands in a command prompt window: |
| 28 | + |
| 29 | +```bash |
| 30 | +mkdir chatgpt-sms-python |
| 31 | +cd chatgpt-sms-python |
| 32 | +python -m venv venv |
| 33 | +venv\Scripts\activate |
| 34 | +``` |
| 35 | + |
| 36 | + Clone this repository to your local machine. |
| 37 | + |
| 38 | + ```bash |
| 39 | + git clone https://github.com/<your-username>/python-beginner-projects.git |
| 40 | + ``` |
| 41 | + |
| 42 | +Navigate to the project directory. |
| 43 | + |
| 44 | + ```bash |
| 45 | + cd projects/SMS_chatBot/ |
| 46 | + ``` |
| 47 | + |
| 48 | +Open your terminal there and use this command. |
| 49 | + |
| 50 | +```bash |
| 51 | +pip install openai twilio flask python-dotenv |
| 52 | +``` |
| 53 | + |
| 54 | +The last command uses ```pip```, the Python package installer, to install the four packages that you are going to use in this project, which are: |
| 55 | +* The OpenAI Python client library, to send requests to OpenAI's GPT-3 engine. |
| 56 | +* The Twilio Python Helper library, to work with SMS messages. |
| 57 | +* The Flask framework, to create the web application in Python. |
| 58 | +* The python-dotenv package, to read a configuration file. |
| 59 | + |
| 60 | +As mentioned above, this project needs an OpenAI API Key. After making an OpenAI account, you can get an OpenAI API Key here by clicking on ``` + Create new secret key.``` |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +The Python application will need to have access to this key, so we are going to make a .env file where the API key can safely be stored in the same folder SMS_ChatBot. The application will be able to import this key. |
| 66 | +: |
| 67 | + |
| 68 | +>OPENAI_API_KEY= <YOUR-OPENAI-KEY> |
| 69 | +
|
| 70 | +Make sure that the OPENAI_API_KEY is safe and that you don't expose your .env file in a public location such as GitHub. |
| 71 | + |
| 72 | +Now, your Flask app will need to be visible from the web so Twilio can send requests to it. ngrok lets you do this. With ngrok installed, run ```ngrok http 5000``` in a new terminal tab in the directory your code is in. |
| 73 | + |
| 74 | +You should see the screen above. Grab that ngrok Forwarding URL to configure your Twilio number: select your Twilio number under Active Numbers in your Twilio console, scroll to the Messaging section, and then modify the phone number’s routing by pasting the ngrok URL in the textbox corresponding to when A Message Comes In as shown below: |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +Click Save and now your Twilio Phone Number is configured so that it maps to your web application server running locally on your computer. |
| 79 | + |
| 80 | + |
| 81 | +After completing the above process,we are ready to launch our script. |
| 82 | + |
| 83 | +```bash |
| 84 | +python main.py |
| 85 | +``` |
| 86 | + |
| 87 | +if you are using python 3.X then run this command. |
| 88 | + |
| 89 | +```bash |
| 90 | +python3 main.py |
| 91 | +``` |
| 92 | + |
| 93 | +Now take out your phone and text your Twilio Phone Number a question or prompt so that OpenAI can answer it or generate text! |
| 94 | + |
| 95 | + |
0 commit comments