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

Commit

Permalink
Answerer examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nickoala committed Feb 6, 2016
1 parent c31e769 commit 75783bd
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
54 changes: 54 additions & 0 deletions examples/answerer_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
import time
import threading
import telepot

"""
$ python2.7 answerer_global.py <token>
Use Answerer globally.
In response to an inline query, it echoes the query string in the returned article.
You can easily check that the latest answer is the one displayed.
"""

def handle(msg):
flavor = telepot.flavor(msg)

if flavor == 'inline_query':
# Just dump inline query to answerer
answerer.answer(msg)

elif flavor == 'chosen_inline_result':
result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
print 'Chosen Inline Result:', result_id, from_id, query_string


def compute_answer(inline_query):
query_id, from_id, query_string = telepot.glance(inline_query, flavor='inline_query')
print '%s: Computing for: %s' % (threading.current_thread().name, query_string)

articles = [{'type': 'article',
'id': 'abc', 'title': query_string, 'message_text': query_string}]
return articles

# You may control positional arguments to bot.answerInlineQuery() by returning a tuple
# return (articles, 60)

# You may control keyword arguments to bot.answerInlineQuery() by returning a dict
# return {'results': articles, 'cache_time': 60}


TOKEN = sys.argv[1] # get token from command-line

bot = telepot.Bot(TOKEN)

# Create the Answerer, give it the compute function.
answerer = telepot.helper.Answerer(bot, compute_answer)

bot.notifyOnMessage(handle)
print 'Listening ...'

# Keep the program running.
while 1:
time.sleep(10)
53 changes: 53 additions & 0 deletions examples/answerer_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import sys
import threading
import telepot
from telepot.delegate import per_inline_from_id, create_open

"""
$ python3.2 answerer_handler.py <token>
Use Answerer within a UserHandler.
In response to an inline query, it echoes the query string in the returned article.
You can easily check that the latest answer is the one displayed.
"""

class InlineHandler(telepot.helper.UserHandler):
def __init__(self, seed_tuple, timeout):
super(InlineHandler, self).__init__(seed_tuple, timeout, flavors=['inline_query', 'chosen_inline_result'])

# Create the Answerer, give it the compute function.
self._answerer = telepot.helper.Answerer(self.bot, self.compute_answer)

def compute_answer(self, inline_query):
query_id, from_id, query_string = telepot.glance(inline_query, flavor='inline_query')
print('%s: Computing for: %s' % (threading.current_thread().name, query_string))

articles = [{'type': 'article',
'id': 'abc', 'title': query_string, 'message_text': query_string}]
return articles

# You may control positional arguments to bot.answerInlineQuery() by returning a tuple
# return (articles, 60)

# You may control keyword arguments to bot.answerInlineQuery() by returning a dict
# return {'results': articles, 'cache_time': 60}

def on_message(self, msg):
flavor = telepot.flavor(msg)

if flavor == 'inline_query':
# Just dump inline query to answerer
self._answerer.answer(msg)

elif flavor == 'chosen_inline_result':
result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
print(self.id, ':', 'Chosen Inline Result:', result_id, from_id, query_string)


TOKEN = sys.argv[1]

bot = telepot.DelegatorBot(TOKEN, [
(per_inline_from_id(), create_open(InlineHandler, timeout=60)),
])
bot.notifyOnMessage(run_forever=True)
53 changes: 53 additions & 0 deletions examples/answerera_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import sys
import asyncio
import telepot
import telepot.async

"""
$ python3.4 skeletona.py <token>
Use Answerer globally.
In response to an inline query, it echoes the query string in the returned article.
You can easily check that the latest answer is the one displayed.
"""

@asyncio.coroutine
def handle(msg):
flavor = telepot.flavor(msg)

if flavor == 'inline_query':
# Just dump inline query to answerer
answerer.answer(msg)

elif flavor == 'chosen_inline_result':
result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
print('Chosen Inline Result:', result_id, from_id, query_string)

def compute_answer(inline_query):
query_id, from_id, query_string = telepot.glance(inline_query, flavor='inline_query')
print('Computing for: %s' % query_string)

articles = [{'type': 'article',
'id': 'abc', 'title': query_string, 'message_text': query_string}]
return articles

# You may control positional arguments to bot.answerInlineQuery() by returning a tuple
# return (articles, 60)

# You may control keyword arguments to bot.answerInlineQuery() by returning a dict
# return {'results': articles, 'cache_time': 60}


TOKEN = sys.argv[1] # get token from command-line

bot = telepot.async.Bot(TOKEN)
loop = asyncio.get_event_loop()

# Create the Answerer, give it the compute function.
answerer = telepot.async.helper.Answerer(bot, compute_answer)

loop.create_task(bot.messageLoop(handle))
print('Listening ...')

loop.run_forever()
65 changes: 65 additions & 0 deletions examples/answerera_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import sys
import asyncio
import telepot
from telepot.delegate import per_inline_from_id
from telepot.async.delegate import create_open

"""
$ python3.4 answerera_handler.py <token>
Use Answerer within a UserHandler.
In response to an inline query, it echoes the query string in the returned article.
You can easily check that the latest answer is the one displayed.
"""

class InlineHandler(telepot.helper.UserHandler):
def __init__(self, seed_tuple, timeout):
super(InlineHandler, self).__init__(seed_tuple, timeout, flavors=['inline_query', 'chosen_inline_result'])

# Create the Answerer, give it the compute function.
self._answerer = telepot.async.helper.Answerer(self.bot, self.compute_answer)

@asyncio.coroutine
def compute_answer(self, inline_query):
query_id, from_id, query_string = telepot.glance(inline_query, flavor='inline_query')
print('Computing for: %s' % query_string)

# Test compute function being a coroutine
yield from asyncio.sleep(1)

articles = [{'type': 'article',
'id': 'abc', 'title': query_string, 'message_text': query_string}]
return articles

# You may control positional arguments to bot.answerInlineQuery() by returning a tuple
# return (articles, 60)

# You may control keyword arguments to bot.answerInlineQuery() by returning a dict
# return {'results': articles, 'cache_time': 60}


@asyncio.coroutine
def on_message(self, msg):
flavor = telepot.flavor(msg)

if flavor == 'inline_query':
# Just dump inline query to answerer
self._answerer.answer(msg)

elif flavor == 'chosen_inline_result':
result_id, from_id, query_string = telepot.glance(msg, flavor=flavor)
print(self.id, ':', 'Chosen Inline Result:', result_id, from_id, query_string)


TOKEN = sys.argv[1]

bot = telepot.async.DelegatorBot(TOKEN, [
(per_inline_from_id(), create_open(InlineHandler, timeout=60)),
])
loop = asyncio.get_event_loop()

loop.create_task(bot.messageLoop())
print('Listening ...')

loop.run_forever()

0 comments on commit 75783bd

Please sign in to comment.