Skip to content
This repository has been archived by the owner on Apr 12, 2021. It is now read-only.

Commit

Permalink
Webhook examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nickoala committed Dec 28, 2015
1 parent 5046172 commit c0c1471
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
57 changes: 57 additions & 0 deletions examples/webhook_aiohttp_countera.py
Original file line number Diff line number Diff line change
@@ -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 <token> <listening_port> <webhook_url>
"""

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
47 changes: 47 additions & 0 deletions examples/webhook_aiohttp_skeletona.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import sys
import asyncio
from aiohttp import web
import telepot
import telepot.async

"""
$ python3.4 webhook_aiohttp_skeletona.py <token> <listening_port> <webhook_url>
"""

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
44 changes: 44 additions & 0 deletions examples/webhook_flask_counter.py
Original file line number Diff line number Diff line change
@@ -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 <token> <listening_port> <webhook_url>
"""

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)
37 changes: 37 additions & 0 deletions examples/webhook_flask_skeleton.py
Original file line number Diff line number Diff line change
@@ -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 <token> <listening_port> <webhook_url>
"""

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)

0 comments on commit c0c1471

Please sign in to comment.