Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachments not working #73

Closed
ralgozino opened this issue Sep 25, 2018 · 4 comments
Closed

Attachments not working #73

ralgozino opened this issue Sep 25, 2018 · 4 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@ralgozino
Copy link

ralgozino commented Sep 25, 2018

Describe the bug
Error while trying to reply using reply_webapi() method.

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/mmpy_bot/dispatcher.py", line 89, in dispatch_msg
    func(Message(self._client, msg, self._pool), *args)
  File "/home/administrator/haitiano/it_plugins/epec_plugin.py", line 46, in cortes_epec
    icon_url='https://www.epec.com.ar/favicon.ico',
  File "/usr/local/lib/python3.5/dist-packages/mmpy_bot/dispatcher.py", line 255, in reply_webapi
    self.send_webapi(self._gen_reply(text), *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/mmpy_bot/dispatcher.py", line 258, in send_webapi
    url = self._get_webhook_url_by_id(self._get_first_webhook())
  File "/usr/local/lib/python3.5/dist-packages/mmpy_bot/dispatcher.py", line 247, in _get_first_webhook
    return hooks[0].get('id')
KeyError: 0

How To Reproduce
Create a plugin that replys to some keyword, try to answer with an attachment as shown in the readme:

from mmpy_bot.bot import respond_to


@respond_to('webapi')
def webapi_reply(message):
    attachments = [{
        'fallback': 'Fallback text',
        'author_name': 'Author',
        'author_link': 'http://github.com',
        'text': 'Some text here ...',
        'color': '#59afe1'
    }]
    message.reply_webapi(
        'Attachments example', attachments,
        username='Mattermost-Bot',
        icon_url='https://goo.gl/OF4DBq',
    )
    # Optional: Send message to specified channel
    # message.send_webapi('', attachments, channel_id=message.channel)

Expected behavior
The message should be answered with an attachment.

Operating Environment (please complete the following information):

  • OS: Ubuntu 16.04
  • Python Version: 3.5.2
  • Mattermost Version: 5.1.1
  • mmpy_bot Version: 1.2.1

Additional context
Add any other context about the problem here [e.g. Settings for your bot, API Version]

@seLain seLain added the bug Something isn't working label Sep 28, 2018
@seLain seLain self-assigned this Sep 28, 2018
@seLain
Copy link
Collaborator

seLain commented Sep 28, 2018

@ralgozino Could you check if the bot account has permission required for listing webhooks ?

According to MM APIv4 doc:

List incoming webhooks :
Get a page of a list of incoming webhooks. Optionally filter for a specific team using query parameters.

Permissions :
manage_webhooks for the system or manage_webhooks for the specific team.

reference: https://api.mattermost.com/#tag/webhooks%2Fpaths%2F~1hooks~1incoming%2Fget

@seLain seLain added question Further information is requested and removed bug Something isn't working labels Sep 28, 2018
@ralgozino
Copy link
Author

Hello!
Sorry for the delay.. I had to make the bot user administrator in order to bypass the error. But then, I found another error, I pinged the bot in a team's offtopic channel but the bot answered in anothers team off topic channel :(

@seLain
Copy link
Collaborator

seLain commented Oct 4, 2018

@ralgozino Thanks for the feedback.

It seems the unexpected behavior happens when there are multiple channels with incoming hooks of different teams. It might be a bug. I am looking into it.

@seLain seLain added the bug Something isn't working label Oct 4, 2018
seLain added a commit that referenced this issue Oct 5, 2018
- A fix to issue #73
- Previously if there is not webhook found, mmpy_bot will try to create one for specified team. This is considered inappropriate and removed.
- Support `webhook_id` parameter at `reply_webapi` and `send_webapi` method call
- Document will be updated later if no further fix needed
@seLain
Copy link
Collaborator

seLain commented Oct 5, 2018

hi @ralgozino I made a PR #74 and it's merged to master.
Please check out the latest master to see if it fixed this issue.
We should now be able to specify webhook_id correctly in these two ways:

Method 1: Specify webhook_id as parameter

You can specify webhook_id at reply_webapi and send_webapi method call.
The webhook_id can be generated and acquired from Mattermost Contol Panel.
It is also possible to send messages to different teams/channels in the same message handler, as long as you got needed webhook_ids.

from mmpy_bot.bot import respond_to


@respond_to('reply')
def reply(message):
    attachments = [{
        'fallback': 'Fallback text',
        'author_name': 'Author',
        'author_link': 'http://github.com',
        'text': 'Some text here ...',
        'color': '#59afe1'
    }]
    message.reply_webapi(
        'Response to team I got message from.',
        attachments,
        webhook_id='p7tuwghy37r63jp4nf3tsopque',
    )
    # Optional: Send message to specified channel
    message.send_webapi(
        'Response to another team.',
        attachments,
        webhook_id='aib7mnahsfy5zrt6tf3ycbghic',
    )

Method 2: Specify WEBHOOK_ID in settings.py or local_settings.py

You can also set WEBHOOK_ID in settings.py or local_settings.py.
The WEBHOOK_ID will serve as default webhook id to send message via webhook API if webhook_id is not given at reply_webapi or send_webapi method call.

import os

PLUGINS = [
    'my_plugins',
]

BOT_URL = os.environ.get("BOT_URL", 'http://your_server_dn/api/v4')
BOT_LOGIN = os.environ.get("DRIVERBOT_LOGIN", '[email protected]')
BOT_NAME = os.environ.get("DRIVERBOT_NAME", 'bot')
BOT_PASSWORD = os.environ.get("DRIVERBOT_PASSWORD", 'passwd')

# this team name should be the same as in driver_settings
BOT_TEAM = os.environ.get("BOT_TEAM", 'test_team')

# default public channel name
BOT_CHANNEL = os.environ.get("BOT_CHANNEL", 'off-topic')

# a private channel in BOT_TEAM
BOT_PRIVATE_CHANNEL = os.environ.get("BOT_PRIVATE_CHANNEL", 'test')

SSL_VERIFY = True

# default webhook_id of test_team/off-topic
WEBHOOK_ID = 'p7tuwghy37r63jp4nf3tsopque'

Notice

  • If neither webhook_id (as parameter) nor WEBHOOK_ID (in settings.py) is given, the message will not be send, and a warning will be added to logging.

  • The document will be updated later if there is no further fix needed.

@attzonko attzonko closed this as completed Oct 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants