Building a QR Code Loyalty Card with Aiflo
Let's imagine you own a coffee shop and want to reward your loyal customers. How about a digital loyalty card that uses QR codes? Each time a customer buys a coffee, you scan their unique QR code, and the system automatically tracks their purchases and rewards them with a free coffee after every fifth purchase.
Sounds cool, right? Let's build this using Aiflo!
1. The Goal: A Unique QR Code
We want to generate a unique QR code for each customer. This QR code should contain:
- A unique ID to identify the customer.
- A digital signature to ensure the code was issued by your shop.
- The date and time the card was created.
2. Building the Aiflo Program
We'll use several Aiflo nodes to achieve this:
- uuid: Generates a unique ID for each customer.
- signature: Adds a digital signature to the data for security.
- datetime.now: Records the creation time of the loyalty card.
- object: Combines the ID, signature, and creation time into a JSON object.
- text: Converts the JSON object into a text string that can be encoded in the QR code.
- qr: Generates the QR code image.
- response: Sends the QR code image to the user.
3. Connecting the Nodes
Now, let's connect these nodes to create the workflow:
- Generate a unique ID using the uuid node.
- Sign the ID using the signature node to ensure authenticity.
- Get the current date and time using the datetime.now node.
- Combine the ID, signature, and creation time into a JSON object using the object node.
- Convert the JSON object to a text string using the text node.
- Generate the QR code image from the text string using the qr node.
- Send the QR code image as a response to the user.
4. The Complete Aiflo Code
Here's what the final Aiflo program looks like:
[ { "type":"datetime.now", "then":[{"target":"ticket", "name":"created"}] }, { "type":"uuid", "then": [{"target":"sign_ticket"}, {"target":"ticket", "name":"id"}] }, { "name":"sign_ticket", "type":"signature", "then":[{"target":"ticket", "name":"signature"}]}, { "name":"ticket", "type":"object", "then":[{"target":"ticket_json"}]}, { "name":"ticket_json", "type":"text", "then":[{"target":"qr_maker"}] }, { "name":"qr_maker","type":"qr", "then":[{"target":"response"}] }, { "name":"response", "type":"response" }, { "name":"Content-Type", "type":"response", "value":"image/png" } ]
5. Running the Program
You can run this Aiflo program using the Aiflo CLI or integrate it into a web application. Each time it runs, it will generate a new unique QR code that can be printed on a card for a customer.
This example demonstrates how Aiflo can be used to create practical, real-world applications with just a few lines of easy-to-understand JSON code.