This is my code-
here global variable (lvl) is not increasing when my if condition gets satisfied and the get function is called.
#Invoking Libraries Required
library(shiny)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)
# Defining UI Page Format
ui<- fluidPage(useShinyjs(),tags$head(tags$link(rel="stylesheet", href="style.css")),
titlePanel("Chatbot"),
fluidRow(column( width = 5,
panel(style = "overflow-y:scroll; max-height: 300px; position:relative; align: centre",
textInput("message", label = "",placeholder = "Type yor message here."),
actionButton("send", "Send"), heading = "Smart Advisor", status = "primary")
)
)
)
# Defining Server Controls
server<- function(input, output, session)
{
lvl <<- 1
# Function to Clear the Message Box
clearInput<- function()
{
updateTextInput(session,"message", value = "")
}
# Function to print Invalid Input
invalidInput<- function()
{
insertUI(
selector = "#message",
where = "beforeBegin",
ui=div(class="chat-bubbles",
div(class="bubble",p("Kindly provide a valid input."))
)
)
clearInput()
}
# Function to revert to a message
replyMessage<- function(lvl,msg)
{
message('Level: ',lvl)
message('Message:',msg)
switch(lvl,
# Conditions for Level 1
if (grepl("^[a-zA-Z][a-zA-Z ]+[a-zA-Z]$",input$message, perl=T)) {
insertUI(
selector = "#message",
where = "beforeBegin",
ui=div(class="chat-bubbles",
div(class="bubble",style="text-align: right;",
p(tags$b(msg), tags$br(),
Sys.time())
),
div(class="bubble",
wellPanel(
p( )
)
)
)
)
clearInput()
lvl <- lvl + 1
}
else
{
invalidInput()
},
# Conditions for Level 2
if (msg == 1 | msg == 2 | msg == 3 | msg == 4 | msg == 5 | msg == 6)
{
insertUI(
selector = "#message",
where = "beforeBegin",
ui=div(class="chat-bubbles",
tags$style("position align-right;"),
div(class="bubble",style="text-align: right;",
p(tags$b(msg), tags$br(),
Sys.time())),
div(class="bubble",
wellPanel(
)
)))
clearInput()
}
else
{
invalidInput()
}
)
}
# Function to check blank in Message box
getMessage<- function(lvl)
{
observeEvent(input$send,{
if(input$message == '')
{
insertUI(
selector = "#message",
where = "beforeBegin",
ui=div(class="chat-bubbles",
div(class="bubble",p("Kindly provide a valid input."))
)
)
msg = ''
clearInput()
}
else
{
msg = input$message
replyMessage(lvl,msg)
}
})
}
# Main Function
startConversation<- function()
{
insertUI(
selector = "#message",
where = "beforeBegin",
ui=div(class="chat-bubbles",
div(class="bubble",
p( "Hello, can I get your name please?")
)))
getMessage(lvl)
message('Inside Main Function')
}
startConversation()
}
# Calling Shiny Application
shinyApp(ui, server)