diff --git a/examples/webhook_aiohttp_countera.py b/examples/webhook_aiohttp_countera.py new file mode 100644 index 0000000..bf7aac3 --- /dev/null +++ b/examples/webhook_aiohttp_countera.py @@ -0,0 +1,57 @@ +import sys +import asyncio +from aiohttp import web +import telepot +import telepot.async +from telepot.delegate import per_chat_id +from telepot.async.delegate import create_open + +""" +$ python3.4 webhook_aiohttp_countera.py +""" + +class MessageCounter(telepot.helper.ChatHandler): + def __init__(self, seed_tuple, timeout): + super(MessageCounter, self).__init__(seed_tuple, timeout) + self._count = 0 + + @asyncio.coroutine + def on_message(self, msg): + self._count += 1 + yield from self.sender.sendMessage(self._count) + +TOKEN = sys.argv[1] +PORT = int(sys.argv[2]) +URL = sys.argv[3] + +bot = telepot.async.DelegatorBot(TOKEN, [ + (per_chat_id(), create_open(MessageCounter, timeout=10)), +]) +update_queue = asyncio.Queue() # channel between web app and bot + +@asyncio.coroutine +def webhook(request): + data = yield from request.text() + yield from update_queue.put(data) # pass update to bot + return web.Response(body='OK'.encode('utf-8')) + +@asyncio.coroutine +def init(loop): + app = web.Application(loop=loop) + app.router.add_route('GET', '/abc', webhook) + app.router.add_route('POST', '/abc', webhook) + + srv = yield from loop.create_server(app.make_handler(), '0.0.0.0', PORT) + print("Server started ...") + + yield from bot.setWebhook(URL) + + return srv + +loop = asyncio.get_event_loop() +loop.run_until_complete(init(loop)) +loop.create_task(bot.messageLoop(source=update_queue)) # take updates from queue +try: + loop.run_forever() +except KeyboardInterrupt: + pass diff --git a/examples/webhook_aiohttp_skeletona.py b/examples/webhook_aiohttp_skeletona.py new file mode 100644 index 0000000..9a388b2 --- /dev/null +++ b/examples/webhook_aiohttp_skeletona.py @@ -0,0 +1,47 @@ +import sys +import asyncio +from aiohttp import web +import telepot +import telepot.async + +""" +$ python3.4 webhook_aiohttp_skeletona.py +""" + +def handle(msg): + content_type, chat_type, chat_id = telepot.glance2(msg) + print(content_type, chat_type, chat_id) + +TOKEN = sys.argv[1] +PORT = int(sys.argv[2]) +URL = sys.argv[3] + +bot = telepot.async.Bot(TOKEN) +update_queue = asyncio.Queue() # channel between web app and bot + +@asyncio.coroutine +def webhook(request): + data = yield from request.text() + yield from update_queue.put(data) # pass update to bot + return web.Response(body='OK'.encode('utf-8')) + +@asyncio.coroutine +def init(loop): + app = web.Application(loop=loop) + app.router.add_route('GET', '/abc', webhook) + app.router.add_route('POST', '/abc', webhook) + + srv = yield from loop.create_server(app.make_handler(), '0.0.0.0', PORT) + print("Server started ...") + + yield from bot.setWebhook(URL) + + return srv + +loop = asyncio.get_event_loop() +loop.run_until_complete(init(loop)) +loop.create_task(bot.messageLoop(handle, source=update_queue)) # take updates from queue +try: + loop.run_forever() +except KeyboardInterrupt: + pass diff --git a/examples/webhook_flask_counter.py b/examples/webhook_flask_counter.py new file mode 100644 index 0000000..95b15df --- /dev/null +++ b/examples/webhook_flask_counter.py @@ -0,0 +1,44 @@ +import sys +from flask import Flask, request +import telepot +from telepot.delegate import per_chat_id, create_open + +try: + from Queue import Queue +except ImportError: + from queue import Queue + +""" +$ python3.4 webhook_flask_counter.py +""" + +class MessageCounter(telepot.helper.ChatHandler): + def __init__(self, seed_tuple, timeout): + super(MessageCounter, self).__init__(seed_tuple, timeout) + self._count = 0 + + def on_message(self, msg): + self._count += 1 + self.sender.sendMessage(self._count) + + +TOKEN = sys.argv[1] +PORT = int(sys.argv[2]) +URL = sys.argv[3] + +app = Flask(__name__) +update_queue = Queue() # channel between `app` and `bot` + +bot = telepot.DelegatorBot(TOKEN, [ + (per_chat_id(), create_open(MessageCounter, timeout=10)), +]) +bot.notifyOnMessage(source=update_queue) # take updates from queue + +@app.route('/abc', methods=['GET', 'POST']) +def pass_update(): + update_queue.put(request.data) # pass update to bot + return 'OK' + +if __name__ == '__main__': + bot.setWebhook(URL) + app.run(port=PORT, debug=True) diff --git a/examples/webhook_flask_skeleton.py b/examples/webhook_flask_skeleton.py new file mode 100644 index 0000000..b78ea9b --- /dev/null +++ b/examples/webhook_flask_skeleton.py @@ -0,0 +1,37 @@ +import sys +from flask import Flask, request +import telepot + +try: + from Queue import Queue +except ImportError: + from queue import Queue + +""" +$ python2.7 webhook_flask_skeleton.py +""" + +def handle(msg): + print msg + content_type, chat_type, chat_id = telepot.glance2(msg) + print content_type, chat_type, chat_id + + +TOKEN = sys.argv[1] +PORT = int(sys.argv[2]) +URL = sys.argv[3] + +app = Flask(__name__) +bot = telepot.Bot(TOKEN) +update_queue = Queue() # channel between `app` and `bot` + +bot.notifyOnMessage(handle, source=update_queue) # take updates from queue + +@app.route('/abc', methods=['GET', 'POST']) +def pass_update(): + update_queue.put(request.data) # pass update to bot + return 'OK' + +if __name__ == '__main__': + bot.setWebhook(URL) + app.run(port=PORT, debug=True)