Skip to content

Commit

Permalink
tgbot 1.2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
fopina committed Jan 21, 2016
1 parent aedfe66 commit b7bba00
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 189 deletions.
24 changes: 21 additions & 3 deletions plugins/intro.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,37 @@


class IntroPlugin(TGPluginBase):
def __init__(self, intro_text='Hello!', markdown=False):
def __init__(self, intro_text='Hello!', markdown=False, start_menu_builder=None):
super(IntroPlugin, self).__init__()
self._text = intro_text
self._use_markdown = markdown
self._start_menu_builder = start_menu_builder

def list_commands(self):
return (
TGCommandBase('start', self.start, 'Introduction', printable=False),
TGCommandBase('start', self.start, 'introduction', printable=False),
TGCommandBase('help', self.help, 'help', printable=False),
)

def help(self, message, text):
msg = 'You can control me by sending these commands:\n\n'

cmds = self.bot.list_commands()
for ck in cmds:
if ck.printable:
msg += '/%s\n' % ck

self.bot.send_message(message.chat.id, msg)

def start(self, message, text):
keyb = None

if self._start_menu_builder:
keyb = self._start_menu_builder(message.chat)

self.bot.send_message(
message.chat.id,
self._text,
parse_mode='Markdown' if self._use_markdown else None
parse_mode='Markdown' if self._use_markdown else None,
reply_markup=keyb
)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#-e git+https://github.com/fopina/tgbotplug/@v1.2.3#egg=tgbotplug

tgbotplug==1.2.7
tgbotplug==1.2.8
bottle==0.12.8
psycopg2==2.6.1
66 changes: 29 additions & 37 deletions tests/test_intro.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,41 @@
# coding=utf-8
from tgbot import plugintest
from tgbot.botapi import Update
from tgbot import plugintest, pluginbase
from tgbot.botapi import ReplyKeyboardMarkup
from plugins.intro import IntroPlugin


class IntroPluginTest(plugintest.PluginTestCase):
class PluginTest(plugintest.PluginTestCase):
def setUp(self):
self.bot = self.fake_bot('', plugins=[IntroPlugin()])

def test_start_hello(self):
self.bot = self.fake_bot(
'',
plugins=[IntroPlugin()]
)
self.received_id = 1
self.receive_message('/start')
self.assertReplied(self.bot, 'Hello!')
self.assertReplied('Hello!')

def test_start_world_with_menu(self):
def menu(chat):
return ReplyKeyboardMarkup.create(keyboard=[['One']])

def test_start_world(self):
self.bot = self.fake_bot(
'',
plugins=[IntroPlugin(intro_text='World!')]
plugins=[IntroPlugin(intro_text='World!', start_menu_builder=menu)]
)
self.received_id = 1
self.receive_message('/start')
self.assertReplied(self.bot, 'World!')

def receive_message(self, text, sender=None, chat=None):
if sender is None:
sender = {
'id': 1,
'first_name': 'John',
'last_name': 'Doe',
}

if chat is None:
chat = sender

self.bot.process_update(
Update.from_dict({
'update_id': self.received_id,
'message': {
'message_id': self.received_id,
'text': text,
'chat': chat,
'from': sender,
}
})
)
reply = self.pop_reply()
self.assertEqual(reply[1]['text'], 'World!')
self.assertEqual(reply[1]['reply_markup'], {'keyboard': [['One']]})

def test_help(self):
class TestPlugin(pluginbase.TGPluginBase):
def list_commands(self):
return (
pluginbase.TGCommandBase('shoot', None, 'method None breaks for sure'),
)

self.bot = self.fake_bot('', plugins=[IntroPlugin(), TestPlugin()])
self.receive_message('/help')
self.assertReplied(u'''\
You can control me by sending these commands:
self.received_id += 1
/shoot - method None breaks for sure
''')
82 changes: 11 additions & 71 deletions tests/test_priberam.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,38 @@
# coding=utf-8
from tgbot import plugintest
from tgbot.botapi import Update
from plugins.priberam import PriberamPlugin

from requests.packages import urllib3
urllib3.disable_warnings()


class FakeInlineTelegramBot(plugintest.FakeTelegramBot):
# TODO - improve this and add it to tgbotplug
def answer_inline_query(self, inline_query_id, results, cache_time=None, is_personal=None, next_offset=None, **kwargs):
self._sent_messages.append(([inline_query_id, results], kwargs))
self._current_message_id += 1
return FakeInlineTelegramBot.FakeRPCRequest(True)


