<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chat'N'Sell</title>
    <description>The latest articles on DEV Community by Chat'N'Sell (chatnsell).</description>
    <link>https://dev.to/chatnsell</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F16448%2Fcb89b10d-79f3-4e3f-aa9c-81898ff982be.png</url>
      <title>DEV Community: Chat'N'Sell</title>
      <link>https://dev.to/chatnsell</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chatnsell"/>
    <language>en</language>
    <item>
      <title>How we built a Telegram Mini App shop on top of an existing store</title>
      <dc:creator>Max P</dc:creator>
      <pubDate>Wed, 25 Nov 2026 22:06:22 +0000</pubDate>
      <link>https://dev.to/chatnsell/how-we-built-a-telegram-mini-app-shop-on-top-of-an-existing-store-7i4</link>
      <guid>https://dev.to/chatnsell/how-we-built-a-telegram-mini-app-shop-on-top-of-an-existing-store-7i4</guid>
      <description>&lt;p&gt;A Telegram shop sounds like a front-end problem: draw a product grid in a Mini App and you're done. Building &lt;a href="https://chatnsell.co/" rel="noopener noreferrer"&gt;Chat'N'Sell&lt;/a&gt; taught us the grid is the easy part. The hard parts are at the edges: a catalogue in someone else's platform, a bot that belongs to the merchant, a checkout we may not own, and messages that must reach people without annoying them.&lt;/p&gt;

&lt;p&gt;Chat'N'Sell turns an existing online store into a shop inside Telegram: its own bot plus a Mini App storefront built from its catalogue, kept in sync with WooCommerce, Magento / Adobe Commerce, OpenCart, PrestaShop, BigCommerce, Horoshop or KeyCRM.&lt;/p&gt;

&lt;h2&gt;
  
  
  The constraint: the store stays the store
&lt;/h2&gt;

&lt;p&gt;Two rules shaped everything. The store is the source of truth: we keep a copy of the catalogue so the Mini App is fast, but changes only flow from the platform to us. And checkout stays on the store: the shopper pays on the merchant's own checkout, with its payment methods, shipping and taxes. Chat'N'Sell never takes the shopper's money.&lt;/p&gt;

&lt;p&gt;So the platform plugins are thin, free connectors that translate their platform's data. The bot, storefront, discounts and messages are configured in our dashboard.&lt;/p&gt;

&lt;h2&gt;
  
  
  One catalogue shape for seven platforms
&lt;/h2&gt;

&lt;p&gt;Every connector, plugin or API client, produces the same normalised product: title, HTML body, images, up to three options and a flat list of variants with price, availability, stock and a compare-at price (kept only when higher than the price). A product without options has exactly one variant. Everything goes through one &lt;code&gt;ProductSyncService&lt;/code&gt;, so the Mini App and dashboard never know which platform a product came from.&lt;/p&gt;

&lt;p&gt;The shape is simple; the ids are not. Our carts store variant ids as integers, but OpenCart identifies a variant by its option combination, a string like &lt;code&gt;"227:31"&lt;/code&gt;. Instead of a mapping table, the sync derives a stable integer from the composite key and keeps the original next to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;intId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stableId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="s1"&gt;'connect'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'product_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'variant_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;is_numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$raw&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$raw&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;stableId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'%u'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;crc32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"{$productId}:{$raw}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1_000_000_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At checkout we send the original id back unchanged, so the plugin never has to decode ours.&lt;/p&gt;

&lt;p&gt;How changes arrive depends on the platform. Those with a dependable background queue (WooCommerce's Action Scheduler, Magento's cron) push events within a minute. OpenCart and PrestaShop have no reliable cron, so their plugins send during the merchant's save with a 3-second timeout, queue failures locally, and we read that queue every 5 minutes. Horoshop and KeyCRM have no catalogue events, so we poll: Horoshop gets prices and stock every 15 minutes and a full pass hourly; KeyCRM runs slower because its 20 requests a minute are shared with the merchant's other integrations. A nightly full sync catches anything an event missed.&lt;/p&gt;

&lt;h2&gt;
  
  
  A bot per store, and trusting initData
&lt;/h2&gt;

&lt;p&gt;Every store has its own bot, created by the merchant in BotFather. When they paste the token we check it with &lt;code&gt;getMe&lt;/code&gt;, store it encrypted, set the Mini App as the menu button and register a webhook whose URL carries the store id. The webhook's &lt;code&gt;secret_token&lt;/code&gt; is an HMAC of store id and bot token, so it needs no storage and changes with the token. Each update's &lt;code&gt;X-Telegram-Bot-Api-Secret-Token&lt;/code&gt; header is checked with &lt;code&gt;hash_equals&lt;/code&gt;; otherwise anyone who guessed a store id could post fake updates.&lt;/p&gt;

