-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
159 lines (129 loc) · 5.49 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import logging
import getpass
import sleekxmpp
from optparse import OptionParser
from parse import *
from parse import compile
from sig_til import parse_sig_command
from cmd_help import *
from open_hours import *
from bot_plugins.hey import *
# Python versions before 3.0 do not use UTF-8 encoding by default.
if sys.version_info < (3, 0):
reload(sys)
sys.setdefaultencoding('utf8')
else:
raw_input = input
class Botplug(sleekxmpp.ClientXMPP):
def __init__(self, password):
self.room = "snak@conference.orn.li"
self.nick = "botplug"
self.mail = "spam.kran@gmail.com"
self.parsedict = self.create_parse_dict()
sleekxmpp.ClientXMPP.__init__(self, self.mail, password)
self.add_event_handler("session_start", self.start)
self.add_event_handler('message', self.message)
self.add_event_handler("groupchat_message", self.muc_message)
#self.add_event_handler("muc::%s::got_online" % self.room,
# self.muc_online)
def start(self, event):
self.get_roster()
self.send_presence()
self.plugin['xep_0045'].joinMUC(self.room,
self.nick,
# If a room password is needed, use:
# password=the_room_password,
wait=True)
#self.send_message(mto=self.room,
# mbody="Nissen er i huset!",
# mtype='groupchat')
def muc_message(self, msg):
if (msg['body'].startswith(self.nick+":") and self.nick != msg['from']):
query = msg['body'][len(self.nick)+2:].lower().decode('utf-8')
print("got query: " + query)
if str(self.parsedict['sig'].parse(query)) != "None":
self.say(parse_sig_command(msg,query))
elif str(self.parsedict['hej'].parse(query)) != "None":
self.say(hey(msg,query))
elif str(self.parsedict['sov'].parse(query)) != "None":
if str(msg['from']).endswith("Dromi"):
print("closing")
self.say("Botten går i seng :-(")
self.disconnect(wait=True)
exit(0)
else:
self.say("DU IKKE MIN RIGTIGE FAR!!!!!")
elif str(self.parsedict['hjæ'].parse(query)) != "None":
p = self.parsedict['hjæ'].parse(query)
self.say(get_command_help(p['command']))
elif str(self.parsedict['kom'].parse(query)) != "None":
self.say(get_command_list())
elif str(self.parsedict['clo'].parse(query)) != "None":
self.say(opening_hours())
elif str(self.parsedict['sut'].parse(query)) != "None":
self.say(msg['mucnick'] + ": bab knap!")
else:
self.say("Kommandoen er mig ikke bekendt. Brug "+self.nick+": kommandoer for oversigt.")
def message(self, msg):
if msg['type'] in ('normal', 'chat'):
print("Got personal message: " + msg['body'])
msg.reply("det godt du hej!").send()
# Only usable if the bot should speak when someone joins
#def muc_online(self, presence):
def say(self, body):
self.send_message(mto=self.room,
mbody=body,
mtype='groupchat')
def create_parse_dict(self):
parsedict = {}
parsedict['hej'] = compile(u"hej")
parsedict['sig'] = compile(u"sig {}")
parsedict['kom'] = compile(u"kommandoer")
parsedict['hjæ'] = compile(u"hjælp {command}")
parsedict['sov'] = compile(u"gå i seng")
parsedict['clo'] = compile(u"closing time")
parsedict['sut'] = compile(u"sut dut")
return parsedict
if __name__ == '__main__':
# Setup the command line arguments.
optp = OptionParser()
# Output verbosity options.
optp.add_option('-q', '--quiet', help='set logging to ERROR',
action='store_const', dest='loglevel',
const=logging.ERROR, default=logging.INFO)
optp.add_option('-d', '--debug', help='set logging to DEBUG',
action='store_const', dest='loglevel',
const=logging.DEBUG, default=logging.INFO)
optp.add_option('-v', '--verbose', help='set logging to COMM',
action='store_const', dest='loglevel',
const=5, default=logging.INFO)
opts, args = optp.parse_args()
# Setup logging.
logging.basicConfig(level=opts.loglevel,
format='%(levelname)-8s %(message)s')
password = getpass.getpass("Password: ")
# Setup the MUCBot and register plugins.
bot = Botplug(password)
bot.register_plugin('xep_0030') # Service Discovery
bot.register_plugin('xep_0045') # Multi-User Chat
bot.register_plugin('xep_0199') # XMPP Ping
# Connect to the XMPP server and start processing XMPP stanzas.
if bot.connect():
bot.process(block=False)
print("Done")
else:
print("Unable to connect.")
while(True):
inp = raw_input()
if inp == "exit":
print("closing")
# nisse.say("Nissen går i seng :-(")
bot.disconnect(wait=True)
break
elif inp.startswith("say "):
bot.say(inp[4:])
else:
print("unknown input")