The problem with receiving tokens is that a contract can't know when it receives tokens - there is no transaction to the contract which receives the tokens as the tokens are exchanged only inside the token contract's balances. There are lots of posts about it, here's one for example: Detect token transaction to a contract
So basically what you'd need to do is something like this:
1) Create a way for the contract to know that it has received tokens. Probably someone from outside the blockchain has to tell the contract.
2) The contract has to check its own balance inside the desired token contract(s) -
unless your contract can trust the information from step 1 (and the balance information is relayed to it from backend). If it can trust the information then it doesn't have to check the balances as it already knows them.
3) The contract calls the token contracts'