class PriberamPluginTest(plugintest.PluginTestCase):
def setUp(self):
self.bot = FakeInlineTelegramBot(
self.bot = self.fake_bot(
'',
plugins=[PriberamPlugin()],
inline_query=PriberamPlugin(),
no_command=PriberamPlugin(),
)
self.received_id = 1

def test_need_reply(self):
self.receive_message('/priberam')
self.assertReplied(self.bot, 'Qual palavra?')
self.assertReplied('Qual palavra?')

self.receive_message(u'cerveja')
self.assertReplied(self.bot, u'''\
self.assertReplied(u'''\
*cerveja*
_substantivo feminino_
Bebida levemente alcoólica, feita de cevada e lúpulo.
''')

def test_not_found(self):
self.receive_message('/priberam Supercalifragilisticexpialidocious')
self.assertReplied(self.bot, u'Palavra não encontrada')
self.assertReplied(u'Palavra não encontrada')

def test_complex(self):
self.receive_message(u'/priberam anão')
self.assertReplied(self.bot, u'''\
self.assertReplied(u'''\
*anão*
_adjectivo_
`1.`Que tem estatura muito menor que a regular.
Expand All @@ -53,28 +43,28 @@ def test_complex(self):

def test_inline(self):
self.receive_inline(u'bola')
results = self.last_reply(self.bot)
results = self.pop_reply()[1]['results']
self.assertEqual(
[x.title for x in results],
[x['title'] for x in results],
[u'bola', u'bola de cristal', u'bola de neve', u'bola-ao-cesto', u'bolacha']
)

def test_chat(self):
self.receive_message(u'cerveja')
self.assertReplied(self.bot, u'''\
self.assertReplied(u'''\
*cerveja*
_substantivo feminino_
Bebida levemente alcoólica, feita de cevada e lúpulo.
''')

def test_chat_group(self):
chat = {
'id': 1,
'id': -1,
'title': 'test group',
'type': 'group',
}
self.receive_message(u'cerveja', chat=chat)
self.assertReplied(self.bot, u'''\
self.assertReplied(u'''\
*cerveja*
_substantivo feminino_
Bebida levemente alcoólica, feita de cevada e lúpulo.
Expand All @@ -87,54 +77,4 @@ def test_chat_group_no_text(self):
'type': 'group',
}
self.receive_message(u'', chat=chat)
# no replies
self.assertRaises(AssertionError, self.last_reply, self.bot)

def receive_message(self, text, sender=None, chat=None):
if sender is None:
sender = {
'id': 1,
'first_name': 'John',
'last_name': 'Doe',
}

if chat is None:
chat = {'type': 'private'}
chat.update(sender)

self.bot.process_update(
Update.from_dict({
'update_id': self.received_id,
'message': {
'message_id': self.received_id,
'text': text,
'chat': chat,
'from': sender,
}
})
)

self.received_id += 1

def receive_inline(self, query, sender=None):
if sender is None:
sender = {
'id': 1,
'first_name': 'John',
'last_name': 'Doe',
}

self.bot.process_update(
Update.from_dict({
'update_id': self.received_id,
'message': None,
'inline_query': {
'id': self.received_id,
'query': query,
'from': sender,
'offset': '',
}
})
)

self.received_id += 1
self.assertNoReplies()
81 changes: 4 additions & 77 deletions tests/test_websetup.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,16 @@
# coding=utf-8

from tgbot import plugintest, webserver, botapi
from tgbot import plugintest, webserver
import webtest
import priberambot


class FakeTelegramBotRPCRequest(botapi.TelegramBotRPCRequest):
# TODO - improve this and add it to tgbot.plugintest
QUEUE = []

def _async_call(self):
FakeTelegramBotRPCRequest.QUEUE.append((self.api_method, self.params))
if self.api_method == 'getMe':
result = {
'id': 9999999,
'first_name': 'Test',
'last_name': 'Bot',
'username': 'test_bot'
}
else:
result = {}

if self.on_result is None:
self.result = result
else:
self.result = self.on_result(result)

if self.on_success is not None:
self.on_success(self.result)


class WebTest(plugintest.PluginTestCase):
def setUp(self):
botapi.TelegramBotRPCRequest = FakeTelegramBotRPCRequest
FakeTelegramBotRPCRequest.QUEUE = []
self.bot = priberambot.setup('sqlite:///:memory:', '123')
self.bot = self.prepare_bot(priberambot.setup('sqlite:///:memory:', '123'))
self.bot.setup_db()
self.webapp = webtest.TestApp(webserver.wsgi_app([self.bot]))
self.received_id = 1

def test_ping(self):
self.assertEqual(self.webapp.get('/ping/').text, '<b>Pong!</b>')

def test_update_invalid_token(self):
with self.assertRaisesRegexp(webtest.app.AppError, 'Bad response: 404 Not Found'):
self.webapp.post_json('/update/invalid', params=self.build_update('hello'))

def test_web(self):
self.assertEqual(len(FakeTelegramBotRPCRequest.QUEUE), 0)
self.webapp.post_json('/update/123', params=self.build_update(u'hello'))
self.assertTrue(len(FakeTelegramBotRPCRequest.QUEUE))
self.assertEqual(FakeTelegramBotRPCRequest.QUEUE[-1][0], 'sendMessage')
self.assertEqual(FakeTelegramBotRPCRequest.QUEUE[-1][1]['text'], u'Palavra não encontrada')

def build_update(self, text, sender=None, chat=None, reply_to_message_id=None):
if sender is None:
sender = {
'id': 1,
'first_name': 'John',
'last_name': 'Doe',
}

if chat is None:
chat = {'type': 'private'}
chat.update(sender)

reply_to_message = None

if reply_to_message_id is not None:
reply_to_message = {
'message_id': reply_to_message_id,
'chat': chat,
}

update = {
'update_id': self.received_id,
'message': {
'message_id': self.received_id,
'text': text,
'chat': chat,
'from': sender,
'reply_to_message': reply_to_message,
}
}

self.received_id += 1

return update
self.webapp.post_json('/update/123', params=self.build_message(u'Supercalifragilisticexpialidocious'))
self.assertReplied(u'Palavra não encontrada')

0 comments on commit b7bba00

Please sign in to comment.