У меня настроен код для отправки серии сообщений на основе ключевого слова, отправленного пользователем приложения.
Например, если пользователь отправит ключевое слово "ТЕСТ", он получит серию текстасообщения, настроенные для ответа на это ключевое слово.
Когда у меня есть ключевое слово с 4 или менее ответными сообщениями, ответное сообщение отправляется только один раз.
Проблема в том, что если ключевое слово имеет более 4ответные сообщения, ответное сообщение отправляется несколько раз.Каждая серия сообщений отправляется 5 раз, и в итоге отправитель получает 20 ответов (4x5)
class SmoocherMessages(View):
@method_decorator(csrf_exempt) # required
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def post(self, request, *args, **kwargs):
incoming_message = json.loads(request.body.decode('utf-8'))
log = SmoocherLog(msg_plain=incoming_message)
log.save()
return HttpResponse("OK", status=200)
@receiver(post_save, sender=SmoocherLog)
def process_smoocher_post(sender, instance, created, **kwargs):
if created:
#Get the JSON
sm_post = instance.msg_plain
print("SIGNAL TRIGGERRED")
process_smoocher_message(data=sm_post)
def process_smoocher_message(data):
# Remove all punctuations, lower case the text and split it based on space
entry = None
key_word = None
smapp_id = None
sender = None
client = "dbc5994d"
cust = Customer.objects.get(account_id=client)
rez_agent_name = cust.company_name
if cust.agent_name:
rez_agent_name = cust.agent_name
if cust.agent_avatar:
rez_agent_avatar = cust.agent_avatar
if data['trigger']:
entry = data['trigger']
if entry == "message:appUser":
smapp_id = data['app']['_id']
sender = data['appUser']['_id']
if data['messages']:
msg_full = data['messages'][0]
if msg_full['type'] == "text":
#Process the keyword
#Strip Spaces and any special characters
msg_raw = msg_full['text']
msg_cl = re.sub('\W+',' ', msg_raw)
msg = msg_cl.strip()
#TRY TO FIND THE KEYWORD
try:
key_word = CustomerKeyword.objects.get(customer=client,keyword__iexact=msg)
except ObjectDoesNotExist:
# log2 = FBLogger(msg_plain="No Keyword Found, Exception Raised")
default_msg = "INVALID"
key_word = CustomerKeyword.objects.get(customer=client,keyword__iexact=default_msg)
#GET THE RESPONSE SEQUENCE
req_seq = KeywordSequence.objects.filter(keyword=key_word).order_by('sequence')
api_instance = smooch.ConversationApi()
app_id = smapp_id
user_id = sender
conversation_activity_body = None
if rez_agent_name:
rez_name = rez_agent_name
rez_avatar = rez_agent_avatar
conversation_activity_body_start = smooch.ConversationActivity(role='appMaker', type='typing:start', name=rez_name, avatar_url=rez_avatar)
conversation_activity_body_stop = smooch.ConversationActivity(role='appMaker', type='typing:stop', name=rez_name, avatar_url=rez_avatar)
else:
conversation_activity_body = smooch.ConversationActivity(role='appMaker', type='typing:start')
api_instance.conversation_activity(app_id, user_id, conversation_activity_body_start)
for x in req_seq:
msg_rez = x.msg.msg_plain
if msg_full['source']['type'] == "whatsapp":
msg_rez = x.msg.msg_rich
if x.msg.msg_type == "Chat":
# create an instance of the API class
message_post_body1 = smooch.MessagePost(text=msg_rez, role='appMaker', type='text', name=rez_name, avatar_url=rez_avatar)
time.sleep(3)
api_response = api_instance.post_message(app_id, user_id, message_post_body1)
time.sleep(2)
if x.msg.msg_type == "Link":
# create an instance of the API class
response_msg = json.dumps({"text":msg_rez, "role": "appMaker", "type": "text", "actions": [{"type": "link", "text": x.msg.attachment_description, "uri": x.msg.attachment_url}]})
authk = "Bearer " + smooch_jwt
endpoint = "https://api.smooch.io/v1.1/apps/" + app_id + "/appusers/" + user_id + "/messages"
status = requests.post(
endpoint,
headers={"Content-Type": "application/json", "authorization": authk},
data=response_msg)
print("WE HAVE POSTED THE LINK")
time.sleep(3)
if x.msg.msg_type == "Image":
# create an instance of the API class
act = {}
act['uri'] = str(x.msg.attachment_url)
act['type'] = "link"
act['text'] = x.msg.attachment_description
message_post_body = smooch.MessagePost(text=msg_rez, role='appMaker', type='image', media_url=x.msg.attachment_url, actions=act, name=rez_name, avatar_url=rez_avatar)
api_response = api_instance.post_message(app_id, user_id, message_post_body)
time.sleep(2)
if x.msg.msg_type == "File":
# create an instance of the API class
message_post_body2 = smooch.MessagePost(text=msg_rez, role='appMaker', type='file', media_url=x.msg.attachment_url, name=rez_name, avatar_url=rez_avatar)
api_response = api_instance.post_message(app_id, user_id, message_post_body2)
time.sleep(2)
api_instance.conversation_activity(app_id, user_id, conversation_activity_body_stop)
return None