This is an example project showing how to receive SMS messages, look up the stock price based on the body of the message and reply using the Twilio Programmable Messaging API, the Finnhub API and TypeScript.
- Node.js
- A Twilio account (if you don't have one yet, sign up for a free Twilio account here and receive $10 credit when you upgrade)
- A Twilio phone number that can receive SMS messages
- ngrok so that you can respond to webhooks in your local development environment
- A free Finnhub account
Clone the repo and change into the stonks-bot
directory:
git clone https://github.com/philnash/twilio-typescript-examples.git
cd twilio-typescript-examples/stonks-bot
Install the dependencies:
npm install
Copy the .env.example
file to .env
:
cp .env.example .env
Fill in your Finnhub API key.
Compile the project:
npm run build
Note that this runs the TypeScript compiler, tsc
, you could also run npx tsc
to get the same output.
The TypeScript project will be compiled into the dist
directory.
You can also continuously compile the project as it changes with
npm run watch
Start the web server with:
npm start
To respond to an incoming webhook you will need a publicly available URL. ngrok is a tool that can tunnel through from a public URL to your machine. Once you've downloaded and installed ngrok you can run it like so:
ngrok http 3000
The ngrok terminal will show you a URL, like https://RANDOM_STRING.ngrok.io
.
Using the ngrok URL from the last part, you can set up your Twilio phone number with your application. Edit your phone number and in the Messaging section, next to when "A message comes in" enter your ngrok URL with the path /messages
.
https://RANDOM_STRING.ngrok.io/messages
Save the phone number and you are ready.
Send your number a message with a stock symbol and you should receive a message in reply 🎉