I am trying to follow the instructions listed on the GroupMe Website to enable my python program to receive real-time messages from a group. In step 2 of the process the response I got was
'successful': False, 'error': '401:1c5k4zq1mycffe161kgqp13gnvvn:Unknown client'
instead of
"successful":true
similarly it returned
'subscription': '/user/'
instead of
"subscription":"/user/1000112"
I tried the following code with and without
json=
import requests
s = requests.Session()
r=s.post('https://push.groupme.com/faye', json={
"channel":"/meta/subscribe",
"clientId":"1c5k4zq1mycffe161kgqp13gnvvn",
"subscription":"/user/<userid>",
"id":"2",
"ext":
{
"access_token":"SECRET_CODE",
"timestamp":1322556419
}
})
print(r.json())
Any suggestions?
Thank you so much
First you have to run Handshake
and it gives you client_id
import requests
data = [
{
"channel": "/meta/handshake",
"version": "1.0",
"supportedConnectionTypes": ["long-polling"],
"id": "1"
}
]
url = 'https://push.groupme.com/faye'
response = requests.post(url, json=data)
data = response.json()
print(response.text)
#for message in data:
# for key, val in message.items():
# print(f'{key}: {val}')
client_id = data[0]['clientId']
Next you have to use this client_id
in all other requests.
Next you have to login on https://dev.groupme.com (using the same login and password as on https://groupme.com) and create Application
to get ACCESS TOKEN
For test you may need to create own group channel - and it will have function to generate sharing link
like https://groupme.com/join_group/101707363/f0RF78na
and number 101707363
will be needed to subscribe this channel.
ACCESS_TOKEN = 'YfXp........' # from Application created on dev.groupme.com
client_id = data[0]['clientId'] # from Handshake
group_id = 101707363 # from sharing link
timestamp = int(datetime.datetime.now().timestamp())
data = [
{
"channel": "/meta/subscribe",
"clientId": client_id,
"subscription": f"/group/{group_id}",
"id": "2",
"ext": {
"access_token": ACCESS_TOKEN,
"timestamp": timestamp,
}
}
]
url = 'https://push.groupme.com/faye'
response = requests.post(url, json=data)
data = response.json()
print(response.text)
#for message in data:
# for key, val in message.items():
# print(f'{key}: {val}')
And later you can send requests for data - but documentation suggests to use websockets
for this. With normal requests
it waits until it gets new data or it is timeouted. And it blocks all code.
data = [
{
"channel": "/meta/connect",
"clientId": client_id,
"connectionType": "long-polling",
"id": "3"
}
]
url = 'https://push.groupme.com/faye'
response = requests.post(url, json=data)
data = response.json()
print(response.text)
#for message in data:
# for key, val in message.items():
# print(f'{key}: {val}')
And this may need to run in loop to get when you (or someone else) send messages on your group channel (created for test).
At this moment I get only message that I'm typing on my group channel but I still don't get messages. Maybe it needs to increate id
in every request.
My full code for tests:
You have to create own group to get own sharing link with number for group_id
import requests
import datetime
def handshake():
print('--- Step 1: Handshake ---')
data = [
{
"channel": "/meta/handshake",
"version": "1.0",
"supportedConnectionTypes": ["long-polling"],
"id": "1"
}
]
url = 'https://push.groupme.com/faye'
response = requests.post(url, json=data)
data = response.json()
print('- data -')
print(response.text)
for key, val in data[0].items():
print(f'{key}: {val}')
client_id = data[0]['clientId']
print('client_id:', client_id)
return client_id
def subcribe_user_channel(client_id, user_id, ACCESS_TOKEN):
print('--- Step 2: Subscribe to the user channel ---')
timestamp = int(datetime.datetime.now().timestamp())
print(timestamp)
data = [
{
"channel": "/meta/subscribe",
"clientId": client_id,
"subscription": f"/user/{user_id}",
"id": "2",
"ext": {
"access_token": ACCESS_TOKEN,
"timestamp": timestamp, # 1322556419,
}
}
]
#print( datetime.datetime.fromtimestamp(1322556419) )
# 1322556419 == 2011-11-29 09:46:59
url = 'https://push.groupme.com/faye'
response = requests.post(url, json=data)
data = response.json()
print('- data -')
print(response.text)
for messagein in data:
for key, val in message.items():
print(f'{key}: {val}')
def subscribe_group_channel(client_id, group_id, ACCESS_TOKEN):
print('--- Step 2: Subscribe to the user channel ---')
timestamp = int(datetime.datetime.now().timestamp())
print(timestamp)
data = [
{
"channel": "/meta/subscribe",
"clientId": client_id,
"subscription": f"/group/{group_id}",
"id": "2",
"ext": {
"access_token": ACCESS_TOKEN,
"timestamp": timestamp, # 1322556419,
}
}
]
url = 'https://push.groupme.com/faye'
response = requests.post(url, json=data)
data = response.json()
print('- data -')
print(response.text)
for message in data:
for key, val in message.items():
print(f'{key}: {val}')
def poll_data(client_id):
print('--- Step 4: Poll for data ---')
print('# it can hang and wait for data if there is no data at this moment')
data = [
{
"channel": "/meta/connect",
"clientId": client_id,
"connectionType": "long-polling",
"id": "3"
}
]
url = 'https://push.groupme.com/faye'
response = requests.post(url, json=data)
data = response.json()
print('- data -')
print(response.text)
for message in data:
for key, val in message.items():
print(f'{key}: {val}')
# --- main ---
ACCESS_TOKEN = 'YfX..........' # from `Application` created on dev.groupme.com
#user_id = '1000112' # ???
group_id = '101707363' # from sharing link: https://groupme.com/join_group/101707363/f0RF78na
client_id = handshake()
#subscribe_user_channel(client_id, user_id, ACCESS_TOKEN)
subscribe_group_channel(client_id, group_id, ACCESS_TOKEN)
while True:
poll_data(client_id)