Fakultas Ilmu Komputer UI

Commit 3dfa7903 authored by zakiraihan's avatar zakiraihan
Browse files

Adding basic teleram bot and update dependency

parent 3ab5545e
from flask import Flask
from flask import Flask, request
from telebot.mastermind import get_response
import telegram
import os
global bot
global TOKEN
TOKEN = os.environ.get('BOT_TOKEN')
bot = telegram.Bot(token=TOKEN)
URL = os.environ.get('URL')
app = Flask(__name__)
app.config.from_object(os.environ.get('APP_SETTINGS'))
@app.route('/{}'.format(TOKEN), methods=['POST'])
def respond():
# retrieve the message in JSON and then transform it to Telegram object
update = telegram.Update.de_json(request.get_json(force=True), bot)
chat_id = update.message.chat.id
msg_id = update.message.message_id
# Telegram understands UTF-8, so encode text for unicode compatibility
text = update.message.text.encode('utf-8').decode()
print("got text message :", text)
response = get_response(text)
bot.sendMessage(chat_id=chat_id, text=response, reply_to_message_id=msg_id)
return 'ok'
@app.route('/setwebhook/<noNgrok>', methods=['GET', 'POST'])
def set_webhook(noNgrok):
# we use the bot object to link the bot to our app which live
# in the link provided by URL
print (("https://" + noNgrok + URL + '/'))
if (noNgrok == 'prod'):
noNgrok = ""
s = bot.setWebhook('{URL}{HOOK}'.format(URL=("https://" + noNgrok + URL + '/'), HOOK=TOKEN))
# something to let us know things work
if s:
return "webhook setup ok"
else:
return "webhook setup failed"
@app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run()
\ No newline at end of file
app.run(threaded=True)
\ No newline at end of file
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
DEBUG = False
TESTING = False
CSRF_ENABLED = True
SECRET_KEY = 'nediafia'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
class ProductionConfig(Config):
DEBUG = False
class StagingConfig(Config):
DEVELOPMENT = True
DEBUG = True
class DevelopmentConfig(Config):
DEVELOPMENT = True
DEBUG = True
class TestingConfig(Config):
TESTING = True
\ No newline at end of file
asn1crypto==0.24.0
certifi==2019.9.11
cffi==1.12.3
chardet==3.0.4
Click==7.0
cryptography==2.7
Flask==1.1.1
future==0.17.1
idna==2.8
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
pycparser==2.19
python-telegram-bot==12.1.1
requests==2.22.0
six==1.12.0
tornado==6.0.3
urllib3==1.25.6
Werkzeug==0.16.0
def get_response(msg):
"""
you can place your mastermind AI here
could be a very basic simple response like "معلش"
or a complex LSTM network that generate appropriate answer
"""
return "Ahoy!!"
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment