Skip to content

Instantly share code, notes, and snippets.

@darrenparkinson
Last active March 7, 2022 01:51
Show Gist options
  • Save darrenparkinson/8cb2c23d885fb7136870fa9fdd2c43aa to your computer and use it in GitHub Desktop.
Save darrenparkinson/8cb2c23d885fb7136870fa9fdd2c43aa to your computer and use it in GitHub Desktop.
Jabber BOT Demo using Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import logging
import getpass
from optparse import OptionParser
import time
import sleekxmpp
if sys.version_info < (3, 0):
reload(sys)
sys.setdefaultencoding('utf8')
else:
raw_input = input
class JabberDemoBot(sleekxmpp.ClientXMPP):
def __init__(self, jid, password):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
self.add_event_handler("session_start", self.start)
self.add_event_handler("message", self.message)
def start(self, event):
self.send_presence()
#self.get_roster()
def message(self, msg):
if msg['type'] in ('chat', 'normal'):
if msg['body'] == 'test':
self.send_presence(pstatus='On a call', ptype='away')
time.sleep(5)
self.send_presence()
elif msg['body'] == 'bob':
msg.reply("Found : Bob Benson\n%s" % '+44 1234 567890').send()
elif msg['body'] == '7965':
msg.reply("Found : \nCP-7965G=\nCisco UC Phone 7965, Gig Ethernet, Color\n$620\nType = SPARE").send()
else:
print("Message received: %s" % msg['body'])
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)
# JID and password options.
optp.add_option("-j", "--jid", dest="jid",
help="JID to use")
optp.add_option("-p", "--password", dest="password",
help="password to use")
opts, args = optp.parse_args()
# Setup logging.
logging.basicConfig(level=opts.loglevel,
format='%(levelname)-8s %(message)s')
if opts.jid is None:
opts.jid = raw_input("Username: ")
if opts.password is None:
opts.password = getpass.getpass("Password: ")
# Setup the JabberDemoBot and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# not matter.
xmpp = JabberDemoBot(opts.jid, opts.password)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data Forms
xmpp.register_plugin('xep_0060') # PubSub
xmpp.register_plugin('xep_0199') # XMPP Ping
# Connect to the XMPP server and start processing XMPP stanzas.
# if xmpp.connect():
# Specify FQDN and Port if required as below:
if xmpp.connect(('cup.yourdomain.com', 5222)):
xmpp.process(block=False) #if we set this to false, it will let us run other code
print("Done")
else:
print("Unable to connect.")
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment