Skip to content

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

Closed
wants to merge 9 commits into from

Conversation

mogar1980
Copy link
Contributor

jerjou@ could you review this example? Feel free to move to new directory if necessary.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Oct 20, 2016
@theacodes
Copy link
Contributor

@mogar1980 I can review. Since this runs on compute engine, can you put it in the compute_engine folder instead of the app engine one?

(Could this run on App Engine flex instead?)


"""Uncyclobot server example using SleekXMPP client library"""

import sys
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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:
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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()
Copy link
Contributor

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:
Copy link
Contributor

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:

  1. You shouldn't indent the first line
  2. Subsequent lines shouldn't be indented so much. Otherwise, they become code blocks.
  3. If you want something on a new line, you have to separate it by a blank line.
  4. For numbered lists, the actual number you use doesn't matter ;)
  5. 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:
Copy link
Contributor

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
Copy link
Contributor

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'
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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')
Copy link
Contributor

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"
Copy link
Contributor

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
Copy link
Contributor

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...

@mogar1980
Copy link
Contributor Author

Made a few changes in response to comments. Jerjou@ could you PTAL? Thanks.

@mogar1980
Copy link
Contributor Author

Jerjou, Could you please take another look at this pull request? I've
addressed the bulk of the comments from Jon and we need to have a basic
working example by tomorrow EOD at the latest. I'm happy to use this one as
a starting point and update if you guys feel strongly about things such as
moving to Flex, etc. Also, please feel free to move it to whatever
directory you like.

Thanks,
Morgan

On Thu, Oct 20, 2016 at 1:44 PM, Morgan Hallmon [email protected]
wrote:

Perfect! Thanks, Morgan

On Thursday, October 20, 2016, Jon Wayne Parrott [email protected]
wrote:

Yeah I should be able to swing that.

On Thu, Oct 20, 2016 at 1:23 PM Morgan Hallmon [email protected]
wrote:

If you can get the XMPP client to run under flex, then please do move
it. I was worried that there might be issues with the incoming network
path, and didn't want to spend too much time investigating. On that note,
the XMPP client won't respond properly to messages when running locally on
Google-A (learned that lesson the hard way :) ).

Timing is important here as well: we need to have it all done by next
Weds so that we can guarantee docs are 100% by Thurs, when I'd like to send
out the customer announcement.

Does that timing work for you?

Please let me know.

Thanks!
Morgan

On Thu, Oct 20, 2016 at 1:04 PM, Jon Wayne Parrott [email protected]
wrote:

I can take it over and likely finish it up in the next week or so. Do
you mind if I switch it to GAE Flex? It'll make setup for the users much
easier.

On Thu, Oct 20, 2016 at 1:02 PM Morgan Hallmon <
[email protected]> wrote:

Would one of you guys be able to take this over? I'm not sure that I
have time to address all your comments in time as I need to shift to
another big deliverable due next week. As there will be few users (max
~100) who actually try to migrate to an XMPP server, I'm less worried about
editorial control over this sample. Feel free to make whatever changes you
like as long as:

  1. The general functionality is retained (sending messages via GET
    and responding to incoming chats with a Uncyclopedia page on the subject)
  2. The setup in the README is easy to understand and follow.

Please let me know.

Thanks!
Morgan

On Thu, Oct 20, 2016 at 10:44 AM, Jerjou [email protected]
wrote:

@jerjou commented on this pull request.

In appengine/standard/xmpp_wikibot/README.md
#596 (review)
:

@@ -0,0 +1,62 @@
+# Uncyclobot example that can be run on Google Compute Engine
+
+This sample shows how to use the SleekXMPP
+client and Flask to build a simple chatbot that can
+be run on Google Compute Engine. The
+chatbot does two things:

    1. Sends messages to XMPP users via http get:

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://:5000/send_message?recipient=&message=

    If running locally, use:

    http://localhost:5000/send_message?recipient=&message=

  2. Responds to incoming ...

ie:

  1. You shouldn't indent the first line
  2. Subsequent lines shouldn't be indented so much. Otherwise,
    they become code blocks.
  3. If you want something on a new line, you have to separate it
    by a blank line.
  4. For numbered lists, the actual number you use doesn't matter ;)
  5. See here
    https://guides.github.com/features/mastering-markdown/ for a
    markdown tutorial

In appengine/standard/xmpp_wikibot/README.md
#596 (review)
:

@@ -0,0 +1,62 @@
+# Uncyclobot example that can be run on Google Compute Engine
+
+This sample shows how to use the SleekXMPP
+client and Flask to build a simple chatbot that can
+be run on Google Compute Engine. The
+chatbot does two things:

    1. Sends messages to XMPP users via http get:
  •   The server is running on port 5000
    
  •   if running on 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 messages with a wikipedia page on the topic:

capital-W-Uncyclopedia

In appengine/standard/xmpp_wikibot/README.md
#596 (review)
:

