Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. 2D Blocks
  4. The Chat Window Widget

The Chat Window Widget

Scheduled Pinned Locked Moved 2D Blocks
1 Posts 1 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

    Introduction

    Chatting is a very common way of communicating with others. The chat window widget can be used to create many useful applications, such as chatting with friends, chatting with chatbots, chatting with customer service, etc.

     
     

    Add a Chat Window

    To add a new chat window to the stage, you can use the “add chat window” block from the “widget” category:

     add chat window x (0) y (0) width (480) height (360) input rows (1) background (#f0f0f0) border (#0035bd) name [chat1]
    

     
    It will add a chat window like this, with an “input box” at the bottom, and a “chat history” at the top:

    a5d878f8-8fa4-4d04-88b7-c761a8956a27-image.png

     
    Here are the explanations of all the inputs:

    • x and y: The position of the center of the chat window. If x = 0 and y = 0, then the chat window is centered at the center of the stage.
    • width and height: The size of the chat window. To fully occupy the entire stage, use width = 480 and height = 360.
    • number of input rows: how many rows are shown in the input box at the bottom. Usually you can set this to 1 row. If the user would be writing a lot in the input box, then you can display more rows.
    • background and border colors
    • name: The name of this chat window, which will be used to refer to this chat window later.

     
     

    Append Chat Message

     
    To append a new message into the chat history (below previous messages), you can use the following block:

    append to chat (chat1 v) message [Hello!] as [me] icon (USER v) align (Left v) text size (14) color (#ffffff) background (#541725)
    

     
    The message will look like this:

    6dc5234a-d619-4dd8-aa3d-595540c981b1-image.png

     

    The parameters are:

    • chat window name: The name of the chat window
    • message: the content of the new message
    • as: the name displayed next to the message
    • icon: the icon displayed next to the message. You can choose “USER” or “ROBOT”, or you can pick any costume of this sprite.
    • alignment: whether to show the message to the left or right.
    • text size and color: the size and color of the text inside the speech bubble.
    • background color: background color of the speech bubble.

     
     

    The Click Event

     

    When the user clicks the green “send” button at the bottom right, the content of the input box is cleared, but it is not appended to the chat history automatically. Instead, this message is stored as the “value” of the chat window widget. To handle this click event, you need to use this block:

    when widget (chat1 v) clicked
    

    To keep it simple, when the user presses the Enter key, we will assume the “send” button has been clicked, so you can use the above block to handle both the mouse click and Enter key.

    For example, to append the user input to the chat history, you can use the following blocks. Note that the “value of widget” block is used as the message input directly.

    when widget (chat1 v) clicked
        append to chat (chat1 v) message (value of widget (chat1 v)) as [me] icon (USER v) align (Left v) text size (14) color (#ffffff) background (#541725)
    end
    

     
    This would allow us to keep adding messages:

    h6.gif

     
     

    Update Last Chat Message

     

    In some chat applications, you may need to change the content of the last message in the chat history. For example, when the user is chatting with a chatbot, the chatbot’s response may come in small chunks. In this case, we can append the initial message from the chatbot, and then keep updating that message as we receive more additions to it.

    To update the last chat message, use the following block:

    update last chat message to ( ) for chat (chat1 v)
    

    The first input is the new message content, which will replace the content of the last message in the chat history.

    The second input is the name of the chat window widget.

    For example, this is a chat with a chatbot where we keep updating the last message from the chatbot as it speaks to us:

    h7.gif

    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode