contrib_bots: Restructure bots to follow a consistent structure.
Now all the bots that are stored in contrib_bots are in the same file/directory format. The format is specified here #3427. Add tests.py file for encrypt_bot as well. Fixes #3427.
|
@ -6,7 +6,7 @@ from os.path import expanduser
|
||||||
from six.moves import configparser as cp
|
from six.moves import configparser as cp
|
||||||
|
|
||||||
home = expanduser('~')
|
home = expanduser('~')
|
||||||
CONFIG_PATH = home + '/google-commute.ini'
|
CONFIG_PATH = home + '/commute_bot.config'
|
||||||
|
|
||||||
class CommuteHandler(object):
|
class CommuteHandler(object):
|
||||||
'''
|
'''
|
||||||
|
@ -89,10 +89,10 @@ class CommuteHandler(object):
|
||||||
|
|
||||||
# adds API Authentication Key to url request
|
# adds API Authentication Key to url request
|
||||||
def get_api_key(self):
|
def get_api_key(self):
|
||||||
# google-commute.ini must have been moved from
|
# commute_bot.config must have been moved from
|
||||||
# ~/zulip/contrib_bots/bots/commute_bot/CommuteBot/google-commute.ini into
|
# ~/zulip/contrib_bots/bots/commute_bot/commute_bot.config into
|
||||||
# /google-commute.ini for program to work
|
# /commute_bot.config for program to work
|
||||||
# see doc.md for more information
|
# see readme.md for more information
|
||||||
with open(CONFIG_PATH) as settings:
|
with open(CONFIG_PATH) as settings:
|
||||||
config = cp.ConfigParser()
|
config = cp.ConfigParser()
|
||||||
config.readfp(settings)
|
config.readfp(settings)
|
||||||
|
|
|
@ -11,24 +11,24 @@ units and information in various languages.
|
||||||
The bot will respond to the same stream input was in. And if called as
|
The bot will respond to the same stream input was in. And if called as
|
||||||
private message, the bot will reply with a private message.
|
private message, the bot will reply with a private message.
|
||||||
|
|
||||||
To setup the bot, you will first need to move google-commute.ini into
|
To setup the bot, you will first need to move commute_bot.config into
|
||||||
the user home directory and add an API key.
|
the user home directory and add an API key.
|
||||||
|
|
||||||
Move
|
Move
|
||||||
|
|
||||||
```
|
```
|
||||||
~/zulip/contrib_bots/bots/commute_bot/CommuteBot/google-commute.ini
|
~/zulip/contrib_bots/bots/commute_bot/commute_bot.config
|
||||||
```
|
```
|
||||||
|
|
||||||
into
|
into
|
||||||
|
|
||||||
```
|
```
|
||||||
~/google-commute.ini
|
~/commute_bot.config
|
||||||
```
|
```
|
||||||
|
|
||||||
To add an API key, please visit:
|
To add an API key, please visit:
|
||||||
https://developers.google.com/maps/documentation/distance-matrix/start
|
https://developers.google.com/maps/documentation/distance-matrix/start
|
||||||
to retrieve a key and copy your api key into google-commute.ini
|
to retrieve a key and copy your api key into commute_bot.config
|
||||||
|
|
||||||
Sample input and output:
|
Sample input and output:
|
||||||
|
|
|
@ -20,6 +20,7 @@ def test():
|
||||||
expected: %s
|
expected: %s
|
||||||
but got : %s
|
but got : %s
|
||||||
''' % (cmd, expected_response, client_dummy.output))
|
''' % (cmd, expected_response, client_dummy.output))
|
||||||
|
|
||||||
def sample_conversation():
|
def sample_conversation():
|
||||||
return [
|
return [
|
||||||
('@convert 2 m cm', '2.0 m = 200.0 cm\n'),
|
('@convert 2 m cm', '2.0 m = 200.0 cm\n'),
|
||||||
|
|
Before Width: | Height: | Size: 73 KiB After Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 287 KiB After Width: | Height: | Size: 287 KiB |
Before Width: | Height: | Size: 180 KiB After Width: | Height: | Size: 180 KiB |
Before Width: | Height: | Size: 160 KiB After Width: | Height: | Size: 160 KiB |
Before Width: | Height: | Size: 170 KiB After Width: | Height: | Size: 170 KiB |
33
contrib_bots/bots/encrypt_bot/tests.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import encrypt_bot
|
||||||
|
|
||||||
|
def test():
|
||||||
|
for cmd, expected_response in sample_conversation():
|
||||||
|
message = {'content': cmd, 'subject': 'foo',
|
||||||
|
'display_recipient': 'bar'}
|
||||||
|
|
||||||
|
class ClientDummy(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.output = ''
|
||||||
|
|
||||||
|
def send_message(self, params):
|
||||||
|
self.output = params['content']
|
||||||
|
handler = encrypt_bot.EncryptHandler()
|
||||||
|
client_dummy = ClientDummy()
|
||||||
|
handler.handle_message(message, client_dummy, '')
|
||||||
|
if client_dummy.output != expected_response:
|
||||||
|
raise AssertionError('''
|
||||||
|
cmd: %s
|
||||||
|
expected: %s
|
||||||
|
but got : %s
|
||||||
|
''' % (cmd, expected_response, client_dummy.output))
|
||||||
|
|
||||||
|
def sample_conversation():
|
||||||
|
return [
|
||||||
|
('@encrypt Please encrypt this', 'Encrypted/Decrypted text: Cyrnfr rapelcg guvf'),
|
||||||
|
('@encrypt Let\'s Do It', 'Encrypted/Decrypted text: Yrg\'f Qb Vg'),
|
||||||
|
('@encrypt ', 'Encrypted/Decrypted text: '),
|
||||||
|
('@encrypt me&mom together..!!', 'Encrypted/Decrypted text: zr&zbz gbtrgure..!!'),
|
||||||
|
]
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test()
|
|
@ -9,13 +9,13 @@ from six.moves import configparser as cp
|
||||||
from six.moves import range
|
from six.moves import range
|
||||||
|
|
||||||
home = expanduser('~')
|
home = expanduser('~')
|
||||||
CONFIG_PATH = home + '/zulip/contrib_bots/bots/foursquare/FourSquareBot/settings.ini'
|
CONFIG_PATH = home + '/zulip/contrib_bots/bots/foursquare/foursquare.config'
|
||||||
|
|
||||||
def get_api_key():
|
def get_api_key():
|
||||||
# settings.ini must have been moved from
|
# foursquare.config must have been moved from
|
||||||
# ~/zulip/contrib_bots/bots/foursquare/FourSquareBot/settings.ini into
|
# ~/zulip/contrib_bots/bots/foursquare/foursquare.config into
|
||||||
# ~/settings.ini for program to work
|
# ~/foursquare.config for program to work
|
||||||
# see doc.md for more information
|
# see readme.md for more information
|
||||||
with open(CONFIG_PATH) as settings:
|
with open(CONFIG_PATH) as settings:
|
||||||
config = cp.ConfigParser()
|
config = cp.ConfigParser()
|
||||||
config.readfp(settings)
|
config.readfp(settings)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# See readme-github-comment-bot.md for instructions on running this code.
|
# See readme.md for instructions on running this code.
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
from . import github
|
from . import github
|
||||||
|
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 56 KiB |
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 56 KiB |
Before Width: | Height: | Size: 9 KiB After Width: | Height: | Size: 9 KiB |
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
|
@ -23,24 +23,24 @@ answer will be formatted differently depending the chosen command.
|
||||||
|
|
||||||
Question -> `@howdowe use supervisor in elixir`
|
Question -> `@howdowe use supervisor in elixir`
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Answer -> Howdoi would try to **only** respond with the coding section
|
Answer -> Howdoi would try to **only** respond with the coding section
|
||||||
of the answer.
|
of the answer.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
#### Example 2
|
#### Example 2
|
||||||
|
|
||||||
Question -> `@howdoi! stack vs heap`
|
Question -> `@howdoi! stack vs heap`
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Answer -> Howdoi would return the **full** stackoverflow answer via
|
Answer -> Howdoi would return the **full** stackoverflow answer via
|
||||||
**private message** to the original sender. The URL of the answer can be
|
**private message** to the original sender. The URL of the answer can be
|
||||||
seen at the bottom of the message.
|
seen at the bottom of the message.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Note:**
|
**Note:**
|
||||||
|
|
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 83 KiB |
Before Width: | Height: | Size: 49 KiB After Width: | Height: | Size: 49 KiB |
Before Width: | Height: | Size: 64 KiB After Width: | Height: | Size: 64 KiB |
|
@ -15,10 +15,10 @@ CONTRIB_BOTS_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
os.chdir(os.path.dirname(CONTRIB_BOTS_DIR))
|
os.chdir(os.path.dirname(CONTRIB_BOTS_DIR))
|
||||||
sys.path.insert(0, os.path.dirname(CONTRIB_BOTS_DIR))
|
sys.path.insert(0, os.path.dirname(CONTRIB_BOTS_DIR))
|
||||||
|
|
||||||
JOKES_PATH = os.path.join(CONTRIB_BOTS_DIR, 'John/var/jokes.json')
|
JOKES_PATH = os.path.join(CONTRIB_BOTS_DIR, 'assets/var/jokes.json')
|
||||||
DATABASE_PATH = os.path.join(CONTRIB_BOTS_DIR, 'John/var/database.db')
|
DATABASE_PATH = os.path.join(CONTRIB_BOTS_DIR, 'assets/var/database.db')
|
||||||
DIRECTORY_PATH = os.path.join(CONTRIB_BOTS_DIR, 'John')
|
DIRECTORY_PATH = os.path.join(CONTRIB_BOTS_DIR, 'assets')
|
||||||
VAR_PATH = os.path.join(CONTRIB_BOTS_DIR, 'John/var')
|
VAR_PATH = os.path.join(CONTRIB_BOTS_DIR, 'assets/var')
|
||||||
|
|
||||||
if not os.path.exists(DIRECTORY_PATH):
|
if not os.path.exists(DIRECTORY_PATH):
|
||||||
os.makedirs(DIRECTORY_PATH)
|
os.makedirs(DIRECTORY_PATH)
|
||||||
|
|
|
@ -10,7 +10,7 @@ interactive bot that uses machine learning heuristics to simulate a
|
||||||
conversation with the user. He has a great sense of humor and
|
conversation with the user. He has a great sense of humor and
|
||||||
is also powered by Open Source code!
|
is also powered by Open Source code!
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
How it works?
|
How it works?
|
||||||
John is initially trained with Corpus files, or large text files.
|
John is initially trained with Corpus files, or large text files.
|
||||||
|
@ -20,11 +20,11 @@ try to find the response that best matches the input according to the Levenshtei
|
||||||
which is a string metric for measuring the difference between two sequences. If several
|
which is a string metric for measuring the difference between two sequences. If several
|
||||||
responses have the same acurracy, he will choose one at random.
|
responses have the same acurracy, he will choose one at random.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Can he learn by himself?
|
Can he learn by himself?
|
||||||
John's engine allows him to learn from his conversations with people. However,
|
John's engine allows him to learn from his conversations with people. However,
|
||||||
without strict supervision bots that learn from people can do harm, so learning
|
without strict supervision bots that learn from people can do harm, so learning
|
||||||
is currently restricted to his initial corpus.
|
is currently restricted to his initial corpus.
|
||||||
|
|
||||||

|

|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
Before Width: | Height: | Size: 43 KiB After Width: | Height: | Size: 43 KiB |
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
@ -12,29 +12,29 @@ xkcd bot has four commands:
|
||||||
1. `help`
|
1. `help`
|
||||||
This command is used to list all commands that can be used with this bot.
|
This command is used to list all commands that can be used with this bot.
|
||||||
You can use this command by typing `@xkcd help` in a stream.
|
You can use this command by typing `@xkcd help` in a stream.
|
||||||

|

|
||||||
|
|
||||||
2. `latest`
|
2. `latest`
|
||||||
This command is used to fetch the latest comic strip from xkcd. You can use
|
This command is used to fetch the latest comic strip from xkcd. You can use
|
||||||
this command by typing `@xkcd latest` in a stream.
|
this command by typing `@xkcd latest` in a stream.
|
||||||

|

|
||||||
|
|
||||||
3. `random`
|
3. `random`
|
||||||
This command is used to fetch a random comic strip from xkcd. You can use
|
This command is used to fetch a random comic strip from xkcd. You can use
|
||||||
this command by typing `@xkcd random` in a stream, xkcd bot will post a
|
this command by typing `@xkcd random` in a stream, xkcd bot will post a
|
||||||
random xkcd comic strip.
|
random xkcd comic strip.
|
||||||

|

|
||||||
|
|
||||||
4. `<comic_id>`
|
4. `<comic_id>`
|
||||||
To fetch a comic strip based on id, you can directly use `@xkcd <comic_id>`,
|
To fetch a comic strip based on id, you can directly use `@xkcd <comic_id>`,
|
||||||
for example if you want to fetch a comic strip with id 1234, you can type
|
for example if you want to fetch a comic strip with id 1234, you can type
|
||||||
`@xkcd 1234`, xkcd bot will post a comic strip with id 1234.
|
`@xkcd 1234`, xkcd bot will post a comic strip with id 1234.
|
||||||

|

|
||||||
|
|
||||||
If you type a wrong command to xkcd bot, xkcd bot will post information
|
If you type a wrong command to xkcd bot, xkcd bot will post information
|
||||||
you'd get from `@xkcd help`.
|
you'd get from `@xkcd help`.
|
||||||

|

|
||||||
|
|
||||||
And if you type a wrong id, xkcd bot will post a message that an xkcd comic
|
And if you type a wrong id, xkcd bot will post a message that an xkcd comic
|
||||||
strip with that id is not available.
|
strip with that id is not available.
|
||||||

|

|
Before Width: | Height: | Size: 229 KiB After Width: | Height: | Size: 229 KiB |
|
@ -17,7 +17,7 @@ Go to this link:
|
||||||
This is the API that powers the `yoda_bot`. You can read more about it
|
This is the API that powers the `yoda_bot`. You can read more about it
|
||||||
on this page.
|
on this page.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Click on the **Sign Up Free** button at the top and create
|
Click on the **Sign Up Free** button at the top and create
|
||||||
an account. Then click on the **Documentation** tab. Scroll down to the
|
an account. Then click on the **Documentation** tab. Scroll down to the
|
||||||
|
@ -29,9 +29,9 @@ the Yoda Speak API to. Click on the blue **GET THE KEYS** button.
|
||||||
|
|
||||||
On the pop-up that comes up, click on the **COPY** button.
|
On the pop-up that comes up, click on the **COPY** button.
|
||||||
This is your Mashape API Key. It is used
|
This is your Mashape API Key. It is used
|
||||||
to authenticate. Store it in the `yoda_api_key.txt` file.
|
to authenticate. Store it in the `yoda_bot.config` file.
|
||||||
|
|
||||||
The `yoda_api_key.txt` file should be located at `~/yoda_api_key.txt`.
|
The `yoda_bot.config` file should be located at `~/yoda_bot.config`.
|
||||||
|
|
||||||
Example input:
|
Example input:
|
||||||
|
|
0
contrib_bots/bots/yoda_bot/yoda_bot.config
Normal file
|
@ -1,4 +1,4 @@
|
||||||
# See readme-yoda-bot.md for instructions on running this code.
|
# See readme.md for instructions on running this code.
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import os
|
import os
|
||||||
|
@ -17,9 +17,9 @@ HELP_MESSAGE = '''
|
||||||
Users should preface messages with '@yoda'.
|
Users should preface messages with '@yoda'.
|
||||||
|
|
||||||
Before running this, make sure to get a Mashape Api token.
|
Before running this, make sure to get a Mashape Api token.
|
||||||
Instructions are in the 'readme-yoda-bot.md' file.
|
Instructions are in the 'readme.md' file.
|
||||||
Store it in the 'yoda_api_key.txt' file.
|
Store it in the 'yoda_bot.config' file.
|
||||||
The 'yoda_api_key.txt' file should be located at '~/yoda_api_key.txt'.
|
The 'yoda_bot.config' file should be located at '~/yoda_bot.config'.
|
||||||
Example input:
|
Example input:
|
||||||
@yoda You will learn how to speak like me someday.
|
@yoda You will learn how to speak like me someday.
|
||||||
'''
|
'''
|
||||||
|
@ -42,9 +42,9 @@ class YodaSpeakHandler(object):
|
||||||
Users should preface messages with '@yoda'.
|
Users should preface messages with '@yoda'.
|
||||||
|
|
||||||
Before running this, make sure to get a Mashape Api token.
|
Before running this, make sure to get a Mashape Api token.
|
||||||
Instructions are in the 'readme-yoda-bot.md' file.
|
Instructions are in the 'readme.md' file.
|
||||||
Store it in the 'yoda_api_key.txt' file.
|
Store it in the 'yoda_bot.config' file.
|
||||||
The 'yoda_api_key.txt' file should be located at '~/yoda_api_key.txt'.
|
The 'yoda_bot.config' file should be located at '~/yoda_bot.config'.
|
||||||
Example input:
|
Example input:
|
||||||
@yoda You will learn how to speak like me someday.
|
@yoda You will learn how to speak like me someday.
|
||||||
'''
|
'''
|
||||||
|
@ -85,7 +85,7 @@ def send_to_yoda_api(sentence, api_key):
|
||||||
logging.error(error_message)
|
logging.error(error_message)
|
||||||
error_code = response.status_code
|
error_code = response.status_code
|
||||||
error_message = error_message + 'Error code: ' + error_code +\
|
error_message = error_message + 'Error code: ' + error_code +\
|
||||||
' Did you follow the instructions in the `readme-yoda-bot.md` file?'
|
' Did you follow the instructions in the `readme.md` file?'
|
||||||
return error_message
|
return error_message
|
||||||
|
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ def handle_input(client, original_content, stream, subject):
|
||||||
|
|
||||||
except ApiKeyError:
|
except ApiKeyError:
|
||||||
reply_message = 'Invalid Api Key. Did you follow the instructions in the ' \
|
reply_message = 'Invalid Api Key. Did you follow the instructions in the ' \
|
||||||
'`readme-yoda-bot.md` file?'
|
'`readme.md` file?'
|
||||||
logging.error(reply_message)
|
logging.error(reply_message)
|
||||||
|
|
||||||
send_message(client, reply_message, stream, subject)
|
send_message(client, reply_message, stream, subject)
|
||||||
|
@ -124,7 +124,7 @@ def handle_input(client, original_content, stream, subject):
|
||||||
def get_api_key():
|
def get_api_key():
|
||||||
# function for getting Mashape api key
|
# function for getting Mashape api key
|
||||||
home = os.path.expanduser('~')
|
home = os.path.expanduser('~')
|
||||||
with open(home + '/yoda_api_key.txt') as api_key_file:
|
with open(home + '/yoda_bot.config') as api_key_file:
|
||||||
api_key = api_key_file.read().strip()
|
api_key = api_key_file.read().strip()
|
||||||
return api_key
|
return api_key
|
||||||
|
|
||||||
|
|