GithubHelp home page GithubHelp logo

python-chatbot's Introduction

Python Chatbot

A simple, extensible bot for your IRC channels.

Installation

With Pip:

sudo pip install git+https://github.com/harrislapiroff/python-chatbot.git@master#egg=chatbot

Usage

To run a bot, you must write a short python script. For example simple_bot.py:

from chatbot.bots import Bot
from chatbot.contrib import *

bot = Bot(
	nickname = 'bestbot',
	hostname = 'chat.freenode.net',
	port = 6665,
	server_password = 'my_bots_password',
	channels = ('#freenode', '#python'),
	features = (
		PyPIFeature(),
		WikipediaFeature(),
		DictionaryFeature(),
		DiceFeature(),
		ChoiceFeature(),
		SlapbackFeature(),
	)
)

bot.run()

Then run the script:

python simple_bot.py

The Flexible Match Feature

Chatbot comes with a built in Match feature, which is both simple and powerful. You can build an entire bot from Match features alone. Here is an example of a simple bot that will slap people on command.

from chatbot.bots import Bot
from chatbot.contrib.simple import Match
from chatbot.chat import ChatResponse

SLAP_OPTIONS = (
	ChatResponse('slaps \g<target> around a bit with a baseball bat', action=True),
	ChatResponse('slaps \g<target> around a bit with a large trout', action=True),
	ChatResponse('slaps \g<target> around a bit with a piano', action=True),
	ChatResponse('slaps \g<target> around a bit with a french fry', action=True),
)

bot = Bot(
	nickname = 'bestbot',
	hostname = 'chat.freenode.net',
	port = 6665,
	server_password = 'my_bots_password',
	channels = ('#freenode', '#python'),
	features = (
		Match(r'slap (?P<target>[^\s]+) (?P<object>.+)', ChatResponse('slaps \g<target> around a bit \g<object>', action=True), addressing_required=True, allow_continuation=False),
		Match(r'slap (?P<target>.+)', SLAP_OPTIONS, addressing_required=True, allow_continuation=False),
	)
)

bot.run()

In this case, the bot handles two possible matches. The first pattern matches sentences such as bestbot: slap melinath with a frying pan by responding with an action, slaps melinath around a bit with a frying pan. The second pattern matches commands to slap that do not specify the method of slapping (e.g., slap melinath), by choosing an option randomly from SLAP_OPTIONS (e.g., slaps melinath around a bit with a french fry).

python-chatbot's People

Contributors

harrislapiroff avatar hkalexling avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

python-chatbot's Issues

Twitter feature chokes on tweets with unicode characters

Traceback is, e.g.,

Traceback (most recent call last):
  File "/home/hlapiroff/.virtualenvs/granola/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 209, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "/home/hlapiroff/.virtualenvs/granola/local/lib/python2.7/site-packages/twisted/words/protocols/irc.py", line 2413, in dataReceived
    basic.LineReceiver.dataReceived(self, data.replace('\r', ''))
  File "/home/hlapiroff/.virtualenvs/granola/local/lib/python2.7/site-packages/twisted/protocols/basic.py", line 564, in dataReceived
    why = self.lineReceived(line)
  File "/home/hlapiroff/.virtualenvs/granola/local/lib/python2.7/site-packages/twisted/words/protocols/irc.py", line 2421, in lineReceived
    self.handleCommand(command, prefix, params)
--- <exception caught here> ---
  File "/home/hlapiroff/.virtualenvs/granola/local/lib/python2.7/site-packages/twisted/words/protocols/irc.py", line 2465, in handleCommand
    method(prefix, params)
  File "/home/hlapiroff/.virtualenvs/granola/local/lib/python2.7/site-packages/twisted/words/protocols/irc.py", line 1937, in irc_PRIVMSG
    self.privmsg(user, channel, message)
  File "/home/hlapiroff/.virtualenvs/granola/src/chatbot/chatbot/client.py", line 29, in privmsg
    response = feature.handle_query(query)
  File "/home/hlapiroff/.virtualenvs/granola/src/chatbot/chatbot/contrib/twitter.py", line 28, in handle_query
    return ChatResponse(str(response_content))
exceptions.UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 58: ordinal not in range(128)

Fix should be here: https://github.com/harrislapiroff/python-chatbot/blob/master/chatbot/contrib/twitter.py#L27

Output

Currently the Bot.run() process doesn't output anything to the shell. It'd be nice if it gave some useful output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.