Skip to content

Instantly share code, notes, and snippets.

@AWeirdDev
Last active December 4, 2022 12:52
Show Gist options
  • Save AWeirdDev/15bbfcc768ae5c5c550b5b6a6b091cbf to your computer and use it in GitHub Desktop.
Save AWeirdDev/15bbfcc768ae5c5c550b5b6a6b091cbf to your computer and use it in GitHub Desktop.
Creating a classroom using LINELIB

Classroom example

Running a LINE Bot with a basic classroom functionality, with homework and market commands.

The homework.txt

Here's the editting format:

  • HOMEWORK_CONTENT - Homework content
  • HOMEWORK_CONTENT$$DUE - Homework content with a due date.
    • $$none - No due date (green).
    • $$today - Due today (red).
    • $$DATE - Due on a custom date. (e.g. Friday)
  • ;note=NOTE_CONTENT - Note that displays at the bottom of the message.

classroom.py configuration

Make sure to edit the store_items and students!

store_items format:

{
  "item 1": 100 // the price
  "item 2": float("inf") 
}

students format:

{
  "STUDENT_USER_ID": 100 // points
}

You can see the user / student's user id by adding ctx.author.id:

@client.event
def msg(ctx):
  print(ctx.author.id) # prints out the ID.

That's about it! Have fun!

from linelib import Client, New, Long
from flex import flex
import uuid
command = Client.Commands.cmd
store_items = {
"Pencil": 20,
"Apple": 70,
"iPhone 13": float("inf") # too large, Infinity
}
students = {
"STUDENT_USER_ID": 100
}
class Classroom(Client.Commands, prefix=""): # message
@command(name="homework")
def show_homework(ctx):
ctx.reply(
{
"type": "flex",
"altText": "Homework content",
"contents": flex()
} # you can send the message json!
)
@command(name="market")
def market(ctx):
buttons = []
for i in store_items:
buttons.append(
New.QuickReplyButton(
action=New.MessageAction(f"buy {i}", f"{i} $" + str(store_items[i])) # text, label
)
)
ctx.reply([
"Welcome to Rick Astley's market! What would you like to buy with your points?",
New.TextMessage(
"Pick an item",
quick_reply=New.QuickReply(buttons)
)
])
@command(name="buy")
def buy(ctx, stuff: Long << str = None):
if not stuff: return ctx.reply("You did not enter anything to buy!\nUsage: buy <something>")
if not stuff in store_items: return ctx.reply("This item is not available on the store, sorry!")
POINTS = students[ctx.author.id]
# if the student's point is larger / equals to the item's price
if POINTS >= store_items[stuff]:
students[ctx.author.id] -= store_items[stuff]
# oh no money gone
order_id = str(uuid.uuid4())
ctx.reply(f"""Thanks for purchasing in our store! Your order has been recorded (ID: {order_id}),
and you'll receive your commodity in the next class!""")
print(f"[NEW ORDER] {order_id} - {stuff}\n{' '*12}student: {ctx.author.id}")
# you can do anything else!
else:
ctx.reply(f"Item {stuff} requires {store_items[stuff]} points, you only have {POINTS} points, which is not enough. Sorry!")
def flex():
json = {
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "Homework",
"weight": "bold",
"size": "xxl",
"margin": "md"
},
{
"type": "text",
"text": "Homework Assigned",
"size": "xs",
"color": "#aaaaaa",
"wrap": True
},
{
"type": "separator",
"margin": "xxl"
},
{
"type": "box",
"layout": "vertical",
"margin": "xxl",
"spacing": "sm",
"contents": [
# MAIN CONTENT HERE!
]
},
{
"type": "separator",
"margin": "xxl"
},
{
"type": "text",
"text": "Notes from your teacher",
"margin": "lg",
"color": "#aaaaaa",
"size": "xs"
},
{
"type": "text",
"text": "No notes provided."
}
]
},
"styles": {
"footer": {
"separator": True
}
}
}
for i in open('homework.txt', 'r').read().splitlines():
if i.startswith(";note="):
note = i.replace(";note=", "", 1)
json['body']['contents'][6]['text'] = note
continue
[item, due] = i.split("$$")
# assume { item = '1. Eat chocolate' }
# assume { due = '12/02' }
autofill = []
_ = { "today": "Due today", "none": "No due date" }
colors = { "today": "#f53838", "none": "#1bab38" }
if not due in _:
autofill.append(due)
autofill.append("#555555")
else:
autofill.append(_[due])
autofill.append(colors[due])
json['body']['contents'][3]['contents'].append({
"type": "box",
"layout": "horizontal",
"contents": [
{
"type": "text",
"text": item,
"size": "sm",
"color": "#555555",
"flex": 0,
"weight": "bold"
},
{
"type": "text",
"text": autofill[0],
"size": "sm",
"color": autofill[1],
"align": "end"
}
]
})
return json
1. Remember to eat!$$none
2. Finish your draft"$$today
3. Finish your drawing$$12/04
;note=Waltuh, you left your umbrella
from linelib import Client
from classroom import Classroom
client = Client("channel secret", "access token")
client.load_cog(Classroom)
@client.event('ready')
def ready():
print("Classroom ready! Ping: %i" % round(client.ping))
client.run(host="0.0.0.0", port=8080)
@AWeirdDev
Copy link
Author

haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment