-
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
Conversation
Hello @dsharma522, Thanks for your pull request! A Core Committer will review your pull request soon. For code contributions, you can learn more about the review process here. Per the Mattermost Contribution Guide, we need to add you to the list of approved contributors for the Mattermost project. Please help complete the Mattermost contribution license agreement? This is a standard procedure for many open source projects. Please let us know if you have any questions. We are very happy to have you join our growing community! If you're not yet a member, please consider joining our Contributors community channel to meet other contributors and discuss new opportunities with the core team. |
|
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.
Thanks for this contribution @dsharma522 🎉 LGTM, though I have a request to make the PORT
and ROOT_URL
configurable via environment variables.
examples/python/hello-world.py
Outdated
APP_PORT = 8080 | ||
APP_HOST = 'localhost' | ||
ROOT_URL = 'http://localhost:8080' |
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.
Can this be made configurable using environment variables? The port and root URL specifically should be configurable. Please verify it works with an ngrok URL.
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.
Noted! will make it configurable and push commit.
Codecov Report
@@ Coverage Diff @@
## master #352 +/- ##
=======================================
Coverage 27.09% 27.09%
=======================================
Files 88 88
Lines 5155 5155
=======================================
Hits 1397 1397
Misses 3608 3608
Partials 150 150 Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
examples/python/README.md
Outdated
- have python installed , preferably `>=3.0` | ||
- install the requirements mentioned in the `requirement.txt` | ||
- make required changes in the `settings.json` to run the app on custom url & port |
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.
- have python installed , preferably `>=3.0` | |
- install the requirements mentioned in the `requirement.txt` | |
- make required changes in the `settings.json` to run the app on custom url & port | |
- Have python installed, preferably `>=3.0` | |
- Install the requirements mentioned in the `requirement.txt` | |
- Make required changes in the `settings.json` to run the app on custom url & port |
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.
Done
examples/python/settings.json
Outdated
{ | ||
"port": "8080", | ||
"root_url": "https://6fe6-103-157-160-168.in.ngrok.io", | ||
"host": "0.0.0.0" | ||
} |
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.
I think it's confusing how there are 3 sources of configuration (hardcode, settings file, and env vars). I'm thinking we should get rid of this file, as it's not something established among the examples already
examples/python/src/hello-world.py
Outdated
root_dir = os.path.dirname(os.path.dirname(__file__)) | ||
with open(f'{root_dir}/settings.json') as config_file: | ||
settings = json.load(config_file) | ||
for env_name, env_value in settings.items(): | ||
os.environ[env_name] = env_value |
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 environment variables should take precedence over the settings.json
file, rather than the settings.json
file editing the environment here. At the moment, the environment variables will not do anything unless we delete the settings.json
file.
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.
Removed settings.json
file and will use the configured environment variables. The app will use default ones in case not configured.
examples/python/src/hello-world.py
Outdated
def _subscribe_team_join(context: dict) -> None: | ||
site_url = context['mattermost_site_url'] | ||
bot_access_token = context['bot_access_token'] | ||
url = site_url + 'plugins/com.mattermost.apps/api/v1/subscribe' |
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.
Should we do path.join
here instead? Does the site URL always end in a slash?
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.
done.
examples/python/README.md
Outdated
@@ -0,0 +1,7 @@ | |||
### Pre-requisite | |||
- have python installed , preferably `>=3.0` | |||
- install the requirements mentioned in the `requirement.txt` |
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.
pip3 install -r requirements.txt
right? We should explain that here
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.
Done.
'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', | ||
}, |
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.
if you want to expand team & channel, ensure that bot is added to the team & channel
else command will fail to expand the context
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.
I am able to get the app
in the context without even adding the bot to the team/channel. And as expected expansion of team
, channel
failed as it is not the part of any team/channel currently.
Steps to reproduce
- install the app via
/apps install http <url>
(use the same app binding as in the current PR) - run the command
/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.
{
"path": "/first_command",
"expand": {
"app": "all"
},
"context": {
"app_id": "hello-world",
"location": "/command/first-command",
"user_agent": "webapp",
"track_as_submit": true,
"mattermost_site_url": "http://localhost:8066",
"developer_mode": true,
"app_path": "/plugins/com.mattermost.apps/apps/hello-world",
"bot_user_id": "3xxj6snbwby19npr1tca3ys9zo",
"bot_access_token": "3mf1popcwjnzxyeps9kqsqeuoc",
"app": {
"app_id": "hello-world",
"webhook_secret": "zrp5e37iejgtixpfqtjt43ibua",
"bot_user_id": "3xxj6snbwby19npr1tca3ys9zo",
"bot_username": "hello-world",
"remote_oauth2": {}
},
"acting_user": {
"id": "kredsjwqwbrp9r41c86b69akno",
"delete_at": 0,
"username": "",
"auth_service": "",
"email": "",
"nickname": "",
"first_name": "",
"last_name": "",
"position": "",
"roles": "",
"locale": "",
"timezone": null,
"disable_welcome_email": false
},
"oauth2": {}
},
"raw_command": "/first-command "
}
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.
examples/python/src/hello-world.py
Outdated
{ | ||
'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', |
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.
This should instead be in the same array that begins on line 73, rather than creating the new object on line 95
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.
fixed.
# 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. |
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 framework should make sure these subscriptions are removed when the bot is removed from this team cc @levb for discussion
examples/python/README.md
Outdated
export APP_PORT=8080 | ||
export ROOT_URL=http://localhost:8080 | ||
export APP_HOST=localhost |
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.
Some PaaS systems require you to use PORT
as the env variable for the port to listen on. Let's stick with PORT
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.
done
examples/python/src/hello-world.py
Outdated
app = Flask(__name__, static_url_path='/static', static_folder='./static') | ||
|
||
DEFAULT_APP_PORT = 8080 | ||
DEFAULT_APP_HOST = 'localhost' |
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.
For ease of configurability, maybe we make this 0.0.0.0
? Also let's make the env var be HOST
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.
done
examples/python/src/hello-world.py
Outdated
DEFAULT_APP_PORT = 8080 | ||
DEFAULT_APP_HOST = 'localhost' | ||
DEFAULT_ROOT_URL = 'http://localhost:8080' | ||
FORM = { |
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.
Not sure why this is capitalized. Even though it's only defined once, it's not really a "constant"
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.
replaced with lower case variable names
'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', | ||
}, |
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.
This PR has been automatically labelled "stale" because it hasn't had recent activity. |
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.
LGTM! Just a few more suggestions
examples/python/src/hello-world.py
Outdated
'bindings': [ | ||
{ | ||
'location': 'send', | ||
'label': 'send', | ||
'form': form | ||
}, |
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.
Can we have a comment explaining that this is how you make a subcommand /second-command send
? Mainly because there are already plenty of comments explaining things around this area, and this is an important concept to understand imo. Also maybe a comment mentioning that we are including the form here
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.
Also I know I said that form
shouldn't be capitalized, but maybe to make it stand out more, we can make it say SHARED_FORM
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.
Done
Would it be possible to improve the instructions so they include step-by-step guides for getting started? For example, link to python.org for install instructions, describe (or link to) how to install the dependencies in It would be best to include this information now, before merging, so it isn't missed in the future. |
Summary
Ticket Link
Github Issue - #351
This PR adds basic example of Hello-world app in Python language. It also includes the required requirements.txt file which will be needed.