Skip to content
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
merged 8 commits into from
Aug 22, 2022
Merged

Add example Apps in additional languages - Python #352

merged 8 commits into from
Aug 22, 2022

Conversation

dsharma522
Copy link
Contributor

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.

@mattermod
Copy link
Contributor

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?
Once you have signed the CLA, please comment with /check-cla and confirm that the CLA check is green.

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.

@dsharma522
Copy link
Contributor Author

dsharma522 commented Jul 22, 2022

/check-cla

@mickmister mickmister added 2: Dev Review Requires review by a core committer 3: QA Review Requires review by a QA tester labels Jul 22, 2022
Copy link
Contributor

@mickmister mickmister left a 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.

Comment on lines 9 to 11
APP_PORT = 8080
APP_HOST = 'localhost'
ROOT_URL = 'http://localhost:8080'
Copy link
Contributor

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.

Copy link
Contributor Author

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-commenter
Copy link

codecov-commenter commented Jul 23, 2022

Codecov Report

Merging #352 (d2bc7b7) into master (dbc7a94) will not change coverage.
The diff coverage is n/a.

@@           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.

@dsharma522 dsharma522 requested a review from mickmister July 23, 2022 07:04
Comment on lines 2 to 4
- 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 1 to 5
{
"port": "8080",
"root_url": "https://6fe6-103-157-160-168.in.ngrok.io",
"host": "0.0.0.0"
}
Copy link
Contributor

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

Comment on lines 201 to 205
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
Copy link
Contributor

@mickmister mickmister Jul 23, 2022

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.

Copy link
Contributor Author

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.

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'
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@@ -0,0 +1,7 @@
### Pre-requisite
- have python installed , preferably `>=3.0`
- install the requirements mentioned in the `requirement.txt`
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines +84 to +90
'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',
},
Copy link
Contributor

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

Copy link
Contributor Author

@dsharma522 dsharma522 Jul 23, 2022

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 of team , channel failed as it is not the part of any team/channel currently.


Steps to reproduce

  1. install the app via /apps install http <url> (use the same app binding as in the current PR)
  2. 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 "
}

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Screenshot 2022-07-25 at 10 34 52 PM

Comment on lines 99 to 104
{
'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',
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

Comment on lines +158 to +159
# 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.
Copy link
Contributor

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

@dsharma522 dsharma522 requested a review from mickmister July 23, 2022 19:35
Comment on lines 6 to 8
export APP_PORT=8080
export ROOT_URL=http://localhost:8080
export APP_HOST=localhost
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

app = Flask(__name__, static_url_path='/static', static_folder='./static')

DEFAULT_APP_PORT = 8080
DEFAULT_APP_HOST = 'localhost'
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

DEFAULT_APP_PORT = 8080
DEFAULT_APP_HOST = 'localhost'
DEFAULT_ROOT_URL = 'http://localhost:8080'
FORM = {
Copy link
Contributor

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"

Copy link
Contributor Author

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

Comment on lines +84 to +90
'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',
},
Copy link
Contributor

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.

@dsharma522 dsharma522 requested a review from mickmister July 25, 2022 17:19
@mattermod
Copy link
Contributor

This PR has been automatically labelled "stale" because it hasn't had recent activity.
A core team member will check in on the status of the PR to help with questions.
Thank you for your contribution!

Copy link
Contributor

@mickmister mickmister left a 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

Comment on lines 99 to 104
'bindings': [
{
'location': 'send',
'label': 'send',
'form': form
},
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@neflyte
Copy link

neflyte commented Aug 15, 2022

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 requirements.txt (or some other dependency manager like pipenv), what changes are necessary in settings.json.

It would be best to include this information now, before merging, so it isn't missed in the future.

@dsharma522 dsharma522 closed this by deleting the head repository Aug 22, 2022
@dsharma522 dsharma522 reopened this Aug 22, 2022
@mickmister mickmister merged commit 5d18631 into mattermost:master Aug 22, 2022
@hanzei
Copy link
Contributor

hanzei commented Jan 31, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2: Dev Review Requires review by a core committer 3: QA Review Requires review by a QA tester Docs/Done Required documentation has been written
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants