# GEC1506 Final Project Note
[repo - Github]([https://github.com/tangerine1202/gec1506_final](https://github.com/tangerine1202/gec1506_final)
)
[API Spec](https://alanhuang.notion.site/GEC1506-final-3dae6479513d4a189cb048e6ae54c622)
## Git
```bash
git add <file_name>
git commit -m "<msg>"
git push
git pull
```
## Python
### Mac
```
pip3 install virtualenv
```
### Windows (using Conda)
```
conda install flask
pip install line-bot-sdk
```
### virtualenv
- (mac) install brew
```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
- install python3
```bash
brew install python3
```
- install virtualenv
```bash
python3 -m pip install --upgrade pip
python3 -m virtualenv --python=3.8 venv
. venv/bin/activate
pip install flask line-bot-sdk
```
## other tools
- [vscode](https://code.visualstudio.com/)
- [ngrok](https://ngrok.com/download)
- remember to add authtoekn
## Flask
- edit `app.py`
```py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, world'
if __name__ == '__main__':
port = 8000
app.run(host, port)
```
- run `python3 app.py`
- open `127.0.0.1:8000`, you should see "Hello, world".
## LINE
### LINE account
- create LINE Message API Channel, according to [LINE Developers Console overview](https://developers.line.biz/en/docs/line-developers-console/overview/)
- login with your LINE account
- create a Provider
- create a Message API Channel
- after creating Channel, your should see sth like the img below

### Connect Flask server to LINE
- edit `app.py` as the example on [line-bot-sdk-python](https://github.com/line/line-bot-sdk-python)
- change the `channel access token` and `channel secret` in `app.py`
- Open LINE Developers Console
- copy the `channel secret` from `Basic settings` > `Channel secret` and paste to `app.py`
- copy the `channel access token` from `Message API` > `Channel Access Token` and paste to `app.py`
- start flask server and ngrok
```bash
python3 app.py
ngrok http 8000
```
- copy link in ngork terminal panel > `Forwarding` > `https://...ngrok.io`
- paste `{link}/callback` to LINE Developer Console, at `Message API` > `Webhook URL`
- under `Webhook URL`, enable `Use webhook`, `Error statistics aggregation`
- click `Verify` to verify the webook URL
- Try it out! (add line bot from `Message API` > `QR code`)
## dotenv
- [python-dotenv - Github](https://github.com/theskumar/python-dotenv)
- install python-dotenv package
- create and edit `.env`
```
LINE_ACCESS_TOKEN=
LINE_SECRET=
AIRTABLE_API_KEY=
AIRTABLE_BASE_ID=
GOOGLE_PLACE_API_KEY=
```
- edit `dotenv_example.py`
```python
import os
from dotenv import load_dotenv
# load env variables from .env file
load_dotenv()
keys = ['LINE_SECRET', 'LINE_ACCESS_TOKEN']
for key in keys:
# get the env variables
val = os.getenv(key)
print(f'{key}: {val}')
```
### Try it!
- Change `app.py` such that it reads LINE Access Token and LINE Secret from `.env` file
## Airtable API
- [Airtable](https://airtable.com/)
- [Airtable - REST API](https://airtable.com/api)
- [pyAirtable - package](https://pyairtable.readthedocs.io/en/latest/getting-started.html)
### Setup Airtable
- sign up an account ([referral link!](https://airtable.com/invite/r/Ycg6Eyuq))
- add a base
- change the table name from `Table 1` to `Places`
- change columns to following names and field types
- `place_id` (single line text)
- `name` (single line text)
- `address` (single line text)
- add some dummy data

### pyAirtable
Airtable identifies your table by the **base id** and the **table name**.
Airtable use **API key** to authenticate you and your privileges manipulate the table.
You can find the **base id** at [Airtable API](https://airtable.com/api), **API key** at [Airtable account](https://airtable.com/account), and the **table name** should be `Places` as assgined above.
### Try pyAirtable API!
- find your Airtable **API key**, **base id**, **table name**
- put them into your `.env` file
- install pyAirtable package
- create `airtable.py`
- edit `airtable.py` so that you can call a function `getAll()` to get all records from the table
- [offcial sample code here](https://pyairtable.readthedocs.io/en/latest/getting-started.html#quickstart)
```python
import os
from dotenv import load_dotenv
from pyairtable import Table
load_dotenv()
def getAll():
# edit here
api_key = os.environ['AIRTABLE_API_KEY']
base_id = os.environ['AIRTABLE_BASE_ID']
table_name = 'Places'
table = Table(api_key, base_id, table_name)
records = table.all()
return { 'total': len(records), 'data': records }
if __name__ == '__main__':
from pprint import pprint
pprint(getAll())
```
### Add to LINE Bot!
- modify the `app.py`
```python
from airtable import getAll
...
@app.route('/getAll')
def test1():
res = getAll()
return res
...
```
- you should able to see all records by accessing [`127.0.0.1/getAll`](http://127.0.0.1:8000/getAll)
## Requests
- [Requests - package](https://requests.readthedocs.io/en/master/)
- `pip install requests`
- `requests.get("<url>", params=params)`
## Google Map API
- [Google Cloud Platform](https://console.cloud.google.com/?hl=zh-TW)
- [GCP Cloud Setup](https://developers.google.com/maps/documentation/places/web-service/cloud-setup)
- [Find Place API](https://developers.google.com/maps/documentation/places/web-service/search-find-place)
- [(Just FYI, we won't use it) Google API Python Client](https://github.com/googlemaps/google-maps-services-python)
### Setup Google Place API
- according to GCP Cloud Setup doc
- create a project
- create a billing account
- enable places API
- find Maps API Key at `APIs & Services` > `Credentials` > `API Keys`
- get API KEY and put into `.env` as `GOOGLE_PLACE_API_KEY`
- create and edit `placeApi.py`
```python
import os
import requests
from dotenv import load_dotenv
# load your .env file
load_dotenv()
# call Google findPlace API
# inputText: query string
def findPlace(inputText):
# url of the api
url = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json'
inputType = 'textquery'
language = 'zh-TW'
# the fields we wanted
fields = [
'place_id',
'formatted_address',
'geometry',
'name',
'photo'
]
# format parameters
params = {
'input': inputText,
'inputtype': inputType,
'fields': ','.join(fields),
'language': language,
'key': os.getenv('GOOGLE_PLACE_API_KEY')
}
# send request and get the response
response = requests.get(url, params=params)
# parse response from json format into python dictionary
return response.json()
if __name__ == '__main__':
from pprint import pprint
query = '7-11'
pprint(findPlace(query))
```
### Add to LINE Bot!
- modify the `app.py`
- you should able to see get the place information by accessing [`127.0.0.1/findPlace`](http://127.0.0.1:8000/findPlace)
## Try it!
- implement `create` api in `airtable.py`
- you should able to see the added record in your airtable
```python
def create(place):
...
return table.create(place)
if __name__ == '__main__':
place = {
'address': 'sample_address',
'name': 'sample_name',
'place_id': 'sample_place_id'
}
res = create(place)
pprint(res)
```
- connet `findPlace` api and airtable `create` api `airtable.py`