&lt;p&gt;The Mini App sends Telegram's &lt;code&gt;initData&lt;/code&gt; with every API request in an &lt;code&gt;X-Telegram-Init-Data&lt;/code&gt; header, and middleware validates it against that store's own bot token:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$secretKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;hash_hmac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sha256'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$botToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'WebAppData'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;ksort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$dataCheckString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"{$k}={$v}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;implode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$computed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;hash_hmac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sha256'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$dataCheckString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$secretKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;hash_equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$computed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details took longer than the hash. We accept initData up to a day old, because a shopper back from a slow checkout still carries the initData the Mini App opened with. And the first screen fires several requests at once, so the shopper row is created with &lt;code&gt;createOrFirst&lt;/code&gt; on a unique store and user pair: the first insert wins, the others read its row.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handing the cart to the store's own checkout
&lt;/h2&gt;

&lt;p&gt;The cart lives on our side until the shopper taps Checkout. Then we reprice every line with today's bot discounts (an ended discount must not reach the store), remove lines whose variant no longer exists and name them, and build a handoff.&lt;/p&gt;

&lt;p&gt;For our plugins (WooCommerce, OpenCart, PrestaShop) the handoff is a link to the plugin's &lt;code&gt;checkout&lt;/code&gt; endpoint: a base64url JSON payload (lines, the shopper's name and email, a link back to the bot) signed with HMAC-SHA256 over &lt;code&gt;timestamp.payload&lt;/code&gt; using the secret from pairing. The plugin accepts it for 30 minutes, fills the visitor's cart, applies a bot price only when it is below the catalogue price, and opens the normal checkout.&lt;/p&gt;

&lt;p&gt;The payload also carries a &lt;code&gt;ref&lt;/code&gt;, which is how the order finds its way back. We didn't want a lookup table that a cache flush could lose, so the ref proves itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;makeRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Store&lt;/span&gt; &lt;span class="nv"&gt;$store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$telegramUserId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;base_convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ctype_digit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$telegramUserId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nv"&gt;$telegramUserId&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;36&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'-'&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nf"&gt;strtolower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;\&lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Support&lt;/span&gt;\&lt;span class="nc"&gt;Str&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// refSignature(): the first 10 hex characters of an HMAC over&lt;/span&gt;
    &lt;span class="c1"&gt;// the store id and $body, keyed with a server-side secret.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;REF_PREFIX&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$body&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'-'&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;refSignature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$store&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Telegram user id rides inside in base 36 and the signature binds it to the store, in at most 40 characters of &lt;code&gt;[a-z0-9-]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Other platforms take the cart their own way. For Magento we build a guest cart over REST, so stock and option errors show up in the bot before the shopper leaves, and the module's handoff page puts it in their session. BigCommerce gets a server-to-server cart and returns its checkout URL. Horoshop has no cart API, so a script on the shop reads a signed token, fetches the cart from us and opens &lt;code&gt;/checkout/&lt;/code&gt;; with no line-price or coupon API there, the shopper pays the shop's own prices. KeyCRM has no storefront, so the cart becomes an order request (idempotent, keyed by a reference derived from the cart), and the merchant's team confirms it and arranges payment.&lt;/p&gt;

&lt;p&gt;Opening the checkout mattered too. On phones we call &lt;code&gt;openLink&lt;/code&gt;, which layers Telegram's in-app browser over the Mini App, so closing it returns the shopper to the cart. Loading the checkout into the Mini App's own window stranded people: the store's page has no Telegram script to answer the back button.&lt;/p&gt;

&lt;h2&gt;
  
  
  Orders back into the bot
&lt;/h2&gt;

&lt;p&gt;Plugins report only orders that carry a ref, and we keep one only if its ref verifies for this store, so a plugin bug or a forged ref can't turn ordinary sales into bot orders. Every platform's order maps to one money model (subtotal, discounts, shipping, tax, a &lt;code&gt;taxes_included&lt;/code&gt; flag), and any other charge shows as "Other charges", so the breakdown adds up.&lt;/p&gt;

&lt;p&gt;Order updates must survive webhooks delivered twice. Each message (paid, received, shipped, tracking, cancelled, refunded) is claimed by setting its timestamp column with an update that matches only while the column is empty; only the call that set it sends. Orders that first reach us more than three days old are imported history and get nothing. Cash on delivery and bank transfer orders get "order received" at once instead of waiting for an offline payment.&lt;/p&gt;

&lt;p&gt;The cart is kept after the handoff and emptied when the order arrives, so an unfinished checkout is still a cart a reminder can point at.&lt;/p&gt;

&lt;h2&gt;
  
  
  Messages that don't become spam
&lt;/h2&gt;

&lt;p&gt;Broadcasts, abandoned cart reminders, back-in-stock alerts and order updates all end in the same delivery service. Three rules keep it polite.&lt;/p&gt;

