Skip to content

Instantly share code, notes, and snippets.

@akkez
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkez/b396c0e97d27ec2373df to your computer and use it in GitHub Desktop.
Save akkez/b396c0e97d27ec2373df to your computer and use it in GitHub Desktop.
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