I want to show how the framework makes it easier
Two directions of integrations
Real-world example, built specifically for this conference, available on github
you download library for chat bots. Here is a good one vrann/fbcahtbot (because I’ve written it:)
you pass authorization on facebook
you need to deploy chatbot app on AWS and enable https
Once Facebook authorized it, you will receive messages from the messenger to the callback
You can respond invoking Facebook API with the response message and receiver id
Callback
Web endpoint
https
It should be registered with the facebook
It should be subscribed to the webhooks
It should send 200 OK when received the message
Facebook invokes callback with the message in JSON structure we’ve seen before
The job of the Callback script will be simple – Make API request to Magento and pass message to Magento
For this, we need to implement new Magento API which can accept such message structure
Let’s start from the Service Interface – it defines web API endpoint
It tells few things:
- Class and Method which will be invoked by web api call should implement this interface
Input parameter of the method should be MessageTextInterface
What is MessageTextInterface?
Let’s take original JSON request from Facebook
We want little to no modification in it in order to have Magento accept it as Web API request. Otherwise it puts more work on the callback script
Magento API Interfaces represent same data structure: we define every object in JSON structure as a separate interface in PHP. If objects are same we can re-use one interface like you see in sender and recipient
Data Interfaces contains getters and setters for the fields
Type of fields is either scalar value or data interface
Why this is needed?
It allows unified serialization pattern
This is complex case, when we mock messages from external system. (Sometimes it is not possible, when conventions are different)
In usual case you design objects for Magento and write a connector from external system. L
Request is blocking, it means that while Magento haven’t processed request, loaded required information from the database, sent response back to Facebook, the callback will keep the connection.
Facebook will not send next request until previous request is processed, so this delays delivery of other messages
Answer to such kind of problems is Queues
- simple receiver gets request and puts to rabbitmq exchange
Callback.php sends message to the callback.received queue
- I’m lucky to have Magento Enterprise, which already has built-in support of the rabbit. I will just connect it to that Rabbit instance. (Otherwise, I would need to write a connector, etc)
Write communication.xml and queue_consumer.xml and bind it to existing Web API
topic-based exchange
Topic name is the same as a queue name
Here we just listen for the messages, we assume that queue exists on the rabbit (created manually)
Now I can run consumers and have messages processed from the Rabbit!
Handler uses same interface as web api endpoint
Handler uses same input parameter
Queue ”callback.received” is the one where callback will put messages
Consumer name is an alias to run consumers
Difference in two configuration files and we have:
sclability
availability,
Non-blocking requests
For developers, you can two versions of extension: community edition and enterprise edition. For relatively small stores with not too many requests we can do blocking calls
- simple receiver gets request and puts to rabbitmq exchange
Callback.php sends message to the callback.received queue
Show request/response of Facebook messages with the two rabbit listeners in CLI
we can send message back directly, or we can put it back to the queue — this will save us from situations when facebook is unreachable.
We create Interface with the semantic “send message to Facebook”
We write “Remote preference” for it – it generates us implementation which sends message
Write queue_publisher.xml
Write simple rabbit listener which forwards messages to facebook.
These files available in 2.2, before it was just queue.xml
Publisher allows to configure which exchange will be message with the topic posted to
If we don’t define it, it will go to default exchange.
Topology defines binding of exchanges to the queues. Here we need to bind to the queue which response.php script knows about, otherwise it will not receive the message
We bind exchange to which publisher publishes topic to the queue which is read by response.php by the topic which is generated by Remote
you can configure all RabbitMQ topology on the Magento side in the same way as you configure database schema