&lt;p&gt;Pace. Telegram allows about 30 messages a second per bot, so broadcast sends stay at 25 and wait for a slot instead of being skipped:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;throttled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$storeId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;callable&lt;/span&gt; &lt;span class="nv"&gt;$send&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"telegram-bot-{$storeId}"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;\&lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Support&lt;/span&gt;\&lt;span class="nc"&gt;Facades&lt;/span&gt;\&lt;span class="nc"&gt;RateLimiter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;tooManyAttempts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;usleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;250000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    \&lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Support&lt;/span&gt;\&lt;span class="nc"&gt;Facades&lt;/span&gt;\&lt;span class="nc"&gt;RateLimiter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An earlier version used &lt;code&gt;RateLimiter::attempt&lt;/code&gt;, which returns false without sending, so every shopper after the 30th in a minute quietly missed the broadcast.&lt;/p&gt;

&lt;p&gt;Once only. Every delivery is logged per message, so a retried broadcast job skips anyone already reached, and one message never runs in two workers at once.&lt;/p&gt;

&lt;p&gt;Consent. When Telegram answers "bot was blocked by the user", "user is deactivated" or "chat not found", the shopper is marked blocked and left alone until they come back; &lt;code&gt;my_chat_member&lt;/code&gt; updates flip that status both ways. Shoppers can opt out of promotions in the Mini App, and cart reminders, back-in-stock and price-drop alerts skip them. Each of those fires once: one reminder per cart state, after a delay the merchant sets (three hours by default) and only for carts touched in the last week; one alert per subscription; and a price drop counts only at 1% or more, not a rounding wobble.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prices and languages, the store's way
&lt;/h2&gt;

&lt;p&gt;Where the platform exposes its price format, we read symbol position, separators and decimals into one template like &lt;code&gt;{amount} ₴&lt;/code&gt;, applied by the same rules in PHP (bot messages) and JavaScript (Mini App).&lt;/p&gt;

&lt;p&gt;We translate the interface into eight languages. Product text comes only from the platform's own translations, falling back to the store's main language, never machine translation. A shopper's language is the one they saved, else the Mini App's request, else Telegram's, else the one the merchant set for their country, always one the store offers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we'd tell anyone building a Telegram Mini App shop
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Validate initData on every request, against the right bot's token, and decide how long it should stay valid.&lt;/li&gt;
&lt;li&gt;Don't own checkout. Hand off a signed, short-lived cart and let the store handle payment, tax and shipping.&lt;/li&gt;
&lt;li&gt;Make order references self-verifying, so matching orders back needs no state.&lt;/li&gt;
&lt;li&gt;Normalise the catalogue once, and keep the platform's original ids beside your own.&lt;/li&gt;
&lt;li&gt;Rate-limit by waiting, not skipping, and log every delivery so retries can't double-send.&lt;/li&gt;
&lt;li&gt;Treat "blocked" and opt-outs as data you act on immediately.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you run a store on one of these platforms, the connectors are free and messages to your subscribers cost nothing; the bot, storefront and messages are all set up in the Chat'N'Sell dashboard. New to Mini Apps? Our guide &lt;a href="https://chatnsell.co/guides/what-is-a-telegram-mini-app/" rel="noopener noreferrer"&gt;What is a Telegram Mini App&lt;/a&gt; covers the basics. Questions about the internals are welcome in the comments.&lt;/p&gt;

</description>
      <category>telegram</category>
      <category>webdev</category>
      <category>ecommerce</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Selling a Magento 2 catalogue in Telegram: building the connector</title>
      <dc:creator>Max P</dc:creator>
      <pubDate>Wed, 25 Nov 2026 22:05:29 +0000</pubDate>
      <link>https://dev.to/chatnsell/selling-a-magento-2-catalogue-in-telegram-building-the-connector-2043</link>
      <guid>https://dev.to/chatnsell/selling-a-magento-2-catalogue-in-telegram-building-the-connector-2043</guid>
      <description>&lt;p&gt;Plenty of shoppers live in Telegram and skim past marketing email. &lt;a href="https://chatnsell.co/telegram-shop-for-magento/" rel="noopener noreferrer"&gt;Chat'N'Sell&lt;/a&gt; turns an existing online store into a shop inside Telegram: a bot plus a Mini App storefront built from the store's own catalogue, with payment on the store's own checkout.&lt;/p&gt;

&lt;p&gt;This post is about the Magento 2 connector. Magento gives an integrator plenty of machinery: integrations, ACL, declarative schema, fieldsets, CSP. Most of it helped. A few pieces surprised us.&lt;/p&gt;

&lt;h2&gt;
  
  
  The constraint: a thin connector, not a second shop
&lt;/h2&gt;

&lt;p&gt;The store stays the source of truth, and all the logic lives in our service. The bot, the storefront, broadcasts and bot discounts are configured in the Chat'N'Sell dashboard; the module has no settings for any of them. It has four jobs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;let Chat'N'Sell read the catalogue with credentials the merchant can revoke;&lt;/li&gt;
&lt;li&gt;report product, stock, category and bot order changes;&lt;/li&gt;
&lt;li&gt;open carts built in the bot in the store's own checkout;&lt;/li&gt;
&lt;li&gt;tag the orders that came from the bot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It needs Magento Open Source or Adobe Commerce 2.4.6 or newer, PHP 8.1 or newer, and Magento cron.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pairing: an integration created after the save commits
&lt;/h2&gt;

&lt;p&gt;The merchant pastes a one-time code (&lt;code&gt;CNS-XXXX-XXXX&lt;/code&gt;) from our dashboard into &lt;strong&gt;Stores → Configuration → Chat'N'Sell&lt;/strong&gt;. We store only an HMAC of the code, valid for 24 hours, and the module's config backend model blanks the field before it is saved.&lt;/p&gt;

&lt;p&gt;Pairing creates (or reuses) an API integration named "Chat'N'Sell" limited to the catalogue and store settings ACL resources the sync needs to read. Each pairing issues a fresh access token, so moving the store to another account revokes what the old one held. The module sends us the four OAuth credentials, the store view code and the base URL, and we call the store back before accepting: &lt;code&gt;store/storeConfigs&lt;/code&gt; must know the store view, and a one-item &lt;code&gt;products&lt;/code&gt; read must succeed, so a missing Catalog ACL fails at pairing instead of later in a background job. We answer with a signing secret, which the module keeps encrypted in config.&lt;/p&gt;

&lt;p&gt;Our first version paired inside the config save, and every pairing failed with "A consumer having the specified key does not exist": when we called back, the integration and its token were still uncommitted. Now the code's format is checked up front and pairing runs after the commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$code&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pairing&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;assertCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;_getResource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;addCommitCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$storeCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$account&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pairing&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$storeCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="c1"&gt;// ...success message&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LocalizedException&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pairing&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;recordFailure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A failed pairing's reason is kept for the Status field, so the merchant sees why.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading the catalogue: REST, OAuth 1.0a and configurables
&lt;/h2&gt;

&lt;p&gt;The module doesn't serialise products. Our server reads the catalogue through Magento's own REST API with the integration's credentials.&lt;/p&gt;

&lt;p&gt;Every request is signed with OAuth 1.0a (HMAC-SHA256) using all four credentials. A bearer token would be simpler, but since 2.4.4 Magento accepts integration tokens as standalone bearer tokens only when the merchant switches that on; OAuth works on every 2.4.x install as shipped. One detail mattered: the query string is encoded exactly as it was signed, so Magento's check sees the same bytes.&lt;/p&gt;

&lt;p&gt;Mapping works like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;simple and virtual products that are visible in catalogue or search become a product with one variant;&lt;/li&gt;
&lt;li&gt;a configurable becomes one product with a variant per enabled child, and its configurable attributes (Color, Size) become the options;&lt;/li&gt;
&lt;li&gt;children that are "not visible individually" only ever appear as variants.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Children for a whole batch of configurables are fetched together, 100 ids per call. One PHP trap: &lt;code&gt;array_keys()&lt;/code&gt; turns a size of "40" into the integer 40, so option values are cast back to strings. Each variant also keeps the attribute ids and option ids it was built from, because checkout needs exactly those:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$parentSku&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$variant&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'magento'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'parent_sku'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'cartItem'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'quote_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$maskedId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'qty'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sku'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$parentSku&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nv"&gt;$variant&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sku'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$parentSku&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'cartItem'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'product_option'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'extension_attributes'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'configurable_item_options'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$variant&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'magento'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'options'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Store views matter too. The sync reads the paired view's website, media URL, link URL and base currency, and only products enabled and assigned to that website go to the bot. Prices are the regular price or an active special price; catalog price rules and tier prices are left to Magento's checkout, where the shopper pays anyway. When a category name repeats under different parents, it carries its path ("Women › Tops › Jackets") so shoppers can tell them apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Observers write, cron sends
&lt;/h2&gt;

&lt;p&gt;Saving a product must never wait on our API. So every observer that reports something (products, stock items, categories, bot orders, tracking numbers) only inserts a row into a &lt;code&gt;chatnsell_outbox&lt;/code&gt; table declared in &lt;code&gt;db_schema.xml&lt;/code&gt;. Nothing is queued while the store isn't paired, and a failed insert is logged, never thrown.&lt;/p&gt;

&lt;p&gt;Shoppers see configurables, but stock lives on the simple children, so product and stock events carry the configurable parents' SKUs. The stock observer only queues when &lt;code&gt;qty&lt;/code&gt; or &lt;code&gt;is_in_stock&lt;/code&gt; actually changed.&lt;/p&gt;

&lt;p&gt;A cron job runs every minute and sends the queue in batches of 100, signed with HMAC-SHA256 over &lt;code&gt;timestamp.body&lt;/code&gt; in an &lt;code&gt;X-ChatNSell-Signature&lt;/code&gt; header. If a batch fails, its rows get one more attempt counted and the run stops; after ten attempts a row is left alone. Sent rows are kept for seven days.&lt;/p&gt;

