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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions examples/python/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
### Pre-requisite
- 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` with `pip3 install -r requirements.txt`
- Configure the following environment variables to run the app on custom port/url
```
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

```
- 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`
5 changes: 0 additions & 5 deletions examples/python/settings.json

This file was deleted.

27 changes: 7 additions & 20 deletions examples/python/src/hello-world.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import os
import json
from posixpath import join

import requests
from flask import Flask, request
Expand Down Expand Up @@ -51,7 +51,7 @@ def manifest() -> dict:
'/channel_header',
'/command'
],
'root_url': os.environ.get('root_url', DEFAULT_ROOT_URL),
'root_url': os.environ.get('ROOT_URL', DEFAULT_ROOT_URL),
}


Expand Down Expand Up @@ -89,14 +89,8 @@ def on_bindings() -> dict:
# 'channel': 'all',
},
},
}
],
},
{
# binding for a command, command with embedded form
'location': '/command',
'bindings': [
{
},
{ # command with embedded form
'description': 'test command',
'hint': '[This is testing command]',
# this will be the command displayed to user as /second-command
Expand Down Expand Up @@ -176,7 +170,7 @@ def on_bot_joined_team() -> dict:
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'
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 = {
Expand All @@ -197,16 +191,9 @@ def _subscribe_team_join(context: dict) -> None:


if __name__ == '__main__':
# load settings
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

app.run(
debug=True,
host=os.environ.get('host', DEFAULT_APP_HOST),
port=int(os.environ.get('port', DEFAULT_APP_PORT)),
host=os.environ.get('APP_HOST', DEFAULT_APP_HOST),
port=int(os.environ.get('APP_PORT', DEFAULT_APP_PORT)),
use_reloader=False,
)