-
Notifications
You must be signed in to change notification settings - Fork 29
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
Add example Apps in additional languages - Python #352
Changes from 6 commits
3b75b06
0be28a1
d73cd8a
28e2b62
3732dd0
8a54962
6a7efeb
d2bc7b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
### Pre-requisite | ||
- Have python installed, preferably `>=3.0` | ||
- Install the requirements mentioned in the `requirement.txt` with `pip3 install -r requirements.txt` | ||
- Configure the following environment variables to run the app on custom port/url | ||
``` | ||
export PORT=8080 | ||
export ROOT_URL=http://localhost:8080 | ||
export HOST=0.0.0.0 | ||
``` | ||
- To run with `ngrok` | ||
1. Start the ngrok server on 8080 port, `ngrok http 8080` | ||
2. Export the ngrok url. (replace the ngrok url) | ||
``` | ||
export ROOT_URL=https://4492-103-161-231-165.in.ngrok.io | ||
``` | ||
|
||
#### RUN the app | ||
* `python3 hello-world.py` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests | ||
flask |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import logging | ||
import os | ||
from posixpath import join | ||
|
||
import requests | ||
from flask import Flask, request | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
app = Flask(__name__, static_url_path='/static', static_folder='./static') | ||
|
||
default_port = 8080 | ||
default_host = 'localhost' | ||
default_root_url = 'http://localhost:8080' | ||
form = { | ||
'title': 'I am a form!', | ||
'icon': 'icon.png', | ||
'fields': [ | ||
{ | ||
'type': 'text', | ||
'name': 'message', | ||
'label': 'message', | ||
'position': 1, | ||
} | ||
], | ||
'submit': { | ||
'path': '/submit', | ||
}, | ||
} | ||
|
||
|
||
@app.route('/manifest.json') | ||
def manifest() -> dict: | ||
return { | ||
'app_id': 'hello-world', | ||
'display_name': 'Hello world app', | ||
'homepage_url': 'https://github.com/mattermost/mattermost-plugin-apps/tree/master/examples/python/hello-world', | ||
'app_type': 'http', | ||
'icon': 'icon.png', | ||
'requested_permissions': ['act_as_bot'], | ||
'on_install': { | ||
'path': '/install', | ||
'expand': { | ||
'app': 'all', | ||
}, | ||
}, | ||
'bindings': { | ||
'path': '/bindings', | ||
}, | ||
'requested_locations': [ | ||
'/channel_header', | ||
'/command' | ||
], | ||
'root_url': os.environ.get('ROOT_URL', default_root_url), | ||
} | ||
|
||
|
||
@app.route('/submit', methods=['POST']) | ||
def on_form_submit(): | ||
print(request.json) | ||
return {'type': 'ok', 'text': f'Hello form got submitted. Form data: {request.json["values"]}'} | ||
|
||
|
||
@app.route('/bindings', methods=['GET', 'POST']) | ||
def on_bindings() -> dict: | ||
print(f'bindings called with {request.data}') | ||
return { | ||
'type': 'ok', | ||
'data': [ | ||
{ | ||
# binding for a command | ||
'location': '/command', | ||
'bindings': [ | ||
{ | ||
'description': 'test command', | ||
'hint': '[This is testing command]', | ||
# this will be the command displayed to user as /first-command | ||
'label': 'first-command', | ||
'icon': 'icon.png', | ||
'submit': { | ||
'path': '/first_command', | ||
# expand block is optional. This is more of metadata like which channel, team this command | ||
# was called from | ||
'expand': { | ||
'app': 'all', | ||
# if you want to expand team & channel, ensure that bot is added to the team & channel | ||
# else command will fail to expand the context | ||
# 'team': 'all', | ||
# 'channel': 'all', | ||
}, | ||
}, | ||
}, | ||
{ # command with embedded form | ||
'description': 'test command', | ||
'hint': '[This is testing command]', | ||
# this will be the command displayed to user as /second-command | ||
'label': 'second-command', | ||
'icon': 'icon.png', | ||
'bindings': [ | ||
{ | ||
'location': 'send', | ||
'label': 'send', | ||
'form': form | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have a comment explaining that this is how you make a subcommand There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I know I said that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
], | ||
} | ||
], | ||
}, | ||
{ | ||
'location': '/channel_header', | ||
'bindings': [ | ||
{ | ||
'location': 'send-button', | ||
'icon': 'icon.png', | ||
'label': 'send hello message', | ||
'form': form, | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
|
||
|
||
@app.route('/ping', methods=['POST']) | ||
def on_ping() -> dict: | ||
logging.debug('ping...') | ||
return {'type': 'ok'} | ||
|
||
|
||
@app.route('/install', methods=['GET', 'POST']) | ||
def on_install() -> dict: | ||
print(f'on_install called with payload , {request.args}, {request.data}', flush=True) | ||
_subscribe_team_join(request.json['context']) | ||
return {'type': 'ok', 'data': []} | ||
|
||
|
||
@app.route('/first_command', methods=['POST']) | ||
def on_first_command(): | ||
print(f'/first_command called ') | ||
response_message = 'Hello! response from /first_command' | ||
return {'type': 'ok', 'text': response_message} | ||
|
||
|
||
@app.route('/bot_joined_team', methods=['GET', 'POST']) | ||
def on_bot_joined_team() -> dict: | ||
context = request.json['context'] | ||
logging.info( | ||
f'bot_joined_team event received for site:{context["mattermost_site_url"]}, ' | ||
f'team:{context["team"]["id"]} name:{context["team"]["name"]} ' | ||
f'{request.args} {request.data}' | ||
) | ||
# Here one can subscribe to channel_joined/left events as these required team_id now to be subscribed, | ||
# hence use the team_id received in the event and make a call for subscribing to channel_joined/left events. | ||
Comment on lines
+154
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The framework should make sure these subscriptions are removed when the bot is removed from this team cc @levb for discussion |
||
# Also supply {'team_id': team_id} in the request body of the subscription | ||
# { | ||
# 'subject': 'bot_joined_team', | ||
# 'call': { | ||
# 'path': '/bot_joined_team', | ||
# 'expand': { | ||
# 'app': 'all', | ||
# 'team': 'all' | ||
# } | ||
# }, | ||
# 'team_id': 'team_id' # get this team_id when bot_joined_team event occurs | ||
# } | ||
return {'type': 'ok', 'data': []} | ||
|
||
|
||
# Subscribing to events. For example, Subscribe to 'bot_joined_team' event | ||
def _subscribe_team_join(context: dict) -> None: | ||
site_url = context['mattermost_site_url'] | ||
bot_access_token = context['bot_access_token'] | ||
url = join(site_url, 'plugins/com.mattermost.apps/api/v1/subscribe') | ||
logging.info(f'Subscribing to team_join for {site_url}') | ||
headers = {'Authorization': f'BEARER {bot_access_token}'} | ||
body = { | ||
'subject': 'bot_joined_team', | ||
'call': { | ||
'path': '/bot_joined_team', | ||
'expand': { | ||
'app': 'all', | ||
'team': 'all' | ||
} | ||
}, | ||
} | ||
res = requests.post(url, headers=headers, json=body) | ||
if res.status_code != 200: | ||
logging.error(f'Could not subscribe to team_join event for {site_url}') | ||
else: | ||
logging.debug(f'subscribed to team_join event for {site_url}') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run( | ||
debug=True, | ||
host=os.environ.get('HOST', default_host), | ||
port=int(os.environ.get('PORT', default_port)), | ||
use_reloader=False, | ||
) |
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 bot must be a member of the team and channel, otherwise the expand fails and the request is never sent to the App? It seems that we shouldn't show the command to the user in this case. cc @levb
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.
@levb @mickmister ,
I am able to get the
app
in the context without even adding the bot to the team/channel. And as expected expansion ofteam
,channel
failed as it is not the part of any team/channel currently.Steps to reproduce
/apps install http <url>
(use the same app binding as in the current PR)/first-command
and press enter to submit. The command will succeed and won't show any error.Sharing raw response from
ngrok
for ref, if needed.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.
Okay so when you say "command will fail to expand the context", it's sort of a silent error, and the app still receives the request. It's just not the full request with all values. I think this is due to new changes to expand (optional vs required), depending on which version of the App framework you're working with.
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.
No actually "command will fail to expand the context" mean is that command will fail in the UI itself and not silent failure. And app won't receive the request.