Idealista to Messenger
Idealista2Messenger (I2M) is an app that provides the ability to forward notification emails from idealista.com
to Meta's Messenger accounts.
Business Driver
As an idealista.com user I want to share its offer notifications with group of people via different channel than email.
How the application works
- I2M authorizes to Google API with the OAuth 2.0 credentials.
- Using Gmail API I2M fetches emails specified by the custom query.
- Using Facebook's Open Graph API the I2M tells the Meta application to send a message on behalf of the FB Page to its user
Before you start
You will need:
- Google Cloud project.
- Google account with Gmail enabled.
- Facebook Page.
- Meta App.
Configuration
Google
Create Project
- Create the Google Project
Enable the Gmail API
- In the Google Cloud console, enable the Gmail API.
Add required privileges for the project
- In the OAuth screen project's section you need to add privileges to Gmail API with
.../auth/gmail.modify
scope.
Obtain the credentials.json
file
- In the Google Cloud console, go to Menu > APIs & Services > Credentials.
- Click Create Credentials > OAuth client ID.
- Click Application type > Desktop app.
- In the Name field, type a name for the credential. This name is only shown in the Google Cloud console.
- Click Create. The OAuth client created screen appears, showing your new Client ID and Client secret.
- Click OK. The newly created credential appears under OAuth 2.0 Client IDs.
- Save the downloaded JSON file as
credentials.json
, and move the file to config
directory.
Obtain the token.json
file
- The first time you run the I2M, it prompts you to authorize access:
- If you're not already signed in to your Google Account, you're prompted to sign in. If you're signed in to multiple accounts, select one account to use for authorization.
- Click Accept.
- Copy the
code
from the browser, paste it into the command-line prompt, and press Enter.
token.json
file will be created in config
directory.
- Authorization information is stored in the file system, so the next time you run the I2M, you aren't prompted for authorization.
- Create Facebook Page
- Create Meta App
- App might be in dev mode, you will just need to invite users to participate f.e. as a testers
- If you invite someone, their not getting any notification, invitations might be accepted here
- Get a Page Access Token
Obtain Long Lived Paged Access Token
Long-lived Page Access Token procedure:
- Get User Access Token:
- Go with token to Access Token Debugger:
- With long-lived User Access Token get long-lived Page Access Token:
- Long-lived Page Access Token save in
config/page_access_token.json
file.
Obtain Facebook Page Scoped User IDs for sending messages
- CURL:
curl -i -X GET "https://graph.facebook.com/{latest-api-version}/{page-id}/conversations?fields=participants&access_token={page-access-token}"
- Docs: https://developers.facebook.com/docs/messenger-platform/get-started
Putting all together in the .env
file
FB_PAGE_ID
- Facebook Page ID
FB_PAGE_RECIPIENTS
- Comma separated recipients lists (no whitespace allowed)
Run the I2M application
If everything is set up correctly, you should be able run application and see similar output:
go run main.go
Idealista2Messenger 2022.12.10 17:44:37
Main process
Sending 1 message(s) to [<PAGE_SCOPED_USER_ID> <PAGE_SCOPED_USER_ID>]
Message <MESSAGE_ID> to <PAGE_SCOPED_USER_ID> sent
Message <MESSAGE_ID> to <PAGE_SCOPED_USER_ID> sent
Application "Idealista 2 Messenger" has finished processing
Troubleshooting
Gmail token expired or revoked
If your Gmail token is expired or revoked and you can see similar output:
Main process
Unable to retrieve messages: Get "https://gmail.googleapis.com/gmail/v1/users/me/messages?alt=json&prettyPrint=false&q=is%3Aunread+from%3Aidealista.com": oauth2: cannot fetch token: 400 Bad Request
Response: {
"error": "invalid_grant",
"error_description": "Token has been expired or revoked."
}
Application "Idealista 2 Messenger" has finished processing
- Delete the
config/token.json
file: rm -rf config/token.json
- Run the application to regenerate it (See Obtain the
token.json
file section)
Other issues
If you have any questions, just submit an Issue with all the details.