This repository has been archived by the owner on Apr 12, 2021. It is now read-only.
forked from nickoala/telepot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
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
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 |
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
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) |
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
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) |