本文整理汇总了Python中temba.flows.models.Flow.label_to_slug方法的典型用法代码示例。如果您正苦于以下问题:Python Flow.label_to_slug方法的具体用法?Python Flow.label_to_slug怎么用?Python Flow.label_to_slug使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类temba.flows.models.Flow
的用法示例。
在下文中一共展示了Flow.label_to_slug方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: trigger_flow_webhook
# 需要导入模块: from temba.flows.models import Flow [as 别名]
# 或者: from temba.flows.models.Flow import label_to_slug [as 别名]
def trigger_flow_webhook(cls, run, webhook_url, ruleset, msg, action="POST", resthook=None, headers=None):
flow = run.flow
contact = run.contact
org = flow.org
channel = msg.channel if msg else None
contact_urn = msg.contact_urn if (msg and msg.contact_urn) else contact.get_urn()
contact_dict = dict(uuid=contact.uuid, name=contact.name)
if contact_urn:
contact_dict["urn"] = contact_urn.urn
post_data = {
"contact": contact_dict,
"flow": dict(name=flow.name, uuid=flow.uuid, revision=flow.revisions.order_by("revision").last().revision),
"path": run.path,
"results": run.results,
"run": dict(uuid=str(run.uuid), created_on=run.created_on.isoformat()),
}
if msg and msg.id > 0:
post_data["input"] = dict(
urn=msg.contact_urn.urn if msg.contact_urn else None,
text=msg.text,
attachments=(msg.attachments or []),
)
if channel:
post_data["channel"] = dict(name=channel.name, uuid=channel.uuid)
api_user = get_api_user()
if not action: # pragma: needs cover
action = "POST"
webhook_event = cls.objects.create(
org=org,
event=cls.TYPE_FLOW,
channel=channel,
data=post_data,
run=run,
try_count=1,
action=action,
resthook=resthook,
created_by=api_user,
modified_by=api_user,
)
status_code = -1
message = "None"
body = None
start = time.time()
# webhook events fire immediately since we need the results back
try:
# no url, bail!
if not webhook_url:
raise ValueError("No webhook_url specified, skipping send")
# only send webhooks when we are configured to, otherwise fail
if settings.SEND_WEBHOOKS:
requests_headers = http_headers(extra=headers)
# some hosts deny generic user agents, use Temba as our user agent
if action == "GET":
response = requests.get(webhook_url, headers=requests_headers, timeout=10)
else:
requests_headers["Content-type"] = "application/json"
response = requests.post(
webhook_url, data=json.dumps(post_data), headers=requests_headers, timeout=10
)
body = response.text
if body:
body = body.strip()
status_code = response.status_code
else:
print("!! Skipping WebHook send, SEND_WEBHOOKS set to False")
body = "Skipped actual send"
status_code = 200
if ruleset:
run.update_fields({Flow.label_to_slug(ruleset.label): body}, do_save=False)
new_extra = {}
# process the webhook response
try:
response_json = json.loads(body)
# only update if we got a valid JSON dictionary or list
if not isinstance(response_json, dict) and not isinstance(response_json, list):
raise ValueError("Response must be a JSON dictionary or list, ignoring response.")
new_extra = response_json
message = "Webhook called successfully."
except ValueError:
message = "Response must be a JSON dictionary, ignoring response."
run.update_fields(new_extra)
#.........这里部分代码省略.........