Posterous
olek is using Posterous to post everything online. Shouldn't you?
A
 

olek’s posterous

random

« Back to blog

Python: bot gadu-gadu

Nie ma to jak trochę wolnego czasu. Taki sobie pseudo framework bota Gadu-Gadu w postaci plugina do ekg. Słownik callbacks zawiera funkcje, które obsługuje bot. Ciało tych funkcji musi zawierać sie nad nim by nie było błędów. Jeżeli wartość zwracana przez funkcje ma być wielo linijkowa, użyj lambdy ret aby połączyć liste zawierającą odpowiedź w 1 wiadomość zwrotną.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import ekg, re, urllib2

# zewnetrzne moduly uzyte w funkcjach
import shortener

ekg.config.uin = numerek
ekg.config.password = 'haslo'


ret = lambda result: '\n'.join(result)

def tnij(msg):
    '''Skracanie linkow, po przecinku mozna dawac kilka'''
    result = []

    for longUrl in msg:
        result.append(shortener.shorten(longUrl))


    result.append('Aby sprawdzic ile bylo odwiedzin na danym linku dodaj /hits d
o adresu')
    return ret(result)

def reverse(msg):
    '''Odwraca podany ciag znakow, bezuzyteczne'''

    result = ' '.join(msg)

    return result[::-1]

def get_help(msg):
    '''Wlasnie to ogladasz'''
    result = []

    for k, v in callbacks.iteritems():
        result.append(str(k) + ' : ' + str(v.__doc__).strip())


    return ret(result)

callbacks = {
             'pomoc': get_help,
             'odwroc': reverse,
             'tnij' : tnij,
            }

def init():
    return 1


def deinit():
    return 1

def handle_msg(uin, name, msgclass, text, time, secure):
    msg = re.split('\s', text)
    if not callbacks.has_key(msg[0]):
        ekg.command('msg %d Sorry, nie wiem o co Ci chodzi. Moze wpisz pomoc' %

uin)
        return 2

    result = callbacks[msg[0]](msg[1:])
    ekg.command('msg %d %s' % (uin, result))

    return 2

May 14, 2009
acd said...
kul
 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter