Skip to content

Instantly share code, notes, and snippets.

@yujuwon
Last active May 24, 2017 02:25
Show Gist options
  • Select an option

  • Save yujuwon/050b175662fdd973c982fd6406aad139 to your computer and use it in GitHub Desktop.

Select an option

Save yujuwon/050b175662fdd973c982fd6406aad139 to your computer and use it in GitHub Desktop.
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
import os
import random
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def help(bot, update):
bot.sendMessage(update.message.chat_id, text='Help!')
def iam(bot, update):
chat_id = update.message.chat_id
msg = "my chat_id is %d" %(chat_id)
bot.sendMessage(chat_id=update.message.chat_id, text=msg)
def start(bot, update):
chat_id = update.message.chat_id
user = update.message.from_user
msg = "안녕, %s %s! 주팩토리에 온 것을 환영해!" %(user.first_name, user.last_name)
bot.sendMessage(update.message.chat_id, text=msg)
def query(msg) :
return msg
def response(bot, update):
chat_id = update.message.chat_id
user = update.message.from_user
user_name = "%s%s" %(user.last_name, user.first_name)
r_msg = query(update.message.text)
bot.sendMessage(chat_id,
text=r_msg)
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def main():
# Create the EventHandler and pass it your bot's token.
with open(os.path.join('/notebooks/work/','jufactory.keyfile'), 'r') as f :
token = f.readline().strip()
updater = Updater(token)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("iam", iam))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler([Filters.text], response))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until the you presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment