Initial commit

This commit is contained in:
Derek Schmidt 2018-06-26 19:13:29 +00:00
commit 78cf3a4121
Signed by: skeh
GPG key ID: 0F5D491793B4035A
5 changed files with 206 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
config.json

14
Pipfile Normal file
View file

@ -0,0 +1,14 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[dev-packages]
[packages]
pytelegrambotapi = "*"
click = "*"
flask = "*"
[requires]
python_version = "3.6"

112
Pipfile.lock generated Normal file
View file

@ -0,0 +1,112 @@
{
"_meta": {
"hash": {
"sha256": "e193940b1b1f98fb8327516692240b60c21704b142a9ae5d16ed28a3ea79e5fb"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.6"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"certifi": {
"hashes": [
"sha256:13e698f54293db9f89122b0581843a782ad0934a4fe0172d2a980ba77fc61bb7",
"sha256:9fa520c1bacfb634fa7af20a76bcbd3d5fb390481724c597da32c719a7dca4b0"
],
"version": "==2018.4.16"
},
"chardet": {
"hashes": [
"sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
"sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
],
"version": "==3.0.4"
},
"click": {
"hashes": [
"sha256:29f99fc6125fbc931b758dc053b3114e55c77a6e4c6c3a2674a2dc986016381d",
"sha256:f15516df478d5a56180fbf80e68f206010e6d160fc39fa508b65e035fd75130b"
],
"index": "pypi",
"version": "==6.7"
},
"flask": {
"hashes": [
"sha256:2271c0070dbcb5275fad4a82e29f23ab92682dc45f9dfbc22c02ba9b9322ce48",
"sha256:a080b744b7e345ccfcbc77954861cb05b3c63786e93f2b3875e0913d44b43f05"
],
"index": "pypi",
"version": "==1.0.2"
},
"idna": {
"hashes": [
"sha256:156a6814fb5ac1fc6850fb002e0852d56c0c8d2531923a51032d1b70760e186e",
"sha256:684a38a6f903c1d71d6d5fac066b58d7768af4de2b832e426ec79c30daa94a16"
],
"version": "==2.7"
},
"itsdangerous": {
"hashes": [
"sha256:cbb3fcf8d3e33df861709ecaf89d9e6629cff0a217bc2848f1b41cd30d360519"
],
"version": "==0.24"
},
"jinja2": {
"hashes": [
"sha256:74c935a1b8bb9a3947c50a54766a969d4846290e1e788ea44c1392163723c3bd",
"sha256:f84be1bb0040caca4cea721fcbbbbd61f9be9464ca236387158b0feea01914a4"
],
"version": "==2.10"
},
"markupsafe": {
"hashes": [
"sha256:a6be69091dac236ea9c6bc7d012beab42010fa914c459791d627dad4910eb665"
],
"version": "==1.0"
},
"pytelegrambotapi": {
"hashes": [
"sha256:79ba09316ba9b01a0b2c3bf937d8cf57b33072c1caead7a6dc010956683cef5e"
],
"index": "pypi",
"version": "==3.6.3"
},
"requests": {
"hashes": [
"sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1",
"sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6a"
],
"version": "==2.19.1"
},
"six": {
"hashes": [
"sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9",
"sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb"
],
"version": "==1.11.0"
},
"urllib3": {
"hashes": [
"sha256:a68ac5e15e76e7e5dd2b8f94007233e01effe3e50e8daddf69acfd81cb686baf",
"sha256:b5725a0bd4ba422ab0e66e89e030c806576753ea3ee08554382c14e685d117b5"
],
"version": "==1.23"
},
"werkzeug": {
"hashes": [
"sha256:c3fd7a7d41976d9f44db327260e263132466836cef6f91512889ed60ad26557c",
"sha256:d5da73735293558eb1651ee2fddc4d0dedcfa06538b8813a2e20011583c9e49b"
],
"version": "==0.14.1"
}
},
"develop": {}
}

66
main.py Normal file
View file

@ -0,0 +1,66 @@
from flask import Flask, request, abort
import telebot
import json
import time
with open('config.json', 'r') as f:
config = json.load(f)
app = Flask(__name__)
app.config.update(config)
bot = telebot.TeleBot(config['bot']['token'])
bot.remove_webhook()
time.sleep(0.1)
bot.set_webhook(url='https://{base}/{key}/'.format(base=app.config['bot']['base_url'], key=app.config['bot']['token']))
def channel_commands(message, commands):
bot_name = bot.get_me().username
formatted_commands = ['/{cmd}@{usr}'.format(cmd=command, usr=bot_name) for command in commands]
return message.text in formatted_commands
@bot.channel_post_handler(func=lambda msg: channel_commands(msg, ['start']))
@bot.message_handler(commands=['start'])
def start(message):
bot.reply_to(message, "Hi {id}, I'm a dumb bot!".format(id=message.chat.id))
@bot.channel_post_handler(func=lambda msg: channel_commands(msg, ['ping']))
@bot.message_handler(commands=['ping'])
def ping(message):
bot.reply_to(message, "Pong!")
@app.route('/')
def index():
return "hello!\n"
@app.route('/ping/<id>', methods=['POST'])
def notify(id):
if request.headers.get('content-type') == 'application/json':
request_json = request.get_data().decode('utf-8')
data = json.loads(request_json)
bot.send_message(id, data.get('text'))
return json.dumps({"status": "success"})
else:
abort(403)
# This route will match a lot. Make sure it stays at the end!
@app.route('/<key>/', methods=['POST'])
def webhook(key):
if key == app.config['bot']['token']:
if request.headers.get('content-type') == 'application/json':
json_string = request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return ''
else:
abort(403)
else:
abort(404)
if __name__ == '__main__':
app.run()

13
setup.py Normal file
View file

@ -0,0 +1,13 @@
import json
import click
@click.command()
@click.argument('token')
@click.argument('host')
def make_config(token, host):
bare_config = {'bot': {'token': token, 'base_url': host}}
with open('config.json', 'w') as f:
f.write(json.dumps(bare_config, indent=2, sort_keys=True))
if __name__ == '__main__':
make_config()