- Download Telegram on your mobile phone
- Create a new bot
- Open a chat with @BotFather
- Send command /newbot
- When asked, insert a name and a username for your new bot
- BotFather sends you the token to access to your bot. Copy it on computer
- Download this code on your computer
- Open the file matlab_example.py and change the bot_id variable by assigning the token previously taken
- Launch the script that will become the server of your telegram bot (requires python 3)
- on Windows: python matlab_example.py
- on Ubuntu: python3 matlab_example.py
- Test it by opening a chat with your just-created bot and send him a picture.
This class is used to make the basic operations such as receiving and sending text messages and images, as well as documents and audio messages
Example:
from Bot import Bot bot = Bot(bot_id) bot.sendMessage(chat_id, "This is a message")The updater checks for updates and dispatches the type of message received to the specified function (if specified). It's possible to bind a function to a message type by using these setter functions:
setTextHandler(f) setPhotoHandler(f) setVoiceHandler(f)Here there is an example:
from Updater import Updater # create your function to handle a message type, in this case plain text def myTextHandler(bot, message, chat_id, text): # this function sends back the received message bot.sendMessage(chat_id, 'Received this text: ' + text) # instantiate the updater updater = Updater(bot_id) # bind the textHandler of the updater with your custom textHandler updater.setTextHandler(myTextHandler) # lunch the updater updater.start()The file matlab_example.py uses the class Updater to interact with Telegram and runs the Matlab script edges.m when it receives an image