&lt;p&gt;On our side the timestamp must be within five minutes, a batch's SKUs and their parents are deduplicated into re-sync jobs, and a category change triggers an incremental pass over the last day instead of guessing which products moved. Underneath sits a safety net: an incremental sync every half hour and a full one nightly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checkout handoff into the Magento quote
&lt;/h2&gt;

&lt;p&gt;When a shopper taps Checkout in the Mini App, our server creates a guest cart over REST and adds the lines, so stock and option errors show up in the bot before the shopper leaves Telegram. The shopper then opens a signed link to &lt;code&gt;/chatnsell/checkout/handoff&lt;/code&gt; on the store: base64url JSON with an HMAC, valid for an hour, carrying the cart's masked id, the shopper's email and name, their Telegram id and any bot-only prices.&lt;/p&gt;

&lt;p&gt;The module checks the signature and expiry, resolves the masked id to the quote and puts it in the browser's checkout session with &lt;code&gt;replaceQuote()&lt;/code&gt;. If the quote is no longer active (already ordered), the shopper goes to the cart instead of an error page.&lt;/p&gt;

&lt;p&gt;REST can't set a custom line price, so bot discounts travel in the payload by cart item id and the module applies them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$quote&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getAllVisibleItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;isset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCustomPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setOriginalCustomPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getProduct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setIsSuperMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Prefilling took a detour. The email and name are set on the quote, but Luma's checkout form reads its state from the &lt;code&gt;checkout-data&lt;/code&gt; entry in the browser's &lt;code&gt;mage-cache-storage&lt;/code&gt;. So the handoff page writes that entry, expires &lt;code&gt;mage-cache-sessid&lt;/code&gt; and only then redirects, so the form and the minicart both show this cart. Hyvä Checkout and other one-step checkouts get the cart but may not show the prefill. Payment, shipping and tax stay Magento's.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attribution: when the fieldset is not enough
&lt;/h2&gt;

&lt;p&gt;Bot orders are marked by a &lt;code&gt;chatnsell_telegram_user_id&lt;/code&gt; column that the module adds to &lt;code&gt;quote&lt;/code&gt; and &lt;code&gt;sales_order&lt;/code&gt;. The textbook way to carry it from cart to order is a &lt;code&gt;sales_convert_quote&lt;/code&gt; fieldset. We added one, and orders still arrived without it: &lt;code&gt;QuoteManagement&lt;/code&gt; rebuilds the order with &lt;code&gt;mergeDataObjects()&lt;/code&gt;, which keeps only the fields &lt;code&gt;OrderInterface&lt;/code&gt; has getters for, so the custom column came through empty. An observer on &lt;code&gt;sales_model_service_quote_submit_before&lt;/code&gt; copies it explicitly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$quote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$observer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getQuote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$observer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$telegramUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$quote&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$quote&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'chatnsell_telegram_user_id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;$telegramUser&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'chatnsell_telegram_user_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$telegramUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That column is also the privacy boundary: the order observer ignores any order without it. For tagged orders it reports the new order, then only changes to its state, status, totals, payments or refunds, plus new tracking numbers, so the shopper gets updates in Telegram.&lt;/p&gt;

&lt;h2&gt;
  
  
  Widget, CSP and leaving cleanly
&lt;/h2&gt;

&lt;p&gt;Once paired, the module adds our storefront widget (a Telegram popup and launcher, designed in the dashboard) to &lt;code&gt;before.body.end&lt;/code&gt; and removes it in &lt;code&gt;checkout_index_index&lt;/code&gt;: payment pages shouldn't load scripts they don't need. A &lt;code&gt;csp_whitelist.xml&lt;/code&gt; adds our host to &lt;code&gt;script-src&lt;/code&gt;, &lt;code&gt;style-src&lt;/code&gt;, &lt;code&gt;connect-src&lt;/code&gt; and &lt;code&gt;img-src&lt;/code&gt;, so the widget loads under Magento's Content Security Policy.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;module:uninstall --remove-data&lt;/code&gt; should delete the module's settings, including the signing secret, and the "Chat'N'Sell" integration, which revokes the credentials we hold. A &lt;code&gt;Setup\Uninstall&lt;/code&gt; class would be the obvious place, but Magento 2.4.9 no longer runs those for modules that only use declarative schema. So the cleanup is the &lt;code&gt;revert()&lt;/code&gt; of an empty data patch. Reverts run in a transaction and can't drop tables, so the outbox table and the attribution columns go away the declarative way, with &lt;code&gt;module:disable&lt;/code&gt; and &lt;code&gt;setup:upgrade&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we'd tell anyone building for Magento
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Do work that needs committed data in a commit callback, not inside the config save.&lt;/li&gt;
&lt;li&gt;Sign REST calls with OAuth 1.0a; standalone bearer tokens are off by default.&lt;/li&gt;
&lt;li&gt;Send events with the configurable parent's SKU: shoppers see parents, stock lives on children.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;sales_convert_quote&lt;/code&gt; fieldset doesn't carry a custom column through &lt;code&gt;QuoteManagement&lt;/code&gt;. Copy it yourself.&lt;/li&gt;
&lt;li&gt;Luma's checkout prefill lives in browser storage, not only in the quote.&lt;/li&gt;
&lt;li&gt;For uninstall cleanup in a declarative-schema module, use a revertible data patch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you run a Magento store, the connector is free on the &lt;a href="https://commercemarketplace.adobe.com/chatnsell-module-connector.html" rel="noopener noreferrer"&gt;Adobe Commerce Marketplace&lt;/a&gt;. The bot, storefront, broadcasts and discounts are all set up in the Chat'N'Sell dashboard, with a free plan to start. The step-by-step guide is here: &lt;a href="https://chatnsell.co/telegram-shop-for-magento/" rel="noopener noreferrer"&gt;Telegram shop for Magento&lt;/a&gt;. Questions about the internals are welcome in the comments.&lt;/p&gt;

