Joined: 24 Sep 2005 Posts: 14 Location: Peterborough, UK
Posted: Sun Oct 02, 2005 9:14 am Post subject: VB Bot
This a slight adjustment to Riot's bot tutorial. I have amended it so it looks to a text file for it's replies.
You can also set the bot status with commands.
Ok so on your main form add this code:
Code:
Option Explicit
Public WithEvents msn As MsgrObject
Dim lp As Integer
Private Sub msn_OnTextReceived(ByVal pIMSession As Messenger.IMsgrIMSession, ByVal User As Messenger.IMsgrUser, ByVal bstrMsgHeader As String, ByVal bstrMsgText As String, pfEnableDefault As Boolean)
If bstrMsgText = "!setaway" Then
msn.LocalState = MSTATE_AWAY
ElseIf bstrMsgText = "!setbusy" Then
msn.LocalState = MSTATE_BUSY
ElseIf bstrMsgText = "!setonline" Then
msn.LocalState = MSTATE_ONLINE
ElseIf bstrMsgText = "!setonphone" Then
msn.LocalState = MISTATUS_ON_THE_PHONE
ElseIf bstrMsgText = "!setoutlunch" Then
msn.LocalState = MISTATUS_OUT_TO_LUNCH
ElseIf bstrMsgText = "!setbrb" Then
msn.LocalState = MISTATUS_BE_RIGHT_BACK
End If
For lp = 1 To NoInputs
If bstrMsgText = MSNInput(lp) Then
User.SendText bstrMsgHeader, MSNOutput(lp), MMSGTYPE_ALL_RESULTS
End If
Next lp
End Sub
Private Sub Form_Load()
Set msn = New MsgrObject
Open "C:\Replies.txt" For Input As #1
If Not EOF(1) Then
FileIn = Input(LOF(1), #1)
End If
Close #1
For lp = 1 To Len(FileIn)
If Mid(FileIn, lp, 1) = "|" And IOMode = "Input" Then
NoInputs = NoInputs + 1
ReDim Preserve MSNInput(1 To NoInputs)
MSNInput(NoInputs) = Mid(FileIn, LastLine + 1, lp - LastLine - 1)
LastLine = lp
End If
If Mid(FileIn, lp, 1) = "|" And IOMode = "Output" Then
NoOutputs = NoOutputs + 1
ReDim Preserve MSNOutput(1 To NoOutputs)
MSNOutput(NoOutputs) = Mid(FileIn, LastLine + 1, lp - LastLine - 1)
LastLine = lp
End If
If Mid(FileIn, lp, 1) = "#" Then
IOMode = "Output"
NoInputs = NoInputs + 1
ReDim Preserve MSNInput(1 To NoInputs)
MSNInput(NoInputs) = Mid(FileIn, LastLine + 1, lp - LastLine - 1)
LastLine = lp
End If
If Mid(FileIn, lp, 1) = Chr(13) Then
NoOutputs = NoOutputs + 1
ReDim Preserve MSNOutput(1 To NoOutputs)
MSNOutput(NoOutputs) = Mid(FileIn, LastLine + 1, lp - LastLine)
IOMode = "Input"
LastLine = lp + 1
End If
If lp = Len(FileIn) Then
NoOutputs = NoOutputs + 1
ReDim Preserve MSNOutput(1 To NoOutputs)
MSNOutput(NoOutputs) = Mid(FileIn, LastLine + 1, lp - LastLine)
End If
Next lp
End Sub
Now on a module add this code:
Code:
Option Explicit
Public MSNInput() As String
Public MSNOutput() As String
Public IOMode As String
Public LastLine As Integer
Public NoInputs As Integer
Public NoOutputs As Integer
Public FileIn As String
Now create a text file on your C drive call Replies.txt
The replies file works like this:
how are you#I am fine yourself?
hello#Hello there!
You cannot use the | (pipe) to create a random reply, I am unsure of how to do this. If you know then you can add this code yourself, even let us know here so I can add it to mine!
Hi, i have change the way to look in a txt file (i think it's more simple, see below)
And now you can use the pipe to create a random reply :]
In addition, it convert the words in lower case.
Here it is :
(you must have a file named replies.txt in the program path)
Code:
Option Explicit
Public WithEvents msn As MsgrObject
Dim test As Integer
Private Sub Form_Load()
Set msn = New MsgrObject
End Sub
Private Sub msn_OnTextReceived(ByVal pIMSession As Messenger.IMsgrIMSession, ByVal User As Messenger.IMsgrUser, ByVal bstrMsgHeader As String, ByVal Usersay As String, pfEnableDefault As Boolean)
Dim replystring As String
Dim question() As String
Dim replies() As String
Dim nbrrep As Integer
Dim randomnbr As Integer
'convert in lower case
Usersay = LCase(Usersay)
'open file
Open App.Path & "\Replies.txt" For Input As #1
Do Until EOF(1) 'do until the file end
'read line and put in reply string
Line Input #1, replystring
question = Split(replystring, "#")
'test if There is Usersay in question
test = InStr(Usersay, question(0))
If test > 0 Then
'get the number of "|" (=replies) after #
replies = Split(question(1), "|")
nbrrep = UBound(replies)
'make a random number between 0 to nbrrep
Randomize
randomnbr = Int(Rnd() * (nbrrep - 0 + 1)) + 0
'replie with the random number
User.SendText bstrMsgHeader, replies(randomnbr), MMSGTYPE_ALL_RESULTS
End If
Loop
Close #1
End Sub
replies.txt exemple :
Code:
hello#hi|hello|hey !|Hi|hi there
bye#c u|Bye|You already leave me ?
This is just an exemple, to show the basics (i'll maybe post a complete .exe later).
Maybe it looks like a "beginner work" for u : it is.
(and thanks a lot to Riot :p)
Last edited by trotter on Sun Oct 09, 2005 1:49 pm; edited 1 time in total
Visual Basic 6.0 working model feedback: user defined type not defined
You don't have the reference :
OLE automation
Messenger type library
Messenger API type library
Messenger AddIns type library
Messenger Private type library
If anyone has a working VB Bot that you'd like to release, Botworld currently doesn't have any. Feel free to post it there. _________________ Check out Botworld! A dev resource for things bot.
Downloads, articles, news, fourm and more.
http://botworld.marzopolis.com