/simple-telegrambot

A simple Telegram bot written in Python which can send and receive messages.

Primary LanguagePythonMIT LicenseMIT

Simple Telegram Bot

A simple python Telegram bot which sends and receives messages. Easy to use and easy to extend.

Installation

pip install simple-telegrambot

Usage

# test-bot.py
from datetime import datetime, timezone

from simpletelegrambot import telegrambot

def on_message_receive(bot, message):
    utc_time = datetime.utcnow()
    msg_time = utc_time.strftime('%Y-%m-%d %H:%M:%S (UTC)')
    msg_text = message['text']

    print(msg_time, msg_text)

    if msg_text == 'Ping':
        bot.send_message('Pong')
    
def main():
    bot   = telegrambot.TelegramBot('<bot-token>')
    bot.set_message_handler(on_message_receive)
    bot.wait_for_messages()

if __name__ == '__main__':
    main()

Output:

$ python test_bot.py
2018-11-25 18:33:51 (UTC) Ping
2018-11-25 18:33:52 (UTC) Pong

Testing

  • python 3.6.3
  • macOS 10.12.6

Dependencies

License

See the LICENSE file for license rights and limitations (MIT).