Created
August 17, 2016 15:23
-
-
Save vinovator/2d6b44789ae27695f69322307d07bc39 to your computer and use it in GitHub Desktop.
Python app to interact with Telegram bot
This file contains hidden or 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
# 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