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.

import time
import logging
import requests
import json
log_level = logging.DEBUG
logging.basicConfig(format='"%(asctime)s;%(levelname)s;%(message)s"',
                     level=log_level,
                      filemode='a')
 
base_url= "https://exchange-prod-g.2mee.com"
token = <API Key>
app_id =<Application id>

** For getRequest(), postRequest(),putRequest() refer to Appendix.

Company details

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

Application details

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

Application code(refer documentation)

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

Default audience segment

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

Audience count

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

Create message

 data = {}
 data['appId'] = default_app_id 
 data['title'] = 'Python Test Message'
 data['subtitle'] ='Python test message'
 data['text'] = 'This is a very short text test message.'
 data['fbTitle'] = 'Fb Title'
 data['fbSubtitle'] ='Gb Subtitle'
 data['fbText'] = 'Fb This is a very short text test message.' 
 data['expiry'] = '1440'
 data['playTime'] = playtime
 data['about'] = [] 
 data['filterId'] = default_filter_id
 data['companyId'] = company_id
 data['type'] = 'TEXT'
 data['priority'] = 'MEDIUM'
 data['channel'] = 'PUSH'
 data['favourite'] = 0
 data['action'] = "ok"
 response = postRequest("/notification/create", token, data) 
 notification_id = response['id']
 logging.info("NotificationId = %s"%notification_id)

Schedule notification

response = postRequest("/notification/schedule?id=%s"%notification_id, token, "")

You should now receive a notification on your demo application.

Appendix

Get request

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

Post request

def postRequest(api, token, dataarray):
    global base_url

    url = base_url+api 
    response = requests.post(url,  headers={'Authorization' : 'Bearer {}'.format(token),
 'Content-Type' : 'application/json' }, data=json.dumps(dataarray)).content.strip().decode('utf-8')
    logging.debug("Response = %s"%str(response))
    return json.loads(response) 

Last updated