Setting Up an AWS Lambda Price Tracker with Teams Webhook Notifications
This guide is an alternative to the AWS Lambda Price Checker with SNS E-mail Notifications. We will continue to price check an item daily but, instead of using SNS to get a mail alert, we will get a Teams notification.
The entire setup will incur no additional cost, it is well within the AWS free tier. Sadly, in order to have access to webhooks, you need Teams above personal, the webhook requiring the ability to create a channel.
Prerequisites
AWS Account with access to AWS Lambda, EventBridge and Microsoft Teams.
The Lambda Function
Below is the Python code for the Lambda function:
import json
import requests
import re
from bs4 import BeautifulSoup
# Function to handle the Lambda event
def lambda_handler(event, context):
# Define the product to monitor
products = [
{
"url": 'https://www.amazon.de/-/en/AMD-Ryzen-7800X3D-Technology-Architecture/dp/B0BTZB7F88/',
"name": "AMD Ryzen 7 7800X3D",
"threshold": 600.0
}
]
messages = []
for product in products:
price = check_price_amazon(product["url"])
if price is not None and price < product["threshold"]:
send_teams_notification(price, product["url"], product["name"])
messages.append(f'Price for {product["name"]} dropped to {price} EUR! Notification sent.')
else:
messages.append(f'Price for {product["name"]} is still above the threshold.')
return {
'statusCode': 200,
'body': json.dumps(' '.join(messages))
}
# Function to check the price on Amazon
def check_price_amazon(url):
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive"
}
try:
response = requests.get(url, headers=HEADERS, timeout=10)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
price_whole = soup.find('span', {'class': 'a-price-whole'})
price_fraction = soup.find('span', {'class': 'a-price-fraction'})
if price_whole and price_fraction:
# Clean the price string by removing non-numeric characters
price_whole_cleaned = re.sub(r'[^\d]', '', price_whole.text)
price_fraction_cleaned = re.sub(r'[^\d]', '', price_fraction.text)
# Combine whole and fractional parts into a valid float
price = float(f"{price_whole_cleaned}.{price_fraction_cleaned}")
return price
elif response.status_code == 503:
print("Server returned 503. Possible bot detection.")
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return None
# Function to send a notification via Teams Webhook
def send_teams_notification(price, product_url, product_name):
teams_webhook_url = 'WEBHOOK_URL'
headers = {"Content-Type": "application/json"}
message = {
"@context": "http://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0076D7",
"title": "Price Alert!",
"text": f"The price for {product_name} has dropped to {price} EUR! Check it out [here]({product_url})."
}
response = requests.post(teams_webhook_url, headers=headers, json=message)
print(f"Message sent to Teams with response code: {response.status_code}")
Configuration
Steps to Configure the Webhook in Teams
Create a Webhook in Teams (all pictures in this step are from Microsoft):
Open Microsoft Teams and go to the desired channel. Click the ellipsis (...) next to the channel name click on Manage channel and then select Connectors.
Search for Incoming Webhook and click Add.
Provide a name for the webhook (e.g., Price Alerts).
Copy the Webhook URL and paste it into the teams_webhook_url variable in the Lambda function.
To deploy the Lambda function, add the layers, create the EventBridge CRON job and test the function, follow the steps described here: AWS Lambda Price Checker with SNS E-mail Notifications.
Results:
WHEN AND IF the price of your item/items drops bellow the set threshold, you will receive a mail like this: