本文整理汇总了Python中temba.msgs.models.Msg类的典型用法代码示例。如果您正苦于以下问题:Python Msg类的具体用法?Python Msg怎么用?Python Msg使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Msg类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_resume_with_message_in_subflow
def test_resume_with_message_in_subflow(self, mock_report_success, mock_report_failure):
self.get_flow("subflow")
parent_flow = Flow.objects.get(org=self.org, name="Parent Flow")
child_flow = Flow.objects.get(org=self.org, name="Child Flow")
# start the parent flow and then trigger the subflow by picking an option
parent_flow.start([], [self.contact])
Msg.create_incoming(self.channel, "tel:+12065552020", "color")
self.assertEqual(mock_report_success.call_count, 1)
self.assertEqual(mock_report_failure.call_count, 0)
parent_run, child_run = list(FlowRun.objects.order_by("created_on"))
# check the reconstructed session for this run
session = resumes.reconstruct_session(child_run)
self.assertEqual(len(session["runs"]), 2)
self.assertEqual(session["runs"][0]["flow"]["uuid"], str(parent_flow.uuid))
self.assertEqual(session["runs"][1]["flow"]["uuid"], str(child_flow.uuid))
self.assertEqual(session["contact"]["uuid"], str(self.contact.uuid))
self.assertEqual(session["trigger"]["type"], "manual")
self.assertNotIn("results", session)
self.assertNotIn("events", session)
# and then resume by replying
Msg.create_incoming(self.channel, "tel:+12065552020", "I like red")
child_run.refresh_from_db()
parent_run.refresh_from_db()
# subflow run has completed
self.assertIsNotNone(child_run.exited_on)
self.assertIsNone(parent_run.exited_on)
self.assertEqual(mock_report_success.call_count, 2)
self.assertEqual(mock_report_failure.call_count, 0)
示例2: send_message
def send_message(
self,
flow,
message,
restart_participants=False,
contact=None,
initiate_flow=False,
assert_reply=True,
assert_handle=True,
):
"""
Starts the flow, sends the message, returns the reply
"""
if not contact:
contact = self.contact
try:
if contact.is_test:
Contact.set_simulation(True)
incoming = self.create_msg(
direction=INCOMING, contact=contact, contact_urn=contact.get_urn(), text=message
)
# start the flow
if initiate_flow:
flow.start(
groups=[], contacts=[contact], restart_participants=restart_participants, start_msg=incoming
)
else:
flow.start(groups=[], contacts=[contact], restart_participants=restart_participants)
(handled, msgs) = Flow.find_and_handle(incoming)
Msg.mark_handled(incoming)
if assert_handle:
self.assertTrue(handled, "'%s' did not handle message as expected" % flow.name)
else:
self.assertFalse(handled, "'%s' handled message, was supposed to ignore" % flow.name)
# our message should have gotten a reply
if assert_reply:
replies = Msg.objects.filter(response_to=incoming).order_by("pk")
self.assertGreaterEqual(len(replies), 1)
if len(replies) == 1:
self.assertEqual(contact, replies.first().contact)
return replies.first().text
# if it's more than one, send back a list of replies
return [reply.text for reply in replies]
else:
# assert we got no reply
replies = Msg.objects.filter(response_to=incoming).order_by("pk")
self.assertFalse(replies)
return None
finally:
Contact.set_simulation(False)
示例3: test_msg_events_with_attachments
def test_msg_events_with_attachments(self, mock_report_success, mock_report_failure):
# test an outgoing message with media
flow = self.get_flow("color")
flow_json = flow.as_json()
flow_json["action_sets"][2]["actions"][0]["media"] = {"base": "image/jpg:files/blue.jpg"}
flow.update(flow_json)
flow.start([], [self.contact])
Msg.create_incoming(self.channel, "tel:+12065552020", "blue")
self.assertEqual(mock_report_success.call_count, 1)
self.assertEqual(mock_report_failure.call_count, 0)
示例4: handle_async
def handle_async(self, urn, content, date, message_id):
from temba.msgs.models import Msg, USSD
Msg.create_incoming(
channel=self.channel,
org=self.org,
urn=urn,
text=content or "",
sent_on=date,
connection=self,
msg_type=USSD,
external_id=message_id,
)
示例5: test_webhook_mocking
def test_webhook_mocking(self, mock_report_success, mock_report_failure):
flow = self.get_flow("dual_webhook")
# mock the two webhook calls in this flow
self.mockRequest("POST", "/code", '{"code": "ABABUUDDLRS"}', content_type="application/json")
self.mockRequest("GET", "/success", "Success")
flow.start([], [self.contact])
Msg.create_incoming(self.channel, "tel:+12065552020", "Bob")
self.assertAllRequestsMade()
self.assertEqual(mock_report_success.call_count, 1)
self.assertEqual(mock_report_failure.call_count, 0)
示例6: test_trial_throttling
def test_trial_throttling(self, mock_report_success, mock_report_failure):
# first resume in a suitable flow will be trialled
favorites = self.get_flow("favorites")
favorites.start([], [self.contact], interrupt=True)
Msg.create_incoming(self.channel, "tel:+12065552020", "red")
self.assertEqual(mock_report_success.call_count, 1)
self.assertEqual(mock_report_failure.call_count, 0)
Msg.create_incoming(self.channel, "tel:+12065552020", "primus")
# second won't because its too soon
self.assertEqual(mock_report_success.call_count, 1)
self.assertEqual(mock_report_failure.call_count, 0)
示例7: create_unsolicited_incoming
def create_unsolicited_incoming(self, org):
if not org.cache["contacts"]:
return
self._log(" > Receiving unsolicited incoming message in org %s\n" % org.name)
available_contacts = list(set(org.cache["contacts"]) - set(org.cache["activity"]["started"]))
if available_contacts:
contact = Contact.objects.get(id=self.random_choice(available_contacts))
channel = self.random_choice(org.cache["channels"])
urn = contact.urns.first()
if urn:
text = " ".join([self.random_choice(l) for l in INBOX_MESSAGES])
Msg.create_incoming(channel, str(urn), text)
示例8: default
def default(self, line):
"""
Sends a message as the current contact's highest priority URN
"""
urn = self.contact.get_urn()
incoming = Msg.create_incoming(None, (urn.scheme, urn.path), line, date=timezone.now(), org=self.org)
Msg.process_message(incoming)
print((Fore.GREEN + "[%s] " + Fore.YELLOW + ">>" + Fore.MAGENTA + " %s" + Fore.WHITE) % (urn.urn, incoming.text))
# look up any message responses
outgoing = Msg.objects.filter(org=self.org, pk__gt=incoming.pk, direction=OUTGOING)
for response in outgoing:
print((Fore.GREEN + "[%s] " + Fore.YELLOW + "<<" + Fore.MAGENTA + " %s" + Fore.WHITE) % (urn.urn, response.text))
示例9: test_channellog
def test_channellog(self):
contact = self.create_contact("Test", "+250788383383")
msg = Msg.create_outgoing(self.org, self.admin, contact, "This is a test message")
msg = dict_to_struct('MockMsg', msg.as_task_json())
with SegmentProfiler("Channel Log inserts (10,000)", self, force_profile=True):
for i in range(10000):
ChannelLog.log_success(msg, "Sent Message", method="GET", url="http://foo",
request="GET http://foo", response="Ok", response_status="201")
示例10: test_message_incoming
def test_message_incoming(self):
num_contacts = 300
with SegmentProfiler("Creating incoming messages from new contacts", self, False, force_profile=True):
for c in range(0, num_contacts):
scheme, path, channel = self.urn_generators[c % len(self.urn_generators)](c)
Msg.create_incoming(channel, (scheme, path), "Thanks #1", self.user)
with SegmentProfiler("Creating incoming messages from existing contacts", self, False, force_profile=True):
for c in range(0, num_contacts):
scheme, path, channel = self.urn_generators[c % len(self.urn_generators)](c)
Msg.create_incoming(channel, (scheme, path), "Thanks #2", self.user)
# check messages for each channel
incoming_total = 2 * num_contacts
self.assertEqual(incoming_total / 3, Msg.objects.filter(direction=INCOMING, channel=self.tel_mtn).count())
self.assertEqual(incoming_total / 3, Msg.objects.filter(direction=INCOMING, channel=self.tel_tigo).count())
self.assertEqual(incoming_total / 3, Msg.objects.filter(direction=INCOMING, channel=self.twitter).count())
示例11: test_resume_with_expiration_in_subflow
def test_resume_with_expiration_in_subflow(self, mock_report_success, mock_report_failure):
self.get_flow("subflow")
parent_flow = Flow.objects.get(org=self.org, name="Parent Flow")
# start the parent flow and then trigger the subflow by picking an option
parent_flow.start([], [self.contact])
Msg.create_incoming(self.channel, "tel:+12065552020", "color")
parent_run, child_run = list(FlowRun.objects.order_by("created_on"))
# resume by expiring the child run
child_run.expire()
child_run.refresh_from_db()
parent_run.refresh_from_db()
# which should end both our runs
self.assertIsNotNone(child_run.exited_on)
self.assertIsNotNone(parent_run.exited_on)
self.assertEqual(mock_report_success.call_count, 2)
self.assertEqual(mock_report_failure.call_count, 0)
示例12: test_resume_with_message
def test_resume_with_message(self, mock_report_success, mock_report_failure):
favorites = self.get_flow("favorites")
run, = favorites.start([], [self.contact])
# check the reconstructed session for this run
session = resumes.reconstruct_session(run)
self.assertEqual(len(session["runs"]), 1)
self.assertEqual(session["runs"][0]["flow"]["uuid"], str(favorites.uuid))
self.assertEqual(session["contact"]["uuid"], str(self.contact.uuid))
self.assertNotIn("results", session)
self.assertNotIn("events", session)
# and then resume by replying
Msg.create_incoming(
self.channel, "tel:+12065552020", "I like red", attachments=["image/jpeg:http://example.com/red.jpg"]
)
run.refresh_from_db()
self.assertEqual(mock_report_success.call_count, 1)
self.assertEqual(mock_report_failure.call_count, 0)
# and then resume by replying again
Msg.create_incoming(self.channel, "tel:+12065552020", "ooh Primus")
run.refresh_from_db()
self.assertEqual(mock_report_success.call_count, 2)
self.assertEqual(mock_report_failure.call_count, 0)
# simulate session not containing this run
self.assertEqual(set(resumes.compare(run, {"runs": []}).keys()), {"session"})
# simulate differences in the path, results and events
session = resumes.reconstruct_session(run)
session["runs"][0]["path"][0]["node_uuid"] = "wrong node"
session["runs"][0]["results"]["color"]["value"] = "wrong value"
session["runs"][0]["events"][0]["msg"]["text"] = "wrong text"
self.assertTrue(resumes.compare(run, session)["diffs"])
示例13: save
def save(self):
user = self.context['user']
contacts = self.validated_data['contacts']
action = self.validated_data['action']
group = self.validated_data.get('group')
if action == self.ADD:
group.update_contacts(user, contacts, add=True)
elif action == self.REMOVE:
group.update_contacts(user, contacts, add=False)
elif action == self.INTERRUPT:
FlowRun.exit_all_for_contacts(contacts, FlowRun.EXIT_TYPE_INTERRUPTED)
elif action == self.ARCHIVE:
Msg.archive_all_for_contacts(contacts)
else:
for contact in contacts:
if action == self.BLOCK:
contact.block(user)
elif action == self.UNBLOCK:
contact.unblock(user)
elif action == self.DELETE:
contact.release(user)
示例14: create_flow_run
def create_flow_run(self, org):
activity = org.cache["activity"]
flow = activity["flow"]
if activity["unresponded"]:
contact_id = self.random_choice(activity["unresponded"])
activity["unresponded"].remove(contact_id)
contact = Contact.objects.get(id=contact_id)
urn = contact.urns.first()
if urn:
self._log(" > Receiving flow responses for flow %s in org %s\n" % (flow.name, flow.org.name))
inputs = self.random_choice(flow.input_templates)
for text in inputs:
channel = flow.org.cache["channels"][0]
Msg.create_incoming(channel, str(urn), text)
# if more than 10% of contacts have responded, consider flow activity over
if len(activity["unresponded"]) <= (len(activity["started"]) * 0.9):
self.end_flow_activity(flow.org)
示例15: default
def default(self, line):
"""
Sends a message as the current contact's highest priority URN
"""
urn = self.contact.get_urn()
incoming = Msg.create_incoming(None, URN.from_parts(urn.scheme, urn.path),
line, date=timezone.now(), org=self.org)
self.echo((Fore.GREEN + "[%s] " + Fore.YELLOW + ">>" + Fore.MAGENTA + " %s" + Fore.WHITE) % (urn.urn, incoming.text))
# look up any message responses
outgoing = Msg.all_messages.filter(org=self.org, pk__gt=incoming.pk, direction=OUTGOING).order_by('sent_on')
for response in outgoing:
self.echo((Fore.GREEN + "[%s] " + Fore.YELLOW + "<<" + Fore.MAGENTA + " %s" + Fore.WHITE) % (urn.urn, response.text))