Send Telegram Notifications to your Mobile from Python/OpenSesame
Sending Telegram notifications might be very useful for receiving status messages, for example about participants performance during an experiment (see on the right). Getting this to work is remarkably easy – from a coding perspective. You will need around 10minutes to set up the bot prior to this. Te beauty is, you do not have to install any additional software on the experimental computer or your smartphone (provided you already have Telegram).
Get Telegram and Create a Bot
If you don’t have it, get the Telegram Messenger now. Then, follow steps 1 and 2 from this great tutorial to create a bot (only takes a few minutes). make sure you note the authorization token and your chat ID. Then come back here.
The most basic message
It is oh so easy to send a message from the bot via Telegram’s extensive API. You just have to access a specific URL from any webbrowser (or similar service). In the end it looks like this:
So in principle you just have to replace %TOKEN% and %CHAT_ID% with your values and replace %TEXT% with the text you want to send.
Customize message from Python
If it is no issue for you to install new python libraries because you are using raw python, I can highly recommend the Python Telegram Bot. If you don’t want to install other dependencies (for example, because you are using OpenSesame), I wrote a very basic function in Python that you can use in your experiments:
### Send text message
bot_token = 'BOT_TOKEN'
bot_chatID = 'CHAT_ID'
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
Again, just put in your token and the ID and then, once initialized (put it in an inline script in the beginning of your experiment), you can call the funtion from any place in your experimental script by using bot_sendtext(Message) , for example bot_sendtext('The participant has finished the experiment') . You can use Markdown to format the text. Alternatively, find more options in the powerful Telegram Bot API.
Access and Send OpenSesame variable status
Using this script to send status about the participant’s progress in OpenSesame is super simple. Just include the following code (additionally to the method from above) in an inline script right before the feedback item. Make sure to paste the code in the run phase.
text = 'Participant ' + str(var.subject_nr) + ': Block ' + str(var.count_block_loop+1) + ' completed (' + str(round(var.avg_rt,0)) + ' ms; ' + str(round(var.acc,2)) + '% correct)'
The script assumes that you log participant number, mean RT and accuracy in the default OpenSesame variables. The results should look similar to the screenshot above.
If you have more suggetions, I will gladly add them.