Post by Modnary the ltnelis on Apr 14, 2012 20:28:52 GMT
I've noticed that people have been coming HERE for chatbot help, so I might as well solve ALL THE THINGS!
Disclaimer:
If you just "want teh codez," please get the fuck out.
STEP ONE:
If you already have python with a version of 3.x.x, skip this.
First of all, you're going to need Python3.
You can get that here: www.python.org/getit/releases/3.0/
If you don't know what package to install, you're probably going to want the download labelled as
STEP TWO:
Skip this if you know the basics of programming.
Well, hopefully you already know how to program, but if you don't, then check out the tutorials at the official wiki here: docs.python.org/release/3.0.1/tutorial/ and/or what I learned python from a year or two ago: www.ibiblio.org/g2swap/byteofpython/read/. The latter was probably written for Python2.x, so take what it says with a grain of salt, as things have changed a little.
STEP THREE:
This is where the magic begins.
You're going to need ch.py from github by TheHolder. TheHolder is no longer updating ch.py. Speaker has taken over.
github.com/Nullspeaker/ch.py
One thing to note is that this was originally maintained by a broski who went by the pseudonym of "lumirayz", but it was eventually passed to theholder for some arcane reason you won't give a fuck about anyway.
I'd dedicate a directory to this stuff so it's not as messy.
For example:
(Linux) /home/$USER/pybotshit/
(Windows) C:\...thingsandstuffIdon'tknowbecauseI'malinuxfag\Desktop\pythonbotshit
The important files are ch.py and example.py, everything else you can toss on Socrates' grave.
ch.py is the library.
example.py is an example bot.
make a file called main.py, this is where we'll do something similar to what example.py has, and where your bot will have its directions on what to do.
STEP FOUR:
Where the actual magic begins.
Let's go through example.py and decide what we would need from it to put in main.py...
In the very beginning of ch.py we have
"import ch" is importing the ch.py mentioned earlier as an object we can access its methods and variables from, such as ch.dostuff(). Should know this from step two.
We're going to need this, so add "import ch" as the first line of your main.py
importing sys is only needed for the urllib conditional present in the snippet I quoted.
importing re is used for regular expressions which you should go and learn on your own, but they're not required. Some of the recipes I post on here may/may not require it.
The conditional and sys are only required if you want to do shit involving web requests.
Next, we have
Completely unneeded for our purposes.
Next, we have this big ol' class definition beginning with
Keep that. In a moment we'll look more closely at the onMessage()
Finally, at the end, we have
Just keep that too.
STEP FIVE:
The part where I [REDACTED] you.
I'm going to assume you're not an idiot here.
If you're having trouble following along here, you're going to want to hit up step two and learn MOAR PROGRAMMIN'.
That's really the only way to write a decent bot, to know how to program. :/
In this onMessage() function, this is how it works:
1) We get the message and its poster.
2) We take the message and split it into two parts, the ["first","part and then the rest of the string"].
(cwutIdidthar?)
3) Then we check if the first part of the message is equal to things like "/help" or "/domewildly", and then do things in response to that.
Fun things I like to have are:
Your simple case-insensitive strcmp!
used as such:
This is what I use for splitting my message into the two parts, the two parts are named "fmsg" and "lmsg" because of the terms "former" and "latter".
msgsplit I still use for multiple argument commands.
STEP SIX: THE BEST MOTHERFAQ'ING PART
Frequently Asked Questions:
"HALP MY CODE NO WORK! IT BE THROW EXCEPTIONS AND SHIZZLYDOOBOP~"
That's not a question, but however, if you get any exception, google it.
...To be continued.
Disclaimer:
If you just "want teh codez," please get the fuck out.
STEP ONE:
If you already have python with a version of 3.x.x, skip this.
First of all, you're going to need Python3.
You can get that here: www.python.org/getit/releases/3.0/
If you don't know what package to install, you're probably going to want the download labelled as
Windows x86 MSI Installer (3.0)
STEP TWO:
Skip this if you know the basics of programming.
Well, hopefully you already know how to program, but if you don't, then check out the tutorials at the official wiki here: docs.python.org/release/3.0.1/tutorial/ and/or what I learned python from a year or two ago: www.ibiblio.org/g2swap/byteofpython/read/. The latter was probably written for Python2.x, so take what it says with a grain of salt, as things have changed a little.
STEP THREE:
This is where the magic begins.
github.com/Nullspeaker/ch.py
One thing to note is that this was originally maintained by a broski who went by the pseudonym of "lumirayz", but it was eventually passed to theholder for some arcane reason you won't give a fuck about anyway.
I'd dedicate a directory to this stuff so it's not as messy.
For example:
(Linux) /home/$USER/pybotshit/
(Windows) C:\...thingsandstuffIdon'tknowbecauseI'malinuxfag\Desktop\pythonbotshit
The important files are ch.py and example.py, everything else you can toss on Socrates' grave.
ch.py is the library.
example.py is an example bot.
make a file called main.py, this is where we'll do something similar to what example.py has, and where your bot will have its directions on what to do.
STEP FOUR:
Where the actual magic begins.
Let's go through example.py and decide what we would need from it to put in main.py...
In the very beginning of ch.py we have
import ch
import random
import sys
import re
if sys.version_info[0] > 2:
import urllib.request as urlreq
else:
import urllib2 as urlreq
"import ch" is importing the ch.py mentioned earlier as an object we can access its methods and variables from, such as ch.dostuff(). Should know this from step two.
We're going to need this, so add "import ch" as the first line of your main.py
importing sys is only needed for the urllib conditional present in the snippet I quoted.
importing re is used for regular expressions which you should go and learn on your own, but they're not required. Some of the recipes I post on here may/may not require it.
The conditional and sys are only required if you want to do shit involving web requests.
Next, we have
dictionary = dict() #volatile... of course...
dancemoves = [
"(>^.^)>",
"(v^.^)v",
"v(^.^v)",
"<(^.^<)"
]
Completely unneeded for our purposes.
Next, we have this big ol' class definition beginning with
class TestBot(ch.RoomManager):
def onInit(self):
...shitandstuff
Keep that. In a moment we'll look more closely at the onMessage()
Finally, at the end, we have
if __name__ == "__main__": TestBot.easy_start()
Just keep that too.
STEP FIVE:
The part where I [REDACTED] you.
I'm going to assume you're not an idiot here.
If you're having trouble following along here, you're going to want to hit up step two and learn MOAR PROGRAMMIN'.
That's really the only way to write a decent bot, to know how to program. :/
In this onMessage() function, this is how it works:
1) We get the message and its poster.
2) We take the message and split it into two parts, the ["first","part and then the rest of the string"].
(cwutIdidthar?)
3) Then we check if the first part of the message is equal to things like "/help" or "/domewildly", and then do things in response to that.
Fun things I like to have are:
isMatch=lambda x,y: x.lower()==y.lower()
Your simple case-insensitive strcmp!
used as such:
if isMatch(messagebeginning,"/dirtythingswithmymother"): room.say("That is what I do erryday")
msgsplit=message.body.split(" ")
fmsg=msgsplit[0]
lmsg=" ".join(msgsplit[1:])
This is what I use for splitting my message into the two parts, the two parts are named "fmsg" and "lmsg" because of the terms "former" and "latter".
msgsplit I still use for multiple argument commands.
STEP SIX: THE BEST MOTHERFAQ'ING PART
Frequently Asked Questions:
"HALP MY CODE NO WORK! IT BE THROW EXCEPTIONS AND SHIZZLYDOOBOP~"
That's not a question, but however, if you get any exception, google it.
...To be continued.