Skip to content

Commit

Permalink
send simple image
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzhak committed Apr 30, 2017
1 parent b2120a5 commit 3921827
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions botstory/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ async def say(self, body, user, options):
return await self.send_text_message_to_all_interfaces(
recipient=user, text=body, options=options)

async def send_image(self, url, user):
tasks = [interface.send_image(user, url) for _, interface in self.interfaces.items()]
return [body for body in await asyncio.gather(*tasks)]

async def send_template(self, payload, user):
tasks = [interface.send_template(recipient=user,
payload=payload)
Expand Down
19 changes: 18 additions & 1 deletion botstory/chat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def mock_interface(mocker):
'send_list',
aiohttp.test_utils.make_mocked_coro('something'),
)
mocker.patch.object(
botstory.integrations.fb.messenger.FBInterface,
'send_image',
aiohttp.test_utils.make_mocked_coro('something'),
)
mocker.patch.object(
botstory.integrations.fb.messenger.FBInterface,
'send_template',
Expand Down Expand Up @@ -199,7 +204,7 @@ async def then(ctx):


@pytest.mark.asyncio
async def test_should_send_tempalate_based_message(mock_interface):
async def test_should_send_template_based_message(mock_interface):
with answer.Talk() as talk:
story = talk.story
story.use(mock_interface)
Expand Down Expand Up @@ -255,3 +260,15 @@ async def test_should_send_tempalate_based_message(mock_interface):

mock_interface.send_template.assert_called_once_with(recipient=talk.user,
payload=payload)


@pytest.mark.asyncio
async def test_send_image(mock_interface):
with answer.Talk() as talk:
story = talk.story
story.use(mock_interface)

await story.send_image('http://some.ua/image.gif',
user=talk.user)

mock_interface.send_image.assert_called_once_with(talk.user, 'http://some.ua/image.gif')
21 changes: 21 additions & 0 deletions botstory/integrations/fb/messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ async def send_list(self, recipient, elements, buttons=None, options=None):
'buttons': buttons,
})

async def send_image(self, recipient, url):
return await self.http.post(
self.api_uri + '/me/messages/',
params={
'access_token': self.token,
},
json={
'recipient': {
'id': recipient['facebook_user_id'],
},

'message': {
'attachment': {
'type': 'image',
'payload': {
'url': url,
},
},
},
})

async def send_template(self, recipient, payload):
"""
send template based message
Expand Down
29 changes: 29 additions & 0 deletions botstory/integrations/fb/messenger_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,35 @@ async def test_should_send_template_based_message():
)


@pytest.mark.asyncio
async def test_send_image():
with answer.Talk() as talk:
story = talk.story
fb_interface = story.use(messenger.FBInterface(page_access_token='qwerty1'))
mock_http = story.use(mockhttp.MockHttpInterface())
await story.start()
await fb_interface.send_image(talk.user, 'http://shevchenko.ua/image.gif')
mock_http.post.assert_called_with(
'https://graph.facebook.com/v2.6/me/messages/',
params={
'access_token': 'qwerty1',
},
json={
'message': {
'attachment': {
'type': 'image',
'payload': {
'url': 'http://shevchenko.ua/image.gif',
},
}
},
'recipient': {
'id': talk.user['facebook_user_id'],
},
}
)


@pytest.mark.asyncio
async def test_integration():
user = utils.build_fake_user()
Expand Down
3 changes: 3 additions & 0 deletions botstory/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ async def ask(self, body, quick_replies=None, options=None, user=None):
async def list_elements(self, elements=None, buttons=None, user=None, options=None):
return await self.chat.list_elements(elements, buttons, user, options)

async def send_image(self, url, user):
return await self.chat.send_image(url, user)

async def send_template(self, payload, user):
"""
send template based message (button, generic, list, receipt, airline and etc
Expand Down

0 comments on commit 3921827

Please sign in to comment.