</description>
      <category>magento</category>
      <category>php</category>
      <category>ecommerce</category>
      <category>telegram</category>
    </item>
    <item>
      <title>Selling a WooCommerce catalogue in Telegram: building the connector</title>
      <dc:creator>Max P</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:58:24 +0000</pubDate>
      <link>https://dev.to/chatnsell/selling-a-woocommerce-catalogue-in-telegram-building-the-connector-34j8</link>
      <guid>https://dev.to/chatnsell/selling-a-woocommerce-catalogue-in-telegram-building-the-connector-34j8</guid>
      <description>&lt;p&gt;A lot of shoppers spend their day in Telegram and rarely open marketing email. That is why we built &lt;a href="https://chatnsell.co/telegram-shop-for-woocommerce/" rel="noopener noreferrer"&gt;Chat'N'Sell&lt;/a&gt;: it turns an existing store into a shop inside Telegram, a bot plus a Mini App storefront built from the store's own catalogue.&lt;/p&gt;

&lt;p&gt;This post is about the WooCommerce connector, and about where WordPress's own infrastructure helped and where it got in the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  The constraint: a thin connector, not a second shop
&lt;/h2&gt;

&lt;p&gt;The store stays the source of truth; all logic lives in our service. The bot, the storefront, broadcasts and bot discounts are configured in the Chat'N'Sell dashboard; the plugin has no settings for any of them. It has four jobs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;let Chat'N'Sell read the catalogue through signed, read-only endpoints;&lt;/li&gt;
&lt;li&gt;report changes to products, variations, stock and categories right away;&lt;/li&gt;
&lt;li&gt;open carts built in the bot on the shop's own checkout;&lt;/li&gt;
&lt;li&gt;tag the orders that come from the bot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Shoppers pay on the WooCommerce checkout with the shop's own payment methods, shipping and taxes. Only orders placed from the bot are ever shared. Every other order, and the customer list, stays in the shop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Signed routes under /wp-json
&lt;/h2&gt;

&lt;p&gt;The catalogue is served by a small REST namespace, &lt;code&gt;chatnsell/v1&lt;/code&gt;: &lt;code&gt;info&lt;/code&gt;, &lt;code&gt;products&lt;/code&gt; (up to 100 per page, filterable by &lt;code&gt;updated_since&lt;/code&gt; or a list of &lt;code&gt;ids&lt;/code&gt;), &lt;code&gt;categories&lt;/code&gt;, &lt;code&gt;orders/{id}&lt;/code&gt;, &lt;code&gt;changes?since=&lt;/code&gt; and a signed &lt;code&gt;POST disconnect&lt;/code&gt;. Authentication runs in &lt;code&gt;permission_callback&lt;/code&gt;, so WordPress rejects an unsigned request before any route callback runs.&lt;/p&gt;

&lt;p&gt;Requests are signed with HMAC-SHA256 over &lt;code&gt;timestamp.payload&lt;/code&gt;, with a secret exchanged at pairing and 5 minutes of allowed clock skew. For GET routes the payload is the request target: we hash &lt;code&gt;REQUEST_URI&lt;/code&gt; exactly as it arrived instead of rebuilding the URL from parsed parameters. That also covers sites without pretty permalinks, where routes live under &lt;code&gt;?rest_route=&lt;/code&gt;. POST routes sign the raw body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$signature&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nv"&gt;$secret&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nf"&gt;ctype_digit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$timestamp&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$timestamp&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;MAX_SKEW&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nf"&gt;strpos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$signature&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'v1='&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;hash_equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nf"&gt;hash_hmac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'sha256'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$timestamp&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'.'&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$secret&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$signature&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pairing has a chicken-and-egg problem: our server checks that it can reach the shop before handing over the secret. So while the plugin's pair call is in flight (a two-minute transient), &lt;code&gt;GET info&lt;/code&gt; answers unsigned, returning only what the storefront shows anyway.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variations, including "any"
&lt;/h2&gt;

&lt;p&gt;We normalise every catalogue into one shape: products with up to three options and a flat list of variants. A simple product becomes one variant with its own id. A variable product becomes one variant per published variation, with options from the first three attributes "used for variations", in the merchant's order. Grouped, external and password-protected products are skipped.&lt;/p&gt;

