Need Help Creating a Chat Bot in Visual Basic 2010 or Visual Web Developer Express

Respondido Need Help Creating a Chat Bot in Visual Basic 2010 or Visual Web Developer Express

  • sexta-feira, 17 de agosto de 2012 15:29
     
     

    First - This is my first time creating a chat application. I have been researching for days and doing Trial and Error and I just need some professional Advice or Help

    Here is my Code: I am using Visual Web Developer 2010

    I am using a listbox to show messages and receive messages and a regular text box and a enter buttom

            If TextBox.Text = ("Hi") Or TextBox.Text = ("Hello") Then

                chatbox.Items.Add("Hello My Friend.")

            End If

            If TextBox.Text = "Bye"

                chatbox.Items.Add ("Have a great day.")

            End If


        End Sub

    This Code work for me but I need some help:

    This is what I am needing help with

    1)When I hit "enter" I would like the text that I entered into the text box to be imputed in the chatbox. The problem is I do not know the code for that.

    2) I would like to be able to distinguish between the "user" and the automated reply from the bot. Again the problem is I do not know the code for this.

Todas as Respostas

  • sexta-feira, 17 de agosto de 2012 18:25
     
     Resposta Proposta
    Well i'm proud of myself I have answered my own questions and created my first very own chat bot for my website.

    KMC


  • sexta-feira, 17 de agosto de 2012 18:26
     
     Respondido Contém Código

    First to make the Enter key work you could try setting the form's AcceptButton property to the button you'd press to send the message.

    For a bot like this, I'd also strongly recommend LCase or UCase to make the bot case insensitive, but that's up to you to decide.

    In that button's Click event you could try:

    Dim usernick As String = "User" '<- User's nickname
    chatbox.Items.Add(usernick & ": " & TextBox1.Text)
    Dim botnickname As String = "Bot" '<- Chatbot's nickname
    If LCase(TextBox1.Text) = ("hi") Or LCase(TextBox.Text) = ("hello") Then
    chatbox.Items.Add(botnickname & ": Hello My Friend.")
    ElseIf LCase(TextBox1.Text) = "bye"
    chatbox.Items.Add (botnickname & ": Have a great day.")
    End If

    Is this what you're looking for?

    Sincerely yours,
    - bilde2910


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

  • sexta-feira, 17 de agosto de 2012 18:50
     
     

    Wow thank you for helping me out greatly I really appreciate you. This is what I created earlier. what do you think?

            Dim args() As String = TextBox.Text.Split(" ")
            Dim rand As New System.Random

            chatbox.Items.Add("User: " & TextBox.Text)

            If args(0) = "Hello" Or args(0) = "Hi" Or args(0) = "hi" Or args(0) = "hello" Then

            End If

            Dim r = rand.Next(1, 4)
            If r = 1 Then
                chatbox.Items.Add("Kayla: " & "Good Morning")
            ElseIf r = 2 Then
                chatbox.Items.Add("Kayla: " & "Hello")
            ElseIf r = 3 Then
                chatbox.Items.Add("Kayla: " & "Hello my friend!")
            ElseIf r = 4 Then
                chatbox.Items.Add("Kayla: " & "Good day to you sir/madam.")

            End If


            If TextBox.Text = "Bye" Or TextBox.Text = "bye" Or TextBox.Text = "cya" Or TextBox.Text = "Cya" Then

                chatbox.Items.Add("Good Bye Nice Talking To You.")

            End If

            If TextBox.Text = "wait" Then

                chatbox.Items.Add("Can I help you")

            End If

    I believe what you provided works wonderful. I really appreciate you getting back with me. This means alot. 


    KMC

  • sexta-feira, 17 de agosto de 2012 18:56
     
     

    Ken,

    What happens if the text does not contain the " " character?


    Please call me Frank :)

  • sexta-feira, 17 de agosto de 2012 19:09
     
     

    It throws out this error message " Error 1 "     "   is not declared. It may be inaccessible due to its protection level."

    This error is from MY Code that I posted earlier.


    KMC


    • Editado KenMona sexta-feira, 17 de agosto de 2012 19:14
    •  
  • sexta-feira, 17 de agosto de 2012 19:14
     
     

    It throws out this error message " Error 1 "     "   is not declared. It may be inaccessible due to its protection level."


    KMC

    Then that's kind of a problem huh. ;-)

    Think it through some - how can you do that better?

    How can you make sure that you get all of the valid information that you want no matter what's typed in (I'm assuming that in your program you'll be sure that you'll do it properly)?

    Give it some thought for a while. :)


    Please call me Frank :)

  • sexta-feira, 17 de agosto de 2012 19:18
     
     
    Also ... what happens if the user types in "HELLO" or "hEllo"?

    Please call me Frank :)

  • sexta-feira, 17 de agosto de 2012 19:26
     
     

    With my original Code it throws a another error. Thank you frank for the advice. I really appreciate it. Also thank you Bilde2910 for you help and advice as well. I am going to keep working on my chat bot. Its not perfect but its a start. I will continue to post my and update. I am going to need more critique. Thanks


    KMC

  • sexta-feira, 17 de agosto de 2012 19:33
     
     

    With my original Code it throws a another error. Thank you frank for the advice. I really appreciate it. Also thank you Bilde2910 for you help and advice as well. I am going to keep working on my chat bot. Its not perfect but its a start. I will continue to post my and update. I am going to need more critique. Thanks


    KMC

    Ken,

    Consider something like this instead: Don't bother with trying to sort out words in a line of text, especially if you have nothing which you can depend on to be a delimiter.

    Look for the words IN the line of text directly (although this is prone to errors if other words might also have the keywords in them by sheer coincidence).

    For example something like this:

    If TextBox1.Text.ToLower.Contains("hello") OrElse TextBox1.Text.ToLower.Contains("hi") Then
        ' Do your stuff now
    End If

    Do you see what I'm doing there?

    The drawback to that is that words like "high" would trigger it.

    Give that thought - how can we get that to not happen?


    Please call me Frank :)

  • sexta-feira, 17 de agosto de 2012 19:44
     
     

    Okay Frank, I think I know what you mean. So if the the word contains the trigger word like "Helloo23" the bot will go ahead and post whatever next. Is that correct. Basically if the user makes a typo the bot will recognize and post


    KMC


    • Editado KenMona sexta-feira, 17 de agosto de 2012 19:47
    •  
  • sexta-feira, 17 de agosto de 2012 19:51
     
     

    Okay Frank, I think I know what you mean. So if the the word contains the trigger word like "Helloo23" the bot will go ahead and post whatever next. Is that correct. Basically if the user makes a typo the bot will recognize and post


    KMC


    No, it wouldn't recognize it.

    Here's what that last thing did: First it internally took all the of what they typed to lower case and then made the comparision. That way, no matter if it's all caps or all lower-case or any combination they come up with, it's all the same to the program.

    It next uses the ".Contains" method and that essentially just looks through the text looking for that specific text (as though it were all lower-case whether it is or not).

    BUT!

    You're looking for a word. How can you designate the word? Because of a space - but we already figured out that won't always work.

    So now what?!?

    Of course I'm making this a mind teaser - hoping to get you thinking here. ;-)


    Please call me Frank :)

  • sexta-feira, 17 de agosto de 2012 19:54
     
     
    Frank you really got me thinking. I think im going to take an ibuprofen. Thank you Frank for the push

    KMC

  • sexta-feira, 17 de agosto de 2012 19:57
     
     
    Frank you really got me thinking. I think im going to take an ibuprofen. Thank you Frank for the push

    KMC

    Haha!

    Let's go back to your original way - but with a twist.

    First test to see IF it contains a space character. If it does, then split it based on the space (if it's not string.empty as would happen if they typed two spaces) and from that, convert to lower and make your comparisons.

    If there's NOT a space, then we know it's "all one word" - in that case, convert to lower and make your comparison directly on the textbox's text.

    :)


    Please call me Frank :)

  • sexta-feira, 17 de agosto de 2012 20:13
     
      Contém Código

    You're welcome KenMona,

    Glad to be of help!

    @Frank:

    Are you sure you would not just go ahead with:

    Dim args() As String
    If TextBox1.Text = Nothing Then Exit Sub Else args = Split(LCase(TextBox1.Text), " ")

    I've had no exceptions with that any time.


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

  • sexta-feira, 17 de agosto de 2012 20:19
     
      Contém Código

    You're welcome KenMona,

    Glad to be of help!

    @Frank:

    Are you sure you would not just go ahead with:

    Dim args() As String
    If TextBox1.Text = Nothing Then Exit Sub Else args = Split(LCase(TextBox1.Text), " ")

    I've had no exceptions with that any time.


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

    That doesn't check all instances - such as if it's a single word, what if it's prececeded by or appended with a space (haven't gotten there with him yet ;-), or even something as simple as someone typing in like you would with a letter:

    Hello,

    I was wondering...

    "hello" doesn't exist either as a single word with spaces nor does it exist as a single word because the comma is placed directly after it.

    It's actually not all that many combinations - I was hoping to get him to think it through about how to do it without trying to come up with every conceivable possible way it might be done.

    :)


    Please call me Frank :)

  • sexta-feira, 17 de agosto de 2012 21:44
    Moderador
     
     Respondido Contém Código

    I didn't see your question before, anyways it triggered an interest to me.... Here is a code I came up with, obviously improving upon it is a matter of learning to create a perfect AI.... Any for fun, give it a try...

    Option Strict On
    Public Class Form1
        WithEvents txtChatInput As New TextBox
        WithEvents btnSendInput As New Button
        WithEvents txtOutput As New TextBox
        Dim ChatItems As New List(Of ChatItem)
        Dim CantAnswer As New List(Of String)
        Dim CantRespond As New List(Of String)
        Dim MonospacedFont As New Font("consolas", 12)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Width = 1000
            Me.Height = 500
            txtChatInput.Size = New Size(New Point(191, 20))
            txtChatInput.Location = New Point(13, 29)
            btnSendInput.Size = New Size(New Point(75, 20))
            btnSendInput.Location = New Point(210, 29)
            btnSendInput.Text = "Send"
            txtOutput.Font = MonospacedFont
            txtOutput.Left = 0
            txtOutput.Multiline = True
            txtOutput.WordWrap = False
            txtOutput.ScrollBars = ScrollBars.Both
            txtOutput.Top = txtChatInput.Top + txtChatInput.Height
            txtOutput.Height = Me.ClientRectangle.Height - txtOutput.Top
            txtOutput.Width = Me.ClientRectangle.Width
            ' txtOutput.Dock = DockStyle.Bottom
            Me.Controls.Add(txtChatInput)
            Me.Controls.Add(btnSendInput)
            Me.Controls.Add(txtOutput)
            Dim C1prompts As New List(Of String)
            Dim C1Responses As New List(Of String)
            C1prompts.Add("How are you doing today?")
            C1prompts.Add("How are you doing?")
            C1prompts.Add("How are you?")
            C1prompts.Add("Are you doing well?")
            C1Responses.Add("I'm doing great! Thanks for asking!")
            C1Responses.Add("Not too shabby...")
            C1Responses.Add("I can't compain!")
            Dim ChatItem1 As New ChatItem(C1prompts, C1Responses)
            ChatItems.Add(ChatItem1)
            Dim C2prompts As New List(Of String)
            Dim C2Responses As New List(Of String)
            C2prompts.Add("Yes")
            C2Responses.Add("ok good...")
            C2Responses.Add("good to hear that...")
            Dim ChatItem2 As New ChatItem(C2prompts, C2Responses)
            ChatItems.Add(ChatItem2)
            CantAnswer.Add("I refuse to answer that")
            CantAnswer.Add("I don't even know why you would ask that...")
            CantAnswer.Add("I don't know everything!")
            CantAnswer.Add("What do I look like, an encyclopedia?")
            CantAnswer.Add("Can we talk about something else?")
    
            CantRespond.Add("Is that so?")
            CantRespond.Add("I did not know that...")
            CantRespond.Add("Why are you telling me this?")
            CantRespond.Add("Absolutly amazing...")
            CantRespond.Add("Does that even make sense?")
        End Sub
        Sub btnClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnSendInput.Click
            Randomize()
            Dim Random As New Random
            Dim Index As Integer
    
            Dim Prompt As String = txtChatInput.Text
            Dim PromptFound As Boolean = False
            For Each ChatItem As ChatItem In ChatItems
                If ChatItem.PromptFound(Prompt, False) Then
                    PromptFound = True
                    Select Case ChatItem.PromptCount < 3
                        Case True
                            Index = Random.Next(0, ChatItem.Responses.Count - 1)
                            txtOutput.Text = txtOutput.Text & vbCrLf & "You:         " & Prompt & vbCrLf & "Computer:    " & ChatItem.Responses(Index) & vbCrLf
                            ChatItem.PromptCount = ChatItem.PromptCount + 1
                        Case Else
                            Select Case ChatItem.PromptCount < 4
                                Case True
                                    Select Case InStr(Prompt, "?") > 0
                                        Case True
                                            txtOutput.Text = txtOutput.Text & vbCrLf & "You:         " & Prompt & vbCrLf & "Computer:    " & "You have already asked me that " & ChatItem.PromptCount & " times... You know the answer..." & vbCrLf
                                        Case Else
                                            txtOutput.Text = txtOutput.Text & vbCrLf & "You:         " & Prompt & vbCrLf & "Computer:    " & "You have already said that " & ChatItem.PromptCount & " times... I am going to ignore you..." & vbCrLf
                                    End Select
                                    ChatItem.PromptCount = ChatItem.PromptCount + 1
                                    Exit For
                                Case Else
                                    txtOutput.Text = txtOutput.Text & vbCrLf & "You:         " & Prompt & vbCrLf & "Computer:    " & "dude say something new..." & vbCrLf
                                    ChatItem.PromptCount = ChatItem.PromptCount + 1
                                    Exit For
                            End Select
                    End Select
                Else
                    ChatItem.PromptCount = 0
                End If
            Next
            If Not PromptFound Then
                If Not InStr(Prompt, "?") > 0 Then
                    Index = Random.Next(0, CantRespond.Count - 1)
                    txtOutput.Text = txtOutput.Text & vbCrLf & "You:    " & Prompt & vbCrLf & "Computer:    " & CantRespond(Index) & vbCrLf
                Else
                    Index = Random.Next(0, CantAnswer.Count - 1)
                    txtOutput.Text = txtOutput.Text & vbCrLf & "You:    " & Prompt & vbCrLf & "Computer:    " & CantAnswer(Index) & vbCrLf
                End If
            End If
        End Sub
    End Class
    Class ChatItem
        Public Prompts As New List(Of String)
        Public Responses As List(Of String)
        Public PromptCount As Integer
        Sub New(ByVal Prompts As List(Of String), ByVal Responses As List(Of String))
            Me.Prompts = Prompts
            Me.Responses = Responses
            Me.PromptCount = 0
        End Sub
        Function PromptFound(ByVal Prompt As String, ByVal CaseSensitive As Boolean) As Boolean
            Select Case CaseSensitive
                Case True
                    If Not Prompts.IndexOf(Prompt) = -1 Then Return True
                    Return False
                Case Else
                    Dim Exists As Boolean = False
                    For Each S As String In Prompts
                        If LCase(S) = LCase(Prompt) Then
                            Exists = True
                        End If
                    Next
                    Return Exists
            End Select
        End Function
    End Class


    If you want something you've never had, you need to do something you've never done.


  • segunda-feira, 20 de agosto de 2012 12:54
     
      Contém Código

    Hey Frank and or bilde2910

    So I tried this string of code and I was unsure about it. 

    Dim args() As String
    If TextBox1.Text = Nothing Then Exit Sub Else args = Split(LCase(TextBox1.Text), " ")

    Is this string code just like using 

    If TextBox.Text.ToLower.Contains("hello") OrElse TextBox.Text.ToLower.Contains("hi") Then. 

    I'm still figuring things out to be beneficial for me, Also One more thing that I am working with is:

    chatbox.SelectedIndex = chatbox.Items.Count - 1

    So when I have this in my script and this string works in the list box but I am trying to get it lower than -1 to where it will show the last message that the user types. Any Ideas. Also can either of you teach me more about the creating a chat bot on the direction that I am going.


    KMC



    KMC

  • segunda-feira, 20 de agosto de 2012 14:44
     
     Respondido Contém Código

    Hey Frank and or bilde2910

    So I tried this string of code and I was unsure about it. 

    Dim args() As String
    If TextBox1.Text = Nothing Then Exit Sub Else args = Split(LCase(TextBox1.Text), " ")

    Is this string code just like using 

    If TextBox.Text.ToLower.Contains("hello") OrElse TextBox.Text.ToLower.Contains("hi") Then. 

    I'm still figuring things out to be beneficial for me, Also One more thing that I am working with is:

    chatbox.SelectedIndex = chatbox.Items.Count - 1

    So when I have this in my script and this string works in the list box but I am trying to get it lower than -1 to where it will show the last message that the user types. Any Ideas. Also can either of you teach me more about the creating a chat bot on the direction that I am going.


    KMC



    KMC

    Ken,

    You don't have a script; this is VB Net. You have code.

    ***

    As for the ListBox, the following applies:

    ListBox1.Items(-1) -- That means that nothing is selected.

    ListBox1.Items(0) -- That's the first one in the ListBox.

    ListBox1.Items(ListBox1.Items.Count - 1) -- That's the last one in the ListBox.

    ***

    As for the TextBox, consider something like the code which I have on a page of my site here.

    That one uses one TextBox and a Label (the label is used to indicate the results).

    You might also consider using the .Leave event rather than the .TextChanged event so that it's not fired on every single keystroke that the user makes.


    Please call me Frank :)

  • segunda-feira, 20 de agosto de 2012 15:18
     
     

    Frank, 

    I used the suggested code that you recommended and in my code i tried 

    chatbox.Items(chatbox.Items.Count - 1)

    but it give me an error "property access must assign to the property... so then I changed that and came up with

    chatbox.SelectedIndex = chatbox.Items.Count - 1

    This worked but when the listbox scrolls down due to the amount of chat, it does not show the last message displayed by the chatbot. It shows the last message displayed by the user. Do you follow?



    KMC

  • segunda-feira, 20 de agosto de 2012 15:30
     
      Contém Código

    Ken,

    I'm seeing things out of context I guess by try this:

            Dim lb As New ListBox
            '
            lb.Items.AddRange(New String() {"One", "Two", "Three"})
            '
            Dim itm As String = lb.Items(lb.Items.Count - 1).ToString
            Stop

    When the program gets to "Stop", it will halt. When it does, hover your mouse over the variable "itm".

    Please call me Frank :)

  • segunda-feira, 20 de agosto de 2012 15:40
     
      Contém Código

    He needs to have the userbox to be scrolled to the bottom. Not actually selecting the last item.

    Keep using this:

    chatbox.SelectedIndex = chatbox.Items.Count - 1

    But insert it at the end of the sub if it's not already there.

    "Is this string code just like using 
    If TextBox.Text.ToLower.Contains("hello") OrElse TextBox.Text.ToLower.Contains("hi") Then. 
    I'm still figuring things out to be beneficial for me, Also One more thing that I am working with is:
    chatbox.SelectedIndex = chatbox.Items.Count - 1"

    Nope, it's for declaring the arguments of the command. It checks if the TextBox is empty and if it is, exit the sub and don't do more with it. If it's not, get the arguments, divided by spaces. It's like a replacement for this, which you posted earlier:

    Dim args() As String = TextBox.Text.Split(" ")

    But I suggest using Frank's great advice ;)

    - bilde2910


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

    • Editado bilde2910 segunda-feira, 20 de agosto de 2012 15:42
    •  
  • sexta-feira, 24 de agosto de 2012 02:46
     
      Contém Código

    Hey Guys, 

    Should I have just used a Textbox instead of a listbox. Ive been working on it and I made the textbox a readonly. Now the only problem is that I have been trying to work with the code that I have currently. Instead of using the chatbox.items.add() I thought that I could use the chatbox.text() which does not work. 

    Example: We take out the chatbox.items.add and replace with "chatbox.text" which does not work. So How would I replace the chatbox.items.add with a textbox that is readonly.


    Dim usernick As String = "User" '<- User's nickname
    chatbox.Items.Add(usernick & ": " & TextBox1.Text)
    Dim botnickname As String = "Bot" '<- Chatbot's nickname
    If LCase(TextBox1.Text) = ("hi") Or LCase(TextBox.Text) = ("hello") Then
    chatbox.Items.Add(botnickname & ": Hello My Friend.")
    ElseIf LCase(TextBox1.Text) = "bye"
    chatbox.Items.Add (botnickname & ": Have a great day.")
    End If




    • Editado KenMona sexta-feira, 24 de agosto de 2012 03:18
    •  
  • sexta-feira, 24 de agosto de 2012 05:22
     
      Contém Código

    Instead of this, which we used when we had a ListBox:

            chatbox.Items.Add(user & ": message")

    Use this, since it's now a TextBox:

            chatbox.AppendText(user & ": message" & vbNewLine)

    Remember to set the TextBox's multiline property to True, if you haven't already!

    - bilde2910


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

  • sábado, 25 de agosto de 2012 06:21
     
     

    Bilde2910, 

    I am having some issue with the current line. When I use that line I get a Appendtext "" is not a member system.web.ui.webcontrols.textbox Do I need to change the protected sub to a public or private. Do I need to make a DIM statement in order for it to take effect. I appreciate all your help. Thank you very much


    KMC

  • sábado, 25 de agosto de 2012 06:26
    Moderador
     
     

    Try this instead...

    Textbox.text = textbox.text & yournewtext


    If you want something you've never had, you need to do something you've never done.

  • sábado, 25 de agosto de 2012 07:12
     
      Contém Código

    You're using a web textbox? Didn't even know they existed!

    Try Paul's advice, or if this works it's shorter:

    chatbox.Text &= user & ": message" & vbNewLine

    Why not use a standard textbox?

    System.Windows.Forms.TextBox

    Sincerely yours,
    - bilde2910


    If a post is helpful to you or solves a problem, remember to mark it as answer, propose it as answer or vote up.
    Check out my development so far!

    • Editado bilde2910 sábado, 25 de agosto de 2012 07:13
    •