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

added instagram channel code #9594

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
2453c80
added instagram channel code
Horizon733 Sep 9, 2021
f59b803
Merge branch 'main' into instagram-integration
Horizon733 Sep 9, 2021
a5877f9
changelog md file added
Horizon733 Sep 9, 2021
f15002f
Merge remote-tracking branch 'origin/instagram-integration' into inst…
Horizon733 Sep 9, 2021
c885433
changelog md file added
Horizon733 Sep 9, 2021
64238e8
Merge branch 'main' into instagram-integration
Horizon733 Sep 15, 2021
4a0a645
Merge branch 'main' into instagram-integration
Horizon733 Sep 19, 2021
cd00310
added docstring, renamed classes to prevent conflict with facebook.py…
Horizon733 Sep 21, 2021
25463dc
Merge branch 'main' into instagram-integration
Horizon733 Sep 21, 2021
8849194
Merge branch 'main' into instagram-integration
Horizon733 Oct 23, 2021
c93d462
Merge branch 'main' into instagram-integration
Horizon733 Nov 2, 2021
33112ab
added instagram channel code
Horizon733 Sep 9, 2021
26cdd26
changelog md file added
Horizon733 Sep 9, 2021
f0594c2
changelog md file added
Horizon733 Sep 9, 2021
be6b97c
added docstring, renamed classes to prevent conflict with facebook.py…
Horizon733 Sep 21, 2021
b619d18
Merge remote-tracking branch 'origin/instagram-integration' into inst…
Horizon733 Nov 11, 2021
fab3f1d
modified facebook.py to messenger.py to support Instagram and Faceboo…
Horizon733 Nov 12, 2021
b64700f
modified facebook.py to messenger.py to support Instagram and Faceboo…
Horizon733 Nov 12, 2021
f93fe67
Merge branch 'main' of https://github.com/Horizon733/rasa into instag…
Horizon733 Nov 12, 2021
d22236b
Merge branch 'main' into instagram-integration
Horizon733 Nov 13, 2021
85bf4dd
Merge branch 'main' into instagram-integration
Horizon733 Nov 16, 2021
6242ed7
Merge branch 'main' into instagram-integration
Horizon733 Nov 17, 2021
2ff994e
fixed typo in README.md
Horizon733 Nov 21, 2021
c804cc3
Merge remote-tracking branch 'origin/instagram-integration' into inst…
Horizon733 Nov 21, 2021
d37424b
Merge branch 'main' into instagram-integration
Horizon733 Nov 21, 2021
83d7775
merge and conflicts resolve from Main branch
Horizon733 Jan 5, 2022
6d07ca3
Merge branch 'main' into instagram-integration
Horizon733 Jan 5, 2022
e53c35e
Merge branch 'main' into instagram-integration
Horizon733 Jan 5, 2022
b8e6f03
Merge branch 'main' into instagram-integration
Horizon733 Jan 10, 2022
6e0107c
Merge branch 'main' of https://github.com/Horizon733/rasa into instag…
Horizon733 Jan 17, 2022
cd33e00
Merge remote-tracking branch 'origin/instagram-integration' into inst…
Horizon733 Jan 17, 2022
e4e4e8a
Merge branch 'main' of https://github.com/Horizon733/rasa into instag…
Horizon733 Jun 7, 2022
f6cae03
Merge branch 'main' into instagram-integration
Horizon733 Jun 13, 2022
d8969a9
Merge branch 'main' into instagram-integration
Horizon733 Jul 10, 2022
9fb26ee
added few updates in docs and added tests for messenger
Horizon733 Jul 14, 2022
873cde3
Merge branch 'main' into instagram-integration
Horizon733 Jul 14, 2022
91f476e
Merge branch 'main' into instagram-integration
Horizon733 Jul 21, 2022
674a1ed
added few updates in docs and added tests for messenger
Horizon733 Jul 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 4 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
repos:
- repo: https://github.com/ambv/black
rev: 19.10b0
rev: 21.9b0
hooks:
- id: black
- repo: https://github.com/thlorenz/doctoc
rev: master
rev: v2.0.1
hooks:
- id: doctoc
files: "CONTRIBUTING.md"
- repo: local
hooks:
- id: docstring-check
name: docstring-check
entry: bash -c 'make lint-docstrings'
language: system
types: [python]
pass_filenames: false
- id: doctoc
files: "CONTRIBUTING.md"
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ updates to 3.0. Read more about [our contributor guidelines](#how-to-contribute)
<img align="right" height="244" src="https://www.rasa.com/assets/img/sara/sara-open-source-2.0.png" alt="An image of Sara, the Rasa mascot bird, holding a flag that reads Open Source with one wing, and a wrench in the other" title="Rasa Open Source">

Rasa is an open source machine learning framework to automate text-and voice-based conversations. With Rasa, you can build contextual assistants on:
- Facebook Messenger
- Slack
- Facebook Messengcd - Slack
Horizon733 marked this conversation as resolved.
Show resolved Hide resolved
- Google Hangouts
- Webex Teams
- Microsoft Bot Framework
Expand Down
1 change: 1 addition & 0 deletions changelog/9594.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`Instagram` channel integration
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,31 @@ You need to set up a Facebook app and a page.
**messaging_postback** subscriptions. Insert your callback URL, which will
look like `https://<host>:<port>/webhooks/facebook/webhook`, replacing
the host and port with the appropriate values from your running Rasa X or Rasa Open Source server.


7. Insert the **messenger service** as **facebook** when using Facebook Messenger

Insert the **Verify Token** which has to match the `verify`
entry in your `credentials.yml`.

:::note configure https
Facebook Messenger only forwards
messages to endpoints via `https`, so take appropriate measures to add
it to your setup. For local testing of your bot, see [Testing Channels on Your Local Machine](../messaging-and-voice-channels.mdx#testing-channels-on-your-local-machine).
it to your setup.
:::

For more detailed steps, visit the
[Messenger docs](https://developers.facebook.com/docs/graph-api/webhooks).

### Running On Facebook Messenger

Add the Facebook credentials to your `credentials.yml`:
Add the Messenger credentials to your `credentials.yml`:

```yaml-rasa
facebook:
messenger:
verify: "rasa-bot"
secret: "3e34709d01ea89032asdebfe5a74518"
page-access-token: "EAAbHPa7H9rEBAAuFk4Q3gPKbDedQnx4djJJ1JmQ7CAqO4iJKrQcNT0wtD"
messenger-service: "facebook"
```


Expand Down Expand Up @@ -137,3 +140,117 @@ Both Quick Reply and Button titles in Facebook Messenger have a character limit
- title: Example button F
payload: /example_intent
```

## Instagram Setup
1. You first need to create a Facebook page and Instagram business page.

2. Go to [Instagram](https://www.instagram.com) and under **Settings** you will
find **Accounts Center**, click on it and connect your facebook account with instagram.

3. Now, visit [Facebook](https://www.facebook.com/) on left side click on **Pages**
scroll down on left side and click on **Settings** again scroll down on left side menu
and click on **Instagram**. Link your instagram page with Facebook page.

4. Create/use app for Messenger service on [Facebook Developers](https://www.developers.facebook.com).

5. Once you have them you can add these to your `credentials.yml`.

### Getting Credentials
**How to get the Instagram credentials:**
You need to set up a Facebook app, a facebook page and a Instagram page.

1. To create the app head over to
[Facebook for Developers](https://developers.facebook.com/)
and click on **My Apps** → **Add New App**.

2. Go onto the dashboard for the app and under **Products**,
find the **Messenger** section and click **Set Up**. Scroll down to
**Token Generation** and click on the link to create a new page for your
app.

3. Go to **Messenger** -> **Instagram Settings** Create your facebook page
and select it in the dropdown menu for the **Token Generation**. The
shown **Page Access Token** is the `page-access-token` needed later on.

4. Locate the **App Secret** in the app dashboard under **Settings** → **Basic**.
This will be your `secret`.

5. Use the collected `secret` and `page-access-token` in your
`credentials.yml`, and add a field called `verify` containing
a string of your choice. Start `rasa run` with the
`--credentials credentials.yml` option.

6. Set up a **Webhook** and select at least the **messaging** and
**messaging_postback** subscriptions. Insert your callback URL, which will
look like `https://<host>:<port>/webhooks/messenger/webhook`, replacing
the host and port with the appropriate values from your running Rasa X or Rasa Open Source server.

7. Insert the **messenger service** as **instagram** when using Facebook Messenger

Insert the **Verify Token** which has to match the `verify`
entry in your `credentials.yml`.

### Running On Instagram Messenger

Add the messenger credentials to your `credentials.yml`:

```yaml-rasa
messenger:
verify: "rasa-bot"
secret: "3e34709d01ea89032asdebfe5a74518"
page-access-token: "EAAbHPa7H9rEBAAuFk4Q3gPKbDedQnx4djJJ1JmQ7CAqO4iJKrQcNT0wtD"
messenger-service: "instagram"
```

## Supported response attachments

In addition to typical text, image, and custom responses, the Instagram Messenger
channel supports the following additional response attachments:

* [Quick Replies](https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies)
provide a way to present a set of up to 13 buttons in-conversation that contain a
title and optional image, and appear prominently above the composer. You can also
use quick replies to request a person's email address or phone number.

```yaml-rasa
utter_fb_quick_reply_example:
- text: Hello World!
quick_replies:
- title: Text quick reply
payload: /example_intent
- title: Image quick reply
payload: /example_intent
image_url: http://example.com/img/red.png
# below are Facebook provided quick replies
# the title and payload will be filled
# with the user's information from their profile
- content_type: user_email
title:
payload:
- content_type: user_phone_number
title:
payload:
```

:::note
Both Quick Reply titles in Messenger Messenger have a character limit of
20. Titles longer than 20 characters will be truncated.
:::

### Running Facebook and Instagram Messenger
1. Follow **Facebook** and **Instagram** setup till the end except **Step 7**

2. Insert the **messenger service** as **facebook/instagram** when using Facebook Messenger and Instagram Messenger

```yaml-rasa
messenger:
verify: "rasa-bot"
secret: "3e34709d01ea89032asdebfe5a74518"
page-access-token: "EAAbHPa7H9rEBAAuFk4Q3gPKbDedQnx4djJJ1JmQ7CAqO4iJKrQcNT0wtD"
messenger-service: "facebook/instagram"
```

:::note Buttons usage
Instagram doesn't supports buttons, so we are converting them to quick replies.
While, using messenger for facebook buttons will be converted to quick replies.
:::
2 changes: 1 addition & 1 deletion docs/docs/messaging-and-voice-channels.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Learn how to make your assistant available on:

* [Your Own Website](./connectors/your-own-website.mdx)

* [Facebook Messenger](./connectors/facebook-messenger.mdx)
* [Facebook Messenger](./connectors/facebook-and-instagram-messenger.mdx)

* [Slack](./connectors/slack.mdx)

Expand Down
26 changes: 18 additions & 8 deletions rasa/core/actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,9 @@ async def run(

intent_to_affirm = latest_message.intent.get(INTENT_NAME_KEY)

intent_ranking: List["IntentPrediction"] = latest_message.parse_data.get(
INTENT_RANKING_KEY
) or []
intent_ranking: List["IntentPrediction"] = (
latest_message.parse_data.get(INTENT_RANKING_KEY) or []
)
if (
intent_to_affirm == DEFAULT_NLU_FALLBACK_INTENT_NAME
and len(intent_ranking) > 1
Expand Down Expand Up @@ -977,7 +977,8 @@ def name(self) -> Text:

@staticmethod
def _matches_mapping_conditions(
mapping: Dict[Text, Any], tracker: "DialogueStateTracker",
mapping: Dict[Text, Any],
tracker: "DialogueStateTracker",
) -> bool:
slot_mapping_conditions = mapping.get(MAPPING_CONDITIONS)

Expand All @@ -999,7 +1000,8 @@ def _matches_mapping_conditions(

@staticmethod
def _verify_mapping_conditions(
mapping: Dict[Text, Any], tracker: "DialogueStateTracker",
mapping: Dict[Text, Any],
tracker: "DialogueStateTracker",
) -> bool:
if mapping.get(MAPPING_CONDITIONS) and mapping.get(MAPPING_TYPE) != str(
SlotMapping.FROM_TRIGGER_INTENT
Expand Down Expand Up @@ -1066,7 +1068,11 @@ async def _execute_custom_action(
return [], executed_custom_actions

slot_events = await self._run_custom_action(
custom_action, output_channel, nlg, tracker, domain,
custom_action,
output_channel,
nlg,
tracker,
domain,
)

executed_custom_actions.add(custom_action)
Expand Down Expand Up @@ -1207,12 +1213,16 @@ async def run(


def extract_slot_value_from_predefined_mapping(
mapping: Dict[Text, Any], tracker: "DialogueStateTracker",
mapping: Dict[Text, Any],
tracker: "DialogueStateTracker",
) -> List[Any]:
"""Extracts slot value if slot has an applicable predefined mapping."""
should_fill_entity_slot = mapping.get(MAPPING_TYPE) == str(
SlotMapping.FROM_ENTITY
) and SlotMapping.entity_is_desired(mapping, tracker,)
) and SlotMapping.entity_is_desired(
mapping,
tracker,
)

should_fill_intent_slot = mapping.get(MAPPING_TYPE) == str(SlotMapping.FROM_INTENT)

Expand Down
18 changes: 9 additions & 9 deletions rasa/core/channels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
from typing import Text, Dict, List, Type

from rasa.core.channels.botframework import BotFrameworkInput # noqa: F401
from rasa.core.channels.callback import CallbackInput # noqa: F401
from rasa.core.channels.channel import ( # noqa: F401
InputChannel,
OutputChannel,
UserMessage,
CollectingOutputChannel,
)

# this prevents IDE's from optimizing the imports - we need to import the
# above first, otherwise we will run into import cycles
from rasa.core.channels.socketio import SocketIOInput
from rasa.core.channels.botframework import BotFrameworkInput # noqa: F401
from rasa.core.channels.callback import CallbackInput # noqa: F401
from rasa.core.channels.console import CmdlineInput # noqa: F401
from rasa.core.channels.facebook import FacebookInput # noqa: F401
from rasa.core.channels.hangouts import HangoutsInput # noqa: F401
from rasa.core.channels.mattermost import MattermostInput # noqa: F401
from rasa.core.channels.messenger import MessengerInput # noqa: F401
from rasa.core.channels.rasa_chat import RasaChatInput # noqa: F401
from rasa.core.channels.rest import RestInput # noqa: F401
from rasa.core.channels.rocketchat import RocketChatInput # noqa: F401
from rasa.core.channels.slack import SlackInput # noqa: F401

# this prevents IDE's from optimizing the imports - we need to import the
# above first, otherwise we will run into import cycles
from rasa.core.channels.socketio import SocketIOInput
from rasa.core.channels.telegram import TelegramInput # noqa: F401
from rasa.core.channels.twilio import TwilioInput # noqa: F401
from rasa.core.channels.twilio_voice import TwilioVoiceInput # noqa: F401
from rasa.core.channels.webexteams import WebexTeamsInput # noqa: F401
from rasa.core.channels.hangouts import HangoutsInput # noqa: F401

input_channel_classes: List[Type[InputChannel]] = [
CmdlineInput,
FacebookInput,
MessengerInput,
SlackInput,
TelegramInput,
MattermostInput,
Expand Down