&lt;p&gt;The tricky case is WooCommerce's "any" attribute, as in "Blue, any size". A bot can't ask for "any size", so each "any" is expanded into one row per value the product offers. Those rows need ids, and we didn't want a mapping table, so the choice goes into the id: &lt;code&gt;&amp;lt;variation&amp;gt;:&amp;lt;attribute&amp;gt;=&amp;lt;value&amp;gt;&lt;/code&gt;, built with &lt;code&gt;http_build_query&lt;/code&gt;. At checkout the plugin splits it back and puts that exact value in the cart, so the order records the size the shopper picked.&lt;/p&gt;

&lt;p&gt;Two limits keep this sane. Expansion stops at 100 rows per product; past that, the attribute stays a single "Any Size" row. And because WooCommerce sells a combination through the first variation that matches, a combination an earlier variation already covers is dropped.&lt;/p&gt;

&lt;p&gt;Prices go out as the shop displays them (&lt;code&gt;wc_get_price_to_display&lt;/code&gt;), with tax when the shop shows prices with tax.&lt;/p&gt;

&lt;h2&gt;
  
  
  An outbox on Action Scheduler
&lt;/h2&gt;

&lt;p&gt;Saving one product fires many hooks, and the merchant's Save button must not wait on our API. So every hook we listen to (product and variation saves, stock, trash and restore, categories, currency and price display settings) does one thing: write a row to a &lt;code&gt;{prefix}chatnsell_outbox&lt;/code&gt; table and enqueue a unique async Action Scheduler action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// One pending row per (type, id), moved to the end so the server sees&lt;/span&gt;
&lt;span class="c1"&gt;// the last thing that happened (saved -&amp;gt; trashed -&amp;gt; restored).&lt;/span&gt;
&lt;span class="nv"&gt;$wpdb&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$wpdb&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'DELETE FROM %i WHERE type = %s AND object_id = %d AND sent_at IS NULL'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$object_id&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// ...insert the new row, then:&lt;/span&gt;
&lt;span class="nf"&gt;as_enqueue_async_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;FLUSH_HOOK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GROUP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Variation events are sent as their parent product, which the server then re-reads through &lt;code&gt;products?ids=&lt;/code&gt;. Order events are serialised at send time, so the server gets the latest state.&lt;/p&gt;

&lt;p&gt;A flush sends up to 500 due rows in one signed batch. A failed batch stays queued with exponential backoff (1, 2, 4 minutes and so on, capped at six hours) for up to 15 attempts. A &lt;code&gt;410&lt;/code&gt; means the shop was disconnected in the dashboard: the plugin forgets the secret and stops instead of retrying. Sent rows are kept for a week so &lt;code&gt;GET changes?since=&lt;/code&gt; can answer from the same table.&lt;/p&gt;

&lt;p&gt;One Action Scheduler detail is easy to miss: its "unique" flag also counts the action that is currently running. A flush that schedules a follow-up (for rows queued meanwhile, or a retry) would be deduplicated against itself. We pass the target time as the action's argument, which makes the follow-up a different action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checkout handoff: REST has no cart
&lt;/h2&gt;

&lt;p&gt;When a shopper taps Checkout in the Mini App, they open a signed link to &lt;code&gt;chatnsell/v1/checkout&lt;/code&gt;. Filling the cart in that REST callback doesn't work: REST requests drop the logged-in user and have no cart. So the handoff takes two steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The REST route checks the payload's HMAC and age (30 minutes), stores it in a transient under a random single-use token valid for 10 minutes, and answers with a 302 to &lt;code&gt;/?chatnsell_handoff=&amp;lt;token&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;On that normal front-end request, a &lt;code&gt;wp_loaded&lt;/code&gt; hook (after WooCommerce has loaded the session) empties the cart, adds the lines, applies the bot's coupon, pre-fills billing details without overwriting a signed-in customer's, and redirects to checkout.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The plugin forces a session cookie so a guest's cart survives the redirect. Quantities above what the shop will sell are lowered with a notice naming the line, sold-out lines are listed, and error pages get a "Back to Telegram" button that only ever points to an &lt;code&gt;https://t.me/&lt;/code&gt; link.&lt;/p&gt;

&lt;p&gt;Bot prices were the subtle part. The bot shows prices as the shop displays them, but a cart line holds the price on the basis the shop stores it in, so the plugin converts. And a bot price can only lower a price:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Only the bot's own discounts: never raise a price above the catalogue.&lt;/span&gt;
&lt;span class="c1"&gt;// The bot shows prices as the shop displays them (with tax or not);&lt;/span&gt;
&lt;span class="c1"&gt;// the cart line is set on the basis the shop stores prices in.&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$unit&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;$unit&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nv"&gt;$shown&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;$shown&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$cart_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;PRICE_KEY&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$unit&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$catalog&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nv"&gt;$shown&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;wc_get_rounding_precision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// The bot price covers the units the bot sold, no more: units the&lt;/span&gt;
    &lt;span class="c1"&gt;// shopper adds in the shop's cart pay the catalogue price.&lt;/span&gt;
    &lt;span class="nv"&gt;$cart_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;QTY_KEY&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$cart_data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;CATALOG_KEY&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$catalog&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The price is set in &lt;code&gt;woocommerce_before_calculate_totals&lt;/code&gt;, which both the classic and the block checkout run through. If the shopper raises the quantity in the cart, the line gets a blended unit price: the bot price for the units bought in Telegram, the catalogue price for the rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Orders: HPOS, blocks and "Source: Chat'N'Sell"
