Welcome to the forum and what an interesting question...I am by no means a professional but have had my fair share of bowel issues also. I am not sure what the relationship, if any is with medication and bowels however, it could be possible.
What I have learnt for the best bowel health for me is lots of juicy pears and green in my diet. I am actually starting to adopt a plant based diet to also see if this helps me. Dairy does not agree with me at all so I really have to avoid that.
I think perhaps though the best person to chat to would be your GP and to discuss if the medication could be impacting your bowel health and also your sleep. The body is a tricky machine to figure out sometimes!!
Great to chat to you Bel44 and hope to hear how you go with solving this situation, all the very best and hope you can get some answers on this one.