Created
January 29, 2013 23:14
-
-
Save ralphm/4668909 to your computer and use it in GitHub Desktop.
Twisted/Wokkel web comment bot that sends messages over XMPP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Web comment bot that sends messages over XMPP. | |
Run this with: twistd -ny web_comment.tac | |
""" | |
from twisted.application import service, strports | |
from twisted.words.protocols.jabber.jid import JID | |
from twisted.web import http, server, resource | |
from wokkel import client, xmppim | |
THIS_JID = JID('user@example.org') | |
SECRET = 'secret' | |
WEB_PORT = 'tcp:8000' | |
class CommentResource(resource.Resource): | |
isLeaf = True | |
def __init__(self, client): | |
self.client = client | |
def render_GET(self, request): | |
return b"""<form method="POST"><input name='body'/></form>""" | |
def render_POST(self, request): | |
body = request.args[b'body'][0] | |
message = xmppim.Message(recipient=THIS_JID, body=body) | |
message.stanzaType = u'chat' | |
self.client.send(message.toElement()) | |
request.setResponseCode(http.SEE_OTHER) | |
request.setHeader(b'Location', b'/') | |
return b'' | |
application = service.Application("Comment Bot") | |
xmpp = client.XMPPClient(THIS_JID, SECRET) | |
xmpp.logTraffic = True | |
xmpp.setServiceParent(application) | |
site = server.Site(CommentResource(xmpp)) | |
web = strports.service(WEB_PORT, site) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment