Search…
Python example
Sample code in python on who to use the 2Mee APIs.
Please make sure to obtain the below values from UI as directed
1.API key which is required for API calls in Authorization: Bearer<API Key > format. 2.Application Id. and substitute these values for token, app_id.
1
import time
2
import logging
3
import requests
4
import json
Copied!
1
log_level = logging.DEBUG
2
logging.basicConfig(format='"%(asctime)s;%(levelname)s;%(message)s"',
3
level=log_level,
4
filemode='a')
5
6
base_url= "http://server-v1.2mee.com"
7
token = <API Key>
8
app_id =<Application id>
Copied!
** For getRequest(), postRequest(),putRequest() refer to Appendix.

Company details

1
response = getRequest("/company/find", token)
2
company_id = response['id']
3
logging.info("Company Id = %s"%company_id)
Copied!

Application details

1
response = getRequest("/app/list?company_id=%s"%company_id, token)
2
default_app_name = response[0]['name']
3
logging.info("App Name = %s" % default_app_name)
4
default_app_id= response[0]['id']
5
logging.info("App Id = %s" % default_app_id)
Copied!

Application code(refer documentation)

1
default_app_code = response[0]['code']
2
logging.info("App Code = %s" % default_app_code)
Copied!

Default audience segment

1
response = getRequest("/filter/list?type=&showHidden=false&page=0&app_id=%s"%default_app_id, token)
2
default_filter_id = response['content'][0]['id']
3
logging.info("Filter Id for DEFAULT FILTER =%s"%default_filter_id)
Copied!

Audience count

1
response = getRequest("/filter/count/%s"%default_filter_id, token)
2
device_count = response['devices']['deviceCount']
3
logging.info("Filter Device Count = %s"% device_count)
4
playtime = int(time.time() * 1000) #playtime in milliseconds, is 5 minutes in advance
5
logging.info("Play time is set current time =%d"%playtime)
Copied!

Create message

1
data = {}
2
data['appId'] = default_app_id
3
data['title'] = 'Python Test Message'
4
data['subtitle'] ='Python test message'
5
data['text'] = 'This is a very short text test message.'
6
data['fbTitle'] = 'Fb Title'
7
data['fbSubtitle'] ='Gb Subtitle'
8
data['fbText'] = 'Fb This is a very short text test message.'
9
data['expiry'] = '1440'
10
data['playTime'] = playtime
11
data['about'] = []
12
data['filterId'] = default_filter_id
13
data['companyId'] = company_id
14
data['type'] = 'TEXT'
15
data['priority'] = 'MEDIUM'
16
data['channel'] = 'PUSH'
17
data['favourite'] = 0
18
data['action'] = "ok"
19
response = postRequest("/notification/create", token, data)
20
notification_id = response['id']
21
logging.info("NotificationId = %s"%notification_id)
Copied!

Schedule notification

1
response = postRequest("/notification/schedule?id=%s"%notification_id, token, "")
2
Copied!
You should now receive a notification on your demo application.

Appendix

Get request

1
def getRequest(api, token):
2
global base_url
3
4
url = base_url+api
5
response = requests.get(url, headers={'Authorization' : 'Bearer {}'.format(token),
6
'Content-Type' : 'application/json' }).content.strip().decode('utf-8')
7
logging.debug("Response = %s"%str(response))
8
return json.loads(response)
Copied!

Post request

1
def postRequest(api, token, dataarray):
2
global base_url
3
4
url = base_url+api
5
response = requests.post(url, headers={'Authorization' : 'Bearer {}'.format(token),
6
'Content-Type' : 'application/json' }, data=json.dumps(dataarray)).content.strip().decode('utf-8')
7
logging.debug("Response = %s"%str(response))
8
return json.loads(response)
Copied!
Last modified 1yr ago
Copy link