Created
August 25, 2016 00:05
-
-
Save vinovator/7c9f1458dd5e7734b1150ae975503ca5 to your computer and use it in GitHub Desktop.
Python script to listen and respond to commands sent to Telegram bot. Uses telepot library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vinbot.py | |
# Python 3.5 | |
""" | |
@vin_robot is my telegram bot; telegram.me/vin_robot | |
This python script listens to commands sent to bot and responds accordingly | |
""" | |
import telepot | |
from IGNORE import vinbot_secrets # To fetch token | |
from UTILS import botmap # Mapping between command and response | |
import time | |
# Dict with command - response pairs | |
command_map = botmap.commands | |
def handle(msg): | |
""" | |
Handler function for incoming messages | |
""" | |
# Get sender details | |
user_name = msg["from"]["first_name"] | |
# Get message details | |
content_type, chat_type, chat_id = telepot.glance(msg) | |
# Proceed if message type is text | |
if content_type == "text": | |
command = msg["text"].lower() | |
# print("got command {}".format(command)) | |
# Check if the command is predefined and fetch response | |
if command in command_map: | |
response = command_map[command] | |
else: | |
response = "Sorry {}. I don't recognize that commmand".format( | |
user_name) | |
bot.sendMessage(chat_id, response) | |
if __name__ == "__main__": | |
""" Starting block """ | |
# Read the API token from untracked file | |
token = vinbot_secrets.token | |
# Instantiate the telegram bot | |
bot = telepot.Bot(token) | |
# Add handler to handle incoming messages | |
bot.message_loop(handle) | |
while 1: | |
time.sleep(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment