Skip to content

dros1986/python_bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-bot

Setup

  1. Download Telegram on your mobile phone
  2. Create a new bot
  3. Open a chat with @BotFather
  4. Send command /newbot
  5. When asked, insert a name and a username for your new bot
  6. BotFather sends you the token to access to your bot. Copy it on computer
  7. Download this code on your computer
  8. Open the file matlab_example.py and change the bot_id variable by assigning the token previously taken
  9. Launch the script that will become the server of your telegram bot (requires python 3)
  10. on Windows: python matlab_example.py
  11. on Ubuntu: python3 matlab_example.py
  12. Test it by opening a chat with your just-created bot and send him a picture.

Classes

Bot

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")

Updater

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()

Example with Matlab

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

About

Small python-based Telegram bot framework

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published