&lt;/h2&gt;

&lt;p&gt;The plugin declares compatibility with HPOS and the Cart and Checkout blocks, and it can, because it never touches order tables: orders go through &lt;code&gt;WC_Order&lt;/code&gt; only, carts through &lt;code&gt;WC_Cart&lt;/code&gt; only.&lt;/p&gt;

&lt;p&gt;The bot's order reference is copied from the session onto the order on &lt;code&gt;woocommerce_checkout_order_created&lt;/code&gt; (classic checkout) and &lt;code&gt;woocommerce_store_api_checkout_order_processed&lt;/code&gt; (blocks), then cleared: one bot cart, one order. That reference is also the privacy boundary. &lt;code&gt;orders/{id}&lt;/code&gt; returns 404 for any order without one, and untagged orders never produce events.&lt;/p&gt;

&lt;p&gt;In 1.0.5 we made bot orders visible where merchants already look. WooCommerce has built-in order attribution, so instead of a custom column the plugin writes the standard meta:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$prefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'_wc_order_attribution_'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'source_type'&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'utm'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'utm_source'&lt;/span&gt;   &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SOURCE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'utm_medium'&lt;/span&gt;   &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'telegram'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'utm_campaign'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'bot'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$values&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update_meta_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$prefix&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These values replace whatever the checkout page recorded (the order came from the bot, whatever the browser session looked like), and leftover campaign keys from another source are deleted. WooCommerce would label the source "Chatnsell", so a &lt;code&gt;wc_order_attribution_origin_formatted_source&lt;/code&gt; filter returns "Chat'N'Sell" as written. Each bot order also gets a private note with its reference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup, languages and leaving cleanly
&lt;/h2&gt;

&lt;p&gt;The plugin page mostly points to the dashboard. Before connecting, it checks the shop's own address: on localhost, a private or reserved IP, a single-label host or names like &lt;code&gt;.local&lt;/code&gt; and &lt;code&gt;.test&lt;/code&gt;, Connect is disabled with an explanation, because our server has to load the catalogue from the shop. On plain http it warns that product photos may not show in Telegram.&lt;/p&gt;

&lt;p&gt;After connecting, the page shows a live checklist read from the dashboard (catalogue synced, products chosen, bot connected, test order, plan), with a link for each open step. A step this plugin version doesn't know yet falls back to the server's own label.&lt;/p&gt;

&lt;p&gt;The interface ships in 18 languages, from German to Ukrainian, loaded from the plugin's &lt;code&gt;languages/&lt;/code&gt; folder, with a WordPress.org language pack taking precedence when there is one.&lt;/p&gt;

&lt;p&gt;Uninstall first tells our server the shop has left (best effort, five-second timeout, so a dead network can't hang it), then deletes the plugin's options, unschedules its Action Scheduler group and drops the outbox table.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we'd tell anyone building for WooCommerce
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Authenticate in &lt;code&gt;permission_callback&lt;/code&gt;, and sign &lt;code&gt;REQUEST_URI&lt;/code&gt; as sent instead of rebuilding the URL.&lt;/li&gt;
&lt;li&gt;REST requests have no cart and no user. Do cart work on a normal front-end request.&lt;/li&gt;
&lt;li&gt;Action Scheduler makes a good outbox, but "unique" includes the running action.&lt;/li&gt;
&lt;li&gt;Touch orders only through &lt;code&gt;WC_Order&lt;/code&gt;, and HPOS compatibility is a declaration, not a migration.&lt;/li&gt;
&lt;li&gt;Use WooCommerce's own order attribution before inventing a column.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you run a WooCommerce store, the connector is free on &lt;a href="https://wordpress.org/plugins/chatnsell-for-woocommerce/" rel="noopener noreferrer"&gt;WordPress.org&lt;/a&gt;. The bot, storefront, broadcasts and discounts are all set up in the Chat'N'Sell dashboard, with a free allowance to start. The step-by-step guide is here: &lt;a href="https://chatnsell.co/telegram-shop-for-woocommerce/" rel="noopener noreferrer"&gt;Telegram shop for WooCommerce&lt;/a&gt;. Questions about the internals are welcome in the comments.&lt;/p&gt;

</description>
      <category>woocommerce</category>
      <category>wordpress</category>
      <category>php</category>
      <category>telegram</category>
    </item>
  </channel>
</rss>
