Don't click on links you're not certain of - on sites, in emails, in texts, anywhere. Don't go to questionable sites. Don't download questionable apps (that includes many apps in the Play store).
Something you can try:
Keep the phone totally off the internet for about a week (no wifi, no mobile data) and see if anything like that still happens. (Texts are texts - once someone gets your number, they can text you.) If the phone still does strange things, you have malware. Install
Malwarebytes Anti-Malware and a good antivirus and let them keep running.