Skip to main content

Pub bot ( ดีเจ )

Pub bot คืออะไร?

เป็นบอทที่จะเปลี่ยนแสงของ LED และเล่นเสียงตามตัวอักษรที่เราใส่เข้าไป โดยมี 3 สีประกอบด้วย Red, Green, Blue และความถี่เสียงตามสีนั้นๆ เช่น !yo RGB จะทำการเปิดไฟตามลำดับ แดง เขียว และ น้ำเงิน

1. Create structure

ในส่วนนี้เราจะเขียนโค้ดเพื่อรองรับ command !yo จากผู้ใช้ และนอกจากนั้นเราต้องเตรียมข้อมูล dictionary เพื่อเตรียมไปใช้ในการ integrate กับ project IOT ที่น้องๆทำเมื่อวานนั้นเอง

ส่วนของโค้ดนั้นเราจะอ่าน text จากผู้ใช้และแยกตัวอักษรออกมาอ่านข้อมูลทีละตัวผ่าน for loop ของ python

dictionary มีการทำงานแบบ key value หรือก็คือการจะเข้าถึงข้อมูล red ใน dictionary ("R" -> "red", "Y" -> "yellow") การเรียก dict["R"] จะทำให้ return "red" และ dict["Y"] จะ return "yellow" ออกมาเหมือนกับ dictionary ของจริงเลย

from time import sleep
import discord
import requests
from discord.ext import commands

TOKEN = "YOUR_DISCORD_BOT_TOKEN"

client = commands.Bot(command_prefix="!", intents=discord.Intents.all())
color_dict = {
  "R": "red",
  "G": "green",
  "B": "blue"
}

frequency_dict = {
  "R": 261,
  "G": 293,
  "B": 329
}

@client.event
async def on_ready():
    print("Ready to be DJ?")


@client.command()
async def yo(ctx, letters):
    if letters == "off":
        try:
          lightoff = requests.get(iot_ip + "/off")
        except:
            print("Failed to call IOT API!")
    for letter in letters:
        try:
            # call light api
            if color_dict[letter] == "red":
                print("red triggered")
            elif color_dict[letter] == "green":
                print("green triggered")
            elif color_dict[letter] == "blue":
                print("blue triggered")
            # call sound api
            print(frequency_dict[letter])
            sleep(0.1)
        except:
            print("Failed to call IOT API!")
    await ctx.send("Let's them cook")
        

# put token here naja
client.run(TOKEN)

2. Integration with IOT

ต่อมาเราจะทำการเรียก http request จาก workshop IOT ของเราด้วยการเรียก API ที่เราได้ทำเอาไว้ได้เลย

from time import sleep
import discord
import requests
from discord.ext import commands

TOKEN = "YOUR_DISCORD_BOT_TOKEN"

client = commands.Bot(command_prefix="!", intents=discord.Intents.all())
color_dict = {
  "R": "red",
  "G": "green",
  "B": "blue"
}

frequency_dict = {
  "R": 261,
  "G": 293,
  "B": 329
}
iot_ip = "http://172.20.10.2"

@client.event
async def on_ready():
    print("Ready to be DJ?")


@client.command()
async def yo(ctx, letters):
    if letters == "off":
        try:
          lightoff = requests.get(iot_ip + "/off")
        except:
            print("Failed to call IOT API!")
    for letter in letters:
        try:
            # call light api
            if color_dict[letter] == "red":
                requests.get(iot_ip + "/on?red=1&blue=0&green=0")
            elif color_dict[letter] == "green":
                requests.get(iot_ip + "/on?red=0&blue=0&green=1")
            elif color_dict[letter] == "blue":
                requests.get(iot_ip + "/on?red=0&blue=1&green=0")
            # call sound api
            print(frequency_dict[letter])
            requests.get(iot_ip + "/play?hz=" + str(frequency_dict[letter]) + "&duration=0.1")
            sleep(0.1)
        except:
            print("Failed to call IOT API!")
    await ctx.send("Let's them cook")
        

# put token here naja
client.run(TOKEN)