πŸ•ΉβŒ¨TypeHouse: A web-based typing game made with Python (Flask) and Tuya-API

πŸ•ΉβŒ¨TypeHouse: A web-based typing game made with Python (Flask) and Tuya-API

Introducing TypeHouse πŸ•ΉβŒ¨

TypeHouse is a web-based typing game where users try their best to type as fast as they can to complete given sentences before time runs out.

With the game's introduction out of the way, let's get to its architecture for a bit. 😊

πŸ›  Technologies used

πŸ‘¨πŸΎβ€πŸ’» Using Tuya

Tuya Smart is a global IoT development platform that builds interconnectivity standards to bridge the intelligent needs of brands, OEMs, developers, and retail chains across a broad range of smart devices and industries.

Tuya IoT core service lets you efficiently and securely connect all your devices to the cloud with really nice advantages like secure and reliable transmission, stable performance, and more. Go ahead and check it out here.

Based on the global public cloud, Tuya connects different intelligent scenarios and smart devices by providing hardware development tools, integrating public cloud services, and offering an intelligent business development platform.

We'll get more into using their various API services later in this article!

πŸ’‘ Main Idea

I've always loved the idea of building a full-stack application, and Typehouse is my first personal one. On my journey to build products that solve problems I wanted to lay my hands on a full-stack web application, particularly one that adds a little competition and everyone can enjoy playing.

Here is a 2 minutes demo of using TypeHouse!

πŸŽ¨πŸ›  Design and Development

🎨 Design

Sign up: Sign up page

Login page: Login page

Main page: Main page

Leaderboard: Leaderboard page

Link to app - https://typehouse.herokuapp.com/

πŸ›  Development

As stated earlier, I mainly used Flask, Tuya API for Email service, as well as other tools and technologies.

πŸ’­ Why Tuya?

Tuya provides an easy-to-use, free, and accessible API to anyone anywhere. From Email services to Country codes, SMS, and many more, Tuya API can be used in all that. Tuya's Cloud Development Platform provides API products of basic IoT capabilities like device management, AI scenarios, and data analytics services, as well as industry capabilities, helping you create IoT solutions.

I used their Email service in this project which I can show you how to do in a few simple steps

πŸ‘©πŸΎβ€πŸ’» Using Tuya

Step 1:

After that, you'll head to the dashboard and click on the 'Cloud' link on the menu by the left.

Tuya IoT Platform

Click 'Create Cloud Project' and fill in the required fields to suit your development, as an example to test, we'll be using the values in the picture below to access various services.

Tuya Create Cloud project modal

A new modal will pop up asking you to authorize some APIs, since we would be using the Email Service and Country code APIs as examples, add them to your current project. Once you're done, it should look like this.

Tuya Authorise API modal

Note: Leave all default APIs as they are required for you to use the others.

Step 2: Once you fill in the necessary details for the configuration wizard pop-up window, your API service should look like this.

Cloud project dashboard

Awesome! 😊 Now, we can get to coding.

Open your terminal to where your python project is located and execute the following to install Tuya-SDK & Tuya connector.

pip3 install tuya-iot-py-sdk
pip3 install tuya-connector-python

Next, copy this code and fill in the missing details with your project data.

from tuya_connector import (TuyaOpenAPI)

ACCESS_KEY = 'your-access-secret-key'
ACCESS_ID = 'your-access-id'
API_ENDPOINT = 'https://openapi-ueaz.tuyaus.com'
ENDPOINT_COUNTRIES = 'https://openapi-ueaz.tuyaus.com/v3.0/iot-03/all-countries' #Country code API endpoint

Access ID and Access Secret location

Next, copy this code to connect to the Tuya API service

openapi = TuyaOpenAPI(API_ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.connect()

Email Service

To use the Email service to send

sentMail = openapi.post("/v1.0/iot-03/messages/mails/actions/push", dict({  "to_address": "typehouse@gmail.com",
  "template_id": "MAIL_1624531323", 
  "reply_to_address": "hi@gmail.com",
  "template_param": "{\"code\":\"9090\"}"
}))

to_address: The receiver’s address (Required)

template_id: The approved email template ID (Required)

Tuya gives you two default templates to use for either

  • MAIL_1624531323 - For verification (also the one I used above)
  • MAIL_1624531338 - For password retrieval

Tuya also gives you the levity of creating your email template, check this link for more info on how to do so.

reply_to_address: The email address to which the reply is sent (Not required)

template_param: The actual value corresponding to the email template variable, in the JSON format (Not required)

Let's print out the code above and execute it to see if all things worked out on our first try. 😁

print(sentMail)

Result:

{'result': {'send_status': True}, 'success': True, 't': 1639390357965}

Your result should be like what's above if everything checks out.

Also, check your mail to see the message you sent.

Email sent from Tuya

Country Code

You can easily get the list of country codes you need or one that speaks a particular language using Tuya API.

Let's get the list of countries that speak french using the code below.

getCountries = openapi.get("/v3.0/iot-03/all-countries?lang=fre")
print(getCountries)

Result:

{'result': [{'country_code': 'AF', 'country_name': 'Afghanistan', 'national_number': '93'}, {'country_code': 'AL', 'country_name': 'Albania', 'national_number': 
'355'},...

You can query for all countries using /v3.0/iot-03/all-countries?lang=* or filter to whatever specific language you want!

Discover more with Tuya

Tuya offers you the ability to use powerful APIs like Email service, voice messaging, app push notifications, and offers whole a lot more with services like their Smart Home Paas APIs, Industrial General APIs, etc to help you create the IoT application you desire!

Summary

TypeHouse is a web-based typing game built with Flask, Tuya API, and other various technologies.

One thing I love about Tuya is how well and easy to read their documentation is written, and almost all their APIs use the same Request parameter and dictionary format, making it super easy to integrate with any application. Remember to check out their IoT platform when you need basic API services!

References

Β