2016-12-24 09:35:56 -05:00
|
|
|
"""
|
|
|
|
This bot uses the python library `howdoi` which is not a
|
|
|
|
dependency of Zulip. To use this module, you will have to
|
|
|
|
install it in your local machine. In your terminal, enter
|
|
|
|
the following command:
|
|
|
|
|
|
|
|
$ sudo pip install howdoi --upgrade
|
|
|
|
|
|
|
|
Note:
|
|
|
|
* You might have to use `pip3` if you are using python 3.
|
|
|
|
* The install command would also download any dependency
|
|
|
|
required by `howdoi`.
|
|
|
|
"""
|
|
|
|
|
2017-05-26 13:15:53 -04:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-12-24 09:35:56 -05:00
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
from textwrap import fill
|
|
|
|
try:
|
|
|
|
from howdoi.howdoi import howdoi
|
|
|
|
except ImportError:
|
|
|
|
logging.error("Dependency missing!!\n%s" % (__doc__))
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
class HowdoiHandler(object):
|
|
|
|
'''
|
|
|
|
This plugin facilitates searching Stack Overflow for
|
|
|
|
techanical answers based on the Python library `howdoi`.
|
|
|
|
To get the best possible answer, only include keywords
|
|
|
|
in your questions.
|
|
|
|
|
|
|
|
There are two possible commands:
|
2017-02-16 16:56:32 -05:00
|
|
|
* @mention-bot howdowe > This would return the answer to the same
|
2016-12-24 09:35:56 -05:00
|
|
|
stream that it was called from.
|
|
|
|
|
2017-02-16 16:56:32 -05:00
|
|
|
* @mention-bot howdoi > The bot would send a private message to the
|
2016-12-24 09:35:56 -05:00
|
|
|
user containing the answer.
|
|
|
|
|
|
|
|
By default, howdoi only returns the coding section of the
|
|
|
|
first search result if possible, to see the full answer
|
|
|
|
from Stack Overflow, append a '!' to the commands.
|
2017-02-16 16:56:32 -05:00
|
|
|
(ie '@mention-bot howdoi!', '@mention-bot howdowe!')
|
2016-12-24 09:35:56 -05:00
|
|
|
'''
|
|
|
|
|
|
|
|
MAX_LINE_LENGTH = 85
|
|
|
|
|
|
|
|
def usage(self):
|
|
|
|
return '''
|
|
|
|
This plugin will allow users to get techanical
|
|
|
|
answers from Stackoverflow. Users should preface
|
|
|
|
their questions with one of the following:
|
|
|
|
|
2017-02-16 16:56:32 -05:00
|
|
|
* @mention-bot howdowe > Answer to the same stream
|
|
|
|
* @mention-bot howdoi > Answer via private message
|
2016-12-24 09:35:56 -05:00
|
|
|
|
2017-02-16 16:56:32 -05:00
|
|
|
* @mention-bot howdowe! OR @mention-bot howdoi! > Full answer from SO
|
2016-12-24 09:35:56 -05:00
|
|
|
'''
|
|
|
|
|
|
|
|
def line_wrap(self, string, length):
|
|
|
|
lines = string.split("\n")
|
|
|
|
|
|
|
|
wrapped = [(fill(line) if len(line) > length else line)
|
|
|
|
for line in lines]
|
|
|
|
|
|
|
|
return "\n".join(wrapped).strip()
|
|
|
|
|
|
|
|
def get_answer(self, command, query):
|
|
|
|
question = query[len(command):].strip()
|
|
|
|
result = howdoi(dict(
|
|
|
|
query=question,
|
|
|
|
num_answers=1,
|
|
|
|
pos=1,
|
|
|
|
all=command[-1] == '!',
|
|
|
|
color=False
|
|
|
|
))
|
|
|
|
_answer = self.line_wrap(result, HowdoiHandler.MAX_LINE_LENGTH)
|
|
|
|
|
|
|
|
answer = "Answer to '%s':\n```\n%s\n```" % (question, _answer)
|
|
|
|
|
|
|
|
return answer
|
|
|
|
|
|
|
|
def handle_message(self, message, client, state_handler):
|
2017-02-16 16:56:32 -05:00
|
|
|
question = message['content'].strip()
|
2016-12-24 09:35:56 -05:00
|
|
|
|
2017-02-16 16:56:32 -05:00
|
|
|
if question.startswith('howdowe!'):
|
2016-12-24 09:35:56 -05:00
|
|
|
client.send_message(dict(
|
|
|
|
type='stream',
|
|
|
|
to=message['display_recipient'],
|
|
|
|
subject=message['subject'],
|
2017-02-16 16:56:32 -05:00
|
|
|
content=self.get_answer('howdowe!', question)
|
2016-12-24 09:35:56 -05:00
|
|
|
))
|
|
|
|
|
2017-02-16 16:56:32 -05:00
|
|
|
elif question.startswith('howdoi!'):
|
2016-12-24 09:35:56 -05:00
|
|
|
client.send_message(dict(
|
|
|
|
type='private',
|
|
|
|
to=message['sender_email'],
|
2017-02-16 16:56:32 -05:00
|
|
|
content=self.get_answer('howdoi!', question)
|
2016-12-24 09:35:56 -05:00
|
|
|
))
|
|
|
|
|
2017-02-16 16:56:32 -05:00
|
|
|
elif question.startswith('howdowe'):
|
2016-12-24 09:35:56 -05:00
|
|
|
client.send_message(dict(
|
|
|
|
type='stream',
|
|
|
|
to=message['display_recipient'],
|
|
|
|
subject=message['subject'],
|
2017-02-16 16:56:32 -05:00
|
|
|
content=self.get_answer('howdowe', question)
|
2016-12-24 09:35:56 -05:00
|
|
|
))
|
|
|
|
|
2017-02-16 16:56:32 -05:00
|
|
|
elif question.startswith('howdoi'):
|
2016-12-24 09:35:56 -05:00
|
|
|
client.send_message(dict(
|
|
|
|
type='private',
|
|
|
|
to=message['sender_email'],
|
2017-02-16 16:56:32 -05:00
|
|
|
content=self.get_answer('howdoi', question)
|
2016-12-24 09:35:56 -05:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
handler_class = HowdoiHandler
|