Last active
August 29, 2015 14:23
-
-
Save akkez/b396c0e97d27ec2373df to your computer and use it in GitHub Desktop.
Telegram bot example
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
curl https://api.telegram.org/bot<creds>/setWebhook?url=https://domain.me:8443/ |
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 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