Cancel
Showing results for 
Search instead for 
Did you mean: 

Control Simcenter Amesim simulation with Telegram Bot

Experimenter
Experimenter

On YouTube I found a mind blowing video of Alejandro Magnitopic who easily created a working Telegram Bot communicating with a Python application. This video is the base of the project I would like to present to you in this article and most of the source code is coming from Alejandro. The video you can find here: https://www.youtube.com/watch?v=q9YScw0YBB4. It gives you also the first steps of how creating a Telegram Bot. I do not want to describe it in detail here.

 

The first task was to get the request Python resources into Amesim to get a easy to use web communication. Therefore I go from my point of view the easiest way. I went to https://pypi.org/project/ and downloaded the sources. After the download I just copied the resource into my Simcenter Amesim Folder (C:\Amesim\v1600\Amesim\sys\python\win64\Lib) like you can see in the picture below. There are some more resources needed to get requests started. The complete list you can find it the header of my code in the attachment.

 

Python_Lib_Directory.jpgSimcenter Amesim directory for Python ressources

After I had all the resources to get the Python code running I started using the Simcenter Amesim Simulation Scripting routines to control my simulation project. The Simcenter Amesim simulation is very easy as it is just a spring mass damper system. Using the Simcenter Amesim Help it was very easy to create the code finding the correct references. The code allows me to set a parameter, run the simulation and get a variable for further post processing.

 

And that’s it! My Telegram Amesim Bot Python script is ready. There are just some stumbling blocks in the way you have to move to the side:

 

  1. Please make sure that the Python script is in the same folder like your Simcenter Amesim model
  2. Please compile your Simcenter Amesim model going to simulation mode so that there is a working executable. You can also force recompilation in the menu bar under File – Force recompilation (Ctrl + T)
  3. Write the files for simulation in the menu bar under File – Write files for simulation (Ctrl + Shift + W)
  4. Change the Telegram Bot token and the model file name in the source code to your special bot and model.

 

Now I just have to open my Simcenter Amesim Model and start the Python command interpreter and give following command:

import Telegram_Amesim_Bot.py

 

 

 

Amesim_Python_Command_Interpreter.jpgSimcenter Amesim model and launched Telegram Bot in Python command interpreterIf the script started without any error you can use your Telegram App to control your Simcenter Amesim model. The picture below shows the possible commands. You will also see some feedback in the Python command interpreter.

 Telegram_App.jpgTelegram App showing Simcenter Amesim Bot interacting

 

So I hope you will enjoy your new simulation possibilities using Telegram Bot and Simcenter Amesim with Python scripting!

8 REPLIES

Re: Control Simcenter Amesim simulation with Telegram Bot

Siemens Phenom Siemens Phenom
Siemens Phenom
Very nice! Thanks for sharing.

Re: Control Simcenter Amesim simulation with Telegram Bot

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Hi Jesko!

 

Great work! thanks for sharing! this looks really cool!

 

Regards

Dennis

Re: Control Simcenter Amesim simulation with Telegram Bot

Siemens Phenom Siemens Phenom
Siemens Phenom

Tried it and it does work nicely (not on VPN).

 

Being a Python novice, I only struggled to extract the packages properly, this is how it should look like.

Okay there's probably too many folders I left in there, but it does work.

image.png

Re: Control Simcenter Amesim simulation with Telegram Bot

Experimenter
Experimenter

Hi Emmanuel_D,

 

Thanks for validating the code. I also faced the same issue that is not working under some circumstances like VPN connection.

 

Regarding the Python packages I am also a novice. For me it worked also only copying the “request” folder which is highlighted in the picture below. The other folders like .egg-info, tests and the other files are not necessary.

Maybe there is a Python expert in the forum who can explain it to us.

 

 

Requests_Python_Source_Package.jpg

 

Highlighted

Re: Control Simcenter Amesim simulation with Telegram Bot

Siemens Valued Contributor Siemens Valued Contributor
Siemens Valued Contributor

Hi @NicoV, maybe you can chip-in with some of that knowledge of yours on this topic??

 

Cheers

Dennis

Re: Control Simcenter Amesim simulation with Telegram Bot

Siemens Experimenter Siemens Experimenter
Siemens Experimenter

Dear all,

The link in the first message to the pypi.org requests page is not working.
I assume you want to install following package: https://pypi.org/project/requests/

 

The recommended tool to install python packages is called pip: https://en.wikipedia.org/wiki/Pip_(package_manager)

This tool is often already delivered with the python installation (as is the case from Simcenter Amesim 16, earlier releases this needed to be installed first)

 

so the method to install the request package:

  • browse to the Scripts folder under your python installation (for example C:\Program Files (x86)\LMS\LMS Imagine.Lab\v1600\Amesim\sys\python\win64\Scripts)
  • launch a command window there (press cmd + enter)
  • type pip install requests

 

 this installs the package without manual copy pasting.

 

 

Re: Control Simcenter Amesim simulation with Telegram Bot

Experimenter
Experimenter

Hi NicoV,

 

thanks for your explanation. I really like to learn something new on a Friday afternoon. What I like is, that you do not have to downlaod the sources anymore. Just write the command line and that's it.

 

There ist just one point. To get the packages installed I had to use follwoing script:

 

python -m pip install requests

 

 

Have a nice weekend!

e-Digineer

Re: Control Simcenter Amesim simulation with Telegram Bot

Siemens Experimenter Siemens Experimenter
Siemens Experimenter

hi!

good to hear this is what you are looking for Smiley Happy.

 

pip install somePackage

and

 

 

python -m pip install somePackage

are equivalent. The second one goes through the python interpreter however. This works also if you cannot run the pip command directly (possibly because the location where it is installed is not part of your environment path variable). launching the pip command from within the Scripts folder should normally always work.