# Python : Get Custom Rewards

**URL:** <https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240>\
**Category:** Game Development\
**Created:** [January 31, 2021, 1:12pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240 "2021-01-31T13:12:47Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bargalon](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bargalon](https://staging-discuss.dev.twitch.com/u/Bargalon)\
**Post date:** [January 31, 2021, 1:12pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/1 "2021-01-31T13:12:47Z")

</div>

I’m developing a python game in which viewers can interact via cutom rewards.  
The problem is, I can’t get the rewards back.  
I created a token with ‘channel: read: redemptions’ rights.  
Here is my code:

> import websockets  
> import json  
> import asyncio  
> import random
> 
> url = “wss://pubsub-edge.twitch.tv”  
> loop = asyncio.get\_event\_loop()  
> connection = websockets.connect(url, ping\_interval=100)
> 
> client\_id = ‘MYCLIENTID’  
> channel\_id = ‘MYUSERID’  
> access\_token = ‘MYTOKEN’
> 
> async def stay():  
> async with connection as socket:  
> while ():  
> ping = json.dumps({  
> “type”: “PING”})
> 
> ```
> await socket.send(ping)
> res = await socket.recv()
> print(res)
> await asyncio.sleep(100 + random.randrange(1, 50))
> 
> ```
> 
> async def listen():  
> async with connection as socket:  
> data = json.dumps({  
> “type”: “LISTEN”,  
> “data”: {  
> “topics”: [“channel-points-channel-v1.” + channel\_id],  
> “auth\_token”: access\_token,  
> }
> 
> ```
> })
> await socket.send(data)
> resp = await socket.recv()
> 
> dec_resp = json.loads(resp)
> 
> print(dec_resp)
> 
> # while(loop.is_running):
> 
> async for msg in socket:
> pass
> 
> ```
> 
> loop.create\_task(stay())
> 
> loop.run\_until\_complete(listen())

i don’t understand what is wrong !

Could you help me ?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [January 31, 2021, 1:24pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/2 "2021-01-31T13:24:31Z")

</div>

Your Post suggests you are trying to read what points rewards exist.

But your code suggests you are trying to retrieve redemptions (aka Purchases or usage) as they happen instead?

So what are you actually trying to do?

---

<div class="post-metadata">

**Author:** ![Bargalon](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bargalon](https://staging-discuss.dev.twitch.com/u/Bargalon)\
**Post date:** [January 31, 2021, 2:15pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/3 "2021-01-31T14:15:12Z")

</div>

What i want :  
When a viewer uses channel point to buy a reward, i need to get the information about wich reward it is to create a special event in my game.  
Sorry for my english i’m french 😝

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [January 31, 2021, 2:45pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/4 "2021-01-31T14:45:28Z")

</div>

EventSub will let you monitor redemptions created for the rewards created by the same client ID

> **[EventSub Subscription Types](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/)**
>
> Subscription Types

PubSub, will return for all redemptions for “custom” rewards, this will not include emote modifications, highlight my message etc

> **[PubSub](https://dev.twitch.tv/docs/pubsub/)**
>
> PubSub Guide

---

<div class="post-metadata">

**Author:** ![Bargalon](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bargalon](https://staging-discuss.dev.twitch.com/u/Bargalon)\
**Post date:** [January 31, 2021, 3:06pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/5 "2021-01-31T15:06:13Z")

</div>

OK but it doesn’t work with websockets.  
So I have to set up a webhook server to retrieve the data and then process it.  
I don’t have the knowledge to do this.  
Are there any preconfigured solutions to do this with aws or something ?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [January 31, 2021, 3:09pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/6 "2021-01-31T15:09:08Z")

</div>

> [@Bargalon](#):
>
> OK but it doesn’t work with websockets.

I tested on my own channel. It’s working as expected for me.

> [@Bargalon](#):
>
> Are there any preconfigured solutions to do this with aws or something ?

Not to my knowledge no.

---

<div class="post-metadata">

**Author:** ![Bargalon](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bargalon](https://staging-discuss.dev.twitch.com/u/Bargalon)\
**Post date:** [January 31, 2021, 3:17pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/7 "2021-01-31T15:17:16Z")

</div>

Could you tell me what to change on my current code ?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [January 31, 2021, 3:19pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/8 "2021-01-31T15:19:48Z")

</div>

My solution is in nodeJS as I’m not very python fluent

Is your code returning any errors? Collecting any response at all.  
Did you get a response to your Listen request? If so did it return an error?

---

<div class="post-metadata">

**Author:** ![Bargalon](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bargalon](https://staging-discuss.dev.twitch.com/u/Bargalon)\
**Post date:** [January 31, 2021, 5:52pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/9 "2021-01-31T17:52:29Z")

</div>

It’s ok!  
If you need the working code :

```
url = "wss://pubsub-edge.twitch.tv"
loop = asyncio.get_event_loop()
connection = websockets.connect(url, ping_interval=100)

client_id = 'myclientid'
channel_id = 'myuserid'
access_token = 'mytokent '

async def stay():
    async with connection as socket:
        while ():
            ping = json.dumps({
                "type": "PING"})

            await socket.send(ping)
            res = await socket.recv()
            print(res)
            await asyncio.sleep(100 + random.randrange(1, 50))

async def listen():
    async with connection as socket:
        data = json.dumps({
            "type": "LISTEN",
            "data": {
                "topics": ["channel.channel_points_custom_reward_redemption.add" + channel_id],
                "auth_token": access_token,
            }

        })
        await socket.send(data)
        resp = await socket.recv()

        dec_resp = json.loads(resp)

        print(dec_resp)

        # while(loop.is_running):

        async for msg in socket:
			print(msg)
            pass

loop.create_task(stay())

loop.run_until_complete(listen())
```

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [January 31, 2021, 5:53pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/10 "2021-01-31T17:53:40Z")

</div>

> [@Bargalon](#):
>
> If you need the working code :

So that is to say that it is now working? And you fixed the issue, or is it not working?

---

<div class="post-metadata">

**Author:** ![Bargalon](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bargalon](https://staging-discuss.dev.twitch.com/u/Bargalon)\
**Post date:** [January 31, 2021, 7:03pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/11 "2021-01-31T19:03:13Z")

</div>

it’s fixed

---

<div class="post-metadata">

**Author:** ![system](https://d2spem0quai2ol.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://staging-discuss.dev.twitch.com/u/system)\
**Post date:** [March 2, 2021, 1:12pm UTC](https://staging-discuss.dev.twitch.com/t/python-get-custom-rewards/30240/12 "2021-03-02T13:12:51Z")

</div>

This topic was automatically closed after 30 days. New replies are no longer allowed.
