generated from mattermost/mattermost-plugin-starter-template
-
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
Merged
+223
−0
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3b75b06
Add example Apps in additional languages - Python
dsharma522 0be28a1
more examples + configurable environments
dsharma522 d73cd8a
minor change
dsharma522 28e2b62
removed extra file
dsharma522 3732dd0
comments
dsharma522 8a54962
comments
dsharma522 6a7efeb
comments
dsharma522 d2bc7b7
Merge branch 'master' into master
mickmister File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
### Pre-requisite | ||
1. Have [python installed](https://www.python.org/downloads/), preferably `>=3.0` | ||
2. Change working directory to `~/mattermost-plugin-apps/examples/python` | ||
3. Install the requirements mentioned in the `requirement.txt` with `pip3 install -r requirements.txt` | ||
3. 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](https://ngrok.com/download) | ||
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 | ||
1. Change directory to `~/mattermost-plugin-apps/examples/python/src`. | ||
2. Run the app via `python3 hello-world.py` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests | ||
flask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
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' | ||
SHARED_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': [ | ||
{ | ||
# sub-command `send` to send an embedded form here as input to the command. | ||
# E.g. /second-command send "hello-form" | ||
'location': 'send', | ||
'label': 'send', | ||
'form': SHARED_FORM | ||
}, | ||
], | ||
} | ||
], | ||
}, | ||
{ | ||
'location': '/channel_header', | ||
'bindings': [ | ||
{ | ||
'location': 'send-button', | ||
'icon': 'icon.png', | ||
'label': 'send hello message', | ||
'form': SHARED_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, | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.