Skip to content

Instantly share code, notes, and snippets.

@akkez

akkez/tornado-bot.py

Last active Aug 29, 2015
Embed
What would you like to do?
Telegram bot example
curl https://api.telegram.org/bot<creds>/setWebhook?url=https://domain.me:8443/
import requests
import json
from enum import Enum
import tornado.httpserver
import tornado.ioloop
import tornado.web
key = '1234:DEADBEEF'
class I(Enum):
states = {}
class answer(tornado.web.RequestHandler):
# def get(self):
# self.write("hello")
def post(self):
self.write("ok")
print("POST: ", self.request.body)
req = json.loads(self.request.body.decode("utf-8"))
data = {"text": "Hi!", "chat_id": req['message']['chat']['id']}
requests.get("https://api.telegram.org/bot" + key + "/sendMessage", data=data)
print("ok to", req['message']['chat']['id'])
application = tornado.web.Application([
(r'/', answer),
])
if __name__ == '__main__':
http_server = tornado.httpserver.HTTPServer(application, ssl_options={
"certfile": "/home/.../meme.crt",
"keyfile": "/home/.../meme.key",
# https://buy.wosign.com/free/
})
http_server.listen(8443)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.