ChatGPT API continuous chat via Thread ID in make.com

Jonathan shows how to build a Make.com automation that keeps WhatsApp conversations stateful by storing OpenAI thread IDs in a Google Sheet. When a user messages, the flow looks up their thread ID; if found, it continues the chat; if not, it creates a new thread and saves the ID for next time.
Chapters
Hi Jonathan, super quick, um, I've, I've quickly built this. So I don't have access to the WhatsApp, uh, business, uh, business thing, right, the Business Cloud or however it's called. Um, so I just asked ChatGPT how the output looks like, like the JSON output, um, of the— yeah, right, you know, of the, of the WhatsApp webhook when a message comes in it gives you some JSON. So I asked, asked ChatGPT, how does that look like? And it actually gave me an answer. So it should technically look something like this. So let me show you in large. You have one bundle object, is your WhatsApp Business account. Then in entry, you have a collection with the ID or WhatsApp Business account ID, changes, one field messages value. Messages like this. It should look like this. Right here is your body, then you have your contact. So, uh, right here is the WhatsApp ID. This should basically be the ID— let me, let me call it this way— WhatsApp ID. This is basically the ID of the user, I would guess, like, right? Because it's saved under the profile. Then you have metadata and you have the phone number and stuff like this. But, um, I think the WhatsApp ID is better to go off. Uh, not 100% sure. If you say the phone number is better, then go with the phone number. But what I basically want to show you is this is the output it gives. And what this automation now does, and I will let it run through in a second now, it basically— I have, I've set up a Google Sheet, super quick, super easy, one, one sheet, one page, with, with a header. One is WhatsApp ID and one is thread ID, just so that it basically has a little database of, of those values, right? Because we want to know which WhatsApp ID belongs to which thread ID in the end. So then we have our first module, uh, Google Sheets search rows. What we do here is we select this WhatsApp example, that's how I called it, right? Sheet 1, table contains headers, yes. Column range A to C is enough because we only have A and B. And then as filter, you take WA_ID, so WhatsApp ID, equals to, and then you just open this up, go all the way down here to contacts, to WhatsApp ID. So the WhatsApp ID, so we filter for rows where this, this field, uh, this cell, um, equals to the WhatsApp ID of the incoming message, right? And we only want to limit this to 1. It should only return 1, right? If it would return more than 1, it should technically always return either 0 or 1. If it returns over 1, like let's say we remove that limit, it returns over 1, there's something wrong, right? Entirely.. But I like to limit it to 1 because, yeah, right, because 1 is enough. So you can leave that as the way you want. And what it now does is it either returns us 0— this means that the user, there is no entry yet, the user hasn't been created, there was no thread ID that has been created yet. The, um, so it's the very first message from this WhatsApp profile, right? And if it returns 1, then it already found it in the database meaning this user has messaged before and we want to get the thread. What we do now is we add a router and under this router we have, um, two filters, right? So the first one, the upper one, is if the chat exists. The lower one is if the chat doesn't exist. So, um, up here we have basically this filter, chat exists. You set this as fallback route to no. Condition, total number of bundles. So you get from the Google Sheet,, from that module, total number of bundles equal to 1, right? That's when it exists, it found one. Then you just add the message and assistant. You select your assistant, you select the role user, and then the message, you just select the message from in here. Because, um, technically, if I'm correct, I've just mapped it directly like this. There should only be like one message that you get at a time, and there shouldn't be like any arrays of several values. There should only be like one bundle in each because you only get one message, right? So you should be able to just map it the way I do here, just clicking it. If there is some way where, for example, in messages there are several different, um, different entries, different collections, different bundles, whatever, uh, then you, you'll have to see how you do it. But select it this way and see if it works. And it technically should work, but I, as I said, I don't know the exact output the WhatsApp, uh, bundle gives you— the WhatsApp module, I mean, I'm sorry. So this is the message, uh, image files, uh, you can, you can map that if you want, if the user can send images or something, and it should be able to read them. Shouldn't be a problem, right? You can also add image URLs instead. So whatever WhatsApp gives you, if they give you the file data or if they give you the image URL, both should work fine. And then as thread ID, because obviously the chat exists, we just take the thread ID from the Search Rows Google Sheets module, just this thread ID right here, because again, there's only one, so we don't have any annoying stuff with more, more than one bundle, um, more than one collection, stuff like this. The— that's basically what's annoying always in scenarios, but in this case, that shouldn't be the case at all, right? Tool choice, you can set to whatever you want. I've set to auto. So I don't know. And then under advanced options, if you want, you can set up some other stuff here. This is basically what that route looks like. And then you would add the last module, which is, which is WhatsApp send a message. Um, you have the connection already created, then you select as chat ID or whatever you use for that. Uh, you probably select the same ID. You probably also select under Contacts, this WhatsApp ID, or maybe under Messages, this, um, ID. Yeah, probably. Oh no, I don't know, from maybe, or that, that's actually the same right here. So yeah, you'll just see which one you connect. You should already know that, right? But that's basically the last module, but I can't set it up, so I'll remove it. And then for the second way down here, the chat doesn't exist Um, basically the other way around. You can set this as fallback, um, route. Oh, you don't have to. Um, total number of bundles, again in this case equal to 0. Then you message an assistant, and here you set up basically the exact same. You put in the message again, but you don't put in a thread ID, right? Here you don't have a thread ID, so you create a new thread by not providing a thread ID. Here You, you input the thread ID because it already exists and you want to continue this thread. Now last, after you've created this new thread here, you want to add a row to Google Sheets, the chat ID being, yeah, the, the chat ID. So you take that from, from the, from the WhatsApp module at the very beginning. You take the WhatsApp ID again, and then the thread ID, you take it from this OpenAI message and assistant, you right here have the thread ID right there, right? You take that, and if you send a message to this thread ID in the future, it, it will go through, right? It will be in this thread because if now that's automatically saved, when the user sends a second message, it goes through that whole workflow again. It starts, it starts here, here it comes in then it searches the rows again, but this time since it's the second or later message, not the first anymore, it will find that row and it will always go with the upper, upper row. So for the only for each user's first message, it will go right here and then the lower row. And obviously here you also add the WhatsApp Send a Message module at the end. And, um, yeah, that's basically what it looks like. And I can run it once and Since I have some random placeholder stuff in here, you can see the contact WhatsApp ID is some random string, so it won't find it now because this is completely empty, right? So it will go the lower route. Um, tada, it went down here because it didn't find something. We have 0 output bundles, so it went down here. It automatically sent the message, "Hello, I have a question about your service," which was just the placeholder message. What it did now is it created a thread ID right here. That's what we want. And then obviously it has the content, of course, "I'm here to help. What would you like to know about our service?" And obviously you have your assistant which is already trained and everything. You have the instructions already set and everything. So it will be custom to your use case, use case, right? Um, and now afterwards it added a new row. So if we go in here, perfect, we have the WhatsApp ID and we have the thread ID. And now, um, now if we would send a message again, it would just go, uh, the upper way and it will remember what I just said. So I can just write in here what What did I, what did I just write? Right, I, I ask him. So let's see if it works. That is the best way to basically show it. It should go up. Yes, it goes up here. And now let's see what it outputs. Uh, come on, let's see if everything, if I've set up everything correctly. Content text. You wrote hello, I have a question about your service. If you have a specific question or something you'd like to discuss regarding our service, feel free to ask. Perfect, it works flawlessly, it works absolutely amazing. So I really hope this helps. I will now download the blueprint, export the blueprint, and attach it to my message. You have a good one, and, um, yeah, I really hope I could help. Goodbye.










