MLB Daily Game Notification Service
I wanted to get notified when my 2 favorite MLB teams,
Diamondbacks and Braves, play each day.
I tried doing it through my AI assistance but the applications I found
could only tell me about the games once I asked. I wanted to be notified via email and/or
text messages. I wanted to provide this service for friends and family as well.
An additional requirement is that any service I use must be
free. I don’t want to have a monthly fee
to use this service.
Process
I created a data file that contains the user, phone number, and email address plus the team the user wants to be notified about. If they
want email notifications, they provide an email address. If they want a text message, they provide a
phone number.
File format:
ID, User Name, email address, phone number, team abbreviation,
time offset from est.
Steps
2. Get the team information from the API
3. For each user’s team
a. Get the daily game for today
b. Build the data needed for the notification templates.
i. Game ID
ii. Home Team
iii. Away Team
iv. Game Time
v. Team logos
c. If the user has an email address
i. Read in the tokenized HTML template.
ii. Replace the tokenizes with the game data.
iii. Send the email through the SMTP service
d. If the user has a phone number
i. Build the message string with game information.
ii. Send the text message through the SMTP service.
That’s it.
Here is the text message:
Here is the Email:
MLB API Service
I did a Google search for free MLB API services and landed
on API Service. This is an excellent resource for any API service
that you could use. Free and paid.
They have excellent documentation and examples. You can take their JSON results and create a
C# class from it, and it works. You need
to make sure you handle the case sensitivity issues associated with the deserialization
JSON to C# object.
The actual API service I am using is TANK01. They have team details, game details and even
betting odds. Very cool.
SMTP Service
The SMTP service was more challenging. There are some really good ones available like
SendGrid, SMTP.com, MailGun, and Aws SES.
The main issue with these is they are paid services.
I used to use Gmail.
It was easy to integrate with and free.
Google has removed the less secure option and the new more secure way
was more intrusive than I wanted.
I ended up going with STMP2GO. They use a simple REST API to send and get emails. You sign up and get an API key and you are
off and running.
This worked great for the email. It sends HTML emails fine.
Text Message Service
I ran into a roadblock with this service. Most are paid services. I went with just using the SMTP service above
to send text messages. You can send an email
to a phone number if you include the part for the service provided. For ATT it is phone number@txt.att.net.
For me, this worked fine.
The downside is that you can only send text messages, so the team logos
and other page links did not work so I removed them from the message.
HTML Email Template
Building the HTML email template took a little extra care. The trick is using in-line styles and not a CSS
style sheet.
Once I had the template created, I used the format of
%%TokenName%% to place the tokens for the specific email values that would
require to be replaced.
In the code, it was a simple find-and-replace call.
result =
result.Replace("%%AWAYImage%%", game.OpponentTeamLogo);
Deployment
For deployment, it needs to run on a schedule. It needs to run by 9:00 Eastern time to make
sure the notification goes out before the games actually start.
We have several options for a scheduled service:
Each has its own pros and cons. Azure Service and functions could cost
money. Not very, but they are not free.
Since I have an extra computer just sitting around, I am
going to use that. That limits me to the
Windows Service or a Windows Schedule task.
I am going with the Scheduled Tasks.
This allowed me to just build the application as a console app.
I have used these in the past and they work well once set up. The Windows service just seemed
more overhead than I wanted to do.
I will manually run it for the first few days just to test
and verify it is working.
Summary
I found this project fun and useful. The actual coding part
took less than a day. I spent most of
the time looking for an SMTP / Text message service that was free.
The API service look-up site is well worth the time to check out all the different services you can use.
Comments
Post a Comment