-
Notifications
You must be signed in to change notification settings - Fork 6.6k
adding new xmpp wikibot example #596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@mogar1980 I can review. Since this runs on compute engine, can you put it in the (Could this run on App Engine flex instead?) |
|
||
"""Uncyclobot server example using SleekXMPP client library""" | ||
|
||
import sys |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imports need to be two sections: standard lib, then thirdparty and should be in alphabetical order:
import getpass
import json
import logging
import optparse
import sys
import threading
import urllib
from flask import Flask, request
import requests
import sleekxmpp
import sys | ||
import logging | ||
import getpass | ||
from optparse import OptionParser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer argparse over optparse.
|
||
import sleekxmpp | ||
|
||
# Python versions before 3.0 do not use UTF-8 encoding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be needed. If you remove it, what happens?
logging.basicConfig(level=opts.loglevel, | ||
format='%(levelname)-8s %(message)s') | ||
|
||
if opts.jid is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is intended to run on the server, don't ever use input.
if xmpp.connect(): | ||
# If you do not have the dnspython library installed, you will need | ||
# to manually specify the name of the server if it does not match | ||
# the one in the JID. For example, to use Google Talk you would |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Google Talk is dead, no?
how it may be used. | ||
""" | ||
if msg['type'] in ('chat', 'normal'): | ||
msg_body = "%(body)s" % msg |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use format
over %
throughout.
msg_body = "%(body)s" % msg | ||
logging.info("Message sent was: " + msg_body) | ||
encoded_body = urllib.quote_plus(msg_body) | ||
svrResponse = requests.get("https://en.wikipedia.org/w/api.php?action=parse&prop=sections&format=json&page=" + encoded_body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use single quotes throughout.
msg_body = "%(body)s" % msg | ||
logging.info("Message sent was: " + msg_body) | ||
encoded_body = urllib.quote_plus(msg_body) | ||
svrResponse = requests.get("https://en.wikipedia.org/w/api.php?action=parse&prop=sections&format=json&page=" + encoded_body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python variables are snake_case
, never lowerCamelCase
.
# xmpp.ca_certs = "path/to/ca/cert" | ||
|
||
# start the app server and run it as a thread so that the XMPP server may also start | ||
threading.Thread(target=run_server).start() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer to have the web app start the xmpp server as a background thread, see here: http://stackoverflow.com/questions/14384739/how-can-i-add-a-background-thread-to-flask
client and [Flask](http://flask.pocoo.org/) to build a simple chatbot that can | ||
be run on [Google Compute Engine](https://cloud.google.com/compute/). The | ||
chatbot does two things: | ||
1. Sends messages to XMPP users via http get: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The formatting gets messed up here. You can see what it'll look like by clicking the "View" button in this review.
It should look like:
...chatbot does two things:
1. Sends messages to XMPP users via http get:
The server is running on port 5000.
If running on a virtual machine, use:
http://<MACHINE IP>:5000/send_message?recipient=<RECIPIENT ADDRESS>&message=<MSG>
If running locally, use:
http://localhost:5000/send_message?recipient=<RECIPIENT ADDRESS>&message=<MSG>
1. Responds to incoming ...
ie:
- You shouldn't indent the first line
- Subsequent lines shouldn't be indented so much. Otherwise, they become code blocks.
- If you want something on a new line, you have to separate it by a blank line.
- For numbered lists, the actual number you use doesn't matter ;)
- See here for a markdown tutorial
If running locally use: | ||
http://localhost:5000/send_message?recipient=<RECIPIENT ADDRESS>&message=<MSG> | ||
|
||
2. Responds to incoming messages with a wikipedia page on the topic: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
capital-W-Uncyclopedia
|
||
## Setup | ||
|
||
Follow the intstructions at the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instructions
[Compute Engine Quickstart Guide](https://cloud.google.com/compute/docs/quickstart-linux) | ||
on how to create a project, create a virtual machine, and connect to your | ||
instance via SSH. Once you have done this, you may jump to 'Installing files | ||
and dependencies' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...jump to '[Installing files and dependencies](#installing-files-and-dependencies)'
|
||
### Installing files and dependencies | ||
|
||
First, install the wikibot.py and requirements.txt files onto your remote |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`wikibot.py` ... `requirements.txt`
(ie surround with backticks, by convention)
First, install the wikibot.py and requirements.txt files onto your remote | ||
instance. See the guide on | ||
[Transferring Files](https://cloud.google.com/compute/docs/instances/transfer-files) | ||
for more information on how to do this using the Mac file browser, scp, or |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`scp`
|
||
### Running on your local machine | ||
|
||
You may also run the sample locally by simply copying wikibot.py to a project |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`wikibot.py`
def send_message(): | ||
try: | ||
recipient = request.args.get('recipient') | ||
message = request.args.get('message') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're using .get
, there should never be a KeyError
return "message sent to:" + recipient + " with body:" + message | ||
else: | ||
logging.info("chat client or recipient or message does not exist!") | ||
return "message failed to send" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably return an error code as well:
return 'message failed to send', 500
how it may be used. | ||
""" | ||
if msg['type'] in ('chat', 'normal'): | ||
msg_body = "%(body)s" % msg |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't you just do msg_body = msg['body']
? The string format seems gratuitous...
Made a few changes in response to comments. Jerjou@ could you PTAL? Thanks. |
Jerjou, Could you please take another look at this pull request? I've Thanks, On Thu, Oct 20, 2016 at 1:44 PM, Morgan Hallmon [email protected]
|
Can you move these files into |
I believe this has been superceded with #618 |
jerjou@ could you review this example? Feel free to move to new directory if necessary.