Skip to content

Commit

Permalink
start interactive game by direct link
Browse files Browse the repository at this point in the history
  • Loading branch information
eamin-dev authored and istride committed Aug 20, 2024
1 parent 0c2b227 commit 488d36a
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions interactive/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,17 @@ def serve(self, request, *args, **kwargs):

if not user:
return redirect("/")

if request.method == "GET":
chat = Message.objects.filter(to=user).order_by("-created_at").first()
if not chat:
self.send_message_to_rapidpro(user=user, text=self.trigger_string)

if request.method == "POST":
form = MessageSendForm(request.POST)

if form.is_valid():
channel_url = self.channel.request_url

data = {"from": user, "text": form.cleaned_data["text"]}

try:
response = requests.post(url=channel_url, data=data)
response.raise_for_status()
except requests.exceptions.RequestException:
return redirect("/")
self.send_message_to_rapidpro(user=user, text=form.cleaned_data["text"])

return redirect(self.get_url(request))

Expand Down Expand Up @@ -165,5 +162,13 @@ def send_message_on_language_switch(self, request, user):
referer_lang = current_lang

if referer_lang != current_lang:
data = {"from": user, "text": self.trigger_string}
requests.post(url=self.channel.request_url, data=data)
self.send_message_to_rapidpro(user=user, text=self.trigger_string)

def send_message_to_rapidpro(self, user, text):
data = {"from": user, "text": text}

try:
response = requests.post(url=self.channel.request_url, data=data)
response.raise_for_status()
except requests.exceptions.RequestException as e:
return redirect("/")

0 comments on commit 488d36a

Please sign in to comment.