+chatbot does two things:

    1. Sends messages to XMPP users via http get:
  •   The server is running on port 5000
    
  •   if running on 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 messages with a wikipedia page on the topic:
  •   Send a message with a topic (e.g., 'Hawaii') to the XMPP account the server is using
    
  •   It should respond with a Uncyclopedia page (when one exists)
    
    +## Setup
    +
    +Follow the intstructions at the

instructions

In appengine/standard/xmpp_wikibot/README.md
#596 (review)
:

  •   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 messages with a wikipedia page on the topic:
  •   Send a message with a topic (e.g., 'Hawaii') to the XMPP account the server is using
    
  •   It should respond with a Uncyclopedia page (when one exists)
    
    +## Setup
    +
    +Follow the intstructions at the
    +Compute Engine Quickstart Guide
    +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'

...jump to 'Installing files and dependencies'


In appengine/standard/xmpp_wikibot/README.md
#596 (review)
:

+## Setup
+
+Follow the intstructions at the
+Compute Engine Quickstart Guide
+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'
+
+You should also download the Google Cloud SDK.
+It will allow you to access many of the features of Google Compute Engine via
+your local machine.
+
+
+### Installing files and dependencies
+
+First, install the wikibot.py and requirements.txt files onto your remote

wikibot.py ... requirements.txt

(ie surround with backticks, by convention)

In appengine/standard/xmpp_wikibot/README.md
#596 (review)
:

+Compute Engine Quickstart Guide
+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'
+
+You should also download the Google Cloud SDK.
+It will allow you to access many of the features of Google Compute Engine via
+your local machine.
+
+
+### Installing files and dependencies
+
+First, install the wikibot.py and requirements.txt files onto your remote
+instance. See the guide on
+Transferring Files
+for more information on how to do this using the Mac file browser, scp, or

scp


In appengine/standard/xmpp_wikibot/README.md
#596 (review)
:

+## Running the sample
+
+You'll need to have an XMPP account prior to actually running the sample.
+If you do not have one, you can easily create an account at one of the many
+XMPP servers such as Jabber.at.
+Once you have an account, run the following command:
+

  • python wikibot.py

+Then add the username (e.g., '[email protected]') and password for the account that
+you'd like to use for your chatbot.
+
+
+### Running on your local machine
+
+You may also run the sample locally by simply copying wikibot.py to a project

wikibot.py


In appengine/standard/xmpp_wikibot/wikibot.py
#596 (review)
:

+# ourselves to UTF-8.
+if sys.version_info < (3, 0):

  • from sleekxmpp.util.misc_ops import setdefaultencoding
  • setdefaultencoding('utf8')
    +else:
  • raw_input = input

+app = Flask(name)
+
+chat_client = None #this will be initialized when the wikibot is constructed
+
[email protected]('/send_message', methods=['GET'])
+def send_message():

  • try:
  •    recipient = request.args.get('recipient')
    
  •    message = request.args.get('message')
    

If you're using .get, there should never be a KeyError

http://werkzeug.pocoo.org/docs/0.11/datastructures/#werkzeug.datastructures.MultiDict.get

In appengine/standard/xmpp_wikibot/wikibot.py
#596 (review)
:

+chat_client = None #this will be initialized when the wikibot is constructed
+
[email protected]('/send_message', methods=['GET'])
+def send_message():

  • try:
  •    recipient = request.args.get('recipient')
    
  •    message = request.args.get('message')
    
  • except KeyError as e:
  •    logging.info("key error: {0}".format(e))
    
  • if chat_client and recipient and message:
  •    chat_client.send_message(mto=recipient, mbody=message)
    
  •    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"

Should probably return an error code as well:

return 'message failed to send', 500


In appengine/standard/xmpp_wikibot/wikibot.py
#596 (review)
:

  • def message(self, msg):
  •    """
    
  •    Process incoming message stanzas. Be aware that this also
    
  •    includes MUC messages and error messages. It is usually
    
  •    a good idea to check the messages's type before processing
    
  •    or sending replies. If the message is the appropriate type,
    
  •    then the bot checks wikipedia to see if the message string
    
  •    exists as a page on the site. If so, it sends this link back
    
  •    to the sender in the reply.
    
  •    Arguments:
    
  •        msg -- The received message stanza. See the SleekXMPP documentation
    
  •               for stanza objects and the Message stanza to see
    
  •               how it may be used.
    
  •    """
    
  •    if msg['type'] in ('chat', 'normal'):
    
  •        msg_body = "%(body)s" % msg
    

Couldn't you just do msg_body = msg['body']? The string format seems
gratuitous...


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
#596 (review),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AUBaDtK349qsW2OuyQGYvBABm3scWo3lks5q16iXgaJpZM4KbnJO
.

@jerjou
Copy link
Contributor

jerjou commented Oct 26, 2016

Can you move these files into compute/ rather than appengine/standard, since it's not an appengine app?

@jerjou
Copy link
Contributor

jerjou commented Nov 8, 2016

I believe this has been superceded with #618

@jerjou jerjou closed this Nov 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants