Created
March 20, 2018 15:27
-
-
Save SkymanOne/e2c8c6c18179a65223c403e5b2470a0d to your computer and use it in GitHub Desktop.
Base telegram bot heroku application
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
import telebot | |
import os | |
from telebot import types | |
from flask import Flask, request | |
# если в окуржении есть переменная HEROKU, значит получаем токен из переменной окружения | |
if 'HEROKU' in list(os.environ.keys()): | |
TOKEN = str(os.environ.get('TOKEN')) | |
# иначе импортируем его из скрытого в файлы в папке проекта | |
else: | |
import token_key | |
TOKEN = token_key.token | |
bot = telebot.TeleBot(TOKEN) | |
server = Flask(__name__) | |
# если в окуржении есть переменная HEROKU, значит поднимаем сервер | |
# иначе запускаем прослушку | |
if 'HEROKU' in list(os.environ.keys()): | |
@server.route('/' + TOKEN, methods=['POST']) | |
def get_message(): | |
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))]) | |
return "!", 200 | |
@server.route('/') | |
def webhook(): | |
bot.remove_webhook() | |
bot.set_webhook(url='url of heroku project' + TOKEN) | |
return '!', 200 | |
if __name__ == '__main__': | |
server.run(host="0.0.0.0", port=int(os.environ.get('PORT', 5000))) | |
else: | |
bot.remove_webhook() | |
bot.polling(True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment