Skip to content

Instantly share code, notes, and snippets.

@vinovator
Created August 17, 2016 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinovator/2d6b44789ae27695f69322307d07bc39 to your computer and use it in GitHub Desktop.
Save vinovator/2d6b44789ae27695f69322307d07bc39 to your computer and use it in GitHub Desktop.
Python app to interact with Telegram bot
# vin_bot_hello.py
# Python 2.7.6
"""
Python app to interact with Telegram bot
"""
import telepot
import time
import pandas as pd # to read token from csv
# This file contains the token for telegram bot API
token_secret = "Telegram Bot Token.csv"
def handle(msg):
"""
The handler code to deal with bot's incoming messages
"""
# Get sender details
user_name = msg["from"]["first_name"] + " " + msg["from"]["last_name"]
# Get message details
content_type, chat_type, chat_id = telepot.glance(msg)
if content_type == "text":
command = msg["text"]
print("Got command {0}".format(command))
if("/hello" in command):
bot.sendMessage(
chat_id,
"Hello {0}, How are you doing today?".format(user_name))
if __name__ == "__main__":
""" Start block """
# Read the API token from secret scv file
df = pd.read_csv(token_secret)
token = df["token"][0]
# Instantiate the bot using the token provided by botfather
bot = telepot.Bot(token)
# Add the handle function to be called for each new received message
bot.message_loop(handle)
# Wait for new messages
while 1:
time.sleep(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment