X Tutup
Skip to content

Commit cd42524

Browse files
committed
Merge pull request python-telegram-bot#174 from python-telegram-bot/inlinebots
Implement Inlinebots
2 parents ee56ffc + b5875a3 commit cd42524

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1778
-450
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ install:
1111
- pip install -r requirements.txt
1212
- pip install -r requirements-dev.txt
1313
script:
14-
- nosetests -v --with-coverage --cover-package telegram/
14+
- nosetests -v --with-flaky --no-flaky-report --with-coverage --cover-package=telegram/
1515
- flake8 telegram
1616
- 'if [[ $TRAVIS_PYTHON_VERSION != 2.6 ]]; then pylint -E telegram --disable=no-name-in-module,import-error; fi'
1717
after_success:

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
**2016-01-09**
2+
3+
*Released 3.3b1*
4+
5+
- Implement inline bots (beta)
6+
17
**2016-01-05**
28

39
*Released 3.2.0*

README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ Let's add some functionality to our bot. We want to add the ``/caps`` command, t
227227
...
228228
>>> dispatcher.addTelegramCommandHandler('caps', caps)
229229

230+
To enable our bot to respond to inline queries, we can add the following (you will also have to talk to BotFather)::
231+
232+
>>> from telegram import InlineQueryResultArticle
233+
>>> def inline_caps(bot, update):
234+
... # If you activated inline feedback, updates might either contain
235+
... # inline_query or chosen_inline_result, the other one will be None
236+
... if update.inline_query:
237+
... query = bot.update.inline_query.query
238+
... results = list()
239+
... results.append(InlineQueryResultArticle(query.upper(), 'Caps', text_caps))
240+
... bot.answerInlineQuery(update.inline_query.id, results)
241+
...
242+
>>> dispatcher.addTelegramInlineHandler(inline_caps)
243+
230244
Now it's time to stop the bot::
231245

232246
>>> updater.stop()

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858
# built documents.
5959
#
6060
# The short X.Y version.
61-
version = '3.2'
61+
version = '3.3'
6262
# The full version, including alpha/beta/rc tags.
63-
release = '3.2.0'
63+
release = '3.3b1'
6464

6565
# The language for content autogenerated by Sphinx. Refer to documentation
6666
# for a list of supported languages.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
telegram.choseninlineresult module
2+
==================================
3+
4+
.. automodule:: telegram.choseninlineresult
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
telegram.inlinequery module
2+
===========================
3+
4+
.. automodule:: telegram.inlinequery
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
telegram.inlinequeryresult module
2+
=================================
3+
4+
.. automodule:: telegram.inlinequeryresult
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

docs/source/telegram.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Submodules
1212
telegram.updater
1313
telegram.dispatcher
1414
telegram.jobqueue
15+
telegram.inlinequery
16+
telegram.inlinequeryresult
17+
telegram.choseninlineresult
1518
telegram.chataction
1619
telegram.contact
1720
telegram.document

examples/inlinebot.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Simple Bot to reply to Telegram messages
5+
# This program is dedicated to the public domain under the CC0 license.
6+
7+
"""
8+
This Bot uses the Updater class to handle the bot.
9+
10+
First, a few handler functions are defined. Then, those functions are passed to
11+
the Dispatcher and registered at their respective places.
12+
Then, the bot is started and runs until we press Ctrl-C on the command line.
13+
14+
Usage:
15+
Basic inline bot example. Applies different text transformations.
16+
Press Ctrl-C on the command line or send a signal to the process to stop the
17+
bot.
18+
"""
19+
from random import getrandbits
20+
21+
import re
22+
23+
from telegram import Updater, Update, InlineQueryResultArticle, ParseMode
24+
import logging
25+
26+
# Enable logging
27+
logging.basicConfig(
28+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
29+
level=logging.INFO)
30+
31+
logger = logging.getLogger(__name__)
32+
33+
34+
# Define a few command handlers. These usually take the two arguments bot and
35+
# update. Error handlers also receive the raised TelegramError object in error.
36+
def start(bot, update):
37+
bot.sendMessage(update.message.chat_id, text='Hi!')
38+
39+
40+
def help(bot, update):
41+
bot.sendMessage(update.message.chat_id, text='Help!')
42+
43+
44+
def escape_markdown(text):
45+
"""Helper function to escape telegram markup symbols"""
46+
escape_chars = '\*_`\['
47+
return re.sub(r'([%s])' % escape_chars, r'\\\1', text)
48+
49+
50+
def inlinequery(bot, update):
51+
if update.inline_query is not None and update.inline_query.query:
52+
query = update.inline_query.query
53+
results = list()
54+
55+
results.append(InlineQueryResultArticle(
56+
id=hex(getrandbits(64))[2:],
57+
title="Caps",
58+
message_text=query.upper()))
59+
60+
results.append(InlineQueryResultArticle(
61+
id=hex(getrandbits(64))[2:],
62+
title="Bold",
63+
message_text="*%s*" % escape_markdown(query),
64+
parse_mode=ParseMode.MARKDOWN))
65+
66+
results.append(InlineQueryResultArticle(
67+
id=hex(getrandbits(64))[2:],
68+
title="Italic",
69+
message_text="_%s_" % escape_markdown(query),
70+
parse_mode=ParseMode.MARKDOWN))
71+
72+
bot.answerInlineQuery(update.inline_query.id, results=results)
73+
74+
75+
76+
def error(bot, update, error):
77+
logger.warn('Update "%s" caused error "%s"' % (update, error))
78+
79+
80+
def main():
81+
# Create the Updater and pass it your bot's token.
82+
updater = Updater("TOKEN")
83+
84+
# Get the dispatcher to register handlers
85+
dp = updater.dispatcher
86+
87+
# on different commands - answer in Telegram
88+
dp.addTelegramCommandHandler("start", start)
89+
dp.addTelegramCommandHandler("help", help)
90+
91+
# on noncommand i.e message - echo the message on Telegram
92+
dp.addTelegramInlineHandler(inlinequery)
93+
94+
# log all errors
95+
dp.addErrorHandler(error)
96+
97+
# Start the Bot
98+
updater.start_polling()
99+
100+
# Block until the user presses Ctrl-C or the process receives SIGINT,
101+
# SIGTERM or SIGABRT. This should be used most of the time, since
102+
# start_polling() is non-blocking and will stop the bot gracefully.
103+
updater.idle()
104+
105+
if __name__ == '__main__':
106+
main()

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ nose
33
pep257
44
pylint
55
unittest2
6+
flaky

0 commit comments

Comments
 (0)
X Tutup