diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 000000000..b25735215 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,15 @@ +# Lines starting with '#' are comments. +# Each line is a file pattern followed by one or more owners. + +# More details are here: https://help.github.com/articles/about-codeowners/ + +# The '*' pattern is global owners. + +# Order is important. The last matching pattern has the most precedence. +# The folders are ordered as follows: + +# In each subsection folders are ordered first by depth, then alphabetically. +# This should make it easy to add new rules without breaking existing ones. + +# Global rule: +* @microsoft/botframework-sdk \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/python-sdk-bug.md b/.github/ISSUE_TEMPLATE/python-sdk-bug.md index 3fd6037d9..435fe4310 100644 --- a/.github/ISSUE_TEMPLATE/python-sdk-bug.md +++ b/.github/ISSUE_TEMPLATE/python-sdk-bug.md @@ -1,9 +1,13 @@ --- name: Python SDK Bug about: Create a bug report for a bug you found in the Bot Builder Python SDK - +title: "" +labels: "needs-triage, bug" +assignees: "" --- +### [Github issues](https://github.com/Microsoft/botbuilder-python) should be used for bugs and feature requests. Use [Stack Overflow](https://stackoverflow.com/questions/tagged/botframework) for general "how-to" questions. + ## Version What package version of the SDK are you using. @@ -25,5 +29,3 @@ If applicable, add screenshots to help explain your problem. ## Additional context Add any other context about the problem here. - -[bug] diff --git a/.github/ISSUE_TEMPLATE/python-sdk-feature-request.md b/.github/ISSUE_TEMPLATE/python-sdk-feature-request.md index e3f0aad0e..d498599d9 100644 --- a/.github/ISSUE_TEMPLATE/python-sdk-feature-request.md +++ b/.github/ISSUE_TEMPLATE/python-sdk-feature-request.md @@ -1,9 +1,13 @@ --- name: Python SDK Feature Request about: Suggest a feature for the Bot Builder Python SDK - +title: "" +labels: "needs-triage, feature-request" +assignees: "" --- +### Use this [query](https://github.com/Microsoft/botbuilder-python/issues?q=is%3Aissue+is%3Aopen++label%3Afeature-request+) to search for the most popular feature requests. + **Is your feature request related to a problem? Please describe.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] @@ -15,5 +19,3 @@ A clear and concise description of any alternative solutions or features you've **Additional context** Add any other context or screenshots about the feature request here. - -[enhancement] diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 000000000..e8870cd7e --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,14 @@ +Fixes # + +## Description + + +## Specific Changes + + + - + - + - + +## Testing + \ No newline at end of file diff --git a/.github/workflows/create-parity-issue.yml b/.github/workflows/create-parity-issue.yml new file mode 100644 index 000000000..51f47a190 --- /dev/null +++ b/.github/workflows/create-parity-issue.yml @@ -0,0 +1,43 @@ +name: create-parity-issue.yml + +on: + workflow_dispatch: + inputs: + prDescription: + description: PR description + default: 'No description provided' + required: true + prNumber: + description: PR number + required: true + prTitle: + description: PR title + required: true + sourceRepo: + description: repository PR is sourced from + required: true + +jobs: + createIssue: + name: create issue + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - uses: joshgummersall/create-issue@main + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + title: | + port: ${{ github.event.inputs.prTitle }} (#${{ github.event.inputs.prNumber }}) + labels: | + ["parity", "needs-triage", "ExemptFromDailyDRIReport"] + body: | + The changes in [${{ github.event.inputs.prTitle }} (#${{ github.event.inputs.prNumber }})](https://github.com/${{ github.event.inputs.sourceRepo }}/pull/${{ github.event.inputs.prNumber }}) may need to be ported to maintain parity with `${{ github.event.inputs.sourceRepo }}`. + +
+ ${{ github.event.inputs.prDescription }} +
+ + Please review and, if necessary, port the changes. diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 8ae9df9dc..000000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,13 +0,0 @@ - -on: [issues] - -jobs: - on-issue-update: - runs-on: ubuntu-latest - name: Tag issues - steps: - - name: Issue tagging - id: issue-autotagger - uses: christopheranderson/issue-autotagger@v1 - with: - repo-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/pr-style.yml b/.github/workflows/pr-style.yml new file mode 100644 index 000000000..51dbf531a --- /dev/null +++ b/.github/workflows/pr-style.yml @@ -0,0 +1,18 @@ +name: pr-style.yml + +on: + pull_request: + types: [opened, edited, synchronize] + +jobs: + prStyle: + name: pr-style + runs-on: ubuntu-latest + + steps: + - uses: joshgummersall/pr-style@main + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + require_issue: "true" + skip_authors: "dependabot" diff --git a/.gitignore b/.gitignore index e1501373b..fcaec6436 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Python cache __pycache__/ *.pyc +.ipynb_checkpoints/ # Virtual environment env*/ @@ -67,4 +68,4 @@ src/build *.pubxml # [begoldsm] ignore virtual env if it exists. -adlEnv/ \ No newline at end of file +adlEnv/ diff --git a/.pylintrc b/.pylintrc index a134068ff..70444a1d8 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,24 +1,77 @@ [MASTER] +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Clear in-memory caches upon conclusion of linting. Useful if running pylint +# in a server-like mode. +clear-cache-post-run=no + +# Load and enable all available extensions. Use --list-extensions to see a list +# all available extensions. +#enable-all-extensions= + +# In error mode, messages with a category besides ERROR or FATAL are +# suppressed, and no reports are done by default. Error mode is compatible with +# disabling specific errors. +#errors-only= + +# Always return a 0 (non-error) status code, even if lint errors are found. +# This is primarily useful in continuous integration scripts. +#exit-zero= + # A comma-separated list of package or module names from where C extensions may # be loaded. Extensions are loading into the active Python interpreter and may # run arbitrary code. +extension-pkg-allow-list= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. (This is an alternative name to extension-pkg-allow-list +# for backward compatibility.) extension-pkg-whitelist= -# Add files or directories to the blacklist. They should be base names, not -# paths. -ignore=CVS,build,botbuilder-schema,samples,django_tests,Generator,operations,operations_async,schema +# Return non-zero exit code if any of these messages/categories are detected, +# even if score is above --fail-under value. Syntax same as enable. Messages +# specified are enabled, while categories only check already-enabled messages. +fail-on= + +# Specify a score threshold under which the program will exit with error. +fail-under=10 + +# Interpret the stdin as a python script, whose filename needs to be passed as +# the module_or_package argument. +#from-stdin= + +# Files or directories to be skipped. They should be base names, not paths. +ignore=CVS + +# Add files or directories matching the regular expressions patterns to the +# ignore-list. The regex matches against paths and can be in Posix or Windows +# format. Because '\\' represents the directory delimiter on Windows systems, +# it can't be used as an escape character. +ignore-paths= -# Add files or directories matching the regex patterns to the blacklist. The -# regex matches against base names, not paths. -ignore-patterns=setup.py,azure_bdist_wheel.py +# Files or directories matching the regular expression patterns are skipped. +# The regex matches against base names, not paths. The default value ignores +# Emacs file locks +ignore-patterns=^\.# + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis). It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= # Python code to execute, usually for sys.path manipulation such as # pygtk.require(). #init-hook= # Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the -# number of processors available to use. +# number of processors available to use, and will cap the count on Windows to +# avoid hangs. jobs=1 # Control the amount of potential inferred values when inferring a single @@ -26,15 +79,25 @@ jobs=1 # complex, nested conditions. limit-inference-results=100 -# List of plugins (as comma separated values of python modules names) to load, +# List of plugins (as comma separated values of python module names) to load, # usually to register additional checkers. load-plugins= # Pickle collected data for later comparisons. persistent=yes -# Specify a configuration file. -#rcfile= +# Minimum Python version to use for version dependent checks. Will default to +# the version used to run pylint. +py-version=3.8 + +# Discover python modules and packages in the file system subtree. +recursive=no + +# Add paths to the list of the source roots. Supports globbing patterns. The +# source root is an absolute path or a path relative to the current working +# directory used to determine a package namespace for modules located under the +# source root. +source-roots= # When enabled, pylint would attempt to guess common misconfiguration and emit # user-friendly hints instead of false-positive error messages. @@ -44,337 +107,8 @@ suggestion-mode=yes # active Python interpreter and may run arbitrary code. unsafe-load-any-extension=no - -[MESSAGES CONTROL] - -# Only show warnings with the listed confidence levels. Leave empty to show -# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. -confidence= - -# Disable the message, report, category or checker with the given id(s). You -# can either give multiple identifiers separated by comma (,) or put this -# option multiple times (only on the command line, not in the configuration -# file where it should appear only once). You can also use "--disable=all" to -# disable everything first and then reenable specific checks. For example, if -# you want to run only the similarities checker, you can use "--disable=all -# --enable=similarities". If you want to run only the classes checker, but have -# no Warning level messages displayed, use "--disable=all --enable=classes -# --disable=W". -disable=print-statement, - parameter-unpacking, - unpacking-in-except, - old-raise-syntax, - backtick, - long-suffix, - old-ne-operator, - old-octal-literal, - import-star-module-level, - non-ascii-bytes-literal, - raw-checker-failed, - bad-inline-option, - locally-disabled, - file-ignored, - suppressed-message, - useless-suppression, - deprecated-pragma, - use-symbolic-message-instead, - apply-builtin, - basestring-builtin, - buffer-builtin, - cmp-builtin, - coerce-builtin, - execfile-builtin, - file-builtin, - long-builtin, - raw_input-builtin, - reduce-builtin, - standarderror-builtin, - unicode-builtin, - xrange-builtin, - coerce-method, - delslice-method, - getslice-method, - setslice-method, - no-absolute-import, - old-division, - dict-iter-method, - dict-view-method, - next-method-called, - metaclass-assignment, - indexing-exception, - raising-string, - reload-builtin, - oct-method, - hex-method, - nonzero-method, - cmp-method, - input-builtin, - round-builtin, - intern-builtin, - unichr-builtin, - map-builtin-not-iterating, - zip-builtin-not-iterating, - range-builtin-not-iterating, - filter-builtin-not-iterating, - using-cmp-argument, - eq-without-hash, - div-method, - idiv-method, - rdiv-method, - exception-message-attribute, - invalid-str-codec, - sys-max-int, - bad-python3-import, - deprecated-string-function, - deprecated-str-translate-call, - deprecated-itertools-function, - deprecated-types-field, - next-method-defined, - dict-items-not-iterating, - dict-keys-not-iterating, - dict-values-not-iterating, - deprecated-operator-function, - deprecated-urllib-function, - xreadlines-attribute, - deprecated-sys-function, - exception-escape, - comprehension-escape, - bad-continuation, - duplicate-code, - redefined-outer-name, - missing-docstring, - too-many-instance-attributes, - too-few-public-methods, - redefined-builtin, - too-many-arguments, - no-self-use, - fixme, - broad-except, - bare-except, - too-many-public-methods, - cyclic-import, - too-many-locals, - too-many-function-args, - too-many-return-statements, - import-error, - no-name-in-module, - too-many-branches - -# Enable the message, report, category or checker with the given id(s). You can -# either give multiple identifier separated by comma (,) or put this option -# multiple time (only on the command line, not in the configuration file where -# it should appear only once). See also the "--disable" option for examples. -enable=c-extension-no-member - - -[REPORTS] - -# Python expression which should return a note less than 10 (10 is the highest -# note). You have access to the variables errors warning, statement which -# respectively contain the number of errors / warnings messages and the total -# number of statements analyzed. This is used by the global evaluation report -# (RP0004). -evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) - -# Template used to display messages. This is a python new-style format string -# used to format the message information. See doc for all details. -#msg-template= - -# Set the output format. Available formats are text, parseable, colorized, json -# and msvs (visual studio). You can also give a reporter class, e.g. -# mypackage.mymodule.MyReporterClass. -output-format=text - -# Tells whether to display a full report or only the messages. -reports=no - -# Activate the evaluation score. -score=yes - - -[REFACTORING] - -# Maximum number of nested blocks for function / method body -max-nested-blocks=5 - -# Complete name of functions that never returns. When checking for -# inconsistent-return-statements if a never returning function is called then -# it will be considered as an explicit return statement and no message will be -# printed. -never-returning-functions=sys.exit - - -[LOGGING] - -# Format style used to check logging format string. `old` means using % -# formatting, while `new` is for `{}` formatting. -logging-format-style=old - -# Logging modules to check that the string format arguments are in logging -# function parameter format. -logging-modules=logging - - -[SPELLING] - -# Limits count of emitted suggestions for spelling mistakes. -max-spelling-suggestions=4 - -# Spelling dictionary name. Available dictionaries: none. To make it working -# install python-enchant package.. -spelling-dict= - -# List of comma separated words that should not be checked. -spelling-ignore-words= - -# A path to a file that contains private dictionary; one word per line. -spelling-private-dict-file= - -# Tells whether to store unknown words to indicated private dictionary in -# --spelling-private-dict-file option instead of raising a message. -spelling-store-unknown-words=no - - -[MISCELLANEOUS] - -# List of note tags to take in consideration, separated by a comma. -notes=FIXME, - XXX, - TODO - - -[TYPECHECK] - -# List of decorators that produce context managers, such as -# contextlib.contextmanager. Add to this list to register other decorators that -# produce valid context managers. -contextmanager-decorators=contextlib.contextmanager - -# List of members which are set dynamically and missed by pylint inference -# system, and so shouldn't trigger E1101 when accessed. Python regular -# expressions are accepted. -generated-members= - -# Tells whether missing members accessed in mixin class should be ignored. A -# mixin class is detected if its name ends with "mixin" (case insensitive). -ignore-mixin-members=yes - -# Tells whether to warn about missing members when the owner of the attribute -# is inferred to be None. -ignore-none=yes - -# This flag controls whether pylint should warn about no-member and similar -# checks whenever an opaque object is returned when inferring. The inference -# can return multiple potential results while evaluating a Python object, but -# some branches might not be evaluated, which results in partial inference. In -# that case, it might be useful to still emit no-member and other checks for -# the rest of the inferred objects. -ignore-on-opaque-inference=yes - -# List of class names for which member attributes should not be checked (useful -# for classes with dynamically set attributes). This supports the use of -# qualified names. -ignored-classes=optparse.Values,thread._local,_thread._local - -# List of module names for which member attributes should not be checked -# (useful for modules/projects where namespaces are manipulated during runtime -# and thus existing member attributes cannot be deduced by static analysis. It -# supports qualified module names, as well as Unix pattern matching. -ignored-modules= - -# Show a hint with possible names when a member name was not found. The aspect -# of finding the hint is based on edit distance. -missing-member-hint=yes - -# The minimum edit distance a name should have in order to be considered a -# similar match for a missing member name. -missing-member-hint-distance=1 - -# The total number of similar names that should be taken in consideration when -# showing a hint for a missing member. -missing-member-max-choices=1 - - -[VARIABLES] - -# List of additional names supposed to be defined in builtins. Remember that -# you should avoid defining new builtins when possible. -additional-builtins= - -# Tells whether unused global variables should be treated as a violation. -allow-global-unused-variables=yes - -# List of strings which can identify a callback function by name. A callback -# name must start or end with one of those strings. -callbacks=cb_, - _cb - -# A regular expression matching the name of dummy variables (i.e. expected to -# not be used). -dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ - -# Argument names that match this expression will be ignored. Default to name -# with leading underscore. -ignored-argument-names=_.*|^ignored_|^unused_ - -# Tells whether we should check for unused import in __init__ files. -init-import=no - -# List of qualified module names which can have objects that can redefine -# builtins. -redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io - - -[FORMAT] - -# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. -expected-line-ending-format= - -# Regexp for a line that is allowed to be longer than the limit. -ignore-long-lines=^\s*(# )??$ - -# Number of spaces of indent required inside a hanging or continued line. -indent-after-paren=4 - -# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 -# tab). -indent-string=' ' - -# Maximum number of characters on a single line. -max-line-length=120 - -# Maximum number of lines in a module. -max-module-lines=1000 - -# List of optional constructs for which whitespace checking is disabled. `dict- -# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. -# `trailing-comma` allows a space between comma and closing bracket: (a, ). -# `empty-line` allows space-only lines. -no-space-check=trailing-comma, - dict-separator - -# Allow the body of a class to be on the same line as the declaration if body -# contains single statement. -single-line-class-stmt=no - -# Allow the body of an if to be on the same line as the test if there is no -# else. -single-line-if-stmt=no - - -[SIMILARITIES] - -# Ignore comments when computing similarities. -ignore-comments=yes - -# Ignore docstrings when computing similarities. -ignore-docstrings=yes - -# Ignore imports when computing similarities. -ignore-imports=no - -# Minimum lines number of a similarity. -min-similarity-lines=4 +# In verbose mode, extra non-checker-related info will be displayed. +#verbose= [BASIC] @@ -383,13 +117,15 @@ min-similarity-lines=4 argument-naming-style=snake_case # Regular expression matching correct argument names. Overrides argument- -# naming-style. +# naming-style. If left empty, argument names will be checked with the set +# naming style. #argument-rgx= # Naming style matching correct attribute names. attr-naming-style=snake_case # Regular expression matching correct attribute names. Overrides attr-naming- +# style. If left empty, attribute names will be checked with the set naming # style. #attr-rgx= @@ -401,24 +137,38 @@ bad-names=foo, tutu, tata +# Bad variable names regexes, separated by a comma. If names match any regex, +# they will always be refused +bad-names-rgxs= + # Naming style matching correct class attribute names. class-attribute-naming-style=any # Regular expression matching correct class attribute names. Overrides class- -# attribute-naming-style. +# attribute-naming-style. If left empty, class attribute names will be checked +# with the set naming style. #class-attribute-rgx= +# Naming style matching correct class constant names. +class-const-naming-style=UPPER_CASE + +# Regular expression matching correct class constant names. Overrides class- +# const-naming-style. If left empty, class constant names will be checked with +# the set naming style. +#class-const-rgx= + # Naming style matching correct class names. class-naming-style=PascalCase # Regular expression matching correct class names. Overrides class-naming- -# style. +# style. If left empty, class names will be checked with the set naming style. #class-rgx= # Naming style matching correct constant names. const-naming-style=UPPER_CASE # Regular expression matching correct constant names. Overrides const-naming- +# style. If left empty, constant names will be checked with the set naming # style. #const-rgx= @@ -430,7 +180,8 @@ docstring-min-length=-1 function-naming-style=snake_case # Regular expression matching correct function names. Overrides function- -# naming-style. +# naming-style. If left empty, function names will be checked with the set +# naming style. #function-rgx= # Good variable names which should always be accepted, separated by a comma. @@ -441,6 +192,10 @@ good-names=i, Run, _ +# Good variable names regexes, separated by a comma. If names match any regex, +# they will always be accepted +good-names-rgxs= + # Include a hint for the correct naming format with invalid-name. include-naming-hint=no @@ -448,21 +203,22 @@ include-naming-hint=no inlinevar-naming-style=any # Regular expression matching correct inline iteration names. Overrides -# inlinevar-naming-style. +# inlinevar-naming-style. If left empty, inline iteration names will be checked +# with the set naming style. #inlinevar-rgx= # Naming style matching correct method names. method-naming-style=snake_case # Regular expression matching correct method names. Overrides method-naming- -# style. +# style. If left empty, method names will be checked with the set naming style. #method-rgx= # Naming style matching correct module names. module-naming-style=snake_case # Regular expression matching correct module names. Overrides module-naming- -# style. +# style. If left empty, module names will be checked with the set naming style. #module-rgx= # Colon-delimited sets of names that determine each other's naming style when @@ -478,86 +234,62 @@ no-docstring-rgx=^_ # These decorators are taken in consideration only for invalid-name. property-classes=abc.abstractproperty +# Regular expression matching correct type alias names. If left empty, type +# alias names will be checked with the set naming style. +#typealias-rgx= + +# Regular expression matching correct type variable names. If left empty, type +# variable names will be checked with the set naming style. +#typevar-rgx= + # Naming style matching correct variable names. variable-naming-style=snake_case # Regular expression matching correct variable names. Overrides variable- -# naming-style. +# naming-style. If left empty, variable names will be checked with the set +# naming style. #variable-rgx= -[STRING] - -# This flag controls whether the implicit-str-concat-in-sequence should -# generate a warning on implicit string concatenation in sequences defined over -# several lines. -check-str-concat-over-line-jumps=no +[CLASSES] - -[IMPORTS] - -# Allow wildcard imports from modules that define __all__. -allow-wildcard-with-all=no - -# Analyse import fallback blocks. This can be used to support both Python 2 and -# 3 compatible code, which means that the block might have code that exists -# only in one or another interpreter, leading to false positives when analysed. -analyse-fallback-blocks=no - -# Deprecated modules which should not be used, separated by a comma. -deprecated-modules=optparse,tkinter.tix - -# Create a graph of external dependencies in the given file (report RP0402 must -# not be disabled). -ext-import-graph= - -# Create a graph of every (i.e. internal and external) dependencies in the -# given file (report RP0402 must not be disabled). -import-graph= - -# Create a graph of internal dependencies in the given file (report RP0402 must -# not be disabled). -int-import-graph= - -# Force import order to recognize a module as part of the standard -# compatibility libraries. -known-standard-library= - -# Force import order to recognize a module as part of a third party library. -known-third-party=enchant - - -[CLASSES] +# Warn about protected attribute access inside special methods +check-protected-access-in-special-methods=no # List of method names used to declare (i.e. assign) instance attributes. defining-attr-methods=__init__, __new__, - setUp + setUp, + __post_init__ # List of member names, which should be excluded from the protected access # warning. -exclude-protected=_asdict, - _fields, - _replace, - _source, - _make +exclude-protected=_asdict,_fields,_replace,_source,_make,os._exit # List of valid names for the first argument in a class method. valid-classmethod-first-arg=cls # List of valid names for the first argument in a metaclass class method. -valid-metaclass-classmethod-first-arg=cls +valid-metaclass-classmethod-first-arg=mcs,cls [DESIGN] +# List of regular expressions of class ancestor names to ignore when counting +# public methods (see R0903) +exclude-too-few-public-methods= + +# List of qualified class names to ignore when counting class parents (see +# R0901) +ignored-parents= + # Maximum number of arguments for function / method. max-args=5 # Maximum number of attributes for a class (see R0902). max-attributes=7 -# Maximum number of boolean expressions in an if statement. +# Maximum number of boolean expressions in an if statement (see R0916). max-bool-expr=5 # Maximum number of branch for function / method body. @@ -584,7 +316,376 @@ min-public-methods=2 [EXCEPTIONS] -# Exceptions that will emit a warning when being caught. Defaults to -# "BaseException, Exception". -overgeneral-exceptions=BaseException, - Exception +# Exceptions that will emit a warning when caught. +overgeneral-exceptions=builtins.BaseException,builtins.Exception + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=120 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[IMPORTS] + +# List of modules that can be imported at any level, not just the top level +# one. +allow-any-import-level= + +# Allow explicit reexports by alias from a package __init__. +allow-reexport-from-package=no + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules= + +# Output a graph (.gv or any supported image format) of external dependencies +# to the given file (report RP0402 must not be disabled). +ext-import-graph= + +# Output a graph (.gv or any supported image format) of all (i.e. internal and +# external) dependencies to the given file (report RP0402 must not be +# disabled). +import-graph= + +# Output a graph (.gv or any supported image format) of internal dependencies +# to the given file (report RP0402 must not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + +# Couples of modules and preferred modules, separated by a comma. +preferred-modules= + + +[LOGGING] + +# The type of string formatting that logging methods do. `old` means using % +# formatting, `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, CONTROL_FLOW, INFERENCE, INFERENCE_FAILURE, +# UNDEFINED. +confidence=HIGH, + CONTROL_FLOW, + INFERENCE, + INFERENCE_FAILURE, + UNDEFINED + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then re-enable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=raw-checker-failed, + bad-inline-option, + locally-disabled, + file-ignored, + suppressed-message, + useless-suppression, + deprecated-pragma, + use-symbolic-message-instead, + duplicate-code, + redefined-outer-name, + missing-docstring, + too-many-instance-attributes, + too-few-public-methods, + redefined-builtin, + too-many-arguments, + fixme, + broad-except, + bare-except, + too-many-public-methods, + cyclic-import, + too-many-locals, + too-many-function-args, + too-many-return-statements, + import-error, + no-name-in-module, + too-many-branches, + too-many-ancestors, + too-many-nested-blocks, + attribute-defined-outside-init, + super-with-arguments, + missing-timeout, + broad-exception-raised, + exec-used, + unspecified-encoding, + unused-variable, + consider-using-f-string, + raise-missing-from, + invalid-name, + useless-object-inheritance, + no-else-raise, + implicit-str-concat, + use-dict-literal, + use-list-literal, + unnecessary-dunder-call, + consider-using-in, + consider-using-with, + useless-parent-delegation, + f-string-without-interpolation, + global-variable-not-assigned, + dangerous-default-value, + wrong-import-order, + wrong-import-position, + ungrouped-imports, + import-outside-toplevel, + consider-using-from-import, + reimported, + unused-import, + unused-argument, + arguments-renamed, + unused-private-member, + unidiomatic-typecheck, + protected-access, + used-before-assignment, + invalid-overridden-method, + no-member, + deprecated-module, + too-many-lines, + c-extension-no-member, + unsubscriptable-object + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable= + + +[METHOD_ARGS] + +# List of qualified names (i.e., library.method) which require a timeout +# parameter e.g. 'requests.api.get,requests.api.post' +timeout-methods=requests.api.delete,requests.api.get,requests.api.head,requests.api.options,requests.api.patch,requests.api.post,requests.api.put,requests.api.request + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + +# Regular expression of note tags to take in consideration. +notes-rgx= + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit,argparse.parse_error + + +[REPORTS] + +# Python expression which should return a score less than or equal to 10. You +# have access to the variables 'fatal', 'error', 'warning', 'refactor', +# 'convention', and 'info' which contain the number of messages in each +# category, as well as 'statement' which is the total number of statements +# analyzed. This score is used by the global evaluation report (RP0004). +evaluation=max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +#output-format= + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[SIMILARITIES] + +# Comments are removed from the similarity computation +ignore-comments=yes + +# Docstrings are removed from the similarity computation +ignore-docstrings=yes + +# Imports are removed from the similarity computation +ignore-imports=yes + +# Signatures are removed from the similarity computation +ignore-signatures=yes + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. No available dictionaries : You need to install +# both the python package and the system dependency for enchant to work.. +spelling-dict= + +# List of comma separated words that should be considered directives if they +# appear at the beginning of a comment and should not be checked. +spelling-ignore-comment-directives=fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy: + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains the private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to the private dictionary (see the +# --spelling-private-dict-file option) instead of raising a message. +spelling-store-unknown-words=no + + +[STRING] + +# This flag controls whether inconsistent-quotes generates a warning when the +# character used as a quote delimiter is used inconsistently within a module. +check-quote-consistency=no + +# This flag controls whether the implicit-str-concat should generate a warning +# on implicit string concatenation in sequences defined over several lines. +check-str-concat-over-line-jumps=no + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of symbolic message names to ignore for Mixin members. +ignored-checks-for-mixins=no-member, + not-async-context-manager, + not-context-manager, + attribute-defined-outside-init + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local,argparse.Namespace + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + +# Regex pattern to define which classes are considered mixins. +mixin-class-rgx=.*[Mm]ixin + +# List of decorators that change the signature of a decorated function. +signature-mutators= + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of names allowed to shadow builtins +allowed-redefined-builtins= + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 000000000..f9ba8cf65 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,9 @@ +# Microsoft Open Source Code of Conduct + +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). + +Resources: + +- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/) +- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) +- Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns diff --git a/Contributing.md b/Contributing.md new file mode 100644 index 000000000..41a2e6153 --- /dev/null +++ b/Contributing.md @@ -0,0 +1,23 @@ +# Instructions for Contributing Code + +## Contributing bug fixes and features + +The Bot Framework team is currently accepting contributions in the form of bug fixes and new +features. Any submission must have an issue tracking it in the issue tracker that has + been approved by the Bot Framework team. Your pull request should include a link to + the bug that you are fixing. If you've submitted a PR for a bug, please post a + comment in the bug to avoid duplication of effort. + +## Legal + +If your contribution is more than 15 lines of code, you will need to complete a Contributor +License Agreement (CLA). Briefly, this agreement testifies that you are granting us permission + to use the submitted change according to the terms of the project's license, and that the work + being submitted is under appropriate copyright. + +Please submit a Contributor License Agreement (CLA) before submitting a pull request. +You may visit https://cla.azure.com to sign digitally. Alternatively, download the +agreement ([Microsoft Contribution License Agreement.docx](https://www.codeplex.com/Download?ProjectName=typescript&DownloadId=822190) or + [Microsoft Contribution License Agreement.pdf](https://www.codeplex.com/Download?ProjectName=typescript&DownloadId=921298)), sign, scan, + and email it back to . Be sure to include your github user name along with the agreement. Once we have received the + signed CLA, we'll review the request. \ No newline at end of file diff --git a/README.md b/README.md index e4347845d..4e90050c0 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,157 @@ # ![Bot Framework SDK v4 Python](./doc/media/FrameWorkPython.png) -### [Click here to find out what's new with Bot Framework](https://docs.microsoft.com/en-us/azure/bot-service/what-is-new?view=azure-bot-service-4.0) +# ARCHIVE NOTICE: -# Bot Framework SDK v4 for Python -[![Build Status](https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master)](https://fuselabs.visualstudio.com/SDK_v4/_build/latest?definitionId=771&branchName=master) -[![roadmap badge](https://img.shields.io/badge/visit%20the-roadmap-blue.svg)](https://github.com/Microsoft/botbuilder-python/wiki/Roadmap) -[![Coverage Status](https://coveralls.io/repos/github/microsoft/botbuilder-python/badge.svg?branch=HEAD)](https://coveralls.io/github/microsoft/botbuilder-python?branch=HEAD) -[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) +> We are in the process of archiving the Bot Framework Python SDK repository on GitHub. This means that this project will no longer be updated or maintained. Customers using this tool will not be disrupted. However, the tool will no longer be supported through +> service tickets in the Azure portal and will not receive product updates. -This repository contains code for the Python version of the [Microsoft Bot Framework SDK](https://github.com/Microsoft/botbuilder). The Bot Framework SDK v4 enables developers to model conversation and build sophisticated bot applications using Python. This Python version is GA and ready for production usage. +> To build agents with your choice of AI services, orchestration, and knowledge, consider using the [Microsoft 365 Agents SDK](https://github.com/microsoft/agents). The Agents SDK is GA and has support for C#, JavaScript or Python. You can learn more about the Agents SDK at aka.ms/agents. If you're looking for a SaaS-based agent platform, consider Microsoft Copilot Studio. If you have an existing bot built with the Bot Framework SDK, you can update your bot to the Agents SDK. You can review the core changes and updates at Bot Framework SDK to Agents SDK migration guidance [here](https://learn.microsoft.com/en-us/microsoft-365/agents-sdk/bf-migration-guidance). Support tickets for the Bot Framework SDK will no longer be serviced as of December 31, 2025. -This repo is part the [Microsoft Bot Framework](https://github.com/Microsoft/botframework) - a comprehensive framework for building enterprise-grade conversational AI experiences. +> We plan to archive this project no later than end of December of 2025. -In addition to the Python SDK, Bot Builder supports creating bots in other popular programming languages like [.Net SDK](https://github.com/Microsoft/botbuilder-dotnet), [JavaScript](https://github.com/Microsoft/botbuilder-js), and [Java](https://github.com/Microsoft/botbuilder-java). +This repository contains code for the Python version of the [Microsoft Bot Framework SDK](https://github.com/Microsoft/botframework-sdk), which is part of the Microsoft Bot Framework - a comprehensive framework for building enterprise-grade conversational AI experiences. -To get started see the [Azure Bot Service Documentation](https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0) for the v4 SDK. +This SDK enables developers to model conversation and build sophisticated bot applications using Python. SDKs for [JavaScript](https://github.com/Microsoft/botbuilder-js) and [.NET](https://github.com/Microsoft/botbuilder-dotnet) are also available. + +To get started building bots using the SDK, see the [Azure Bot Service Documentation](https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0). + +For more information jump to a section below. + +* [Build status](#build-status) +* [Packages](#packages) +* [Getting started](#getting-started) +* [Getting support and providing feedback](#getting-support-and-providing-feedback) +* [Contributing and our code of conduct](contributing-and-our-code-of-conduct) +* [Reporting security issues](#reporting-security-issues) + +## Build Status + +| Branch | Description | Build Status | Coverage Status | Code Style | + |----|---------------|--------------|-----------------|--| +| Main | 4.17.0 Builds | [![Build Status](https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=main)](https://fuselabs.visualstudio.com/SDK_v4/_build/latest?definitionId=771&branchName=main) | [![Coverage Status](https://coveralls.io/repos/github/microsoft/botbuilder-python/badge.svg?branch=HEAD)](https://coveralls.io/github/microsoft/botbuilder-python?branch=HEAD) | [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) | ## Packages -- [![PyPI version](https://badge.fury.io/py/botbuilder-ai.svg)](https://pypi.org/project/botbuilder-ai/) botbuilder-ai -- [![PyPI version](https://badge.fury.io/py/botbuilder-applicationinsights.svg)](https://pypi.org/project/botbuilder-applicationinsights/) botbuilder-applicationinsights -- [![PyPI version](https://badge.fury.io/py/botbuilder-azure.svg)](https://pypi.org/project/botbuilder-azure/) botbuilder-azure -- [![PyPI version](https://badge.fury.io/py/botbuilder-core.svg)](https://pypi.org/project/botbuilder-core/) botbuilder-core -- [![PyPI version](https://badge.fury.io/py/botbuilder-dialogs.svg)](https://pypi.org/project/botbuilder-dialogs/) botbuilder-dialogs -- [![PyPI version](https://badge.fury.io/py/botbuilder-schema.svg)](https://pypi.org/project/botbuilder-schema/) botbuilder-schema -- [![PyPI version](https://badge.fury.io/py/botframework-connector.svg)](https://pypi.org/project/botframework-connector/) botframework-connector - -## Contributing -This project welcomes contributions and suggestions. Most contributions require you to agree to a -Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us -the rights to use your contribution. For details, visit https://cla.microsoft.com. -When you submit a pull request, a CLA-bot will automatically determine whether you need to provide -a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions -provided by the bot. You will only need to do this once across all repos using our CLA. +| Build | Released Package | + |----|---------------| +| botbuilder-ai | [![PyPI version](https://badge.fury.io/py/botbuilder-ai.svg)](https://pypi.org/project/botbuilder-ai/) | +| botbuilder-applicationinsights | [![PyPI version](https://badge.fury.io/py/botbuilder-applicationinsights.svg)](https://pypi.org/project/botbuilder-applicationinsights/) | +| botbuilder-azure | [![PyPI version](https://badge.fury.io/py/botbuilder-azure.svg)](https://pypi.org/project/botbuilder-azure/) | +| botbuilder-core | [![PyPI version](https://badge.fury.io/py/botbuilder-core.svg)](https://pypi.org/project/botbuilder-core/) | +| botbuilder-dialogs | [![PyPI version](https://badge.fury.io/py/botbuilder-dialogs.svg)](https://pypi.org/project/botbuilder-dialogs/) | +| botbuilder-schema | [![PyPI version](https://badge.fury.io/py/botbuilder-schema.svg)](https://pypi.org/project/botbuilder-schema/) | +| botframework-connector | [![PyPI version](https://badge.fury.io/py/botframework-connector.svg)](https://pypi.org/project/botframework-connector/) | +| + +## Getting Started +To get started building bots using the SDK, see the [Azure Bot Service Documentation](https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0). + +The [Bot Framework Samples](https://github.com/microsoft/botbuilder-samples) includes a rich set of samples repository. + +If you want to debug an issue, would like to [contribute](#contributing-code), or understand how the Bot Builder SDK works, instructions for building and testing the SDK are below. + +### Prerequisites +- [Git](https://git-scm.com/downloads) +- [Python 3.8.17 - 3.11.x](https://www.python.org/downloads/) + +Python "Virtual Environments" allow Python packages to be installed in an isolated location for a particular application, rather than being installed globally, as such it is common practice to use them. Click [here](https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments) to learn more about creating _and activating_ Virtual Environments in Python. + +### Clone +Clone a copy of the repo: +```bash +git clone https://github.com/Microsoft/botbuilder-python.git +``` +Change to the SDK's directory: +```bash +cd botbuilder-python +``` + +### Using the SDK locally + +To use a local copy of the SDK you can link to these packages with the pip -e option. + +```bash +pip install -e ./libraries/botbuilder-schema +pip install -e ./libraries/botframework-connector +pip install -e ./libraries/botframework-streaming +pip install -e ./libraries/botbuilder-core +pip install -e ./libraries/botbuilder-ai +pip install -e ./libraries/botbuilder-applicationinsights +pip install -e ./libraries/botbuilder-dialogs +pip install -e ./libraries/botbuilder-azure +pip install -e ./libraries/botbuilder-integration-applicationinsights-aiohttp +pip install -e ./libraries/botbuilder-adapters-slack +pip install -e ./libraries/botbuilder-integration-aiohttp +pip install -e ./libraries/botbuilder-testing +``` + +### Running unit tests +First execute the following command from the root level of the repo: +```bash +pip install -r ./libraries/botframework-connector/tests/requirements.txt +pip install -r ./libraries/botbuilder-core/tests/requirements.txt +pip install -r ./libraries/botbuilder-ai/tests/requirements.txt +``` + +Then enter run pytest by simply typing it into your CLI: + +```bash +pytest +``` + +This is the expected output: +```bash +============================= test session starts ============================= +platform win32 -- Python 3.8.2, pytest-3.4.0, py-1.5.2, pluggy-0.6.0 +rootdir: C:\projects\botbuilder-python, inifile: +plugins: cov-2.5.1 +... +``` + +## Getting support and providing feedback +Below are the various channels that are available to you for obtaining support and providing feedback. Please pay carful attention to which channel should be used for which type of content. e.g. general "how do I..." questions should be asked on Stack Overflow, Twitter or Gitter, with GitHub issues being for feature requests and bug reports. + +### Github issues +[Github issues](https://github.com/Microsoft/botbuilder-python/issues) should be used for bugs and feature requests. + +### Stack overflow +[Stack Overflow](https://stackoverflow.com/questions/tagged/botframework) is a great place for getting high-quality answers. Our support team, as well as many of our community members are already on Stack Overflow providing answers to 'how-to' questions. + +### Azure Support +If you issues relates to [Azure Bot Service](https://azure.microsoft.com/en-gb/services/bot-service/), you can take advantage of the available [Azure support options](https://azure.microsoft.com/en-us/support/options/). + +### Twitter +We use the [@msbotframework](https://twitter.com/msbotframework) account on twitter for announcements and members from the development team watch for tweets for [@msbotframework](https://twitter.com/msbotframework). + +### Gitter Chat Room +The [Gitter Channel](https://gitter.im/Microsoft/BotBuilder) provides a place where the Community can get together and collaborate. + +## Contributing and our code of conduct +We welcome contributions and suggestions. Please see our [contributing guidelines](./Contributing.md) for more information. + This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). -For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or -contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. + +For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact + [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. + +### Contributing Code + +In order to create pull requests, submitted code must pass ```pylint``` and ```black``` checks. Run both tools on every file you've changed. + +For more information and installation instructions, see: + +* [black](https://pypi.org/project/black/) +* [pylint](https://pylint.org/) ## Reporting Security Issues -Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) at [secure@microsoft.com](mailto:secure@microsoft.com). You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the [MSRC PGP](https://technet.microsoft.com/en-us/security/dn606155) key, can be found in the [Security TechCenter](https://technet.microsoft.com/en-us/security/default). +Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) +at [secure@microsoft.com](mailto:secure@microsoft.com). You should receive a response within 24 hours. If for some + reason you do not, please follow up via email to ensure we received your original message. Further information, + including the [MSRC PGP](https://technet.microsoft.com/en-us/security/dn606155) key, can be found in the +[Security TechCenter](https://technet.microsoft.com/en-us/security/default). Copyright (c) Microsoft Corporation. All rights reserved. + +Licensed under the [MIT](./LICENSE) License. + + diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 000000000..869fdfe2b --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,41 @@ + + +## Security + +Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/). + +If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below. + +## Reporting Security Issues + +**Please do not report security vulnerabilities through public GitHub issues.** + +Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report). + +If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey). + +You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc). + +Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: + + * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.) + * Full paths of source file(s) related to the manifestation of the issue + * The location of the affected source code (tag/branch/commit or direct URL) + * Any special configuration required to reproduce the issue + * Step-by-step instructions to reproduce the issue + * Proof-of-concept or exploit code (if possible) + * Impact of the issue, including how an attacker might exploit the issue + +This information will help us triage your report more quickly. + +If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs. + +## Preferred Languages + +We prefer all communications to be in English. + +## Policy + +Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd). + + diff --git a/UsingTestPyPI.md b/UsingTestPyPI.md new file mode 100644 index 000000000..4bbe31a4f --- /dev/null +++ b/UsingTestPyPI.md @@ -0,0 +1,19 @@ +# Using TestPyPI to consume rc builds +The BotBuilder SDK rc build feed is found on [TestPyPI](https://test.pypi.org/). + +The daily builds will be available soon through the mentioned feed as well. + + +# Configure TestPyPI + +You can tell pip to download packages from TestPyPI instead of PyPI by specifying the --index-url flag (in the example below, replace 'botbuilder-core' for the name of the library you want to install) + +```bash +$ pip install --index-url https://test.pypi.org/simple/ botbuilder-core +``` +If you want to allow pip to also pull other packages from PyPI you can specify --extra-index-url to point to PyPI. +This is useful when the package you’re testing has dependencies: + +```bash +pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ botbuilder-core +``` diff --git a/doc/SkillClaimsValidation.md b/doc/SkillClaimsValidation.md new file mode 100644 index 000000000..53d761ddf --- /dev/null +++ b/doc/SkillClaimsValidation.md @@ -0,0 +1,58 @@ +# HowTo: Block all Skill Claims + +Write a class that conforms to the `ValidateClaims` interface and throws an exception if the claims are skill claims: +```python +class AllowedSkillsClaimsValidator: + + config_key = "ALLOWED_CALLERS" + + def __init__(self, config: DefaultConfig): + if not config: + raise TypeError( + "AllowedSkillsClaimsValidator: config object cannot be None." + ) + + # ALLOWED_CALLERS is the setting in config.py file + # that consists of the list of parent bot ids that are allowed to access the skill + # to add a new parent bot simply go to the AllowedCallers and add + # the parent bot's microsoft app id to the list + caller_list = getattr(config, self.config_key) + if caller_list is None: + raise TypeError(f'"{self.config_key}" not found in configuration.') + self._allowed_callers = caller_list + + @property + def claims_validator(self) -> Callable[[List[Dict]], Awaitable]: + async def allow_callers_claims_validator(claims: Dict[str, object]): + if skillValidation.is_skill_claim(claims): + raise PermissionError( + "Invalid call from a skill." + ) + + return + + return allow_callers_claims_validator +``` + +Update `BotFrameworkAdapter` instantiation, to pass the `AuthenticationConfiguration` constructor the function defined above: +```python +AUTH_CONFIG = AuthenticationConfiguration( + claims_validator=AllowedSkillsClaimsValidator(CONFIG).claims_validator +) +SETTINGS = BotFrameworkAdapterSettings( + ..., + auth_configuration=AUTH_CONFIG, +) +ADAPTER = BotFrameworkAdapter( + ..., + SETTINGS, +) +``` + +For SingleTenant type bots, the additional issuers must be added based on the tenant id: +```python +AUTH_CONFIG = AuthenticationConfiguration( + claims_validator=AllowedSkillsClaimsValidator(CONFIG).claims_validator, + tenant_id=the_tenant_id +) +``` diff --git a/generators/LICENSE.md b/generators/LICENSE.md new file mode 100644 index 000000000..506ab97e5 --- /dev/null +++ b/generators/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Microsoft Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/generators/README.md b/generators/README.md new file mode 100644 index 000000000..6d90531c3 --- /dev/null +++ b/generators/README.md @@ -0,0 +1,215 @@ +# python-generator-botbuilder + +Cookiecutter generators for [Bot Framework v4](https://dev.botframework.com). Will let you quickly set up a conversational AI bot +using core AI capabilities. + +## About + +`python-generator-botbuilder` will help you build new conversational AI bots using the [Bot Framework v4](https://dev.botframework.com). + +## Templates + +The generator supports three different template options. The table below can help guide which template is right for you. + +| Template | Description | +| ---------- | --------- | +| Echo Bot | A good template if you want a little more than "Hello World!", but not much more. This template handles the very basics of sending messages to a bot, and having the bot process the messages by repeating them back to the user. This template produces a bot that simply "echoes" back to the user anything the user says to the bot. | +| Core Bot | Our most advanced template, the Core template provides 6 core features every bot is likely to have. This template covers the core features of a Conversational-AI bot using [LUIS](https://www.luis.ai). See the **Core Bot Features** table below for more details. | +| Empty Bot | A good template if you are familiar with Bot Framework v4, and simply want a basic skeleton project. Also a good option if you want to take sample code from the documentation and paste it into a minimal bot in order to learn. | + +### How to Choose a Template + +| Template | When This Template is a Good Choice | +| -------- | -------- | +| Echo Bot | You are new to Bot Framework v4 and want a working bot with minimal features. | +| Core Bot | You understand some of the core concepts of Bot Framework v4 and are beyond the concepts introduced in the Echo Bot template. You're familiar with or are ready to learn concepts such as language understanding using LUIS, managing multi-turn conversations with Dialogs, handling user initiated Dialog interruptions, and using Adaptive Cards to welcome your users. | +| Empty Bot | You are a seasoned Bot Framework v4 developer. You've built bots before, and want the minimum skeleton of a bot. | + +### Template Overview + +#### Echo Bot Template + +The Echo Bot template is slightly more than the a classic "Hello World!" example, but not by much. This template shows the basic structure of a bot, how a bot recieves messages from a user, and how a bot sends messages to a user. The bot will "echo" back to the user, what the user says to the bot. It is a good choice for first time, new to Bot Framework v4 developers. + +#### Core Bot Template + +The Core Bot template consists of set of core features most every bot is likely to have. Building off of the core message processing features found in the Echo Bot template, this template adds a number of more sophisticated features. The table below lists these features and provides links to additional documentation. + +| Core Bot Features | Description | +| ------------------ | ----------- | +| [Send and receive messages](https://docs.microsoft.com/azure/bot-service/bot-builder-howto-send-messages?view=azure-bot-service-4.0) | The primary way your bot will communicate with users, and likewise receive communication, is through message activities. Some messages may simply consist of plain text, while others may contain richer content such as cards or attachments. | +| [Proactive messaging](https://docs.microsoft.com/azure/bot-service/bot-builder-howto-proactive-message?view=azure-bot-service-4.0) using [Adaptive Cards](https://docs.microsoft.com/azure/bot-service/bot-builder-send-welcome-message?view=azure-bot-service-4.0?#using-adaptive-card-greeting) | The primary goal when creating any bot is to engage your user in a meaningful conversation. One of the best ways to achieve this goal is to ensure that from the moment a user first connects to your bot, they understand your bot’s main purpose and capabilities. We refer to this as "welcoming the user." The Core template uses an [Adaptive Card](http://adaptivecards.io) to implement this behavior. | +| [Language understanding using LUIS](https://docs.microsoft.com/azure/bot-service/bot-builder-howto-v4-luis?view=azure-bot-service-4.0) | The ability to understand what your user means conversationally and contextually can be a difficult task, but can provide your bot a more natural conversation feel. Language Understanding, called LUIS, enables you to do just that so that your bot can recognize the intent of user messages, allow for more natural language from your user, and better direct the conversation flow. | +| [Multi-turn conversation support using Dialogs](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0) | The ability to manage conversations is an important part of the bot/user interation. Bot Framework introduces the concept of a Dialog to handle this conversational pattern. Dialog objects process inbound Activities and generate outbound responses. The business logic of the bot runs either directly or indirectly within Dialog classes. | +| [Managing conversation state](https://docs.microsoft.com/azure/bot-service/bot-builder-howto-v4-state?view=azure-bot-service-4.0) | A key to good bot design is to track the context of a conversation, so that your bot remembers things like the answers to previous questions. | +| [How to handle user-initiated interruptions](https://docs.microsoft.com/azure/bot-service/bot-builder-howto-handle-user-interrupt?view=azure-bot-service-4.0) | While you may think that your users will follow your defined conversation flow step by step, chances are good that they will change their minds or ask a question in the middle of the process instead of answering the question. Handling interruptions means making sure your bot is prepared to handle situations like this. | +| [How to unit test a bot](https://aka.ms/cs-unit-test-docs) | Optionally, the Core Bot template can generate corresponding unit tests that shows how to use the testing framework introduced in Bot Framework version 4.5. Selecting this option provides a complete set of units tests for Core Bot. It shows how to write unit tests to test the various features of Core Bot. To add the Core Bot unit tests, run the generator and answer `yes` when prompted. See below for an example of how to do this from the command line. | + +#### Empty Bot Template + +The Empty Bot template is the minimal skeleton code for a bot. It provides a stub `on_turn` handler but does not perform any actions. If you are experienced writing bots with Bot Framework v4 and want the minimum scaffolding, the Empty template is for you. + +## Features by Template + +| Feature | Empty Bot | Echo Bot | Core Bot* | +| --------- | :-----: | :-----: | :-----: | +| Generate code in Python | X | X | X | +| Support local development and testing using the [Bot Framework Emulator v4](https://www.github.com/microsoft/botframework-emulator) | X | X | X | +| Core bot message processing | | X | X | +| Deploy your bot to Microsoft Azure | | Pending | Pending | +| Welcome new users using Adaptive Card technology | | | X | +| Support AI-based greetings using [LUIS](https://www.luis.ai) | | | X | +| Use Dialogs to manage more in-depth conversations | | | X | +| Manage conversation state | | | X | +| Handle user interruptions | | | X | +| Unit test a bot using Bot Framework Testing framework (optional) | | | X | + +*Core Bot template is a work in progress landing soon. +## Installation + +1. Install [cookiecutter](https://github.com/cookiecutter/cookiecutter) using [pip](https://pip.pypa.io/en/stable/) (we assume you have pre-installed [python 3](https://www.python.org/downloads/)). + + ```bash + pip install cookiecutter + ``` + +2. Verify that cookiecutter has been installed correctly by typing the following into your console: + + ```bash + cookiecutter --help + ``` + + +## Usage + +### Creating a New Bot Project + +To create an Echo Bot project: + +```bash +cookiecutter https://github.com/microsoft/botbuilder-python/releases/download/Templates/echo.zip +``` + +To create a Core Bot project: + +```bash +cookiecutter https://github.com/microsoft/botbuilder-python/releases/download/Templates/core.zip +``` + +To create an Empty Bot project: + +```bash +cookiecutter https://github.com/microsoft/botbuilder-python/releases/download/Templates/empty.zip +``` + +When the generator is launched, it will prompt for the information required to create a new bot. + +### Generator Command Line Options and Arguments + +Cookiecutter supports a set of pre-defined command line options, the complete list with descriptions is available [here](https://cookiecutter.readthedocs.io/en/0.9.1/advanced_usage.html#command-line-options). + +Each generator can recieve a series of named arguments to pre-seed the prompt default value. If the `--no-input` option flag is send, these named arguments will be the default values for the template. + +| Named argument | Description | +| ------------------- | ----------- | +| project_name | The name given to the bot project | +| bot_description | A brief bit of text that describes the purpose of the bot | +| add_tests | **PENDING** _A Core Bot Template Only Feature_. The generator will add unit tests to the Core Bot generated bot. This option is not available to other templates at this time. To learn more about the test framework released with Bot Framework v4.5, see [How to unit test bots](https://aka.ms/js-unit-test-docs). This option is intended to enable automated bot generation for testing purposes. | + +#### Example Using Named Arguments + +This example shows how to pass named arguments to the generator, setting the default bot name to test_project. + +```bash +# Run the generator defaulting the bot name to test_project +cookiecutter https://github.com/microsoft/botbuilder-python/releases/download/Templates/echo.zip project_name="test_project" +``` + +### Generating a Bot Using --no-input + +The generator can be run in `--no-input` mode, which can be used for automated bot creation. When run in `--no-input` mode, the generator can be configured using named arguments as documented above. If a named argument is ommitted a reasonable default will be used. + +#### Default Values + +| Named argument | Default Value | +| ------------------- | ----------- | +| bot_name | `my-chat-bot` | +| bot_description | "Demonstrate the core capabilities of the Microsoft Bot Framework" | +| add_tests | `False`| + +#### Examples Using --no-input + +This example shows how to run the generator in --no-input mode, setting all required options on the command line. + +```bash +# Run the generator, setting all command line options +cookiecutter https://github.com/microsoft/botbuilder-python/releases/download/Templates/echo.zip --no-input project_name="test_bot" bot_description="Test description" +``` + +This example shows how to run the generator in --no-input mode, using all the default command line options. The generator will create a bot project using all the default values specified in the **Default Options** table above. + +```bash +# Run the generator using all default options +cookiecutter https://github.com/microsoft/botbuilder-python/releases/download/Templates/echo.zip --no-input +``` + +This example shows how to run the generator in --no-input mode, with unit tests. + +```bash +# PENDING: Run the generator using all default options +``` + +## Running Your Bot + +### Running Your Bot Locally + +To run your bot locally, type the following in your console: + +```bash +# install dependencies +pip install -r requirements.txt +``` + +```bash +# run the bot +python app.py +``` + +Alternatively to the last command, you can set the file in an environment variable with `set FLASK_APP=app.py` in windows (`export FLASK_APP=app.py` in mac/linux) and then run `flask run --host=127.0.0.1 --port=3978` + +### Interacting With Your Bot Using the Emulator + +- Launch Bot Framework Emulator +- File -> Open Bot +- Enter a Bot URL of `http://localhost:3978/api/messages` + +Once the Emulator is connected, you can interact with and receive messages from your bot. + +#### Lint Compliant Code + +The code generated by the botbuilder generator is pylint compliant to our ruleset. To use pylint as your develop your bot: + +```bash +# Assuming you created a project with the bot_name value 'my_chat_bot' +pylint --rcfile=my_chat_bot/.pylintrc my_chat_bot +``` + +#### Testing Core Bots with Tests (Pending) + +Core Bot templates generated with unit tests can be tested using the following: + +```bash +# launch pytest +pytest +``` + +## Deploy Your Bot to Azure + +After creating the bot and testing it locally, you can deploy it to Azure to make it accessible from anywhere. +To learn how, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete set of deployment instructions. + +If you are new to Microsoft Azure, please refer to [Getting started with Azure](https://azure.microsoft.com/get-started/) for guidance on how to get started on Azure. + +## Logging Issues and Providing Feedback + +Issues and feedback about the botbuilder generator can be submitted through the project's [GitHub Issues](https://github.com/Microsoft/botbuilder-python/issues) page. diff --git a/generators/app/templates/core/cookiecutter.json b/generators/app/templates/core/cookiecutter.json new file mode 100644 index 000000000..4a14b6ade --- /dev/null +++ b/generators/app/templates/core/cookiecutter.json @@ -0,0 +1,4 @@ +{ + "bot_name": "my_chat_bot", + "bot_description": "Demonstrate the core capabilities of the Microsoft Bot Framework" +} \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/.pylintrc b/generators/app/templates/core/{{cookiecutter.bot_name}}/.pylintrc new file mode 100644 index 000000000..9c1c70f04 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/.pylintrc @@ -0,0 +1,498 @@ +[MASTER] + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-whitelist= + +# Add files or directories to the blacklist. They should be base names, not +# paths. +ignore= + +# Add files or directories matching the regex patterns to the blacklist. The +# regex matches against base names, not paths. +ignore-patterns= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use. +jobs=1 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=100 + +# List of plugins (as comma separated values of python modules names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Specify a configuration file. +#rcfile= + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. +confidence= + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=missing-docstring, + too-few-public-methods, + bad-continuation, + no-self-use, + duplicate-code, + broad-except, + no-name-in-module + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[REPORTS] + +# Python expression which should return a note less than 10 (10 is the highest +# note). You have access to the variables errors warning, statement which +# respectively contain the number of errors / warnings messages and the total +# number of statements analyzed. This is used by the global evaluation report +# (RP0004). +evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +#msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +output-format=text + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit + + +[LOGGING] + +# Format style used to check logging format string. `old` means using % +# formatting, while `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. Available dictionaries: none. To make it working +# install python-enchant package.. +spelling-dict= + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to indicated private dictionary in +# --spelling-private-dict-file option instead of raising a message. +spelling-store-unknown-words=no + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether missing members accessed in mixin class should be ignored. A +# mixin class is detected if its name ends with "mixin" (case insensitive). +ignore-mixin-members=yes + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis. It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=120 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# List of optional constructs for which whitespace checking is disabled. `dict- +# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. +# `trailing-comma` allows a space between comma and closing bracket: (a, ). +# `empty-line` allows space-only lines. +no-space-check=trailing-comma, + dict-separator + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[SIMILARITIES] + +# Ignore comments when computing similarities. +ignore-comments=yes + +# Ignore docstrings when computing similarities. +ignore-docstrings=yes + +# Ignore imports when computing similarities. +ignore-imports=no + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. +#class-attribute-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _ + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. +#variable-rgx= + + +[STRING] + +# This flag controls whether the implicit-str-concat-in-sequence should +# generate a warning on implicit string concatenation in sequences defined over +# several lines. +check-str-concat-over-line-jumps=no + + +[IMPORTS] + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules=optparse,tkinter.tix + +# Create a graph of external dependencies in the given file (report RP0402 must +# not be disabled). +ext-import-graph= + +# Create a graph of every (i.e. internal and external) dependencies in the +# given file (report RP0402 must not be disabled). +import-graph= + +# Create a graph of internal dependencies in the given file (report RP0402 must +# not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + + +[CLASSES] + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict, + _fields, + _replace, + _source, + _make + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=cls + + +[DESIGN] + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement. +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=15 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. Defaults to +# "BaseException, Exception". +overgeneral-exceptions=BaseException, + Exception diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/README-LUIS.md b/generators/app/templates/core/{{cookiecutter.bot_name}}/README-LUIS.md new file mode 100644 index 000000000..b6b9b925f --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/README-LUIS.md @@ -0,0 +1,216 @@ +# Setting up LUIS via CLI: + +This README contains information on how to create and deploy a LUIS application. When the bot is ready to be deployed to production, we recommend creating a LUIS Endpoint Resource for usage with your LUIS App. + +> _For instructions on how to create a LUIS Application via the LUIS portal, see these Quickstart steps:_ +> 1. _[Quickstart: Create a new app in the LUIS portal][Quickstart-create]_ +> 2. _[Quickstart: Deploy an app in the LUIS portal][Quickstart-deploy]_ + + [Quickstart-create]: https://docs.microsoft.com/azure/cognitive-services/luis/get-started-portal-build-app + [Quickstart-deploy]:https://docs.microsoft.com/azure/cognitive-services/luis/get-started-portal-deploy-app + +## Table of Contents: + +- [Prerequisites](#Prerequisites) +- [Import a new LUIS Application using a local LUIS application](#Import-a-new-LUIS-Application-using-a-local-LUIS-application) +- [How to create a LUIS Endpoint resource in Azure and pair it with a LUIS Application](#How-to-create-a-LUIS-Endpoint-resource-in-Azure-and-pair-it-with-a-LUIS-Application) + +___ + +## [Prerequisites](#Table-of-Contents): + +#### Install Azure CLI >=2.0.61: + +Visit the following page to find the correct installer for your OS: +- https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest + +#### Install LUIS CLI >=2.4.0: + +Open a CLI of your choice and type the following: + +```bash +npm i -g luis-apis@^2.4.0 +``` + +#### LUIS portal account: + +You should already have a LUIS account with either https://luis.ai, https://eu.luis.ai, or https://au.luis.ai. To determine where to create a LUIS account, consider where you will deploy your LUIS applications, and then place them in [the corresponding region][LUIS-Authoring-Regions]. + +After you've created your account, you need your [Authoring Key][LUIS-AKey] and a LUIS application ID. + + [LUIS-Authoring-Regions]: https://docs.microsoft.com/azure/cognitive-services/luis/luis-reference-regions#luis-authoring-regions] + [LUIS-AKey]: https://docs.microsoft.com/azure/cognitive-services/luis/luis-concept-keys#authoring-key + +___ + +## [Import a new LUIS Application using a local LUIS application](#Table-of-Contents) + +### 1. Import the local LUIS application to luis.ai + +```bash +luis import application --region "LuisAppAuthoringRegion" --authoringKey "LuisAuthoringKey" --appName "FlightBooking" --in "./cognitiveModels/FlightBooking.json" +``` + +Outputs the following JSON: + +```json +{ + "id": "########-####-####-####-############", + "name": "FlightBooking", + "description": "A LUIS model that uses intent and entities.", + "culture": "en-us", + "usageScenario": "", + "domain": "", + "versionsCount": 1, + "createdDateTime": "2019-03-29T18:32:02Z", + "endpoints": {}, + "endpointHitsCount": 0, + "activeVersion": "0.1", + "ownerEmail": "bot@contoso.com", + "tokenizerVersion": "1.0.0" +} +``` + +For the next step, you'll need the `"id"` value for `--appId` and the `"activeVersion"` value for `--versionId`. + +### 2. Train the LUIS Application + +```bash +luis train version --region "LuisAppAuthoringRegion" --authoringKey "LuisAuthoringKey" --appId "LuisAppId" --versionId "LuisAppversion" --wait +``` + +### 3. Publish the LUIS Application + +```bash +luis publish version --region "LuisAppAuthoringRegion" --authoringKey "LuisAuthoringKey" --appId "LuisAppId" --versionId "LuisAppversion" --publishRegion "LuisAppPublishRegion" +``` + +> `--region` corresponds to the region you _author_ your application in. The regions available for this are "westus", "westeurope" and "australiaeast".
+> These regions correspond to the three available portals, https://luis.ai, https://eu.luis.ai, or https://au.luis.ai.
+> `--publishRegion` corresponds to the region of the endpoint you're publishing to, (e.g. "westus", "southeastasia", "westeurope", "brazilsouth").
+> See the [reference docs][Endpoint-API] for a list of available publish/endpoint regions. + + [Endpoint-API]: https://westus.dev.cognitive.microsoft.com/docs/services/5819c76f40a6350ce09de1ac/operations/5819c77140a63516d81aee78 + +Outputs the following: + +```json + { + "versionId": "0.1", + "isStaging": false, + "endpointUrl": "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/########-####-####-####-############", + "region": "westus", + "assignedEndpointKey": null, + "endpointRegion": "westus", + "failedRegions": "", + "publishedDateTime": "2019-03-29T18:40:32Z", + "directVersionPublish": false +} +``` + +To see how to create an LUIS Cognitive Service Resource in Azure, please see [the next README][README-LUIS]. This Resource should be used when you want to move your bot to production. The instructions will show you how to create and pair the resource with a LUIS Application. + + [README-LUIS]: ./README-LUIS.md + +___ + +## [How to create a LUIS Endpoint resource in Azure and pair it with a LUIS Application](#Table-of-Contents) + +### 1. Create a new LUIS Cognitive Services resource on Azure via Azure CLI + +> _Note:_
+> _If you don't have a Resource Group in your Azure subscription, you can create one through the Azure portal or through using:_ +> ```bash +> az group create --subscription "AzureSubscriptionGuid" --location "westus" --name "ResourceGroupName" +> ``` +> _To see a list of valid locations, use `az account list-locations`_ + + +```bash +# Use Azure CLI to create the LUIS Key resource on Azure +az cognitiveservices account create --kind "luis" --name "NewLuisResourceName" --sku "S0" --location "westus" --subscription "AzureSubscriptionGuid" -g "ResourceGroupName" +``` + +The command will output a response similar to the JSON below: + +```json +{ + "endpoint": "https://westus.api.cognitive.microsoft.com/luis/v2.0", + "etag": "\"########-####-####-####-############\"", + "id": "/subscriptions/########-####-####-####-############/resourceGroups/ResourceGroupName/providers/Microsoft.CognitiveServices/accounts/NewLuisResourceName", + "internalId": "################################", + "kind": "luis", + "location": "westus", + "name": "NewLuisResourceName", + "provisioningState": "Succeeded", + "resourceGroup": "ResourceGroupName", + "sku": { + "name": "S0", + "tier": null + }, + "tags": null, + "type": "Microsoft.CognitiveServices/accounts" +} +``` + + + +Take the output from the previous command and create a JSON file in the following format: + +```json +{ + "azureSubscriptionId": "00000000-0000-0000-0000-000000000000", + "resourceGroup": "ResourceGroupName", + "accountName": "NewLuisResourceName" +} +``` + +### 2. Retrieve ARM access token via Azure CLI + +```bash +az account get-access-token --subscription "AzureSubscriptionGuid" +``` + +This will return an object that looks like this: + +```json +{ + "accessToken": "eyJ0eXAiOiJKVtokentokentokentokentokeng1dCI6Ik4tbEMwbi05REFMcXdodUhZbkhRNjNHZUNYYyIsItokenI6Ik4tbEMwbi05REFMcXdodUhZbkhRNjNHZUNYYyJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuY29yZS53aW5kb3dzLm5ldC8iLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC83MmY5ODhiZi04NmYxLTQxYWYtOTFhYi0yZDdjZDAxMWRiNDcvIiwiaWF0IjoxNTUzODc3MTUwLCJuYmYiOjE1NTM4NzcxNTAsImV4cCI6MTU1Mzg4MTA1MCwiX2NsYWltX25hbWVzIjp7Imdyb3VwcyI6InNyYzEifSwiX2NsYWltX3NvdXJjZXMiOnsic3JjMSI6eyJlbmRwb2ludCI6Imh0dHBzOi8vZ3JhcGgud2luZG93cy5uZXQvNzJmOTg4YmYtODZmMS00MWFmLTkxYWItMmQ3Y2QwMTFkYjQ3L3VzZXJzL2ZmZTQyM2RkLWJhM2YtNDg0Ny04NjgyLWExNTI5MDA4MjM4Ny9nZXRNZW1iZXJPYmplY3RzIn19LCJhY3IiOiIxIiwiYWlvIjoiQVZRQXEvOEtBQUFBeGVUc201NDlhVHg4RE1mMFlRVnhGZmxxOE9RSC9PODR3QktuSmRqV1FqTkkwbmxLYzB0bHJEZzMyMFZ5bWZGaVVBSFBvNUFFUTNHL0FZNDRjdk01T3M0SEt0OVJkcE5JZW9WU0dzd0kvSkk9IiwiYW1yIjpbIndpYSIsIm1mYSJdLCJhcHBpZCI6IjA0YjA3Nzk1LThkZGItNDYxYS1iYmVlLTAyZjllMWJmN2I0NiIsImFwcGlkYWNyIjoiMCIsImRldmljZWlkIjoiNDhmNDVjNjEtMTg3Zi00MjUxLTlmZWItMTllZGFkZmMwMmE3IiwiZmFtaWx5X25hbWUiOiJHdW0iLCJnaXZlbl9uYW1lIjoiU3RldmVuIiwiaXBhZGRyIjoiMTY3LjIyMC4yLjU1IiwibmFtZSI6IlN0ZXZlbiBHdW0iLCJvaWQiOiJmZmU0MjNkZC1iYTNmLTQ4NDctODY4Mi1hMTUyOTAwODIzODciLCJvbnByZW1fc2lkIjoiUy0xLTUtMjEtMjEyNzUyMTE4NC0xNjA0MDEyOTIwLTE4ODc5Mjc1MjctMjYwOTgyODUiLCJwdWlkIjoiMTAwMzdGRkVBMDQ4NjlBNyIsInJoIjoiSSIsInNjcCI6InVzZXJfaW1wZXJzb25hdGlvbiIsInN1YiI6Ik1rMGRNMWszN0U5ckJyMjhieUhZYjZLSU85LXVFQVVkZFVhNWpkSUd1Nk0iLCJ0aWQiOiI3MmY5ODhiZi04NmYxLTQxYWYtOTFhYi0yZDdjZDAxMWRiNDciLCJ1bmlxdWVfbmFtZSI6InN0Z3VtQG1pY3Jvc29mdC5jb20iLCJ1cG4iOiJzdGd1bUBtaWNyb3NvZnQuY29tIiwidXRpIjoiT2w2NGN0TXY4RVNEQzZZQWRqRUFtokenInZlciI6IjEuMCJ9.kFAsEilE0mlS1pcpqxf4rEnRKeYsehyk-gz-zJHUrE__oad3QjgDSBDPrR_ikLdweynxbj86pgG4QFaHURNCeE6SzrbaIrNKw-n9jrEtokenlosOxg_0l2g1LeEUOi5Q4gQREAU_zvSbl-RY6sAadpOgNHtGvz3Rc6FZRITfkckSLmsKAOFoh-aWC6tFKG8P52rtB0qVVRz9tovBeNqkMYL49s9ypduygbXNVwSQhm5JszeWDgrFuVFHBUP_iENCQYGQpEZf_KvjmX1Ur1F9Eh9nb4yI2gFlKncKNsQl-tokenK7-tokentokentokentokentokentokenatoken", + "expiresOn": "2200-12-31 23:59:59.999999", + "subscription": "AzureSubscriptionGuid", + "tenant": "tenant-guid", + "tokenType": "Bearer" +} +``` + +The value needed for the next step is the `"accessToken"`. + +### 3. Use `luis add appazureaccount` to pair your LUIS resource with a LUIS Application + +```bash +luis add appazureaccount --in "path/to/created/requestBody.json" --appId "LuisAppId" --authoringKey "LuisAuthoringKey" --armToken "accessToken" +``` + +If successful, it should yield a response like this: + +```json +{ + "code": "Success", + "message": "Operation Successful" +} +``` + +### 4. See the LUIS Cognitive Services' keys + +```bash +az cognitiveservices account keys list --name "NewLuisResourceName" --subscription "AzureSubscriptionGuid" -g "ResourceGroupName" +``` + +This will return an object that looks like this: + +```json +{ + "key1": "9a69####dc8f####8eb4####399f####", + "key2": "####f99e####4b1a####fb3b####6b9f" +} +``` \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/README.md b/generators/app/templates/core/{{cookiecutter.bot_name}}/README.md new file mode 100644 index 000000000..4581ecc5b --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/README.md @@ -0,0 +1,60 @@ +# CoreBot + +Bot Framework v4 core bot sample. + +This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to: + +- Use [LUIS](https://www.luis.ai) to implement core AI capabilities +- Implement a multi-turn conversation using Dialogs +- Handle user interruptions for such things as `Help` or `Cancel` +- Prompt for and validate requests for information from the user + +## Prerequisites + +This sample **requires** prerequisites in order to run. + +### Overview + +This bot uses [LUIS](https://www.luis.ai), an AI based cognitive service, to implement language understanding. + +### Install Python 3.6 + + +### Create a LUIS Application to enable language understanding + +LUIS language model setup, training, and application configuration steps can be found [here](https://docs.microsoft.com/azure/bot-service/bot-builder-howto-v4-luis?view=azure-bot-service-4.0&tabs=cs). + +If you wish to create a LUIS application via the CLI, these steps can be found in the [README-LUIS.md](README-LUIS.md). + +## Running the sample +- Run `pip install -r requirements.txt` to install all dependencies +- Update LuisAppId, LuisAPIKey and LuisAPIHostName in `config.py` with the information retrieved from the [LUIS portal](https://www.luis.ai) +- Run `python app.py` + + +## Testing the bot using Bot Framework Emulator + +[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. + +- Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) + +### Connect to the bot using Bot Framework Emulator + +- Launch Bot Framework Emulator +- Enter a Bot URL of `http://localhost:3978/api/messages` + + +## Further reading + +- [Bot Framework Documentation](https://docs.botframework.com) +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) +- [Dialogs](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0) +- [Gathering Input Using Prompts](https://docs.microsoft.com/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=csharp) +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) +- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) +- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) +- [.NET Core CLI tools](https://docs.microsoft.com/dotnet/core/tools/?tabs=netcore2x) +- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) +- [Azure Portal](https://portal.azure.com) +- [Language Understanding using LUIS](https://docs.microsoft.com/azure/cognitive-services/luis/) +- [Channels and Bot Connector Service](https://docs.microsoft.com/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) \ No newline at end of file diff --git a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/handlers/__init__.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/__init__.py similarity index 97% rename from samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/handlers/__init__.py rename to generators/app/templates/core/{{cookiecutter.bot_name}}/__init__.py index 5b7f7a925..d7fe4b228 100644 --- a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/handlers/__init__.py +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/__init__.py @@ -1,2 +1,2 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/adapter_with_error_handler.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/adapter_with_error_handler.py new file mode 100644 index 000000000..7959968ce --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/adapter_with_error_handler.py @@ -0,0 +1,55 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +import sys +import traceback +from datetime import datetime + +from botbuilder.core import ( + ConversationState, + TurnContext, +) +from botbuilder.integration.aiohttp import CloudAdapter, ConfigurationBotFrameworkAuthentication +from botbuilder.schema import ActivityTypes, Activity + + +class AdapterWithErrorHandler(CloudAdapter): + def __init__( + self, + settings: ConfigurationBotFrameworkAuthentication, + conversation_state: ConversationState, + ): + super().__init__(settings) + self._conversation_state = conversation_state + + # Catch-all for errors. + async def on_error(context: TurnContext, error: Exception): + # This check writes out errors to console log + # NOTE: In production environment, you should consider logging this to Azure + # application insights. + print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) + traceback.print_exc() + + # Send a message to the user + await context.send_activity("The bot encountered an error or bug.") + await context.send_activity( + "To continue to run this bot, please fix the bot source code." + ) + # Send a trace activity if we're talking to the Bot Framework Emulator + if context.activity.channel_id == "emulator": + # Create a trace activity that contains the error object + trace_activity = Activity( + label="TurnError", + name="on_turn_error Trace", + timestamp=datetime.utcnow(), + type=ActivityTypes.trace, + value=f"{error}", + value_type="https://www.botframework.com/schemas/error", + ) + # Send a trace activity, which will be displayed in Bot Framework Emulator + await context.send_activity(trace_activity) + + # Clear out state + nonlocal self + await self._conversation_state.delete(context) + + self.on_turn_error = on_error diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/app.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/app.py new file mode 100644 index 000000000..c19ea27e2 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/app.py @@ -0,0 +1,76 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +""" +This sample shows how to create a bot that demonstrates the following: +- Use [LUIS](https://www.luis.ai) to implement core AI capabilities. +- Implement a multi-turn conversation using Dialogs. +- Handle user interruptions for such things as `Help` or `Cancel`. +- Prompt for and validate requests for information from the user. +""" +from http import HTTPStatus +from aiohttp import web +from aiohttp.web import Request, Response, json_response +from botbuilder.core import ( + ConversationState, + MemoryStorage, + UserState, +) +from botbuilder.core.integration import aiohttp_error_middleware +from botbuilder.integration.aiohttp import ConfigurationBotFrameworkAuthentication +from botbuilder.schema import Activity + +from config import DefaultConfig +from dialogs import MainDialog, BookingDialog +from bots import DialogAndWelcomeBot + +from adapter_with_error_handler import AdapterWithErrorHandler +from flight_booking_recognizer import FlightBookingRecognizer + +CONFIG = DefaultConfig() + +# Create adapter. +# See https://aka.ms/about-bot-adapter to learn more about how bots work. +SETTINGS = ConfigurationBotFrameworkAuthentication(CONFIG) + +# Create MemoryStorage, UserState and ConversationState +MEMORY = MemoryStorage() +USER_STATE = UserState(MEMORY) +CONVERSATION_STATE = ConversationState(MEMORY) + +# Create adapter. +# See https://aka.ms/about-bot-adapter to learn more about how bots work. +ADAPTER = AdapterWithErrorHandler(SETTINGS, CONVERSATION_STATE) + +# Create dialogs and Bot +RECOGNIZER = FlightBookingRecognizer(CONFIG) +BOOKING_DIALOG = BookingDialog() +DIALOG = MainDialog(RECOGNIZER, BOOKING_DIALOG) +BOT = DialogAndWelcomeBot(CONVERSATION_STATE, USER_STATE, DIALOG) + + +# Listen for incoming requests on /api/messages +async def messages(req: Request) -> Response: + # Main bot message handler. + if "application/json" in req.headers["Content-Type"]: + body = await req.json() + else: + return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE) + + activity = Activity().deserialize(body) + auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" + + response = await ADAPTER.process_activity(auth_header, activity, BOT.on_turn) + if response: + return json_response(data=response.body, status=response.status) + return Response(status=HTTPStatus.OK) + + +APP = web.Application(middlewares=[aiohttp_error_middleware]) +APP.router.add_post("/api/messages", messages) + +if __name__ == "__main__": + try: + web.run_app(APP, host="localhost", port=CONFIG.PORT) + except Exception as error: + raise error diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/booking_details.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/booking_details.py new file mode 100644 index 000000000..53364aabc --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/booking_details.py @@ -0,0 +1,18 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List + + +class BookingDetails: + def __init__( + self, + destination: str = None, + origin: str = None, + travel_date: str = None, + unsupported_airports: List[str] = None, + ): + self.destination = destination + self.origin = origin + self.travel_date = travel_date + self.unsupported_airports = unsupported_airports or [] diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/bots/__init__.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/bots/__init__.py similarity index 92% rename from samples/experimental/101.corebot-bert-bidaf/bot/bots/__init__.py rename to generators/app/templates/core/{{cookiecutter.bot_name}}/bots/__init__.py index ee478912d..ee51d6cec 100644 --- a/samples/experimental/101.corebot-bert-bidaf/bot/bots/__init__.py +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/bots/__init__.py @@ -1,8 +1,7 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""bots module.""" - -from .dialog_bot import DialogBot -from .dialog_and_welcome_bot import DialogAndWelcomeBot - -__all__ = ["DialogBot", "DialogAndWelcomeBot"] +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .dialog_bot import DialogBot +from .dialog_and_welcome_bot import DialogAndWelcomeBot + +__all__ = ["DialogBot", "DialogAndWelcomeBot"] diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/bots/dialog_and_welcome_bot.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/bots/dialog_and_welcome_bot.py similarity index 57% rename from samples/experimental/101.corebot-bert-bidaf/bot/bots/dialog_and_welcome_bot.py rename to generators/app/templates/core/{{cookiecutter.bot_name}}/bots/dialog_and_welcome_bot.py index c6e63079c..6e1ef90d0 100644 --- a/samples/experimental/101.corebot-bert-bidaf/bot/bots/dialog_and_welcome_bot.py +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/bots/dialog_and_welcome_bot.py @@ -1,41 +1,39 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -"""Main dialog to welcome users.""" + import json import os.path + from typing import List -from botbuilder.core import TurnContext -from botbuilder.schema import Activity, Attachment, ChannelAccount -from helpers.activity_helper import create_activity_reply +from botbuilder.core import MessageFactory, TurnContext +from botbuilder.schema import Attachment, ChannelAccount + +from helpers import DialogHelper from .dialog_bot import DialogBot class DialogAndWelcomeBot(DialogBot): - """Main dialog to welcome users implementation.""" async def on_members_added_activity( self, members_added: List[ChannelAccount], turn_context: TurnContext ): for member in members_added: # Greet anyone that was not the target (recipient) of this message. - # To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards - # for more details. + # To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details. if member.id != turn_context.activity.recipient.id: welcome_card = self.create_adaptive_card_attachment() - response = self.create_response(turn_context.activity, welcome_card) + response = MessageFactory.attachment(welcome_card) await turn_context.send_activity(response) - - def create_response(self, activity: Activity, attachment: Attachment): - """Create an attachment message response.""" - response = create_activity_reply(activity) - response.attachments = [attachment] - return response + await DialogHelper.run_dialog( + self.dialog, + turn_context, + self.conversation_state.create_property("DialogState"), + ) # Load attachment from file. def create_adaptive_card_attachment(self): - """Create an adaptive card.""" relative_path = os.path.abspath(os.path.dirname(__file__)) - path = os.path.join(relative_path, "resources/welcomeCard.json") + path = os.path.join(relative_path, "../cards/welcomeCard.json") with open(path) as card_file: card = json.load(card_file) diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/bots/dialog_bot.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/bots/dialog_bot.py similarity index 76% rename from samples/experimental/101.corebot-bert-bidaf/bot/bots/dialog_bot.py rename to generators/app/templates/core/{{cookiecutter.bot_name}}/bots/dialog_bot.py index 2f161c4c7..caf39b3d9 100644 --- a/samples/experimental/101.corebot-bert-bidaf/bot/bots/dialog_bot.py +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/bots/dialog_bot.py @@ -1,15 +1,13 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -"""Implements bot Activity handler.""" from botbuilder.core import ActivityHandler, ConversationState, UserState, TurnContext from botbuilder.dialogs import Dialog -from helpers.dialog_helper import DialogHelper +from helpers import DialogHelper -class DialogBot(ActivityHandler): - """Main activity handler for the bot.""" +class DialogBot(ActivityHandler): def __init__( self, conversation_state: ConversationState, @@ -28,14 +26,11 @@ def __init__( self.conversation_state = conversation_state self.user_state = user_state self.dialog = dialog - self.dialogState = self.conversation_state.create_property( - "DialogState" - ) # pylint: disable=C0103 async def on_turn(self, turn_context: TurnContext): await super().on_turn(turn_context) - # Save any state changes that might have occured during the turn. + # Save any state changes that might have occurred during the turn. await self.conversation_state.save_changes(turn_context, False) await self.user_state.save_changes(turn_context, False) @@ -44,4 +39,4 @@ async def on_message_activity(self, turn_context: TurnContext): self.dialog, turn_context, self.conversation_state.create_property("DialogState"), - ) # pylint: disable=C0103 + ) diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/bots/resources/welcomeCard.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/cards/welcomeCard.json similarity index 80% rename from samples/experimental/101.corebot-bert-bidaf/bot/bots/resources/welcomeCard.json rename to generators/app/templates/core/{{cookiecutter.bot_name}}/cards/welcomeCard.json index d9a35548c..cc10cda9f 100644 --- a/samples/experimental/101.corebot-bert-bidaf/bot/bots/resources/welcomeCard.json +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/cards/welcomeCard.json @@ -1,46 +1,46 @@ -{ - "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", - "type": "AdaptiveCard", - "version": "1.0", - "body": [ - { - "type": "Image", - "url": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU", - "size": "stretch" - }, - { - "type": "TextBlock", - "spacing": "medium", - "size": "default", - "weight": "bolder", - "text": "Welcome to Bot Framework!", - "wrap": true, - "maxLines": 0 - }, - { - "type": "TextBlock", - "size": "default", - "isSubtle": "true", - "text": "Now that you have successfully run your bot, follow the links in this Adaptive Card to expand your knowledge of Bot Framework.", - "wrap": true, - "maxLines": 0 - } - ], - "actions": [ - { - "type": "Action.OpenUrl", - "title": "Get an overview", - "url": "https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0" - }, - { - "type": "Action.OpenUrl", - "title": "Ask a question", - "url": "https://stackoverflow.com/questions/tagged/botframework" - }, - { - "type": "Action.OpenUrl", - "title": "Learn how to deploy", - "url": "https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-deploy-azure?view=azure-bot-service-4.0" - } - ] +{ + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.0", + "body": [ + { + "type": "Image", + "url": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU", + "size": "stretch" + }, + { + "type": "TextBlock", + "spacing": "medium", + "size": "default", + "weight": "bolder", + "text": "Welcome to Bot Framework!", + "wrap": true, + "maxLines": 0 + }, + { + "type": "TextBlock", + "size": "default", + "isSubtle": "true", + "text": "Now that you have successfully run your bot, follow the links in this Adaptive Card to expand your knowledge of Bot Framework.", + "wrap": true, + "maxLines": 0 + } + ], + "actions": [ + { + "type": "Action.OpenUrl", + "title": "Get an overview", + "url": "https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0" + }, + { + "type": "Action.OpenUrl", + "title": "Ask a question", + "url": "https://stackoverflow.com/questions/tagged/botframework" + }, + { + "type": "Action.OpenUrl", + "title": "Learn how to deploy", + "url": "https://docs.microsoft.com/azure/bot-service/bot-builder-howto-deploy-azure?view=azure-bot-service-4.0" + } + ] } \ No newline at end of file diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/training_data/FlightBooking.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/cognitiveModels/FlightBooking.json similarity index 59% rename from samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/training_data/FlightBooking.json rename to generators/app/templates/core/{{cookiecutter.bot_name}}/cognitiveModels/FlightBooking.json index 43781ee85..f0e4b9770 100644 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/training_data/FlightBooking.json +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/cognitiveModels/FlightBooking.json @@ -1,17 +1,20 @@ { "luis_schema_version": "3.2.0", "versionId": "0.1", - "name": "Airline Reservation", - "desc": "A LUIS model that uses intent and entities.", + "name": "FlightBooking", + "desc": "Luis Model for CoreBot", "culture": "en-us", "tokenizerVersion": "1.0.0", "intents": [ { - "name": "Book flight" + "name": "BookFlight" }, { "name": "Cancel" }, + { + "name": "GetWeather" + }, { "name": "None" } @@ -40,25 +43,36 @@ { "canonicalForm": "Paris", "list": [ - "paris" + "paris", + "cdg" ] }, { "canonicalForm": "London", "list": [ - "london" + "london", + "lhr" ] }, { "canonicalForm": "Berlin", "list": [ - "berlin" + "berlin", + "txl" ] }, { "canonicalForm": "New York", "list": [ - "new york" + "new york", + "jfk" + ] + }, + { + "canonicalForm": "Seattle", + "list": [ + "seattle", + "sea" ] } ], @@ -78,24 +92,61 @@ "patterns": [], "utterances": [ { - "text": "book flight from london to paris on feb 14th", - "intent": "Book flight", + "text": "book a flight", + "intent": "BookFlight", + "entities": [] + }, + { + "text": "book a flight from new york", + "intent": "BookFlight", "entities": [ { - "entity": "To", - "startPos": 27, - "endPos": 31 - }, + "entity": "From", + "startPos": 19, + "endPos": 26 + } + ] + }, + { + "text": "book a flight from seattle", + "intent": "BookFlight", + "entities": [ + { + "entity": "From", + "startPos": 19, + "endPos": 25 + } + ] + }, + { + "text": "book a hotel in new york", + "intent": "None", + "entities": [] + }, + { + "text": "book a restaurant", + "intent": "None", + "entities": [] + }, + { + "text": "book flight from london to paris on feb 14th", + "intent": "BookFlight", + "entities": [ { "entity": "From", "startPos": 17, "endPos": 22 + }, + { + "entity": "To", + "startPos": 27, + "endPos": 31 } ] }, { "text": "book flight to berlin on feb 14th", - "intent": "Book flight", + "intent": "BookFlight", "entities": [ { "entity": "To", @@ -106,7 +157,7 @@ }, { "text": "book me a flight from london to paris", - "intent": "Book flight", + "intent": "BookFlight", "entities": [ { "entity": "From", @@ -136,23 +187,13 @@ "entities": [] }, { - "text": "I don't want that", - "intent": "Cancel", - "entities": [] - }, - { - "text": "not this one", - "intent": "Cancel", - "entities": [] - }, - { - "text": "don't want that", - "intent": "Cancel", + "text": "find an airport near me", + "intent": "None", "entities": [] }, { "text": "flight to paris", - "intent": "Book flight", + "intent": "BookFlight", "entities": [ { "entity": "To", @@ -163,7 +204,7 @@ }, { "text": "flight to paris from london on feb 14th", - "intent": "Book flight", + "intent": "BookFlight", "entities": [ { "entity": "To", @@ -179,23 +220,23 @@ }, { "text": "fly from berlin to paris on may 5th", - "intent": "Book flight", + "intent": "BookFlight", "entities": [ - { - "entity": "To", - "startPos": 19, - "endPos": 23 - }, { "entity": "From", "startPos": 9, "endPos": 14 + }, + { + "entity": "To", + "startPos": 19, + "endPos": 23 } ] }, { "text": "go to paris", - "intent": "Book flight", + "intent": "BookFlight", "entities": [ { "entity": "To", @@ -206,28 +247,60 @@ }, { "text": "going from paris to berlin", - "intent": "Book flight", + "intent": "BookFlight", "entities": [ - { - "entity": "To", - "startPos": 20, - "endPos": 25 - }, { "entity": "From", "startPos": 11, "endPos": 15 + }, + { + "entity": "To", + "startPos": 20, + "endPos": 25 } ] }, + { + "text": "i'd like to rent a car", + "intent": "None", + "entities": [] + }, { "text": "ignore", "intent": "Cancel", "entities": [] }, + { + "text": "travel from new york to paris", + "intent": "BookFlight", + "entities": [ + { + "entity": "From", + "startPos": 12, + "endPos": 19 + }, + { + "entity": "To", + "startPos": 24, + "endPos": 28 + } + ] + }, + { + "text": "travel to new york", + "intent": "BookFlight", + "entities": [ + { + "entity": "To", + "startPos": 10, + "endPos": 17 + } + ] + }, { "text": "travel to paris", - "intent": "Book flight", + "intent": "BookFlight", "entities": [ { "entity": "To", @@ -235,6 +308,31 @@ "endPos": 14 } ] + }, + { + "text": "what's the forecast for this friday?", + "intent": "GetWeather", + "entities": [] + }, + { + "text": "what's the weather like for tomorrow", + "intent": "GetWeather", + "entities": [] + }, + { + "text": "what's the weather like in new york", + "intent": "GetWeather", + "entities": [] + }, + { + "text": "what's the weather like?", + "intent": "GetWeather", + "entities": [] + }, + { + "text": "winter is coming", + "intent": "None", + "entities": [] } ], "settings": [] diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/config.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/config.py new file mode 100644 index 000000000..f2d31d7ad --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/config.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os + +class DefaultConfig: + """ Bot Configuration """ + + PORT = 3978 + APP_ID = os.environ.get("MicrosoftAppId", "") + APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") + APP_TYPE = os.environ.get("MicrosoftAppType", "MultiTenant") + APP_TENANTID = os.environ.get("MicrosoftAppTenantId", "") + LUIS_APP_ID = os.environ.get("LuisAppId", "") + LUIS_API_KEY = os.environ.get("LuisAPIKey", "") + # LUIS endpoint host name, ie "westus.api.cognitive.microsoft.com" + LUIS_API_HOST_NAME = os.environ.get("LuisAPIHostName", "") diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-AzureBot-with-rg.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-AzureBot-with-rg.json new file mode 100644 index 000000000..92b2e84c9 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-AzureBot-with-rg.json @@ -0,0 +1,33 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "azureBotId": { + "value": "" + }, + "azureBotSku": { + "value": "S1" + }, + "azureBotRegion": { + "value": "global" + }, + "botEndpoint": { + "value": "" + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + }, + "tenantId": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-BotApp-with-rg.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-BotApp-with-rg.json new file mode 100644 index 000000000..eb36c03fb --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-BotApp-with-rg.json @@ -0,0 +1,48 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "appServiceName": { + "value": "" + }, + "existingAppServicePlanName": { + "value": "" + }, + "existingAppServicePlanLocation": { + "value": "" + }, + "newAppServicePlanName": { + "value": "" + }, + "newAppServicePlanLocation": { + "value": "West US" + }, + "newAppServicePlanSku": { + "value": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + } + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "appSecret": { + "value": "" + }, + "tenantId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/readme.md b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/readme.md new file mode 100644 index 000000000..19d77be80 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/readme.md @@ -0,0 +1,48 @@ +# Usage +BotApp must be deployed prior to AzureBot. + +### Command line: +`az login`
+`az deployment group create --resource-group --template-file --parameters @` + +# parameters-for-template-BotApp-with-rg: + +- **appServiceName**:(required) The Name of the Bot App Service. + +- (choose an existingAppServicePlan or create a new AppServicePlan) + - **existingAppServicePlanName**: The name of the App Service Plan. + - **existingAppServicePlanLocation**: The location of the App Service Plan. + - **newAppServicePlanName**: The name of the App Service Plan. + - **newAppServicePlanLocation**: The location of the App Service Plan. + - **newAppServicePlanSku**: The SKU of the App Service Plan. Defaults to Standard values. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** + +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. + +- **appSecret**:(required for MultiTenant and SingleTenant) Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. + +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. + +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. + +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource + + + +# parameters-for-template-AzureBot-with-rg: + +- **azureBotId**:(required) The globally unique and immutable bot ID. +- **azureBotSku**: The pricing tier of the Bot Service Registration. **Allowed values are: F0, S1(default)**. +- **azureBotRegion**: Specifies the location of the new AzureBot. **Allowed values are: global(default), westeurope**. +- **botEndpoint**: Use to handle client messages, Such as https://.azurewebsites.net/api/messages. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-AzureBot-with-rg.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-AzureBot-with-rg.json new file mode 100644 index 000000000..b2b686dcc --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-AzureBot-with-rg.json @@ -0,0 +1,121 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "azureBotId": { + "type": "string", + "metadata": { + "description": "The globally unique and immutable bot ID." + } + }, + "azureBotSku": { + "type": "string", + "defaultValue": "S1", + "metadata": { + "description": "The pricing tier of the Bot Service Registration. Allowed values are: F0, S1(default)." + } + }, + "azureBotRegion": { + "type": "string", + "defaultValue": "global", + "metadata": { + "description": "Specifies the location of the new AzureBot. Allowed values are: global(default), westeurope." + } + }, + "botEndpoint": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Use to handle client messages, Such as https://.azurewebsites.net/api/messages." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant", + "UserAssignedMSI" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "UMSIName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication." + } + }, + "UMSIResourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + } + }, + "variables": { + "botEndpoint": "[if(empty(parameters('botEndpoint')), concat('https://', parameters('azureBotId'), '.azurewebsites.net/api/messages'), parameters('botEndpoint'))]", + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "msiResourceId": "" + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "msiResourceId": "" + }, + "UserAssignedMSI": { + "tenantId": "[variables('tenantId')]", + "msiResourceId": "[variables('msiResourceId')]" + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]", + "msiResourceId": "[variables('appTypeDef')[parameters('appType')].msiResourceId]" + } + }, + "resources": [ + { + "apiVersion": "2021-05-01-preview", + "type": "Microsoft.BotService/botServices", + "name": "[parameters('azureBotId')]", + "location": "[parameters('azureBotRegion')]", + "kind": "azurebot", + "sku": { + "name": "[parameters('azureBotSku')]" + }, + "properties": { + "name": "[parameters('azureBotId')]", + "displayName": "[parameters('azureBotId')]", + "iconUrl": "https://docs.botframework.com/static/devportal/client/images/bot-framework-default.png", + "endpoint": "[variables('botEndpoint')]", + "msaAppId": "[parameters('appId')]", + "msaAppTenantId": "[variables('appType').tenantId]", + "msaAppMSIResourceId": "[variables('appType').msiResourceId]", + "msaAppType": "[parameters('appType')]", + "luisAppIds": [], + "schemaTransformationVersion": "1.3", + "isCmekEnabled": false, + "isIsolated": false + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-BotApp-with-rg.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-BotApp-with-rg.json new file mode 100644 index 000000000..23a23b1cc --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-BotApp-with-rg.json @@ -0,0 +1,278 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "appServiceName": { + "type": "string", + "metadata": { + "description": "The globally unique name of the Web App." + } + }, + "existingAppServicePlanName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Name of the existing App Service Plan used to create the Web App for the bot." + } + }, + "existingAppServicePlanLocation": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "newAppServicePlanName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The name of the new App Service Plan." + } + }, + "newAppServicePlanLocation": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "newAppServicePlanSku": { + "type": "object", + "defaultValue": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + }, + "metadata": { + "description": "The SKU of the App Service Plan. Defaults to Standard values." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant", + "UserAssignedMSI" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "appSecret": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. Required for MultiTenant and SingleTenant app types. Defaults to \"\"." + } + }, + "UMSIName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication. Defaults to \"\"." + } + }, + "UMSIResourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. Defaults to \"\"." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + } + }, + "variables": { + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "useExistingServicePlan": "[not(empty(parameters('existingAppServicePlanName')))]", + "servicePlanName": "[if(variables('useExistingServicePlan'), parameters('existingAppServicePlanName'), parameters('newAppServicePlanName'))]", + "servicePlanLocation": "[if(variables('useExistingServicePlan'), parameters('existingAppServicePlanLocation'), parameters('newAppServicePlanLocation'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "identity": { "type": "None" } + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "identity": { "type": "None" } + }, + "UserAssignedMSI": { + "tenantId": "[variables('tenantId')]", + "identity": { + "type": "UserAssigned", + "userAssignedIdentities": { + "[variables('msiResourceId')]": {} + } + } + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]", + "identity": "[variables('appTypeDef')[parameters('appType')].identity]" + } + }, + "resources": [ + { + "comments": "Create a new App Service Plan if no existing App Service Plan name was passed in.", + "type": "Microsoft.Web/serverfarms", + "condition": "[not(variables('useExistingServicePlan'))]", + "name": "[variables('servicePlanName')]", + "apiVersion": "2018-02-01", + "location": "[parameters('newAppServicePlanLocation')]", + "sku": "[parameters('newAppServicePlanSku')]", + "kind": "linux", + "properties": { + "name": "[variables('servicePlanName')]", + "perSiteScaling": false, + "reserved": true, + "targetWorkerCount": 0, + "targetWorkerSizeId": 0 + } + }, + { + "comments": "Create a Web App using an App Service Plan", + "type": "Microsoft.Web/sites", + "apiVersion": "2015-08-01", + "location": "[variables('servicePlanLocation')]", + "kind": "app,linux", + "dependsOn": [ + "[resourceId('Microsoft.Web/serverfarms', variables('servicePlanName'))]" + ], + "name": "[parameters('appServiceName')]", + "identity": "[variables('appType').identity]", + "properties": { + "name": "[parameters('appServiceName')]", + "enabled": true, + "hostNameSslStates": [ + { + "name": "[concat(parameters('appServiceName'), '.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Standard" + }, + { + "name": "[concat(parameters('appServiceName'), '.scm.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Repository" + } + ], + "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('servicePlanName'))]", + "reserved": true, + "scmSiteAlsoStopped": false, + "clientAffinityEnabled": false, + "clientCertEnabled": false, + "hostNamesDisabled": false, + "containerSize": 0, + "dailyMemoryTimeQuota": 0, + "httpsOnly": false, + "siteConfig": { + "appSettings": [ + { + "name": "SCM_DO_BUILD_DURING_DEPLOYMENT", + "value": "true" + }, + { + "name": "MicrosoftAppType", + "value": "[parameters('appType')]" + }, + { + "name": "MicrosoftAppId", + "value": "[parameters('appId')]" + }, + { + "name": "MicrosoftAppPassword", + "value": "[parameters('appSecret')]" + }, + { + "name": "MicrosoftAppTenantId", + "value": "[variables('appType').tenantId]" + } + ], + "cors": { + "allowedOrigins": [ + "https://botservice.hosting.portal.azure.net", + "https://hosting.onecloud.azure-test.net/" + ] + } + } + } + }, + { + "type": "Microsoft.Web/sites/config", + "apiVersion": "2016-08-01", + "name": "[concat(parameters('appServiceName'), '/web')]", + "location": "[variables('servicePlanLocation')]", + "dependsOn": [ + "[resourceId('Microsoft.Web/sites', parameters('appServiceName'))]" + ], + "properties": { + "numberOfWorkers": 1, + "defaultDocuments": [ + "Default.htm", + "Default.html", + "Default.asp", + "index.htm", + "index.html", + "iisstart.htm", + "default.aspx", + "index.php", + "hostingstart.html" + ], + "netFrameworkVersion": "v4.0", + "phpVersion": "", + "pythonVersion": "", + "nodeVersion": "", + "linuxFxVersion": "PYTHON|3.9", + "requestTracingEnabled": false, + "remoteDebuggingEnabled": false, + "remoteDebuggingVersion": "VS2017", + "httpLoggingEnabled": true, + "logsDirectorySizeLimit": 35, + "detailedErrorLoggingEnabled": false, + "publishingUsername": "[concat('$', parameters('appServiceName'))]", + "scmType": "None", + "use32BitWorkerProcess": true, + "webSocketsEnabled": false, + "alwaysOn": false, + "appCommandLine": "gunicorn --bind 0.0.0.0 --worker-class aiohttp.worker.GunicornWebWorker --timeout 600 app:APP", + "managedPipelineMode": "Integrated", + "virtualApplications": [ + { + "virtualPath": "/", + "physicalPath": "site\\wwwroot", + "preloadEnabled": false, + "virtualDirectories": null + } + ], + "winAuthAdminState": 0, + "winAuthTenantState": 0, + "customAppPoolIdentityAdminState": false, + "customAppPoolIdentityTenantState": false, + "loadBalancing": "LeastRequests", + "routingRules": [], + "experiments": { + "rampUpRules": [] + }, + "autoHealEnabled": false, + "vnetName": "", + "minTlsVersion": "1.2", + "ftpsState": "Disabled", + "reservedInstanceCount": 0 + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-AzureBot-new-rg.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-AzureBot-new-rg.json new file mode 100644 index 000000000..e51036f85 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-AzureBot-new-rg.json @@ -0,0 +1,39 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "value": "" + }, + "groupLocation": { + "value": "" + }, + "azureBotId": { + "value": "" + }, + "azureBotSku": { + "value": "S1" + }, + "azureBotRegion": { + "value": "global" + }, + "botEndpoint": { + "value": "" + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + }, + "tenantId": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-BotApp-new-rg.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-BotApp-new-rg.json new file mode 100644 index 000000000..de2dba051 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-BotApp-new-rg.json @@ -0,0 +1,48 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "value": "" + }, + "groupLocation": { + "value": "" + }, + "appServiceName": { + "value": "" + }, + "appServicePlanName": { + "value": "" + }, + "appServicePlanLocation": { + "value": "" + }, + "appServicePlanSku": { + "value": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + } + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "appSecret": { + "value": "" + }, + "tenantId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/readme.md b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/readme.md new file mode 100644 index 000000000..4c752364b --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/readme.md @@ -0,0 +1,45 @@ +# Usage +BotApp must be deployed prior to AzureBot. + +### Command line: +`az login`
+`az deployment sub create --template-file --location --parameters @` + +# parameters-for-template-BotApp-new-rg: + +- **groupName**:(required) Specifies the name of the new Resource Group. +- **groupLocation**:(required) Specifies the location of the new Resource Group. + +- **appServiceName**:(required) The location of the App Service Plan. +- **appServicePlanName**:(required) The name of the App Service Plan. +- **appServicePlanLocation**: The location of the App Service Plan. Defaults to use groupLocation. +- **appServicePlanSku**: The SKU of the App Service Plan. Defaults to Standard values. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. +- **appSecret**:(required for MultiTenant and SingleTenant) Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource + + + +# parameters-for-template-AzureBot-new-rg: + +- **groupName**:(required) Specifies the name of the new Resource Group. +- **groupLocation**:(required) Specifies the location of the new Resource Group. + +- **azureBotId**:(required) The globally unique and immutable bot ID. Also used to configure the displayName of the bot, which is mutable. +- **azureBotSku**: The pricing tier of the Bot Service Registration. **Allowed values are: F0, S1(default)**. +- **azureBotRegion**: Specifies the location of the new AzureBot. **Allowed values are: global(default), westeurope**. +- **botEndpoint**: Use to handle client messages, Such as https://.azurewebsites.net/api/messages. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-AzureBot-new-rg.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-AzureBot-new-rg.json new file mode 100644 index 000000000..63fbf970d --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-AzureBot-new-rg.json @@ -0,0 +1,139 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "type": "string", + "metadata": { + "description": "Specifies the name of the Resource Group." + } + }, + "groupLocation": { + "type": "string", + "metadata": { + "description": "Specifies the location of the Resource Group." + } + }, + "azureBotId": { + "type": "string", + "metadata": { + "description": "The globally unique and immutable bot ID." + } + }, + "azureBotSku": { + "type": "string", + "defaultValue": "S1", + "metadata": { + "description": "The pricing tier of the Bot Service Registration. Acceptable values are F0 and S1." + } + }, + "azureBotRegion": { + "type": "string", + "defaultValue": "global", + "metadata": { + "description": "" + } + }, + "botEndpoint": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Use to handle client messages, Such as https://.azurewebsites.net/api/messages." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + } + }, + "variables": { + "botEndpoint": "[if(empty(parameters('botEndpoint')), concat('https://', parameters('azureBotId'), '.azurewebsites.net/api/messages'), parameters('botEndpoint'))]", + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "msiResourceId": "" + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "msiResourceId": "" + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]" + } + }, + "resources": [ + { + "name": "[parameters('groupName')]", + "type": "Microsoft.Resources/resourceGroups", + "apiVersion": "2018-05-01", + "location": "[parameters('groupLocation')]", + "properties": {} + }, + { + "type": "Microsoft.Resources/deployments", + "apiVersion": "2018-05-01", + "name": "storageDeployment", + "resourceGroup": "[parameters('groupName')]", + "dependsOn": [ + "[resourceId('Microsoft.Resources/resourceGroups/', parameters('groupName'))]" + ], + "properties": { + "mode": "Incremental", + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": {}, + "variables": {}, + "resources": [ + { + "apiVersion": "2021-03-01", + "type": "Microsoft.BotService/botServices", + "name": "[parameters('azureBotId')]", + "location": "[parameters('azureBotRegion')]", + "kind": "azurebot", + "sku": { + "name": "[parameters('azureBotSku')]" + }, + "properties": { + "name": "[parameters('azureBotId')]", + "displayName": "[parameters('azureBotId')]", + "iconUrl": "https://docs.botframework.com/static/devportal/client/images/bot-framework-default.png", + "endpoint": "[variables('botEndpoint')]", + "msaAppId": "[parameters('appId')]", + "msaAppTenantId": "[variables('appType').tenantId]", + "msaAppType": "[parameters('appType')]", + "luisAppIds": [], + "schemaTransformationVersion": "1.3", + "isCmekEnabled": false, + "isIsolated": false + } + } + ] + } + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-BotApp-new-rg.json b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-BotApp-new-rg.json new file mode 100644 index 000000000..381b57abf --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-BotApp-new-rg.json @@ -0,0 +1,294 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "type": "string", + "metadata": { + "description": "Specifies the name of the Resource Group." + } + }, + "groupLocation": { + "type": "string", + "metadata": { + "description": "Specifies the location of the Resource Group." + } + }, + "appServiceName": { + "type": "string", + "metadata": { + "description": "The globally unique name of the Web App." + } + }, + "appServicePlanName": { + "type": "string", + "metadata": { + "description": "The name of the App Service Plan." + } + }, + "appServicePlanLocation": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "appServicePlanSku": { + "type": "object", + "defaultValue": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + }, + "metadata": { + "description": "The SKU of the App Service Plan. Defaults to Standard values." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant", + "UserAssignedMSI" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "appSecret": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. Required for MultiTenant and SingleTenant app types. Defaults to \"\"." + } + }, + "UMSIName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication. Defaults to \"\"." + } + }, + "UMSIResourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. Defaults to \"\"." + } + } + }, + "variables": { + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "appServicePlanName": "[parameters('appServicePlanName')]", + "resourcesLocation": "[if(empty(parameters('appServicePlanLocation')), parameters('groupLocation'), parameters('appServicePlanLocation'))]", + "appServiceName": "[parameters('appServiceName')]", + "resourceGroupId": "[concat(subscription().id, '/resourceGroups/', parameters('groupName'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "identity": { "type": "None" } + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "identity": { "type": "None" } + }, + "UserAssignedMSI": { + "tenantId": "[variables('tenantId')]", + "identity": { + "type": "UserAssigned", + "userAssignedIdentities": { + "[variables('msiResourceId')]": {} + } + } + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]", + "identity": "[variables('appTypeDef')[parameters('appType')].identity]" + } + }, + "resources": [ + { + "name": "[parameters('groupName')]", + "type": "Microsoft.Resources/resourceGroups", + "apiVersion": "2018-05-01", + "location": "[parameters('groupLocation')]", + "properties": {} + }, + { + "type": "Microsoft.Resources/deployments", + "apiVersion": "2018-05-01", + "name": "storageDeployment", + "resourceGroup": "[parameters('groupName')]", + "dependsOn": [ + "[resourceId('Microsoft.Resources/resourceGroups/', parameters('groupName'))]" + ], + "properties": { + "mode": "Incremental", + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": {}, + "variables": {}, + "resources": [ + { + "comments": "Create a new App Service Plan", + "type": "Microsoft.Web/serverfarms", + "name": "[variables('appServicePlanName')]", + "apiVersion": "2018-02-01", + "location": "[variables('resourcesLocation')]", + "sku": "[parameters('appServicePlanSku')]", + "kind": "linux", + "properties": { + "name": "[variables('appServicePlanName')]", + "perSiteScaling": false, + "reserved": true, + "targetWorkerCount": 0, + "targetWorkerSizeId": 0 + } + }, + { + "comments": "Create a Web App using the new App Service Plan", + "type": "Microsoft.Web/sites", + "apiVersion": "2015-08-01", + "location": "[variables('resourcesLocation')]", + "kind": "app,linux", + "dependsOn": [ + "[concat(variables('resourceGroupId'), '/providers/Microsoft.Web/serverfarms/', variables('appServicePlanName'))]" + ], + "name": "[variables('appServiceName')]", + "identity": "[variables('appType').identity]", + "properties": { + "name": "[variables('appServiceName')]", + "hostNameSslStates": [ + { + "name": "[concat(parameters('appServiceName'), '.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Standard" + }, + { + "name": "[concat(parameters('appServiceName'), '.scm.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Repository" + } + ], + "serverFarmId": "[variables('appServicePlanName')]", + "siteConfig": { + "appSettings": [ + { + "name": "SCM_DO_BUILD_DURING_DEPLOYMENT", + "value": "true" + }, + { + "name": "MicrosoftAppType", + "value": "[parameters('appType')]" + }, { + "name": "MicrosoftAppId", + "value": "[parameters('appId')]" + }, + { + "name": "MicrosoftAppPassword", + "value": "[parameters('appSecret')]" + }, + { + "name": "MicrosoftAppTenantId", + "value": "[variables('appType').tenantId]" + } + ], + "cors": { + "allowedOrigins": [ + "https://botservice.hosting.portal.azure.net", + "https://hosting.onecloud.azure-test.net/" + ] + }, + "webSocketsEnabled": true + } + } + }, + { + "type": "Microsoft.Web/sites/config", + "apiVersion": "2016-08-01", + "name": "[concat(parameters('appServiceName'), '/web')]", + "location": "[variables('resourcesLocation')]", + "dependsOn": [ + "[concat(variables('resourceGroupId'), '/providers/Microsoft.Web/sites/', parameters('appServiceName'))]" + ], + "properties": { + "numberOfWorkers": 1, + "defaultDocuments": [ + "Default.htm", + "Default.html", + "Default.asp", + "index.htm", + "index.html", + "iisstart.htm", + "default.aspx", + "index.php", + "hostingstart.html" + ], + "netFrameworkVersion": "v4.0", + "phpVersion": "", + "pythonVersion": "", + "nodeVersion": "", + "linuxFxVersion": "PYTHON|3.9", + "requestTracingEnabled": false, + "remoteDebuggingEnabled": false, + "remoteDebuggingVersion": "VS2017", + "httpLoggingEnabled": true, + "logsDirectorySizeLimit": 35, + "detailedErrorLoggingEnabled": false, + "publishingUsername": "[concat('$', parameters('appServiceName'))]", + "scmType": "None", + "use32BitWorkerProcess": true, + "webSocketsEnabled": false, + "alwaysOn": false, + "appCommandLine": "gunicorn --bind 0.0.0.0 --worker-class aiohttp.worker.GunicornWebWorker --timeout 600 app:APP", + "managedPipelineMode": "Integrated", + "virtualApplications": [ + { + "virtualPath": "/", + "physicalPath": "site\\wwwroot", + "preloadEnabled": false, + "virtualDirectories": null + } + ], + "winAuthAdminState": 0, + "winAuthTenantState": 0, + "customAppPoolIdentityAdminState": false, + "customAppPoolIdentityTenantState": false, + "loadBalancing": "LeastRequests", + "routingRules": [], + "experiments": { + "rampUpRules": [] + }, + "autoHealEnabled": false, + "vnetName": "", + "minTlsVersion": "1.2", + "ftpsState": "Disabled", + "reservedInstanceCount": 0 + } + } + ], + "outputs": {} + } + } + } + ] +} \ No newline at end of file diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/__init__.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/__init__.py similarity index 92% rename from samples/experimental/101.corebot-bert-bidaf/bot/dialogs/__init__.py rename to generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/__init__.py index d37afdc97..5c3aa9845 100644 --- a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/__init__.py +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/__init__.py @@ -1,6 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -"""Dialogs module""" + from .booking_dialog import BookingDialog from .cancel_and_help_dialog import CancelAndHelpDialog from .date_resolver_dialog import DateResolverDialog diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/booking_dialog.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/booking_dialog.py similarity index 57% rename from samples/experimental/101.corebot-bert-bidaf/bot/dialogs/booking_dialog.py rename to generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/booking_dialog.py index 52b71eb30..a73e95d20 100644 --- a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/booking_dialog.py +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/booking_dialog.py @@ -1,23 +1,23 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -"""Flight booking dialog.""" from botbuilder.dialogs import WaterfallDialog, WaterfallStepContext, DialogTurnResult from botbuilder.dialogs.prompts import ConfirmPrompt, TextPrompt, PromptOptions from botbuilder.core import MessageFactory +from botbuilder.schema import InputHints + from datatypes_date_time.timex import Timex + from .cancel_and_help_dialog import CancelAndHelpDialog from .date_resolver_dialog import DateResolverDialog class BookingDialog(CancelAndHelpDialog): - """Flight booking implementation.""" - def __init__(self, dialog_id: str = None): super(BookingDialog, self).__init__(dialog_id or BookingDialog.__name__) self.add_dialog(TextPrompt(TextPrompt.__name__)) - # self.add_dialog(ConfirmPrompt(ConfirmPrompt.__name__)) + self.add_dialog(ConfirmPrompt(ConfirmPrompt.__name__)) self.add_dialog(DateResolverDialog(DateResolverDialog.__name__)) self.add_dialog( WaterfallDialog( @@ -26,7 +26,7 @@ def __init__(self, dialog_id: str = None): self.destination_step, self.origin_step, self.travel_date_step, - # self.confirm_step, + self.confirm_step, self.final_step, ], ) @@ -37,41 +37,52 @@ def __init__(self, dialog_id: str = None): async def destination_step( self, step_context: WaterfallStepContext ) -> DialogTurnResult: - """Prompt for destination.""" + """ + If a destination city has not been provided, prompt for one. + :param step_context: + :return DialogTurnResult: + """ booking_details = step_context.options if booking_details.destination is None: + message_text = "Where would you like to travel to?" + prompt_message = MessageFactory.text( + message_text, message_text, InputHints.expecting_input + ) return await step_context.prompt( - TextPrompt.__name__, - PromptOptions( - prompt=MessageFactory.text("To what city would you like to travel?") - ), - ) # pylint: disable=line-too-long,bad-continuation - else: - return await step_context.next(booking_details.destination) + TextPrompt.__name__, PromptOptions(prompt=prompt_message) + ) + return await step_context.next(booking_details.destination) async def origin_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: - """Prompt for origin city.""" + """ + If an origin city has not been provided, prompt for one. + :param step_context: + :return DialogTurnResult: + """ booking_details = step_context.options # Capture the response to the previous step's prompt booking_details.destination = step_context.result if booking_details.origin is None: + message_text = "From what city will you be travelling?" + prompt_message = MessageFactory.text( + message_text, message_text, InputHints.expecting_input + ) return await step_context.prompt( - TextPrompt.__name__, - PromptOptions( - prompt=MessageFactory.text("From what city will you be travelling?") - ), - ) # pylint: disable=line-too-long,bad-continuation - else: - return await step_context.next(booking_details.origin) + TextPrompt.__name__, PromptOptions(prompt=prompt_message) + ) + return await step_context.next(booking_details.origin) async def travel_date_step( self, step_context: WaterfallStepContext ) -> DialogTurnResult: - """Prompt for travel date. - This will use the DATE_RESOLVER_DIALOG.""" - + """ + If a travel date has not been provided, prompt for one. + This will use the DATE_RESOLVER_DIALOG. + :param step_context: + :return DialogTurnResult: + """ booking_details = step_context.options # Capture the results of the previous step @@ -81,39 +92,46 @@ async def travel_date_step( ): return await step_context.begin_dialog( DateResolverDialog.__name__, booking_details.travel_date - ) # pylint: disable=line-too-long - else: - return await step_context.next(booking_details.travel_date) + ) + return await step_context.next(booking_details.travel_date) async def confirm_step( self, step_context: WaterfallStepContext ) -> DialogTurnResult: - """Confirm the information the user has provided.""" + """ + Confirm the information the user has provided. + :param step_context: + :return DialogTurnResult: + """ booking_details = step_context.options # Capture the results of the previous step booking_details.travel_date = step_context.result - msg = ( - f"Please confirm, I have you traveling to: { booking_details.destination }" - f" from: { booking_details.origin } on: { booking_details.travel_date}." + message_text = ( + f"Please confirm, I have you traveling to: { booking_details.destination } from: " + f"{ booking_details.origin } on: { booking_details.travel_date}." + ) + prompt_message = MessageFactory.text( + message_text, message_text, InputHints.expecting_input ) # Offer a YES/NO prompt. return await step_context.prompt( - ConfirmPrompt.__name__, PromptOptions(prompt=MessageFactory.text(msg)) + ConfirmPrompt.__name__, PromptOptions(prompt=prompt_message) ) async def final_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: - """Complete the interaction and end the dialog.""" + """ + Complete the interaction and end the dialog. + :param step_context: + :return DialogTurnResult: + """ if step_context.result: booking_details = step_context.options - booking_details.travel_date = step_context.result return await step_context.end_dialog(booking_details) - else: - return await step_context.end_dialog() + return await step_context.end_dialog() def is_ambiguous(self, timex: str) -> bool: - """Ensure time is correct.""" timex_property = Timex(timex) return "definite" not in timex_property.types diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/cancel_and_help_dialog.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/cancel_and_help_dialog.py similarity index 53% rename from samples/experimental/101.corebot-bert-bidaf/bot/dialogs/cancel_and_help_dialog.py rename to generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/cancel_and_help_dialog.py index 760ecc457..d11f65fd6 100644 --- a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/cancel_and_help_dialog.py +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/cancel_and_help_dialog.py @@ -1,27 +1,17 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -"""Handle cancel and help intents.""" + from botbuilder.dialogs import ( ComponentDialog, DialogContext, DialogTurnResult, DialogTurnStatus, ) -from botbuilder.schema import ActivityTypes +from botbuilder.schema import ActivityTypes, InputHints +from botbuilder.core import MessageFactory class CancelAndHelpDialog(ComponentDialog): - """Implementation of handling cancel and help.""" - - async def on_begin_dialog( - self, inner_dc: DialogContext, options: object - ) -> DialogTurnResult: - result = await self.interrupt(inner_dc) - if result is not None: - return result - - return await super(CancelAndHelpDialog, self).on_begin_dialog(inner_dc, options) - async def on_continue_dialog(self, inner_dc: DialogContext) -> DialogTurnResult: result = await self.interrupt(inner_dc) if result is not None: @@ -30,16 +20,25 @@ async def on_continue_dialog(self, inner_dc: DialogContext) -> DialogTurnResult: return await super(CancelAndHelpDialog, self).on_continue_dialog(inner_dc) async def interrupt(self, inner_dc: DialogContext) -> DialogTurnResult: - """Detect interruptions.""" if inner_dc.context.activity.type == ActivityTypes.message: text = inner_dc.context.activity.text.lower() - if text == "help" or text == "?": - await inner_dc.context.send_activity("Show Help...") + help_message_text = "Show Help..." + help_message = MessageFactory.text( + help_message_text, help_message_text, InputHints.expecting_input + ) + + if text in ("help", "?"): + await inner_dc.context.send_activity(help_message) return DialogTurnResult(DialogTurnStatus.Waiting) - if text == "cancel" or text == "quit": - await inner_dc.context.send_activity("Cancelling") + cancel_message_text = "Cancelling" + cancel_message = MessageFactory.text( + cancel_message_text, cancel_message_text, InputHints.ignoring_input + ) + + if text in ("cancel", "quit"): + await inner_dc.context.send_activity(cancel_message) return await inner_dc.cancel_all_dialogs() return None diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/date_resolver_dialog.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/date_resolver_dialog.py similarity index 60% rename from samples/experimental/101.corebot-bert-bidaf/bot/dialogs/date_resolver_dialog.py rename to generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/date_resolver_dialog.py index bef1a94f3..c6b163f8c 100644 --- a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/date_resolver_dialog.py +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/date_resolver_dialog.py @@ -1,6 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -"""Handle date/time resolution for booking dialog.""" + from botbuilder.core import MessageFactory from botbuilder.dialogs import WaterfallDialog, DialogTurnResult, WaterfallStepContext from botbuilder.dialogs.prompts import ( @@ -9,13 +9,13 @@ PromptOptions, DateTimeResolution, ) +from botbuilder.schema import InputHints from datatypes_date_time.timex import Timex + from .cancel_and_help_dialog import CancelAndHelpDialog class DateResolverDialog(CancelAndHelpDialog): - """Resolve the date""" - def __init__(self, dialog_id: str = None): super(DateResolverDialog, self).__init__( dialog_id or DateResolverDialog.__name__ @@ -37,46 +37,43 @@ def __init__(self, dialog_id: str = None): async def initial_step( self, step_context: WaterfallStepContext ) -> DialogTurnResult: - """Prompt for the date.""" timex = step_context.options - prompt_msg = "On what date would you like to travel?" - reprompt_msg = ( - "I'm sorry, for best results, please enter your travel " - "date including the month, day and year." + prompt_msg_text = "On what date would you like to travel?" + prompt_msg = MessageFactory.text( + prompt_msg_text, prompt_msg_text, InputHints.expecting_input + ) + + reprompt_msg_text = "I'm sorry, for best results, please enter your travel date " \ + "including the month, day and year." + reprompt_msg = MessageFactory.text( + reprompt_msg_text, reprompt_msg_text, InputHints.expecting_input ) if timex is None: # We were not given any date at all so prompt the user. return await step_context.prompt( DateTimePrompt.__name__, - PromptOptions( # pylint: disable=bad-continuation - prompt=MessageFactory.text(prompt_msg), - retry_prompt=MessageFactory.text(reprompt_msg), - ), + PromptOptions(prompt=prompt_msg, retry_prompt=reprompt_msg), + ) + # We have a Date we just need to check it is unambiguous. + if "definite" not in Timex(timex).types: + # This is essentially a "reprompt" of the data we were given up front. + return await step_context.prompt( + DateTimePrompt.__name__, PromptOptions(prompt=reprompt_msg) ) - else: - # We have a Date we just need to check it is unambiguous. - if "definite" in Timex(timex).types: - # This is essentially a "reprompt" of the data we were given up front. - return await step_context.prompt( - DateTimePrompt.__name__, PromptOptions(prompt=reprompt_msg) - ) - else: - return await step_context.next(DateTimeResolution(timex=timex)) + + return await step_context.next(DateTimeResolution(timex=timex)) async def final_step(self, step_context: WaterfallStepContext): - """Cleanup - set final return value and end dialog.""" timex = step_context.result[0].timex return await step_context.end_dialog(timex) @staticmethod async def datetime_prompt_validator(prompt_context: PromptValidatorContext) -> bool: - """ Validate the date provided is in proper form. """ if prompt_context.recognized.succeeded: timex = prompt_context.recognized.value[0].timex.split("T")[0] - # TODO: Needs TimexProperty return "definite" in Timex(timex).types return False diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/main_dialog.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/main_dialog.py new file mode 100644 index 000000000..c0e97da76 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/dialogs/main_dialog.py @@ -0,0 +1,133 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.dialogs import ( + ComponentDialog, + WaterfallDialog, + WaterfallStepContext, + DialogTurnResult, +) +from botbuilder.dialogs.prompts import TextPrompt, PromptOptions +from botbuilder.core import MessageFactory, TurnContext +from botbuilder.schema import InputHints + +from booking_details import BookingDetails +from flight_booking_recognizer import FlightBookingRecognizer + +from helpers import LuisHelper, Intent +from .booking_dialog import BookingDialog + + +class MainDialog(ComponentDialog): + def __init__( + self, luis_recognizer: FlightBookingRecognizer, booking_dialog: BookingDialog + ): + super(MainDialog, self).__init__(MainDialog.__name__) + + self._luis_recognizer = luis_recognizer + self._booking_dialog_id = booking_dialog.id + + self.add_dialog(TextPrompt(TextPrompt.__name__)) + self.add_dialog(booking_dialog) + self.add_dialog( + WaterfallDialog( + "WFDialog", [self.intro_step, self.act_step, self.final_step] + ) + ) + + self.initial_dialog_id = "WFDialog" + + async def intro_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: + if not self._luis_recognizer.is_configured: + await step_context.context.send_activity( + MessageFactory.text( + "NOTE: LUIS is not configured. To enable all capabilities, add 'LuisAppId', 'LuisAPIKey' and " + "'LuisAPIHostName' to the appsettings.json file.", + input_hint=InputHints.ignoring_input, + ) + ) + + return await step_context.next(None) + message_text = ( + str(step_context.options) + if step_context.options + else "What can I help you with today?" + ) + prompt_message = MessageFactory.text( + message_text, message_text, InputHints.expecting_input + ) + + return await step_context.prompt( + TextPrompt.__name__, PromptOptions(prompt=prompt_message) + ) + + async def act_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: + if not self._luis_recognizer.is_configured: + # LUIS is not configured, we just run the BookingDialog path with an empty BookingDetailsInstance. + return await step_context.begin_dialog( + self._booking_dialog_id, BookingDetails() + ) + + # Call LUIS and gather any potential booking details. (Note the TurnContext has the response to the prompt.) + intent, luis_result = await LuisHelper.execute_luis_query( + self._luis_recognizer, step_context.context + ) + + if intent == Intent.BOOK_FLIGHT.value and luis_result: + # Show a warning for Origin and Destination if we can't resolve them. + await MainDialog._show_warning_for_unsupported_cities( + step_context.context, luis_result + ) + + # Run the BookingDialog giving it whatever details we have from the LUIS call. + return await step_context.begin_dialog(self._booking_dialog_id, luis_result) + + if intent == Intent.GET_WEATHER.value: + get_weather_text = "TODO: get weather flow here" + get_weather_message = MessageFactory.text( + get_weather_text, get_weather_text, InputHints.ignoring_input + ) + await step_context.context.send_activity(get_weather_message) + + else: + didnt_understand_text = ( + "Sorry, I didn't get that. Please try asking in a different way" + ) + didnt_understand_message = MessageFactory.text( + didnt_understand_text, didnt_understand_text, InputHints.ignoring_input + ) + await step_context.context.send_activity(didnt_understand_message) + + return await step_context.next(None) + + async def final_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: + # If the child dialog ("BookingDialog") was cancelled or the user failed to confirm, + # the Result here will be null. + if step_context.result is not None: + result = step_context.result + + # Now we have all the booking details call the booking service. + + # If the call to the booking service was successful tell the user. + # time_property = Timex(result.travel_date) + # travel_date_msg = time_property.to_natural_language(datetime.now()) + msg_txt = f"I have you booked to {result.destination} from {result.origin} on {result.travel_date}" + message = MessageFactory.text(msg_txt, msg_txt, InputHints.ignoring_input) + await step_context.context.send_activity(message) + + prompt_message = "What else can I do for you?" + return await step_context.replace_dialog(self.id, prompt_message) + + @staticmethod + async def _show_warning_for_unsupported_cities( + context: TurnContext, luis_result: BookingDetails + ) -> None: + if luis_result.unsupported_airports: + message_text = ( + f"Sorry but the following airports are not supported:" + f" {', '.join(luis_result.unsupported_airports)}" + ) + message = MessageFactory.text( + message_text, message_text, InputHints.ignoring_input + ) + await context.send_activity(message) diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/flight_booking_recognizer.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/flight_booking_recognizer.py new file mode 100644 index 000000000..744e26fc8 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/flight_booking_recognizer.py @@ -0,0 +1,34 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.ai.luis import LuisApplication, LuisRecognizer +from botbuilder.core import Recognizer, RecognizerResult, TurnContext + +from config import DefaultConfig + + +class FlightBookingRecognizer(Recognizer): + def __init__(self, configuration: DefaultConfig): + self._recognizer = None + + luis_is_configured = ( + configuration.LUIS_APP_ID + and configuration.LUIS_API_KEY + and configuration.LUIS_API_HOST_NAME + ) + if luis_is_configured: + luis_application = LuisApplication( + configuration.LUIS_APP_ID, + configuration.LUIS_API_KEY, + "https://" + configuration.LUIS_API_HOST_NAME, + ) + + self._recognizer = LuisRecognizer(luis_application) + + @property + def is_configured(self) -> bool: + # Returns true if luis is configured in the appsettings.json and initialized. + return self._recognizer is not None + + async def recognize(self, turn_context: TurnContext) -> RecognizerResult: + return await self._recognizer.recognize(turn_context) diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/helpers/__init__.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/helpers/__init__.py new file mode 100644 index 000000000..39a6dd2b9 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/helpers/__init__.py @@ -0,0 +1,11 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .luis_helper import Intent, LuisHelper +from .dialog_helper import DialogHelper + +__all__ = [ + "DialogHelper", + "LuisHelper", + "Intent" +] diff --git a/samples/experimental/sso/parent/helpers/dialog_helper.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/helpers/dialog_helper.py old mode 100755 new mode 100644 similarity index 97% rename from samples/experimental/sso/parent/helpers/dialog_helper.py rename to generators/app/templates/core/{{cookiecutter.bot_name}}/helpers/dialog_helper.py index 6b2646b0b..062271fd8 --- a/samples/experimental/sso/parent/helpers/dialog_helper.py +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/helpers/dialog_helper.py @@ -1,19 +1,19 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import StatePropertyAccessor, TurnContext -from botbuilder.dialogs import Dialog, DialogSet, DialogTurnStatus - - -class DialogHelper: - @staticmethod - async def run_dialog( - dialog: Dialog, turn_context: TurnContext, accessor: StatePropertyAccessor - ): - dialog_set = DialogSet(accessor) - dialog_set.add(dialog) - - dialog_context = await dialog_set.create_context(turn_context) - results = await dialog_context.continue_dialog() - if results.status == DialogTurnStatus.Empty: - await dialog_context.begin_dialog(dialog.id) +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import StatePropertyAccessor, TurnContext +from botbuilder.dialogs import Dialog, DialogSet, DialogTurnStatus + + +class DialogHelper: + @staticmethod + async def run_dialog( + dialog: Dialog, turn_context: TurnContext, accessor: StatePropertyAccessor + ): + dialog_set = DialogSet(accessor) + dialog_set.add(dialog) + + dialog_context = await dialog_set.create_context(turn_context) + results = await dialog_context.continue_dialog() + if results.status == DialogTurnStatus.Empty: + await dialog_context.begin_dialog(dialog.id) diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/helpers/luis_helper.py b/generators/app/templates/core/{{cookiecutter.bot_name}}/helpers/luis_helper.py new file mode 100644 index 000000000..cc8a18be4 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/helpers/luis_helper.py @@ -0,0 +1,104 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +from enum import Enum +from typing import Dict +from botbuilder.ai.luis import LuisRecognizer +from botbuilder.core import IntentScore, TopIntent, TurnContext + +from booking_details import BookingDetails + + +class Intent(Enum): + BOOK_FLIGHT = "BookFlight" + CANCEL = "Cancel" + GET_WEATHER = "GetWeather" + NONE_INTENT = "NoneIntent" + + +def top_intent(intents: Dict[Intent, dict]) -> TopIntent: + max_intent = Intent.NONE_INTENT + max_value = 0.0 + + for intent, value in intents: + intent_score = IntentScore(value) + if intent_score.score > max_value: + max_intent, max_value = intent, intent_score.score + + return TopIntent(max_intent, max_value) + + +class LuisHelper: + @staticmethod + async def execute_luis_query( + luis_recognizer: LuisRecognizer, turn_context: TurnContext + ) -> (Intent, object): + """ + Returns an object with pre-formatted LUIS results for the bot's dialogs to consume. + """ + result = None + intent = None + + try: + recognizer_result = await luis_recognizer.recognize(turn_context) + + intent = ( + sorted( + recognizer_result.intents, + key=recognizer_result.intents.get, + reverse=True, + )[:1][0] + if recognizer_result.intents + else None + ) + + if intent == Intent.BOOK_FLIGHT.value: + result = BookingDetails() + + # We need to get the result from the LUIS JSON which at every level + # returns an array. + to_entities = recognizer_result.entities.get("$instance", {}).get( + "To", [] + ) + if len(to_entities) > 0: + if recognizer_result.entities.get("To", [{"$instance": {}}])[0][ + "$instance" + ]: + result.destination = to_entities[0]["text"].capitalize() + else: + result.unsupported_airports.append( + to_entities[0]["text"].capitalize() + ) + + from_entities = recognizer_result.entities.get("$instance", {}).get( + "From", [] + ) + if len(from_entities) > 0: + if recognizer_result.entities.get("From", [{"$instance": {}}])[0][ + "$instance" + ]: + result.origin = from_entities[0]["text"].capitalize() + else: + result.unsupported_airports.append( + from_entities[0]["text"].capitalize() + ) + + # This value will be a TIMEX. And we are only interested in a Date so + # grab the first result and drop the Time part. + # TIMEX is a format that represents DateTime expressions that include + # some ambiguity. e.g. missing a Year. + date_entities = recognizer_result.entities.get("datetime", []) + if date_entities: + timex = date_entities[0]["timex"] + + if timex: + datetime = timex[0].split("T")[0] + + result.travel_date = datetime + + else: + result.travel_date = None + + except Exception as err: + print(err) + + return intent, result diff --git a/generators/app/templates/core/{{cookiecutter.bot_name}}/requirements.txt b/generators/app/templates/core/{{cookiecutter.bot_name}}/requirements.txt new file mode 100644 index 000000000..0c93ce564 --- /dev/null +++ b/generators/app/templates/core/{{cookiecutter.bot_name}}/requirements.txt @@ -0,0 +1,4 @@ +botbuilder-integration-aiohttp>=4.14.8 +botbuilder-dialogs>=4.15.0 +botbuilder-ai>=4.14.8 +datatypes-date-time>=1.0.0.a2 diff --git a/generators/app/templates/echo/cookiecutter.json b/generators/app/templates/echo/cookiecutter.json new file mode 100644 index 000000000..4a14b6ade --- /dev/null +++ b/generators/app/templates/echo/cookiecutter.json @@ -0,0 +1,4 @@ +{ + "bot_name": "my_chat_bot", + "bot_description": "Demonstrate the core capabilities of the Microsoft Bot Framework" +} \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/.pylintrc b/generators/app/templates/echo/{{cookiecutter.bot_name}}/.pylintrc new file mode 100644 index 000000000..1baee5edb --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/.pylintrc @@ -0,0 +1,497 @@ +[MASTER] + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-whitelist= + +# Add files or directories to the blacklist. They should be base names, not +# paths. +ignore= + +# Add files or directories matching the regex patterns to the blacklist. The +# regex matches against base names, not paths. +ignore-patterns= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use. +jobs=1 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=100 + +# List of plugins (as comma separated values of python modules names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Specify a configuration file. +#rcfile= + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. +confidence= + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=missing-docstring, + too-few-public-methods, + bad-continuation, + no-self-use, + duplicate-code, + broad-except + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[REPORTS] + +# Python expression which should return a note less than 10 (10 is the highest +# note). You have access to the variables errors warning, statement which +# respectively contain the number of errors / warnings messages and the total +# number of statements analyzed. This is used by the global evaluation report +# (RP0004). +evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +#msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +output-format=text + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit + + +[LOGGING] + +# Format style used to check logging format string. `old` means using % +# formatting, while `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. Available dictionaries: none. To make it working +# install python-enchant package.. +spelling-dict= + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to indicated private dictionary in +# --spelling-private-dict-file option instead of raising a message. +spelling-store-unknown-words=no + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether missing members accessed in mixin class should be ignored. A +# mixin class is detected if its name ends with "mixin" (case insensitive). +ignore-mixin-members=yes + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis. It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=120 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# List of optional constructs for which whitespace checking is disabled. `dict- +# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. +# `trailing-comma` allows a space between comma and closing bracket: (a, ). +# `empty-line` allows space-only lines. +no-space-check=trailing-comma, + dict-separator + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[SIMILARITIES] + +# Ignore comments when computing similarities. +ignore-comments=yes + +# Ignore docstrings when computing similarities. +ignore-docstrings=yes + +# Ignore imports when computing similarities. +ignore-imports=no + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. +#class-attribute-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _ + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. +#variable-rgx= + + +[STRING] + +# This flag controls whether the implicit-str-concat-in-sequence should +# generate a warning on implicit string concatenation in sequences defined over +# several lines. +check-str-concat-over-line-jumps=no + + +[IMPORTS] + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules=optparse,tkinter.tix + +# Create a graph of external dependencies in the given file (report RP0402 must +# not be disabled). +ext-import-graph= + +# Create a graph of every (i.e. internal and external) dependencies in the +# given file (report RP0402 must not be disabled). +import-graph= + +# Create a graph of internal dependencies in the given file (report RP0402 must +# not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + + +[CLASSES] + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict, + _fields, + _replace, + _source, + _make + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=cls + + +[DESIGN] + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement. +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=15 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. Defaults to +# "BaseException, Exception". +overgeneral-exceptions=BaseException, + Exception diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/README.md b/generators/app/templates/echo/{{cookiecutter.bot_name}}/README.md new file mode 100644 index 000000000..cc26d0f4d --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/README.md @@ -0,0 +1,42 @@ +# {{cookiecutter.bot_name}} + +{{cookiecutter.bot_description}} + +This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. + +## Prerequisites + +This sample **requires** prerequisites in order to run. + +### Install Python 3.6 + +## Running the sample +- Run `pip install -r requirements.txt` to install all dependencies +- Run `python app.py` + + +## Testing the bot using Bot Framework Emulator + +[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. + +- Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) + +### Connect to the bot using Bot Framework Emulator + +- Launch Bot Framework Emulator +- Enter a Bot URL of `http://localhost:3978/api/messages` + + +## Further reading + +- [Bot Framework Documentation](https://docs.botframework.com) +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) +- [Dialogs](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0) +- [Gathering Input Using Prompts](https://docs.microsoft.com/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=csharp) +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) +- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) +- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) +- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) +- [Azure Portal](https://portal.azure.com) +- [Language Understanding using LUIS](https://docs.microsoft.com/azure/cognitive-services/luis/) +- [Channels and Bot Connector Service](https://docs.microsoft.com/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/__init__.py b/generators/app/templates/echo/{{cookiecutter.bot_name}}/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/scenarios/action-based-messaging-extension/app.py b/generators/app/templates/echo/{{cookiecutter.bot_name}}/app.py similarity index 73% rename from scenarios/action-based-messaging-extension/app.py rename to generators/app/templates/echo/{{cookiecutter.bot_name}}/app.py index 4643ee6af..8dc9fbecb 100644 --- a/scenarios/action-based-messaging-extension/app.py +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/app.py @@ -1,89 +1,83 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import json -import sys -from datetime import datetime - -from aiohttp import web -from aiohttp.web import Request, Response, json_response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes -from bots import TeamsMessagingExtensionsActionBot -from config import DefaultConfig - -CONFIG = DefaultConfig() - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error(context: TurnContext, error: Exception): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = on_error - -# Create the Bot -BOT = TeamsMessagingExtensionsActionBot() - - -# Listen for incoming requests on /api/messages -async def messages(req: Request) -> Response: - # Main bot message handler. - if "application/json" in req.headers["Content-Type"]: - body = await req.json() - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - - try: - invoke_response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - if invoke_response: - return json_response(data=invoke_response.body, status=invoke_response.status) - return Response(status=201) - except PermissionError: - return Response(status=401) - except Exception: - return Response(status=500) - - -APP = web.Application() -APP.router.add_post("/api/messages", messages) - -if __name__ == "__main__": - try: - web.run_app(APP, host="localhost", port=CONFIG.PORT) - except Exception as error: - raise error +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import sys +import traceback +from datetime import datetime + +from http import HTTPStatus +from aiohttp import web +from aiohttp.web import Request, Response, json_response +from botbuilder.core import TurnContext +from botbuilder.core.integration import aiohttp_error_middleware +from botbuilder.integration.aiohttp import CloudAdapter, ConfigurationBotFrameworkAuthentication +from botbuilder.schema import Activity, ActivityTypes + +from bot import MyBot +from config import DefaultConfig + +CONFIG = DefaultConfig() + +# Create adapter. +# See https://aka.ms/about-bot-adapter to learn more about how bots work. +ADAPTER = CloudAdapter(ConfigurationBotFrameworkAuthentication(CONFIG)) + +# Catch-all for errors. +async def on_error(context: TurnContext, error: Exception): + # This check writes out errors to console log .vs. app insights. + # NOTE: In production environment, you should consider logging this to Azure + # application insights. + print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) + traceback.print_exc() + + # Send a message to the user + await context.send_activity("The bot encountered an error or bug.") + await context.send_activity( + "To continue to run this bot, please fix the bot source code." + ) + # Send a trace activity if we're talking to the Bot Framework Emulator + if context.activity.channel_id == "emulator": + # Create a trace activity that contains the error object + trace_activity = Activity( + label="TurnError", + name="on_turn_error Trace", + timestamp=datetime.utcnow(), + type=ActivityTypes.trace, + value=f"{error}", + value_type="https://www.botframework.com/schemas/error", + ) + # Send a trace activity, which will be displayed in Bot Framework Emulator + await context.send_activity(trace_activity) + + +ADAPTER.on_turn_error = on_error + +# Create the Bot +BOT = MyBot() + + +# Listen for incoming requests on /api/messages +async def messages(req: Request) -> Response: + # Main bot message handler. + if "application/json" in req.headers["Content-Type"]: + body = await req.json() + else: + return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE) + + activity = Activity().deserialize(body) + auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" + + response = await ADAPTER.process_activity(auth_header, activity, BOT.on_turn) + if response: + return json_response(data=response.body, status=response.status) + return Response(status=HTTPStatus.OK) + + +APP = web.Application(middlewares=[aiohttp_error_middleware]) +APP.router.add_post("/api/messages", messages) + +if __name__ == "__main__": + try: + web.run_app(APP, host="localhost", port=CONFIG.PORT) + except Exception as error: + raise error diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/bot.py b/generators/app/templates/echo/{{cookiecutter.bot_name}}/bot.py new file mode 100644 index 000000000..89b2eefdb --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/bot.py @@ -0,0 +1,21 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import ActivityHandler, TurnContext +from botbuilder.schema import ChannelAccount + + +class MyBot(ActivityHandler): + # See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types. + + async def on_message_activity(self, turn_context: TurnContext): + await turn_context.send_activity(f"Echo: '{ turn_context.activity.text }'") + + async def on_members_added_activity( + self, + members_added: ChannelAccount, + turn_context: TurnContext + ): + for member_added in members_added: + if member_added.id != turn_context.activity.recipient.id: + await turn_context.send_activity("Hello and welcome!") diff --git a/scenarios/action-based-messaging-extension/config.py b/generators/app/templates/echo/{{cookiecutter.bot_name}}/config.py similarity index 68% rename from scenarios/action-based-messaging-extension/config.py rename to generators/app/templates/echo/{{cookiecutter.bot_name}}/config.py index 6b5116fba..aad3896f4 100644 --- a/scenarios/action-based-messaging-extension/config.py +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/config.py @@ -1,13 +1,14 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") +#!/usr/bin/env python3 +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os + +class DefaultConfig: + """ Bot Configuration """ + + PORT = 3978 + APP_ID = os.environ.get("MicrosoftAppId", "") + APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") + APP_TYPE = os.environ.get("MicrosoftAppType", "MultiTenant") + APP_TENANTID = os.environ.get("MicrosoftAppTenantId", "") diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-AzureBot-with-rg.json b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-AzureBot-with-rg.json new file mode 100644 index 000000000..92b2e84c9 --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-AzureBot-with-rg.json @@ -0,0 +1,33 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "azureBotId": { + "value": "" + }, + "azureBotSku": { + "value": "S1" + }, + "azureBotRegion": { + "value": "global" + }, + "botEndpoint": { + "value": "" + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + }, + "tenantId": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-BotApp-with-rg.json b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-BotApp-with-rg.json new file mode 100644 index 000000000..eb36c03fb --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-BotApp-with-rg.json @@ -0,0 +1,48 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "appServiceName": { + "value": "" + }, + "existingAppServicePlanName": { + "value": "" + }, + "existingAppServicePlanLocation": { + "value": "" + }, + "newAppServicePlanName": { + "value": "" + }, + "newAppServicePlanLocation": { + "value": "West US" + }, + "newAppServicePlanSku": { + "value": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + } + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "appSecret": { + "value": "" + }, + "tenantId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/readme.md b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/readme.md new file mode 100644 index 000000000..19d77be80 --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/readme.md @@ -0,0 +1,48 @@ +# Usage +BotApp must be deployed prior to AzureBot. + +### Command line: +`az login`
+`az deployment group create --resource-group --template-file --parameters @` + +# parameters-for-template-BotApp-with-rg: + +- **appServiceName**:(required) The Name of the Bot App Service. + +- (choose an existingAppServicePlan or create a new AppServicePlan) + - **existingAppServicePlanName**: The name of the App Service Plan. + - **existingAppServicePlanLocation**: The location of the App Service Plan. + - **newAppServicePlanName**: The name of the App Service Plan. + - **newAppServicePlanLocation**: The location of the App Service Plan. + - **newAppServicePlanSku**: The SKU of the App Service Plan. Defaults to Standard values. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** + +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. + +- **appSecret**:(required for MultiTenant and SingleTenant) Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. + +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. + +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. + +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource + + + +# parameters-for-template-AzureBot-with-rg: + +- **azureBotId**:(required) The globally unique and immutable bot ID. +- **azureBotSku**: The pricing tier of the Bot Service Registration. **Allowed values are: F0, S1(default)**. +- **azureBotRegion**: Specifies the location of the new AzureBot. **Allowed values are: global(default), westeurope**. +- **botEndpoint**: Use to handle client messages, Such as https://.azurewebsites.net/api/messages. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-AzureBot-with-rg.json b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-AzureBot-with-rg.json new file mode 100644 index 000000000..b2b686dcc --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-AzureBot-with-rg.json @@ -0,0 +1,121 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "azureBotId": { + "type": "string", + "metadata": { + "description": "The globally unique and immutable bot ID." + } + }, + "azureBotSku": { + "type": "string", + "defaultValue": "S1", + "metadata": { + "description": "The pricing tier of the Bot Service Registration. Allowed values are: F0, S1(default)." + } + }, + "azureBotRegion": { + "type": "string", + "defaultValue": "global", + "metadata": { + "description": "Specifies the location of the new AzureBot. Allowed values are: global(default), westeurope." + } + }, + "botEndpoint": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Use to handle client messages, Such as https://.azurewebsites.net/api/messages." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant", + "UserAssignedMSI" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "UMSIName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication." + } + }, + "UMSIResourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + } + }, + "variables": { + "botEndpoint": "[if(empty(parameters('botEndpoint')), concat('https://', parameters('azureBotId'), '.azurewebsites.net/api/messages'), parameters('botEndpoint'))]", + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "msiResourceId": "" + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "msiResourceId": "" + }, + "UserAssignedMSI": { + "tenantId": "[variables('tenantId')]", + "msiResourceId": "[variables('msiResourceId')]" + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]", + "msiResourceId": "[variables('appTypeDef')[parameters('appType')].msiResourceId]" + } + }, + "resources": [ + { + "apiVersion": "2021-05-01-preview", + "type": "Microsoft.BotService/botServices", + "name": "[parameters('azureBotId')]", + "location": "[parameters('azureBotRegion')]", + "kind": "azurebot", + "sku": { + "name": "[parameters('azureBotSku')]" + }, + "properties": { + "name": "[parameters('azureBotId')]", + "displayName": "[parameters('azureBotId')]", + "iconUrl": "https://docs.botframework.com/static/devportal/client/images/bot-framework-default.png", + "endpoint": "[variables('botEndpoint')]", + "msaAppId": "[parameters('appId')]", + "msaAppTenantId": "[variables('appType').tenantId]", + "msaAppMSIResourceId": "[variables('appType').msiResourceId]", + "msaAppType": "[parameters('appType')]", + "luisAppIds": [], + "schemaTransformationVersion": "1.3", + "isCmekEnabled": false, + "isIsolated": false + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-BotApp-with-rg.json b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-BotApp-with-rg.json new file mode 100644 index 000000000..71425ee9a --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-BotApp-with-rg.json @@ -0,0 +1,278 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "appServiceName": { + "type": "string", + "metadata": { + "description": "The globally unique name of the Web App." + } + }, + "existingAppServicePlanName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Name of the existing App Service Plan used to create the Web App for the bot." + } + }, + "existingAppServicePlanLocation": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "newAppServicePlanName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The name of the new App Service Plan." + } + }, + "newAppServicePlanLocation": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "newAppServicePlanSku": { + "type": "object", + "defaultValue": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + }, + "metadata": { + "description": "The SKU of the App Service Plan. Defaults to Standard values." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant", + "UserAssignedMSI" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "appSecret": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. Required for MultiTenant and SingleTenant app types. Defaults to \"\"." + } + }, + "UMSIName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication. Defaults to \"\"." + } + }, + "UMSIResourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. Defaults to \"\"." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + } + }, + "variables": { + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "useExistingServicePlan": "[not(empty(parameters('existingAppServicePlanName')))]", + "servicePlanName": "[if(variables('useExistingServicePlan'), parameters('existingAppServicePlanName'), parameters('newAppServicePlanName'))]", + "servicePlanLocation": "[if(variables('useExistingServicePlan'), parameters('existingAppServicePlanLocation'), parameters('newAppServicePlanLocation'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "identity": { "type": "None" } + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "identity": { "type": "None" } + }, + "UserAssignedMSI": { + "tenantId": "[variables('tenantId')]", + "identity": { + "type": "UserAssigned", + "userAssignedIdentities": { + "[variables('msiResourceId')]": {} + } + } + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]", + "identity": "[variables('appTypeDef')[parameters('appType')].identity]" + } + }, + "resources": [ + { + "comments": "Create a new App Service Plan if no existing App Service Plan name was passed in.", + "type": "Microsoft.Web/serverfarms", + "condition": "[not(variables('useExistingServicePlan'))]", + "name": "[variables('servicePlanName')]", + "apiVersion": "2018-02-01", + "location": "[parameters('newAppServicePlanLocation')]", + "sku": "[parameters('newAppServicePlanSku')]", + "kind": "linux", + "properties": { + "name": "[variables('servicePlanName')]", + "perSiteScaling": false, + "reserved": true, + "targetWorkerCount": 0, + "targetWorkerSizeId": 0 + } + }, + { + "comments": "Create a Web App using an App Service Plan", + "type": "Microsoft.Web/sites", + "apiVersion": "2015-08-01", + "location": "[variables('servicePlanLocation')]", + "kind": "app,linux", + "dependsOn": [ + "[resourceId('Microsoft.Web/serverfarms', variables('servicePlanName'))]" + ], + "name": "[parameters('appServiceName')]", + "identity": "[variables('appType').identity]", + "properties": { + "name": "[parameters('appServiceName')]", + "enabled": true, + "hostNameSslStates": [ + { + "name": "[concat(parameters('appServiceName'), '.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Standard" + }, + { + "name": "[concat(parameters('appServiceName'), '.scm.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Repository" + } + ], + "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('servicePlanName'))]", + "reserved": true, + "scmSiteAlsoStopped": false, + "clientAffinityEnabled": false, + "clientCertEnabled": false, + "hostNamesDisabled": false, + "containerSize": 0, + "dailyMemoryTimeQuota": 0, + "httpsOnly": false, + "siteConfig": { + "appSettings": [ + { + "name": "SCM_DO_BUILD_DURING_DEPLOYMENT", + "value": "true" + }, + { + "name": "MicrosoftAppType", + "value": "[parameters('appType')]" + }, + { + "name": "MicrosoftAppId", + "value": "[parameters('appId')]" + }, + { + "name": "MicrosoftAppPassword", + "value": "[parameters('appSecret')]" + }, + { + "name": "MicrosoftAppTenantId", + "value": "[variables('appType').tenantId]" + } + ], + "cors": { + "allowedOrigins": [ + "https://botservice.hosting.portal.azure.net", + "https://hosting.onecloud.azure-test.net/" + ] + } + } + } + }, + { + "type": "Microsoft.Web/sites/config", + "apiVersion": "2016-08-01", + "name": "[concat(parameters('appServiceName'), '/web')]", + "location": "[variables('servicePlanLocation')]", + "dependsOn": [ + "[resourceId('Microsoft.Web/sites', parameters('appServiceName'))]" + ], + "properties": { + "numberOfWorkers": 1, + "defaultDocuments": [ + "Default.htm", + "Default.html", + "Default.asp", + "index.htm", + "index.html", + "iisstart.htm", + "default.aspx", + "index.php", + "hostingstart.html" + ], + "netFrameworkVersion": "v4.0", + "phpVersion": "", + "pythonVersion": "", + "nodeVersion": "", + "linuxFxVersion": "PYTHON|3.9", + "requestTracingEnabled": false, + "remoteDebuggingEnabled": false, + "remoteDebuggingVersion": "VS2022", + "httpLoggingEnabled": true, + "logsDirectorySizeLimit": 35, + "detailedErrorLoggingEnabled": false, + "publishingUsername": "[concat('$', parameters('appServiceName'))]", + "scmType": "None", + "use32BitWorkerProcess": true, + "webSocketsEnabled": false, + "alwaysOn": false, + "appCommandLine": "gunicorn --bind 0.0.0.0 --worker-class aiohttp.worker.GunicornWebWorker --timeout 600 app:APP", + "managedPipelineMode": "Integrated", + "virtualApplications": [ + { + "virtualPath": "/", + "physicalPath": "site\\wwwroot", + "preloadEnabled": false, + "virtualDirectories": null + } + ], + "winAuthAdminState": 0, + "winAuthTenantState": 0, + "customAppPoolIdentityAdminState": false, + "customAppPoolIdentityTenantState": false, + "loadBalancing": "LeastRequests", + "routingRules": [], + "experiments": { + "rampUpRules": [] + }, + "autoHealEnabled": false, + "vnetName": "", + "minTlsVersion": "1.2", + "ftpsState": "Disabled", + "reservedInstanceCount": 0 + } + } + ] +} diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-AzureBot-new-rg.json b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-AzureBot-new-rg.json new file mode 100644 index 000000000..e51036f85 --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-AzureBot-new-rg.json @@ -0,0 +1,39 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "value": "" + }, + "groupLocation": { + "value": "" + }, + "azureBotId": { + "value": "" + }, + "azureBotSku": { + "value": "S1" + }, + "azureBotRegion": { + "value": "global" + }, + "botEndpoint": { + "value": "" + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + }, + "tenantId": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-BotApp-new-rg.json b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-BotApp-new-rg.json new file mode 100644 index 000000000..de2dba051 --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-BotApp-new-rg.json @@ -0,0 +1,48 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "value": "" + }, + "groupLocation": { + "value": "" + }, + "appServiceName": { + "value": "" + }, + "appServicePlanName": { + "value": "" + }, + "appServicePlanLocation": { + "value": "" + }, + "appServicePlanSku": { + "value": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + } + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "appSecret": { + "value": "" + }, + "tenantId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/readme.md b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/readme.md new file mode 100644 index 000000000..4c752364b --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/readme.md @@ -0,0 +1,45 @@ +# Usage +BotApp must be deployed prior to AzureBot. + +### Command line: +`az login`
+`az deployment sub create --template-file --location --parameters @` + +# parameters-for-template-BotApp-new-rg: + +- **groupName**:(required) Specifies the name of the new Resource Group. +- **groupLocation**:(required) Specifies the location of the new Resource Group. + +- **appServiceName**:(required) The location of the App Service Plan. +- **appServicePlanName**:(required) The name of the App Service Plan. +- **appServicePlanLocation**: The location of the App Service Plan. Defaults to use groupLocation. +- **appServicePlanSku**: The SKU of the App Service Plan. Defaults to Standard values. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. +- **appSecret**:(required for MultiTenant and SingleTenant) Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource + + + +# parameters-for-template-AzureBot-new-rg: + +- **groupName**:(required) Specifies the name of the new Resource Group. +- **groupLocation**:(required) Specifies the location of the new Resource Group. + +- **azureBotId**:(required) The globally unique and immutable bot ID. Also used to configure the displayName of the bot, which is mutable. +- **azureBotSku**: The pricing tier of the Bot Service Registration. **Allowed values are: F0, S1(default)**. +- **azureBotRegion**: Specifies the location of the new AzureBot. **Allowed values are: global(default), westeurope**. +- **botEndpoint**: Use to handle client messages, Such as https://.azurewebsites.net/api/messages. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-AzureBot-new-rg.json b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-AzureBot-new-rg.json new file mode 100644 index 000000000..63fbf970d --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-AzureBot-new-rg.json @@ -0,0 +1,139 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "type": "string", + "metadata": { + "description": "Specifies the name of the Resource Group." + } + }, + "groupLocation": { + "type": "string", + "metadata": { + "description": "Specifies the location of the Resource Group." + } + }, + "azureBotId": { + "type": "string", + "metadata": { + "description": "The globally unique and immutable bot ID." + } + }, + "azureBotSku": { + "type": "string", + "defaultValue": "S1", + "metadata": { + "description": "The pricing tier of the Bot Service Registration. Acceptable values are F0 and S1." + } + }, + "azureBotRegion": { + "type": "string", + "defaultValue": "global", + "metadata": { + "description": "" + } + }, + "botEndpoint": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Use to handle client messages, Such as https://.azurewebsites.net/api/messages." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + } + }, + "variables": { + "botEndpoint": "[if(empty(parameters('botEndpoint')), concat('https://', parameters('azureBotId'), '.azurewebsites.net/api/messages'), parameters('botEndpoint'))]", + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "msiResourceId": "" + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "msiResourceId": "" + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]" + } + }, + "resources": [ + { + "name": "[parameters('groupName')]", + "type": "Microsoft.Resources/resourceGroups", + "apiVersion": "2018-05-01", + "location": "[parameters('groupLocation')]", + "properties": {} + }, + { + "type": "Microsoft.Resources/deployments", + "apiVersion": "2018-05-01", + "name": "storageDeployment", + "resourceGroup": "[parameters('groupName')]", + "dependsOn": [ + "[resourceId('Microsoft.Resources/resourceGroups/', parameters('groupName'))]" + ], + "properties": { + "mode": "Incremental", + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": {}, + "variables": {}, + "resources": [ + { + "apiVersion": "2021-03-01", + "type": "Microsoft.BotService/botServices", + "name": "[parameters('azureBotId')]", + "location": "[parameters('azureBotRegion')]", + "kind": "azurebot", + "sku": { + "name": "[parameters('azureBotSku')]" + }, + "properties": { + "name": "[parameters('azureBotId')]", + "displayName": "[parameters('azureBotId')]", + "iconUrl": "https://docs.botframework.com/static/devportal/client/images/bot-framework-default.png", + "endpoint": "[variables('botEndpoint')]", + "msaAppId": "[parameters('appId')]", + "msaAppTenantId": "[variables('appType').tenantId]", + "msaAppType": "[parameters('appType')]", + "luisAppIds": [], + "schemaTransformationVersion": "1.3", + "isCmekEnabled": false, + "isIsolated": false + } + } + ] + } + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-BotApp-new-rg.json b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-BotApp-new-rg.json new file mode 100644 index 000000000..381b57abf --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-BotApp-new-rg.json @@ -0,0 +1,294 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "type": "string", + "metadata": { + "description": "Specifies the name of the Resource Group." + } + }, + "groupLocation": { + "type": "string", + "metadata": { + "description": "Specifies the location of the Resource Group." + } + }, + "appServiceName": { + "type": "string", + "metadata": { + "description": "The globally unique name of the Web App." + } + }, + "appServicePlanName": { + "type": "string", + "metadata": { + "description": "The name of the App Service Plan." + } + }, + "appServicePlanLocation": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "appServicePlanSku": { + "type": "object", + "defaultValue": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + }, + "metadata": { + "description": "The SKU of the App Service Plan. Defaults to Standard values." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant", + "UserAssignedMSI" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "appSecret": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. Required for MultiTenant and SingleTenant app types. Defaults to \"\"." + } + }, + "UMSIName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication. Defaults to \"\"." + } + }, + "UMSIResourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. Defaults to \"\"." + } + } + }, + "variables": { + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "appServicePlanName": "[parameters('appServicePlanName')]", + "resourcesLocation": "[if(empty(parameters('appServicePlanLocation')), parameters('groupLocation'), parameters('appServicePlanLocation'))]", + "appServiceName": "[parameters('appServiceName')]", + "resourceGroupId": "[concat(subscription().id, '/resourceGroups/', parameters('groupName'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "identity": { "type": "None" } + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "identity": { "type": "None" } + }, + "UserAssignedMSI": { + "tenantId": "[variables('tenantId')]", + "identity": { + "type": "UserAssigned", + "userAssignedIdentities": { + "[variables('msiResourceId')]": {} + } + } + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]", + "identity": "[variables('appTypeDef')[parameters('appType')].identity]" + } + }, + "resources": [ + { + "name": "[parameters('groupName')]", + "type": "Microsoft.Resources/resourceGroups", + "apiVersion": "2018-05-01", + "location": "[parameters('groupLocation')]", + "properties": {} + }, + { + "type": "Microsoft.Resources/deployments", + "apiVersion": "2018-05-01", + "name": "storageDeployment", + "resourceGroup": "[parameters('groupName')]", + "dependsOn": [ + "[resourceId('Microsoft.Resources/resourceGroups/', parameters('groupName'))]" + ], + "properties": { + "mode": "Incremental", + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": {}, + "variables": {}, + "resources": [ + { + "comments": "Create a new App Service Plan", + "type": "Microsoft.Web/serverfarms", + "name": "[variables('appServicePlanName')]", + "apiVersion": "2018-02-01", + "location": "[variables('resourcesLocation')]", + "sku": "[parameters('appServicePlanSku')]", + "kind": "linux", + "properties": { + "name": "[variables('appServicePlanName')]", + "perSiteScaling": false, + "reserved": true, + "targetWorkerCount": 0, + "targetWorkerSizeId": 0 + } + }, + { + "comments": "Create a Web App using the new App Service Plan", + "type": "Microsoft.Web/sites", + "apiVersion": "2015-08-01", + "location": "[variables('resourcesLocation')]", + "kind": "app,linux", + "dependsOn": [ + "[concat(variables('resourceGroupId'), '/providers/Microsoft.Web/serverfarms/', variables('appServicePlanName'))]" + ], + "name": "[variables('appServiceName')]", + "identity": "[variables('appType').identity]", + "properties": { + "name": "[variables('appServiceName')]", + "hostNameSslStates": [ + { + "name": "[concat(parameters('appServiceName'), '.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Standard" + }, + { + "name": "[concat(parameters('appServiceName'), '.scm.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Repository" + } + ], + "serverFarmId": "[variables('appServicePlanName')]", + "siteConfig": { + "appSettings": [ + { + "name": "SCM_DO_BUILD_DURING_DEPLOYMENT", + "value": "true" + }, + { + "name": "MicrosoftAppType", + "value": "[parameters('appType')]" + }, { + "name": "MicrosoftAppId", + "value": "[parameters('appId')]" + }, + { + "name": "MicrosoftAppPassword", + "value": "[parameters('appSecret')]" + }, + { + "name": "MicrosoftAppTenantId", + "value": "[variables('appType').tenantId]" + } + ], + "cors": { + "allowedOrigins": [ + "https://botservice.hosting.portal.azure.net", + "https://hosting.onecloud.azure-test.net/" + ] + }, + "webSocketsEnabled": true + } + } + }, + { + "type": "Microsoft.Web/sites/config", + "apiVersion": "2016-08-01", + "name": "[concat(parameters('appServiceName'), '/web')]", + "location": "[variables('resourcesLocation')]", + "dependsOn": [ + "[concat(variables('resourceGroupId'), '/providers/Microsoft.Web/sites/', parameters('appServiceName'))]" + ], + "properties": { + "numberOfWorkers": 1, + "defaultDocuments": [ + "Default.htm", + "Default.html", + "Default.asp", + "index.htm", + "index.html", + "iisstart.htm", + "default.aspx", + "index.php", + "hostingstart.html" + ], + "netFrameworkVersion": "v4.0", + "phpVersion": "", + "pythonVersion": "", + "nodeVersion": "", + "linuxFxVersion": "PYTHON|3.9", + "requestTracingEnabled": false, + "remoteDebuggingEnabled": false, + "remoteDebuggingVersion": "VS2017", + "httpLoggingEnabled": true, + "logsDirectorySizeLimit": 35, + "detailedErrorLoggingEnabled": false, + "publishingUsername": "[concat('$', parameters('appServiceName'))]", + "scmType": "None", + "use32BitWorkerProcess": true, + "webSocketsEnabled": false, + "alwaysOn": false, + "appCommandLine": "gunicorn --bind 0.0.0.0 --worker-class aiohttp.worker.GunicornWebWorker --timeout 600 app:APP", + "managedPipelineMode": "Integrated", + "virtualApplications": [ + { + "virtualPath": "/", + "physicalPath": "site\\wwwroot", + "preloadEnabled": false, + "virtualDirectories": null + } + ], + "winAuthAdminState": 0, + "winAuthTenantState": 0, + "customAppPoolIdentityAdminState": false, + "customAppPoolIdentityTenantState": false, + "loadBalancing": "LeastRequests", + "routingRules": [], + "experiments": { + "rampUpRules": [] + }, + "autoHealEnabled": false, + "vnetName": "", + "minTlsVersion": "1.2", + "ftpsState": "Disabled", + "reservedInstanceCount": 0 + } + } + ], + "outputs": {} + } + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/echo/{{cookiecutter.bot_name}}/requirements.txt b/generators/app/templates/echo/{{cookiecutter.bot_name}}/requirements.txt new file mode 100644 index 000000000..2b2e3ab57 --- /dev/null +++ b/generators/app/templates/echo/{{cookiecutter.bot_name}}/requirements.txt @@ -0,0 +1 @@ +botbuilder-integration-aiohttp>=4.15.0 diff --git a/generators/app/templates/empty/cookiecutter.json b/generators/app/templates/empty/cookiecutter.json new file mode 100644 index 000000000..4a14b6ade --- /dev/null +++ b/generators/app/templates/empty/cookiecutter.json @@ -0,0 +1,4 @@ +{ + "bot_name": "my_chat_bot", + "bot_description": "Demonstrate the core capabilities of the Microsoft Bot Framework" +} \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/.pylintrc b/generators/app/templates/empty/{{cookiecutter.bot_name}}/.pylintrc new file mode 100644 index 000000000..1baee5edb --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/.pylintrc @@ -0,0 +1,497 @@ +[MASTER] + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-whitelist= + +# Add files or directories to the blacklist. They should be base names, not +# paths. +ignore= + +# Add files or directories matching the regex patterns to the blacklist. The +# regex matches against base names, not paths. +ignore-patterns= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use. +jobs=1 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=100 + +# List of plugins (as comma separated values of python modules names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Specify a configuration file. +#rcfile= + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. +confidence= + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=missing-docstring, + too-few-public-methods, + bad-continuation, + no-self-use, + duplicate-code, + broad-except + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[REPORTS] + +# Python expression which should return a note less than 10 (10 is the highest +# note). You have access to the variables errors warning, statement which +# respectively contain the number of errors / warnings messages and the total +# number of statements analyzed. This is used by the global evaluation report +# (RP0004). +evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +#msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +output-format=text + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit + + +[LOGGING] + +# Format style used to check logging format string. `old` means using % +# formatting, while `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. Available dictionaries: none. To make it working +# install python-enchant package.. +spelling-dict= + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to indicated private dictionary in +# --spelling-private-dict-file option instead of raising a message. +spelling-store-unknown-words=no + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether missing members accessed in mixin class should be ignored. A +# mixin class is detected if its name ends with "mixin" (case insensitive). +ignore-mixin-members=yes + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis. It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=120 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# List of optional constructs for which whitespace checking is disabled. `dict- +# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. +# `trailing-comma` allows a space between comma and closing bracket: (a, ). +# `empty-line` allows space-only lines. +no-space-check=trailing-comma, + dict-separator + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[SIMILARITIES] + +# Ignore comments when computing similarities. +ignore-comments=yes + +# Ignore docstrings when computing similarities. +ignore-docstrings=yes + +# Ignore imports when computing similarities. +ignore-imports=no + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. +#class-attribute-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _ + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. +#variable-rgx= + + +[STRING] + +# This flag controls whether the implicit-str-concat-in-sequence should +# generate a warning on implicit string concatenation in sequences defined over +# several lines. +check-str-concat-over-line-jumps=no + + +[IMPORTS] + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules=optparse,tkinter.tix + +# Create a graph of external dependencies in the given file (report RP0402 must +# not be disabled). +ext-import-graph= + +# Create a graph of every (i.e. internal and external) dependencies in the +# given file (report RP0402 must not be disabled). +import-graph= + +# Create a graph of internal dependencies in the given file (report RP0402 must +# not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + + +[CLASSES] + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict, + _fields, + _replace, + _source, + _make + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=cls + + +[DESIGN] + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement. +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=15 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. Defaults to +# "BaseException, Exception". +overgeneral-exceptions=BaseException, + Exception diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/README.md b/generators/app/templates/empty/{{cookiecutter.bot_name}}/README.md new file mode 100644 index 000000000..cc26d0f4d --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/README.md @@ -0,0 +1,42 @@ +# {{cookiecutter.bot_name}} + +{{cookiecutter.bot_description}} + +This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. + +## Prerequisites + +This sample **requires** prerequisites in order to run. + +### Install Python 3.6 + +## Running the sample +- Run `pip install -r requirements.txt` to install all dependencies +- Run `python app.py` + + +## Testing the bot using Bot Framework Emulator + +[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. + +- Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) + +### Connect to the bot using Bot Framework Emulator + +- Launch Bot Framework Emulator +- Enter a Bot URL of `http://localhost:3978/api/messages` + + +## Further reading + +- [Bot Framework Documentation](https://docs.botframework.com) +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) +- [Dialogs](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0) +- [Gathering Input Using Prompts](https://docs.microsoft.com/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=csharp) +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) +- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) +- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) +- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) +- [Azure Portal](https://portal.azure.com) +- [Language Understanding using LUIS](https://docs.microsoft.com/azure/cognitive-services/luis/) +- [Channels and Bot Connector Service](https://docs.microsoft.com/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/__init__.py b/generators/app/templates/empty/{{cookiecutter.bot_name}}/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/scenarios/task-module/app.py b/generators/app/templates/empty/{{cookiecutter.bot_name}}/app.py similarity index 72% rename from scenarios/task-module/app.py rename to generators/app/templates/empty/{{cookiecutter.bot_name}}/app.py index 4fa136703..29f91ab47 100644 --- a/scenarios/task-module/app.py +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/app.py @@ -1,93 +1,83 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import json -import sys -from datetime import datetime - -from aiohttp import web -from aiohttp.web import Request, Response, json_response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes -from bots import TaskModuleBot -from config import DefaultConfig - -CONFIG = DefaultConfig() - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error(context: TurnContext, error: Exception): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = on_error - -# Create the Bot -BOT = TaskModuleBot() - - -# Listen for incoming requests on /api/messages -async def messages(req: Request) -> Response: - # Main bot message handler. - if "application/json" in req.headers["Content-Type"]: - body = await req.json() - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - - try: - invoke_response = await ADAPTER.process_activity( - activity, auth_header, BOT.on_turn - ) - if invoke_response: - return json_response( - data=invoke_response.body, status=invoke_response.status - ) - return Response(status=201) - except PermissionError: - return Response(status=401) - except Exception: - return Response(status=500) - - -APP = web.Application() -APP.router.add_post("/api/messages", messages) - -if __name__ == "__main__": - try: - web.run_app(APP, host="localhost", port=CONFIG.PORT) - except Exception as error: - raise error +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import sys +import traceback +from datetime import datetime + +from http import HTTPStatus +from aiohttp import web +from aiohttp.web import Request, Response, json_response +from botbuilder.core import TurnContext +from botbuilder.core.integration import aiohttp_error_middleware +from botbuilder.integration.aiohttp import CloudAdapter, ConfigurationBotFrameworkAuthentication +from botbuilder.schema import Activity, ActivityTypes + +from bot import MyBot +from config import DefaultConfig + +CONFIG = DefaultConfig() + +# Create adapter. +# See https://aka.ms/about-bot-adapter to learn more about how bots work. +ADAPTER = CloudAdapter(ConfigurationBotFrameworkAuthentication(CONFIG)) + +# Catch-all for errors. +async def on_error(context: TurnContext, error: Exception): + # This check writes out errors to console log .vs. app insights. + # NOTE: In production environment, you should consider logging this to Azure + # application insights. + print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) + traceback.print_exc() + + # Send a message to the user + await context.send_activity("The bot encountered an error or bug.") + await context.send_activity( + "To continue to run this bot, please fix the bot source code." + ) + # Send a trace activity if we're talking to the Bot Framework Emulator + if context.activity.channel_id == "emulator": + # Create a trace activity that contains the error object + trace_activity = Activity( + label="TurnError", + name="on_turn_error Trace", + timestamp=datetime.utcnow(), + type=ActivityTypes.trace, + value=f"{error}", + value_type="https://www.botframework.com/schemas/error", + ) + # Send a trace activity, which will be displayed in Bot Framework Emulator + await context.send_activity(trace_activity) + + +ADAPTER.on_turn_error = on_error + +# Create the main dialog +BOT = MyBot() + + +# Listen for incoming requests on /api/messages +async def messages(req: Request) -> Response: + # Main bot message handler. + if "application/json" in req.headers["Content-Type"]: + body = await req.json() + else: + return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE) + + activity = Activity().deserialize(body) + auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" + + response = await ADAPTER.process_activity(auth_header, activity, BOT.on_turn) + if response: + return json_response(data=response.body, status=response.status) + return Response(status=HTTPStatus.OK) + + +APP = web.Application(middlewares=[aiohttp_error_middleware]) +APP.router.add_post("/api/messages", messages) + +if __name__ == "__main__": + try: + web.run_app(APP, host="localhost", port=CONFIG.PORT) + except Exception as error: + raise error diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/bot.py b/generators/app/templates/empty/{{cookiecutter.bot_name}}/bot.py new file mode 100644 index 000000000..598d68408 --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/bot.py @@ -0,0 +1,16 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import ActivityHandler, TurnContext +from botbuilder.schema import ChannelAccount + + +class MyBot(ActivityHandler): + async def on_members_added_activity( + self, + members_added: ChannelAccount, + turn_context: TurnContext + ): + for member_added in members_added: + if member_added.id != turn_context.activity.recipient.id: + await turn_context.send_activity("Hello world!") diff --git a/scenarios/action-based-messaging-extension-fetch-task/config.py b/generators/app/templates/empty/{{cookiecutter.bot_name}}/config.py similarity index 68% rename from scenarios/action-based-messaging-extension-fetch-task/config.py rename to generators/app/templates/empty/{{cookiecutter.bot_name}}/config.py index 6b5116fba..aad3896f4 100644 --- a/scenarios/action-based-messaging-extension-fetch-task/config.py +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/config.py @@ -1,13 +1,14 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") +#!/usr/bin/env python3 +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os + +class DefaultConfig: + """ Bot Configuration """ + + PORT = 3978 + APP_ID = os.environ.get("MicrosoftAppId", "") + APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") + APP_TYPE = os.environ.get("MicrosoftAppType", "MultiTenant") + APP_TENANTID = os.environ.get("MicrosoftAppTenantId", "") diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-AzureBot-with-rg.json b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-AzureBot-with-rg.json new file mode 100644 index 000000000..92b2e84c9 --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-AzureBot-with-rg.json @@ -0,0 +1,33 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "azureBotId": { + "value": "" + }, + "azureBotSku": { + "value": "S1" + }, + "azureBotRegion": { + "value": "global" + }, + "botEndpoint": { + "value": "" + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + }, + "tenantId": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-BotApp-with-rg.json b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-BotApp-with-rg.json new file mode 100644 index 000000000..eb36c03fb --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/parameters-for-template-BotApp-with-rg.json @@ -0,0 +1,48 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "appServiceName": { + "value": "" + }, + "existingAppServicePlanName": { + "value": "" + }, + "existingAppServicePlanLocation": { + "value": "" + }, + "newAppServicePlanName": { + "value": "" + }, + "newAppServicePlanLocation": { + "value": "West US" + }, + "newAppServicePlanSku": { + "value": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + } + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "appSecret": { + "value": "" + }, + "tenantId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/readme.md b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/readme.md new file mode 100644 index 000000000..19d77be80 --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/readme.md @@ -0,0 +1,48 @@ +# Usage +BotApp must be deployed prior to AzureBot. + +### Command line: +`az login`
+`az deployment group create --resource-group --template-file --parameters @` + +# parameters-for-template-BotApp-with-rg: + +- **appServiceName**:(required) The Name of the Bot App Service. + +- (choose an existingAppServicePlan or create a new AppServicePlan) + - **existingAppServicePlanName**: The name of the App Service Plan. + - **existingAppServicePlanLocation**: The location of the App Service Plan. + - **newAppServicePlanName**: The name of the App Service Plan. + - **newAppServicePlanLocation**: The location of the App Service Plan. + - **newAppServicePlanSku**: The SKU of the App Service Plan. Defaults to Standard values. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** + +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. + +- **appSecret**:(required for MultiTenant and SingleTenant) Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. + +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. + +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. + +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource + + + +# parameters-for-template-AzureBot-with-rg: + +- **azureBotId**:(required) The globally unique and immutable bot ID. +- **azureBotSku**: The pricing tier of the Bot Service Registration. **Allowed values are: F0, S1(default)**. +- **azureBotRegion**: Specifies the location of the new AzureBot. **Allowed values are: global(default), westeurope**. +- **botEndpoint**: Use to handle client messages, Such as https://.azurewebsites.net/api/messages. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-AzureBot-with-rg.json b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-AzureBot-with-rg.json new file mode 100644 index 000000000..b2b686dcc --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-AzureBot-with-rg.json @@ -0,0 +1,121 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "azureBotId": { + "type": "string", + "metadata": { + "description": "The globally unique and immutable bot ID." + } + }, + "azureBotSku": { + "type": "string", + "defaultValue": "S1", + "metadata": { + "description": "The pricing tier of the Bot Service Registration. Allowed values are: F0, S1(default)." + } + }, + "azureBotRegion": { + "type": "string", + "defaultValue": "global", + "metadata": { + "description": "Specifies the location of the new AzureBot. Allowed values are: global(default), westeurope." + } + }, + "botEndpoint": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Use to handle client messages, Such as https://.azurewebsites.net/api/messages." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant", + "UserAssignedMSI" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "UMSIName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication." + } + }, + "UMSIResourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + } + }, + "variables": { + "botEndpoint": "[if(empty(parameters('botEndpoint')), concat('https://', parameters('azureBotId'), '.azurewebsites.net/api/messages'), parameters('botEndpoint'))]", + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "msiResourceId": "" + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "msiResourceId": "" + }, + "UserAssignedMSI": { + "tenantId": "[variables('tenantId')]", + "msiResourceId": "[variables('msiResourceId')]" + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]", + "msiResourceId": "[variables('appTypeDef')[parameters('appType')].msiResourceId]" + } + }, + "resources": [ + { + "apiVersion": "2021-05-01-preview", + "type": "Microsoft.BotService/botServices", + "name": "[parameters('azureBotId')]", + "location": "[parameters('azureBotRegion')]", + "kind": "azurebot", + "sku": { + "name": "[parameters('azureBotSku')]" + }, + "properties": { + "name": "[parameters('azureBotId')]", + "displayName": "[parameters('azureBotId')]", + "iconUrl": "https://docs.botframework.com/static/devportal/client/images/bot-framework-default.png", + "endpoint": "[variables('botEndpoint')]", + "msaAppId": "[parameters('appId')]", + "msaAppTenantId": "[variables('appType').tenantId]", + "msaAppMSIResourceId": "[variables('appType').msiResourceId]", + "msaAppType": "[parameters('appType')]", + "luisAppIds": [], + "schemaTransformationVersion": "1.3", + "isCmekEnabled": false, + "isIsolated": false + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-BotApp-with-rg.json b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-BotApp-with-rg.json new file mode 100644 index 000000000..979ec221b --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployUseExistResourceGroup/template-BotApp-with-rg.json @@ -0,0 +1,278 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "appServiceName": { + "type": "string", + "metadata": { + "description": "The globally unique name of the Web App." + } + }, + "existingAppServicePlanName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Name of the existing App Service Plan used to create the Web App for the bot." + } + }, + "existingAppServicePlanLocation": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "newAppServicePlanName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The name of the new App Service Plan." + } + }, + "newAppServicePlanLocation": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "newAppServicePlanSku": { + "type": "object", + "defaultValue": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + }, + "metadata": { + "description": "The SKU of the App Service Plan. Defaults to Standard values." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant", + "UserAssignedMSI" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "appSecret": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. Required for MultiTenant and SingleTenant app types. Defaults to \"\"." + } + }, + "UMSIName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication. Defaults to \"\"." + } + }, + "UMSIResourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. Defaults to \"\"." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + } + }, + "variables": { + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "useExistingServicePlan": "[not(empty(parameters('existingAppServicePlanName')))]", + "servicePlanName": "[if(variables('useExistingServicePlan'), parameters('existingAppServicePlanName'), parameters('newAppServicePlanName'))]", + "servicePlanLocation": "[if(variables('useExistingServicePlan'), parameters('existingAppServicePlanLocation'), parameters('newAppServicePlanLocation'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "identity": { "type": "None" } + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "identity": { "type": "None" } + }, + "UserAssignedMSI": { + "tenantId": "[variables('tenantId')]", + "identity": { + "type": "UserAssigned", + "userAssignedIdentities": { + "[variables('msiResourceId')]": {} + } + } + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]", + "identity": "[variables('appTypeDef')[parameters('appType')].identity]" + } + }, + "resources": [ + { + "comments": "Create a new App Service Plan if no existing App Service Plan name was passed in.", + "type": "Microsoft.Web/serverfarms", + "condition": "[not(variables('useExistingServicePlan'))]", + "name": "[variables('servicePlanName')]", + "apiVersion": "2018-02-01", + "location": "[parameters('newAppServicePlanLocation')]", + "sku": "[parameters('newAppServicePlanSku')]", + "kind": "linux", + "properties": { + "name": "[variables('servicePlanName')]", + "perSiteScaling": false, + "reserved": true, + "targetWorkerCount": 0, + "targetWorkerSizeId": 0 + } + }, + { + "comments": "Create a Web App using an App Service Plan", + "type": "Microsoft.Web/sites", + "apiVersion": "2015-08-01", + "location": "[variables('servicePlanLocation')]", + "kind": "app,linux", + "dependsOn": [ + "[resourceId('Microsoft.Web/serverfarms', variables('servicePlanName'))]" + ], + "name": "[parameters('appServiceName')]", + "identity": "[variables('appType').identity]", + "properties": { + "name": "[parameters('appServiceName')]", + "enabled": true, + "hostNameSslStates": [ + { + "name": "[concat(parameters('appServiceName'), '.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Standard" + }, + { + "name": "[concat(parameters('appServiceName'), '.scm.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Repository" + } + ], + "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('servicePlanName'))]", + "reserved": true, + "scmSiteAlsoStopped": false, + "clientAffinityEnabled": false, + "clientCertEnabled": false, + "hostNamesDisabled": false, + "containerSize": 0, + "dailyMemoryTimeQuota": 0, + "httpsOnly": false, + "siteConfig": { + "appSettings": [ + { + "name": "SCM_DO_BUILD_DURING_DEPLOYMENT", + "value": "true" + }, + { + "name": "MicrosoftAppType", + "value": "[parameters('appType')]" + }, + { + "name": "MicrosoftAppId", + "value": "[parameters('appId')]" + }, + { + "name": "MicrosoftAppPassword", + "value": "[parameters('appSecret')]" + }, + { + "name": "MicrosoftAppTenantId", + "value": "[variables('appType').tenantId]" + } + ], + "cors": { + "allowedOrigins": [ + "https://botservice.hosting.portal.azure.net", + "https://hosting.onecloud.azure-test.net/" + ] + } + } + } + }, + { + "type": "Microsoft.Web/sites/config", + "apiVersion": "2016-08-01", + "name": "[concat(parameters('appServiceName'), '/web')]", + "location": "[variables('servicePlanLocation')]", + "dependsOn": [ + "[resourceId('Microsoft.Web/sites', parameters('appServiceName'))]" + ], + "properties": { + "numberOfWorkers": 1, + "defaultDocuments": [ + "Default.htm", + "Default.html", + "Default.asp", + "index.htm", + "index.html", + "iisstart.htm", + "default.aspx", + "index.php", + "hostingstart.html" + ], + "netFrameworkVersion": "v4.0", + "phpVersion": "", + "pythonVersion": "", + "nodeVersion": "", + "linuxFxVersion": "PYTHON|3.9", + "requestTracingEnabled": false, + "remoteDebuggingEnabled": false, + "remoteDebuggingVersion": "VS2017", + "httpLoggingEnabled": true, + "logsDirectorySizeLimit": 35, + "detailedErrorLoggingEnabled": false, + "publishingUsername": "[concat('$', parameters('appServiceName'))]", + "scmType": "None", + "use32BitWorkerProcess": true, + "webSocketsEnabled": false, + "alwaysOn": false, + "appCommandLine": "gunicorn --bind 0.0.0.0 --worker-class aiohttp.worker.GunicornWebWorker --timeout 600 app:APP", + "managedPipelineMode": "Integrated", + "virtualApplications": [ + { + "virtualPath": "/", + "physicalPath": "site\\wwwroot", + "preloadEnabled": false, + "virtualDirectories": null + } + ], + "winAuthAdminState": 0, + "winAuthTenantState": 0, + "customAppPoolIdentityAdminState": false, + "customAppPoolIdentityTenantState": false, + "loadBalancing": "LeastRequests", + "routingRules": [], + "experiments": { + "rampUpRules": [] + }, + "autoHealEnabled": false, + "vnetName": "", + "minTlsVersion": "1.2", + "ftpsState": "Disabled", + "reservedInstanceCount": 0 + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-AzureBot-new-rg.json b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-AzureBot-new-rg.json new file mode 100644 index 000000000..e51036f85 --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-AzureBot-new-rg.json @@ -0,0 +1,39 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "value": "" + }, + "groupLocation": { + "value": "" + }, + "azureBotId": { + "value": "" + }, + "azureBotSku": { + "value": "S1" + }, + "azureBotRegion": { + "value": "global" + }, + "botEndpoint": { + "value": "" + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + }, + "tenantId": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-BotApp-new-rg.json b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-BotApp-new-rg.json new file mode 100644 index 000000000..de2dba051 --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/parameters-for-template-BotApp-new-rg.json @@ -0,0 +1,48 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "value": "" + }, + "groupLocation": { + "value": "" + }, + "appServiceName": { + "value": "" + }, + "appServicePlanName": { + "value": "" + }, + "appServicePlanLocation": { + "value": "" + }, + "appServicePlanSku": { + "value": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + } + }, + "appType": { + "value": "MultiTenant" + }, + "appId": { + "value": "" + }, + "appSecret": { + "value": "" + }, + "tenantId": { + "value": "" + }, + "UMSIName": { + "value": "" + }, + "UMSIResourceGroupName": { + "value": "" + } + } +} \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/readme.md b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/readme.md new file mode 100644 index 000000000..4c752364b --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/readme.md @@ -0,0 +1,45 @@ +# Usage +BotApp must be deployed prior to AzureBot. + +### Command line: +`az login`
+`az deployment sub create --template-file --location --parameters @` + +# parameters-for-template-BotApp-new-rg: + +- **groupName**:(required) Specifies the name of the new Resource Group. +- **groupLocation**:(required) Specifies the location of the new Resource Group. + +- **appServiceName**:(required) The location of the App Service Plan. +- **appServicePlanName**:(required) The name of the App Service Plan. +- **appServicePlanLocation**: The location of the App Service Plan. Defaults to use groupLocation. +- **appServicePlanSku**: The SKU of the App Service Plan. Defaults to Standard values. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. +- **appSecret**:(required for MultiTenant and SingleTenant) Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource + + + +# parameters-for-template-AzureBot-new-rg: + +- **groupName**:(required) Specifies the name of the new Resource Group. +- **groupLocation**:(required) Specifies the location of the new Resource Group. + +- **azureBotId**:(required) The globally unique and immutable bot ID. Also used to configure the displayName of the bot, which is mutable. +- **azureBotSku**: The pricing tier of the Bot Service Registration. **Allowed values are: F0, S1(default)**. +- **azureBotRegion**: Specifies the location of the new AzureBot. **Allowed values are: global(default), westeurope**. +- **botEndpoint**: Use to handle client messages, Such as https://.azurewebsites.net/api/messages. + +- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.** +- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings. +- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication. +- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. +- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to . + +MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-AzureBot-new-rg.json b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-AzureBot-new-rg.json new file mode 100644 index 000000000..63fbf970d --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-AzureBot-new-rg.json @@ -0,0 +1,139 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "type": "string", + "metadata": { + "description": "Specifies the name of the Resource Group." + } + }, + "groupLocation": { + "type": "string", + "metadata": { + "description": "Specifies the location of the Resource Group." + } + }, + "azureBotId": { + "type": "string", + "metadata": { + "description": "The globally unique and immutable bot ID." + } + }, + "azureBotSku": { + "type": "string", + "defaultValue": "S1", + "metadata": { + "description": "The pricing tier of the Bot Service Registration. Acceptable values are F0 and S1." + } + }, + "azureBotRegion": { + "type": "string", + "defaultValue": "global", + "metadata": { + "description": "" + } + }, + "botEndpoint": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Use to handle client messages, Such as https://.azurewebsites.net/api/messages." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + } + }, + "variables": { + "botEndpoint": "[if(empty(parameters('botEndpoint')), concat('https://', parameters('azureBotId'), '.azurewebsites.net/api/messages'), parameters('botEndpoint'))]", + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "msiResourceId": "" + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "msiResourceId": "" + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]" + } + }, + "resources": [ + { + "name": "[parameters('groupName')]", + "type": "Microsoft.Resources/resourceGroups", + "apiVersion": "2018-05-01", + "location": "[parameters('groupLocation')]", + "properties": {} + }, + { + "type": "Microsoft.Resources/deployments", + "apiVersion": "2018-05-01", + "name": "storageDeployment", + "resourceGroup": "[parameters('groupName')]", + "dependsOn": [ + "[resourceId('Microsoft.Resources/resourceGroups/', parameters('groupName'))]" + ], + "properties": { + "mode": "Incremental", + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": {}, + "variables": {}, + "resources": [ + { + "apiVersion": "2021-03-01", + "type": "Microsoft.BotService/botServices", + "name": "[parameters('azureBotId')]", + "location": "[parameters('azureBotRegion')]", + "kind": "azurebot", + "sku": { + "name": "[parameters('azureBotSku')]" + }, + "properties": { + "name": "[parameters('azureBotId')]", + "displayName": "[parameters('azureBotId')]", + "iconUrl": "https://docs.botframework.com/static/devportal/client/images/bot-framework-default.png", + "endpoint": "[variables('botEndpoint')]", + "msaAppId": "[parameters('appId')]", + "msaAppTenantId": "[variables('appType').tenantId]", + "msaAppType": "[parameters('appType')]", + "luisAppIds": [], + "schemaTransformationVersion": "1.3", + "isCmekEnabled": false, + "isIsolated": false + } + } + ] + } + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-BotApp-new-rg.json b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-BotApp-new-rg.json new file mode 100644 index 000000000..381b57abf --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/deploymentTemplates/deployWithNewResourceGroup/template-BotApp-new-rg.json @@ -0,0 +1,294 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "groupName": { + "type": "string", + "metadata": { + "description": "Specifies the name of the Resource Group." + } + }, + "groupLocation": { + "type": "string", + "metadata": { + "description": "Specifies the location of the Resource Group." + } + }, + "appServiceName": { + "type": "string", + "metadata": { + "description": "The globally unique name of the Web App." + } + }, + "appServicePlanName": { + "type": "string", + "metadata": { + "description": "The name of the App Service Plan." + } + }, + "appServicePlanLocation": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "appServicePlanSku": { + "type": "object", + "defaultValue": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + }, + "metadata": { + "description": "The SKU of the App Service Plan. Defaults to Standard values." + } + }, + "tenantId": { + "type": "string", + "defaultValue": "[subscription().tenantId]", + "metadata": { + "description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"." + } + }, + "appType": { + "type": "string", + "defaultValue": "MultiTenant", + "allowedValues": [ + "MultiTenant", + "SingleTenant", + "UserAssignedMSI" + ], + "metadata": { + "description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"." + } + }, + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "appSecret": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings. Required for MultiTenant and SingleTenant app types. Defaults to \"\"." + } + }, + "UMSIName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication. Defaults to \"\"." + } + }, + "UMSIResourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication. Defaults to \"\"." + } + } + }, + "variables": { + "tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]", + "appServicePlanName": "[parameters('appServicePlanName')]", + "resourcesLocation": "[if(empty(parameters('appServicePlanLocation')), parameters('groupLocation'), parameters('appServicePlanLocation'))]", + "appServiceName": "[parameters('appServiceName')]", + "resourceGroupId": "[concat(subscription().id, '/resourceGroups/', parameters('groupName'))]", + "msiResourceId": "[if(empty(parameters('UMSIName')), '', concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName')))]", + "appTypeDef": { + "MultiTenant": { + "tenantId": "", + "identity": { "type": "None" } + }, + "SingleTenant": { + "tenantId": "[variables('tenantId')]", + "identity": { "type": "None" } + }, + "UserAssignedMSI": { + "tenantId": "[variables('tenantId')]", + "identity": { + "type": "UserAssigned", + "userAssignedIdentities": { + "[variables('msiResourceId')]": {} + } + } + } + }, + "appType": { + "tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]", + "identity": "[variables('appTypeDef')[parameters('appType')].identity]" + } + }, + "resources": [ + { + "name": "[parameters('groupName')]", + "type": "Microsoft.Resources/resourceGroups", + "apiVersion": "2018-05-01", + "location": "[parameters('groupLocation')]", + "properties": {} + }, + { + "type": "Microsoft.Resources/deployments", + "apiVersion": "2018-05-01", + "name": "storageDeployment", + "resourceGroup": "[parameters('groupName')]", + "dependsOn": [ + "[resourceId('Microsoft.Resources/resourceGroups/', parameters('groupName'))]" + ], + "properties": { + "mode": "Incremental", + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": {}, + "variables": {}, + "resources": [ + { + "comments": "Create a new App Service Plan", + "type": "Microsoft.Web/serverfarms", + "name": "[variables('appServicePlanName')]", + "apiVersion": "2018-02-01", + "location": "[variables('resourcesLocation')]", + "sku": "[parameters('appServicePlanSku')]", + "kind": "linux", + "properties": { + "name": "[variables('appServicePlanName')]", + "perSiteScaling": false, + "reserved": true, + "targetWorkerCount": 0, + "targetWorkerSizeId": 0 + } + }, + { + "comments": "Create a Web App using the new App Service Plan", + "type": "Microsoft.Web/sites", + "apiVersion": "2015-08-01", + "location": "[variables('resourcesLocation')]", + "kind": "app,linux", + "dependsOn": [ + "[concat(variables('resourceGroupId'), '/providers/Microsoft.Web/serverfarms/', variables('appServicePlanName'))]" + ], + "name": "[variables('appServiceName')]", + "identity": "[variables('appType').identity]", + "properties": { + "name": "[variables('appServiceName')]", + "hostNameSslStates": [ + { + "name": "[concat(parameters('appServiceName'), '.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Standard" + }, + { + "name": "[concat(parameters('appServiceName'), '.scm.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Repository" + } + ], + "serverFarmId": "[variables('appServicePlanName')]", + "siteConfig": { + "appSettings": [ + { + "name": "SCM_DO_BUILD_DURING_DEPLOYMENT", + "value": "true" + }, + { + "name": "MicrosoftAppType", + "value": "[parameters('appType')]" + }, { + "name": "MicrosoftAppId", + "value": "[parameters('appId')]" + }, + { + "name": "MicrosoftAppPassword", + "value": "[parameters('appSecret')]" + }, + { + "name": "MicrosoftAppTenantId", + "value": "[variables('appType').tenantId]" + } + ], + "cors": { + "allowedOrigins": [ + "https://botservice.hosting.portal.azure.net", + "https://hosting.onecloud.azure-test.net/" + ] + }, + "webSocketsEnabled": true + } + } + }, + { + "type": "Microsoft.Web/sites/config", + "apiVersion": "2016-08-01", + "name": "[concat(parameters('appServiceName'), '/web')]", + "location": "[variables('resourcesLocation')]", + "dependsOn": [ + "[concat(variables('resourceGroupId'), '/providers/Microsoft.Web/sites/', parameters('appServiceName'))]" + ], + "properties": { + "numberOfWorkers": 1, + "defaultDocuments": [ + "Default.htm", + "Default.html", + "Default.asp", + "index.htm", + "index.html", + "iisstart.htm", + "default.aspx", + "index.php", + "hostingstart.html" + ], + "netFrameworkVersion": "v4.0", + "phpVersion": "", + "pythonVersion": "", + "nodeVersion": "", + "linuxFxVersion": "PYTHON|3.9", + "requestTracingEnabled": false, + "remoteDebuggingEnabled": false, + "remoteDebuggingVersion": "VS2017", + "httpLoggingEnabled": true, + "logsDirectorySizeLimit": 35, + "detailedErrorLoggingEnabled": false, + "publishingUsername": "[concat('$', parameters('appServiceName'))]", + "scmType": "None", + "use32BitWorkerProcess": true, + "webSocketsEnabled": false, + "alwaysOn": false, + "appCommandLine": "gunicorn --bind 0.0.0.0 --worker-class aiohttp.worker.GunicornWebWorker --timeout 600 app:APP", + "managedPipelineMode": "Integrated", + "virtualApplications": [ + { + "virtualPath": "/", + "physicalPath": "site\\wwwroot", + "preloadEnabled": false, + "virtualDirectories": null + } + ], + "winAuthAdminState": 0, + "winAuthTenantState": 0, + "customAppPoolIdentityAdminState": false, + "customAppPoolIdentityTenantState": false, + "loadBalancing": "LeastRequests", + "routingRules": [], + "experiments": { + "rampUpRules": [] + }, + "autoHealEnabled": false, + "vnetName": "", + "minTlsVersion": "1.2", + "ftpsState": "Disabled", + "reservedInstanceCount": 0 + } + } + ], + "outputs": {} + } + } + } + ] +} \ No newline at end of file diff --git a/generators/app/templates/empty/{{cookiecutter.bot_name}}/requirements.txt b/generators/app/templates/empty/{{cookiecutter.bot_name}}/requirements.txt new file mode 100644 index 000000000..2b2e3ab57 --- /dev/null +++ b/generators/app/templates/empty/{{cookiecutter.bot_name}}/requirements.txt @@ -0,0 +1 @@ +botbuilder-integration-aiohttp>=4.15.0 diff --git a/libraries/botbuilder-adapters-slack/README.rst b/libraries/botbuilder-adapters-slack/README.rst index a3813c8b3..9465f3997 100644 --- a/libraries/botbuilder-adapters-slack/README.rst +++ b/libraries/botbuilder-adapters-slack/README.rst @@ -1,14 +1,14 @@ -================================= +================================== BotBuilder-Adapters SDK for Python -================================= +================================== -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch -.. image:: https://badge.fury.io/py/botbuilder-dialogs.svg - :target: https://badge.fury.io/py/botbuilder-dialogs +.. image:: https://badge.fury.io/py/botbuilder-adapters-slack.svg + :target: https://badge.fury.io/py/botbuilder-adapters-slack :alt: Latest PyPI package version A dialog stack based conversation manager for Microsoft BotBuilder. @@ -18,7 +18,7 @@ How to Install .. code-block:: python - pip install botbuilder-dialogs + pip install botbuilder-adapters-slack Documentation/Wiki diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/__init__.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/__init__.py index 1ab395b75..079044720 100644 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/__init__.py +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/__init__.py @@ -6,7 +6,7 @@ # -------------------------------------------------------------------------- from .about import __version__ -from .slack_options import SlackAdapterOptions +from .slack_client_options import SlackClientOptions from .slack_client import SlackClient from .slack_adapter import SlackAdapter from .slack_payload import SlackPayload @@ -15,10 +15,11 @@ from .activity_resourceresponse import ActivityResourceResponse from .slack_request_body import SlackRequestBody from .slack_helper import SlackHelper +from .slack_adatper_options import SlackAdapterOptions __all__ = [ "__version__", - "SlackAdapterOptions", + "SlackClientOptions", "SlackClient", "SlackAdapter", "SlackPayload", @@ -27,4 +28,5 @@ "ActivityResourceResponse", "SlackRequestBody", "SlackHelper", + "SlackAdapterOptions", ] diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/about.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/about.py index 2babae85d..d0c18dbaa 100644 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/about.py +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/about.py @@ -5,7 +5,7 @@ __title__ = "botbuilder-adapters-slack" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_adapter.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_adapter.py index 2f1af54e8..26fc80b41 100644 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_adapter.py +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_adapter.py @@ -20,32 +20,38 @@ from .activity_resourceresponse import ActivityResourceResponse from .slack_client import SlackClient from .slack_helper import SlackHelper +from .slack_adatper_options import SlackAdapterOptions class SlackAdapter(BotAdapter, ABC): """ - BotAdapter that can handle incoming slack events. Incoming slack events are deserialized to an Activity - that is dispatch through the middleware and bot pipeline. + BotAdapter that can handle incoming Slack events. Incoming Slack events are deserialized to an Activity that is + dispatched through the middleware and bot pipeline. """ def __init__( self, client: SlackClient, on_turn_error: Callable[[TurnContext, Exception], Awaitable] = None, + options: SlackAdapterOptions = None, ): super().__init__(on_turn_error) self.slack_client = client self.slack_logged_in = False + self.options = options if options else SlackAdapterOptions() async def send_activities( self, context: TurnContext, activities: List[Activity] ) -> List[ResourceResponse]: """ - Standard BotBuilder adapter method to send a message from the bot to the messaging API. + Send a message from the bot to the messaging API. :param context: A TurnContext representing the current incoming message and environment. + :type context: :class:`botbuilder.core.TurnContext` :param activities: An array of outgoing activities to be sent back to the messaging API. + :type activities: :class:`typing.List[Activity]` :return: An array of ResourceResponse objects containing the IDs that Slack assigned to the sent messages. + :rtype: :class:`typing.List[ResourceResponse]` """ if not context: @@ -59,7 +65,7 @@ async def send_activities( if activity.type == ActivityTypes.message: message = SlackHelper.activity_to_slack(activity) - slack_response = await self.slack_client.post_message_to_slack(message) + slack_response = await self.slack_client.post_message(message) if slack_response and slack_response.status_code / 100 == 2: resource_response = ActivityResourceResponse( @@ -76,11 +82,14 @@ async def send_activities( async def update_activity(self, context: TurnContext, activity: Activity): """ - Standard BotBuilder adapter method to update a previous message with new content. + Update a previous message with new content. :param context: A TurnContext representing the current incoming message and environment. + :type context: :class:`botbuilder.core.TurnContext` :param activity: The updated activity in the form '{id: `id of activity to update`, ...}'. - :return: A resource response with the Id of the updated activity. + :type activity: :class:`botbuilder.schema.Activity` + :return: A resource response with the ID of the updated activity. + :rtype: :class:`botbuilder.schema.ResourceResponse` """ if not context: @@ -93,8 +102,10 @@ async def update_activity(self, context: TurnContext, activity: Activity): raise Exception("Activity.conversation is required") message = SlackHelper.activity_to_slack(activity) - results = await self.slack_client.update( - timestamp=message.ts, channel_id=message.channel, text=message.text, + results = await self.slack_client.chat_update( + ts=message.ts, + channel=message.channel, + text=message.text, ) if results.status_code / 100 != 2: @@ -106,11 +117,13 @@ async def delete_activity( self, context: TurnContext, reference: ConversationReference ): """ - Standard BotBuilder adapter method to delete a previous message. + Delete a previous message. :param context: A TurnContext representing the current incoming message and environment. - :param reference: An object in the form "{activityId: `id of message to delete`, - conversation: { id: `id of slack channel`}}". + :type context: :class:`botbuilder.core.TurnContext` + :param reference: An object in the form "{activityId: `id of message to delete`,conversation: { id: `id of Slack + channel`}}". + :type reference: :class:`botbuilder.schema.ConversationReference` """ if not context: @@ -122,27 +135,36 @@ async def delete_activity( if not context.activity.timestamp: raise Exception("Activity.timestamp is required") - await self.slack_client.delete_message( - channel_id=reference.channel_id, timestamp=context.activity.timestamp + await self.slack_client.chat_delete( + channel=reference.conversation.id, ts=reference.activity_id ) async def continue_conversation( self, reference: ConversationReference, callback: Callable, - bot_id: str = None, + bot_id: str = None, # pylint: disable=unused-argument claims_identity: ClaimsIdentity = None, - audience: str = None, + audience: str = None, # pylint: disable=unused-argument ): """ - Sends a proactive message to a conversation. Call this method to proactively send a message to a conversation. - Most _channels require a user to initiate a conversation with a bot before the bot can send activities - to the user. - :param bot_id: Unused for this override. + Send a proactive message to a conversation. + + .. remarks:: + + Most channels require a user to initiate a conversation with a bot before the bot can send activities to the + user. + :param reference: A reference to the conversation to continue. + :type reference: :class:`botbuilder.schema.ConversationReference` :param callback: The method to call for the resulting bot turn. + :type callback: :class:`typing.Callable` + :param bot_id: Unused for this override. + :type bot_id: str :param claims_identity: A ClaimsIdentity for the conversation. + :type claims_identity: :class:`botframework.connector.auth.ClaimsIdentity` :param audience: Unused for this override. + :type audience: str """ if not reference: @@ -170,10 +192,14 @@ async def process(self, req: Request, logic: Callable) -> Response: """ Accept an incoming webhook request and convert it into a TurnContext which can be processed by the bot's logic. - :param req: The aoihttp Request object - :param logic: The method to call for the resulting bot turn. - :return: The aoihttp Response + :param req: The aiohttp Request object. + :type req: :class:`aiohttp.web_request.Request` + :param logic: The method to call for the resulting bot turn. + :type logic: :class:`tying.Callable` + :return: The aiohttp Response. + :rtype: :class:`aiohttp.web_response.Response` """ + if not req: raise Exception("Request is required") @@ -182,15 +208,20 @@ async def process(self, req: Request, logic: Callable) -> Response: self.slack_logged_in = True body = await req.text() + + if ( + self.options.verify_incoming_requests + and not self.slack_client.verify_signature(req, body) + ): + return SlackHelper.response( + req, 401, "Rejected due to mismatched header signature" + ) + slack_body = SlackHelper.deserialize_body(req.content_type, body) if slack_body.type == "url_verification": return SlackHelper.response(req, 200, slack_body.challenge) - if not self.slack_client.verify_signature(req, body): - text = "Rejected due to mismatched header signature" - return SlackHelper.response(req, 401, text) - if ( not self.slack_client.options.slack_verification_token and slack_body.token != self.slack_client.options.slack_verification_token @@ -210,7 +241,9 @@ async def process(self, req: Request, logic: Callable) -> Response: slack_body, self.slack_client ) else: - raise Exception(f"Unknown Slack event type {slack_body.type}") + return SlackHelper.response( + req, 200, f"Unknown Slack event type {slack_body.type}" + ) context = TurnContext(self, activity) await self.run_pipeline(context, logic) diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_adatper_options.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_adatper_options.py new file mode 100644 index 000000000..6532f8e5f --- /dev/null +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_adatper_options.py @@ -0,0 +1,11 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +class SlackAdapterOptions: + """ + Class for defining implementation of the SlackAdapter Options. + """ + + def __init__(self): + self.verify_incoming_requests = True diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_client.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_client.py index 42fb96e81..dd19e9b2a 100644 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_client.py +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_client.py @@ -5,18 +5,18 @@ import hmac import json from io import IOBase -from typing import Union +from typing import List, Union import aiohttp from aiohttp.web_request import Request -from botbuilder.schema import Activity -from botbuilder.adapters.slack import SlackAdapterOptions -from botbuilder.adapters.slack.slack_message import SlackMessage - from slack.web.client import WebClient from slack.web.slack_response import SlackResponse +from botbuilder.schema import Activity +from botbuilder.adapters.slack.slack_client_options import SlackClientOptions +from botbuilder.adapters.slack.slack_message import SlackMessage + POST_MESSAGE_URL = "https://slack.com/api/chat.postMessage" POST_EPHEMERAL_MESSAGE_URL = "https://slack.com/api/chat.postEphemeral" @@ -26,7 +26,7 @@ class SlackClient(WebClient): Slack client that extends https://github.com/slackapi/python-slackclient. """ - def __init__(self, options: SlackAdapterOptions): + def __init__(self, options: SlackClientOptions): if not options or not options.slack_bot_token: raise Exception("SlackAdapterOptions and bot_token are required") @@ -116,7 +116,7 @@ async def files_list_ex( date_to: str = None, count: int = None, page: int = None, - types: [str] = None, + types: List[str] = None, ) -> SlackResponse: args = {} @@ -185,7 +185,7 @@ async def chat_post_ephemeral_ex( target_user: str, parse: str = None, link_names: bool = False, - attachments: [str] = None, # pylint: disable=unused-argument + attachments: List[str] = None, # pylint: disable=unused-argument as_user: bool = False, ) -> SlackResponse: args = { @@ -210,8 +210,8 @@ async def chat_post_message_ex( bot_name: str = None, parse: str = None, link_names: bool = False, - blocks: [str] = None, # pylint: disable=unused-argument - attachments: [str] = None, # pylint: disable=unused-argument + blocks: List[str] = None, # pylint: disable=unused-argument + attachments: List[str] = None, # pylint: disable=unused-argument unfurl_links: bool = False, icon_url: str = None, icon_emoji: str = None, @@ -328,7 +328,7 @@ async def chat_update_ex( bot_name: str = None, parse: str = None, link_names: bool = False, - attachments: [str] = None, # pylint: disable=unused-argument + attachments: List[str] = None, # pylint: disable=unused-argument as_user: bool = False, ): args = { @@ -353,7 +353,7 @@ async def files_upload_ex( self, file: Union[str, IOBase] = None, content: str = None, - channels: [str] = None, + channels: List[str] = None, title: str = None, initial_comment: str = None, file_type: str = None, @@ -374,19 +374,10 @@ async def files_upload_ex( return await self.files_upload(file=file, content=content, **args) - async def get_bot_user_by_team(self, activity: Activity) -> str: - if self.identity: - return self.identity - - if not activity.conversation.properties["team"]: - return None - - user = await self.options.get_bot_user_by_team( - activity.conversation.properties["team"] - ) - if user: - return user - raise Exception("Missing credentials for team.") + async def get_bot_user_identity( + self, activity: Activity # pylint: disable=unused-argument + ) -> str: + return self.identity def verify_signature(self, req: Request, body: str) -> bool: timestamp = req.headers["X-Slack-Request-Timestamp"] @@ -402,7 +393,7 @@ def verify_signature(self, req: Request, body: str) -> bool: return computed_signature == received_signature - async def post_message_to_slack(self, message: SlackMessage) -> SlackResponse: + async def post_message(self, message: SlackMessage) -> SlackResponse: if not message: return None @@ -418,7 +409,9 @@ async def post_message_to_slack(self, message: SlackMessage) -> SlackResponse: if message.blocks: request_content["blocks"] = json.dumps(message.blocks) - session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30),) + session = aiohttp.ClientSession( + timeout=aiohttp.ClientTimeout(total=30), + ) http_verb = "POST" api_url = POST_EPHEMERAL_MESSAGE_URL if message.ephemeral else POST_MESSAGE_URL diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_client_options.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_client_options.py new file mode 100644 index 000000000..244aeb3f6 --- /dev/null +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_client_options.py @@ -0,0 +1,32 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +class SlackClientOptions: + """ + Defines the implementation of the SlackClient options. + """ + + def __init__( + self, + slack_verification_token: str, + slack_bot_token: str, + slack_client_signing_secret: str, + ): + """ + Initializes a new instance of SlackClientOptions. + + :param slack_verification_token: A token for validating the origin of incoming webhooks. + :type slack_verification_token: str + :param slack_bot_token: A token for a bot to work on a single workspace. + :type slack_bot_token: str + :param slack_client_signing_secret: The token used to validate that incoming webhooks originated from Slack. + :type slack_client_signing_secret: str + """ + self.slack_verification_token = slack_verification_token + self.slack_bot_token = slack_bot_token + self.slack_client_signing_secret = slack_client_signing_secret + self.slack_client_id = None + self.slack_client_secret = None + self.slack_redirect_uri = None + self.slack_scopes = [str] diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_event.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_event.py index 689b0b25c..66b810ffb 100644 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_event.py +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_event.py @@ -1,6 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from typing import List from botbuilder.adapters.slack.slack_message import SlackMessage @@ -24,7 +25,7 @@ def __init__(self, **kwargs): self.user = kwargs.get("user") self.user_id = kwargs.get("user_id") self.bot_id = kwargs.get("bot_id") - self.actions: [str] = kwargs.get("actions") + self.actions: List[str] = kwargs.get("actions") self.item = kwargs.get("item") self.item_channel = kwargs.get("item_channel") self.files: [] = kwargs.get("files") diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_helper.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_helper.py index e15604442..c40dd21e9 100644 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_helper.py +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_helper.py @@ -7,14 +7,15 @@ from aiohttp.web_request import Request from aiohttp.web_response import Response +from slack.web.classes.attachments import Attachment + from botbuilder.schema import ( Activity, ConversationAccount, ChannelAccount, ActivityTypes, ) - -from slack.web.classes.attachments import Attachment +from botframework.connector import Channels from .slack_message import SlackMessage from .slack_client import SlackClient @@ -27,10 +28,13 @@ class SlackHelper: @staticmethod def activity_to_slack(activity: Activity) -> SlackMessage: """ - Formats a BotBuilder activity into an outgoing Slack message. + Formats a BotBuilder Activity into an outgoing Slack message. + :param activity: A BotBuilder Activity object. - :return: A Slack message object with {text, attachments, channel, thread ts} as well - as any fields found in activity.channelData + :type activity: :class:`botbuilder.schema.Activity` + :return: A Slack message object with {text, attachments, channel, thread ts} and any fields found in + activity.channelData. + :rtype: :class:`SlackMessage` """ if not activity: @@ -53,7 +57,9 @@ def activity_to_slack(activity: Activity) -> SlackMessage: message.blocks = att.content else: new_attachment = Attachment( - author_name=att.name, thumb_url=att.thumbnail_url, + author_name=att.name, + thumb_url=att.thumbnail_url, + text="", ) attachments.append(new_attachment) @@ -83,13 +89,18 @@ def response( # pylint: disable=unused-argument req: Request, code: int, text: str = None, encoding: str = None ) -> Response: """ - Formats an aiohttp Response - - :param req: The original aoihttp Request - :param code: The HTTP result code to return - :param text: The text to return - :param encoding: The text encoding. Defaults to utf-8 + Formats an aiohttp Response. + + :param req: The original aiohttp Request. + :type req: :class:`aiohttp.web_request.Request` + :param code: The HTTP result code to return. + :type code: int + :param text: The text to return. + :type text: str + :param encoding: The text encoding. Defaults to UTF-8. + :type encoding: str :return: The aoihttp Response + :rtype: :class:`aiohttp.web_response.Response` """ response = Response(status=code) @@ -103,46 +114,64 @@ def response( # pylint: disable=unused-argument @staticmethod def payload_to_activity(payload: SlackPayload) -> Activity: """ - Creates an activity based on the slack event payload. + Creates an activity based on the Slack event payload. - :param payload: The payload of the slack event. + :param payload: The payload of the Slack event. + :type payload: :class:`SlackPayload` :return: An activity containing the event data. + :rtype: :class:`botbuilder.schema.Activity` """ if not payload: raise Exception("payload is required") activity = Activity( - channel_id="slack", - conversation=ConversationAccount(id=payload.channel.id, properties={}), + channel_id=Channels.slack, + conversation=ConversationAccount(id=payload.channel["id"], properties={}), from_property=ChannelAccount( - id=payload.message.bot_id if payload.message.bot_id else payload.user.id + id=( + payload.message.bot_id + if payload.message.bot_id + else payload.user["id"] + ) ), recipient=ChannelAccount(), channel_data=payload, text=None, type=ActivityTypes.event, + value=payload, ) if payload.thread_ts: activity.conversation.properties["thread_ts"] = payload.thread_ts - if payload.actions and ( - payload.type == "block_actions" or payload.type == "interactive_message" - ): - activity.type = ActivityTypes.message - activity.text = payload.actions.value + if payload.actions: + action = payload.actions[0] + + if action["type"] == "button": + activity.text = action["value"] + elif action["type"] == "select": + selected_option = action["selected_options"] + activity.text = selected_option["value"] if selected_option else None + elif action["type"] == "static_select": + activity.text = action["selected_options"]["value"] + + if activity.text: + activity.type = ActivityTypes.message return activity @staticmethod async def event_to_activity(event: SlackEvent, client: SlackClient) -> Activity: """ - Creates an activity based on the slack event data. + Creates an activity based on the Slack event data. - :param event: The data of the slack event. + :param event: The data of the Slack event. + :type event: :class:`SlackEvent` :param client: The Slack client. + :type client: :class:`SlackClient` :return: An activity containing the event data. + :rtype: :class:`botbuilder.schema.Activity` """ if not event: @@ -150,7 +179,7 @@ async def event_to_activity(event: SlackEvent, client: SlackClient) -> Activity: activity = Activity( id=event.event_ts, - channel_id="slack", + channel_id=Channels.slack, conversation=ConversationAccount( id=event.channel if event.channel else event.channel_id, properties={} ), @@ -163,26 +192,27 @@ async def event_to_activity(event: SlackEvent, client: SlackClient) -> Activity: type=ActivityTypes.event, ) - if event.thread_ts: - activity.conversation.properties["thread_ts"] = event.thread_ts - if not activity.conversation.id: if event.item and event.item_channel: activity.conversation.id = event.item_channel else: activity.conversation.id = event.team - activity.recipient.id = await client.get_bot_user_by_team(activity=activity) + activity.recipient.id = await client.get_bot_user_identity(activity=activity) + + if event.thread_ts: + activity.conversation.properties["thread_ts"] = event.thread_ts - # If this is a message originating from a user, we'll mark it as such - # If this is a message from a bot (bot_id != None), we want to ignore it by - # leaving the activity type as Event. This will stop it from being included in dialogs, - # but still allow the Bot to act on it if it chooses (via ActivityHandler.on_event_activity). - # NOTE: This catches a message from ANY bot, including this bot. - # Note also, bot_id here is not the same as bot_user_id so we can't (yet) identify messages - # originating from this bot without doing an additional API call. if event.type == "message" and not event.subtype and not event.bot_id: - activity.type = ActivityTypes.message + if not event.subtype: + activity.type = ActivityTypes.message + activity.text = event.text + + activity.conversation.properties["channel_type"] = event.channel_type + activity.value = event + else: + activity.name = event.type + activity.value = event return activity @@ -191,11 +221,14 @@ async def command_to_activity( body: SlackRequestBody, client: SlackClient ) -> Activity: """ - Creates an activity based on a slack event related to a slash command. + Creates an activity based on a Slack event related to a slash command. - :param body: The data of the slack event. + :param body: The data of the Slack event. + :type body: :class:`SlackRequestBody` :param client: The Slack client. + :type client: :class:`SlackClient` :return: An activity containing the event data. + :rtype: :class:`botbuilder.schema.Activity` """ if not body: @@ -203,16 +236,18 @@ async def command_to_activity( activity = Activity( id=body.trigger_id, - channel_id="slack", + channel_id=Channels.slack, conversation=ConversationAccount(id=body.channel_id, properties={}), from_property=ChannelAccount(id=body.user_id), recipient=ChannelAccount(id=None), channel_data=body, text=body.text, type=ActivityTypes.event, + name="Command", + value=body.command, ) - activity.recipient.id = await client.get_bot_user_by_team(activity) + activity.recipient.id = await client.get_bot_user_identity(activity) activity.conversation.properties["team"] = body.team_id return activity @@ -223,7 +258,9 @@ def query_string_to_dictionary(query: str) -> {}: Converts a query string to a dictionary with key-value pairs. :param query: The query string to convert. + :type query: str :return: A dictionary with the query values. + :rtype: :class:`typing.Dict` """ values = {} @@ -247,9 +284,12 @@ def deserialize_body(content_type: str, request_body: str) -> SlackRequestBody: """ Deserializes the request's body as a SlackRequestBody object. - :param content_type: The content type of the body - :param request_body: The body of the request - :return: A SlackRequestBody object + :param content_type: The content type of the body. + :type content_type: str + :param request_body: The body of the request. + :type request_body: str + :return: A SlackRequestBody object. + :rtype: :class:`SlackRequestBody` """ if not request_body: diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_message.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_message.py index 38a7e3297..f0cca5e24 100644 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_message.py +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_message.py @@ -22,11 +22,11 @@ def __init__(self, **kwargs): self.icons = kwargs.get("icons") self.blocks: [Block] = kwargs.get("blocks") - self.attachments = None - if "attachments" in kwargs: - # Create proper Attachment objects - # It would appear that we can get dict fields from the wire that aren't defined - # in the Attachment class. So only pass in known fields. + # Create proper Attachment objects + # It would appear that we can get dict fields from the wire that aren't defined + # in the Attachment class. So only pass in known fields. + attachments = kwargs.get("attachments") + if attachments is not None: self.attachments = [ Attachment(**{x: att[x] for x in att if x in Attachment.attributes}) for att in kwargs.get("attachments") diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_options.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_options.py deleted file mode 100644 index a855ea98a..000000000 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_options.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - - -class SlackAdapterOptions: - """ - Class for defining implementation of the SlackAdapter Options. - """ - - def __init__( - self, - slack_verification_token: str, - slack_bot_token: str, - slack_client_signing_secret: str, - ): - """ - Initializes new instance of SlackAdapterOptions - :param slack_verification_token: A token for validating the origin of incoming webhooks. - :param slack_bot_token: A token for a bot to work on a single workspace. - :param slack_client_signing_secret: The token used to validate that incoming webhooks are originated from Slack. - """ - self.slack_verification_token = slack_verification_token - self.slack_bot_token = slack_bot_token - self.slack_client_signing_secret = slack_client_signing_secret - self.slack_client_id = None - self.slack_client_secret = None - self.slack_redirect_uri = None - self.slack_scopes = [str] - - async def get_token_for_team(self, team_id: str) -> str: - """ - A method that receives a Slack team id and returns the bot token associated with that team. Required for - multi-team apps. - :param team_id:Team ID. - :return:The bot token associated with the team. - """ - raise NotImplementedError() - - async def get_bot_user_by_team(self, team_id: str) -> str: - """ - A method that receives a Slack team id and returns the bot user id associated with that team. Required for - multi-team apps. - :param team_id:Team ID. - :return:The bot user id associated with that team. - """ - raise NotImplementedError() diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_payload.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_payload.py index 0be8e3666..3d929d362 100644 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_payload.py +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_payload.py @@ -1,27 +1,32 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. - -from typing import Optional, List - +import json +from typing import List from botbuilder.adapters.slack.slack_message import SlackMessage -from slack.web.classes.actions import Action - class SlackPayload: def __init__(self, **kwargs): - self.type: [str] = kwargs.get("type") - self.token: str = kwargs.get("token") - self.channel: str = kwargs.get("channel") - self.thread_ts: str = kwargs.get("thread_ts") - self.team: str = kwargs.get("team") - self.user: str = kwargs.get("user") - self.actions: Optional[List[Action]] = None + payload = json.loads(kwargs.get("payload")) + + self.type: List[str] = payload.get("type") + self.token: str = payload.get("token") + self.channel: str = payload.get("channel") + self.thread_ts: str = payload.get("thread_ts") + self.team: str = payload.get("team") + self.user: str = payload.get("user") + self.actions = payload.get("actions") + self.trigger_id: str = payload.get("trigger_id") + self.action_ts: str = payload.get("action_ts") + self.submission: str = payload.get("submission") + self.callback_id: str = payload.get("callback_id") + self.state: str = payload.get("state") + self.response_url: str = payload.get("response_url") - if "message" in kwargs: - message = kwargs.get("message") + if "message" in payload: + message = payload.get("message") self.message = ( message - if isinstance(message) is SlackMessage + if isinstance(message, SlackMessage) else SlackMessage(**message) ) diff --git a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_request_body.py b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_request_body.py index 7990555c7..f740c6a07 100644 --- a/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_request_body.py +++ b/libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_request_body.py @@ -1,6 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from typing import List from botbuilder.adapters.slack.slack_event import SlackEvent from botbuilder.adapters.slack.slack_payload import SlackPayload @@ -14,7 +15,7 @@ def __init__(self, **kwargs): self.type = kwargs.get("type") self.event_id = kwargs.get("event_id") self.event_time = kwargs.get("event_time") - self.authed_users: [str] = kwargs.get("authed_users") + self.authed_users: List[str] = kwargs.get("authed_users") self.trigger_id = kwargs.get("trigger_id") self.channel_id = kwargs.get("channel_id") self.user_id = kwargs.get("user_id") @@ -22,8 +23,8 @@ def __init__(self, **kwargs): self.command = kwargs.get("command") self.payload: SlackPayload = None - if "payload" in kwargs: - payload = kwargs.get("payload") + payload = kwargs.get("payload") + if payload is not None: self.payload = ( payload if isinstance(payload, SlackPayload) @@ -31,6 +32,6 @@ def __init__(self, **kwargs): ) self.event: SlackEvent = None - if "event" in kwargs: - event = kwargs.get("event") + event = kwargs.get("event") + if event is not None: self.event = event if isinstance(event, SlackEvent) else SlackEvent(**event) diff --git a/libraries/botbuilder-adapters-slack/requirements.txt b/libraries/botbuilder-adapters-slack/requirements.txt index 4d6cdb67c..50f1af767 100644 --- a/libraries/botbuilder-adapters-slack/requirements.txt +++ b/libraries/botbuilder-adapters-slack/requirements.txt @@ -1,4 +1,4 @@ aiohttp pyslack -botbuilder-core>=4.7.1 -slackclient \ No newline at end of file +botbuilder-core==4.17.0 +slackclient diff --git a/libraries/botbuilder-adapters-slack/setup.py b/libraries/botbuilder-adapters-slack/setup.py index d154572f2..25fc99ed8 100644 --- a/libraries/botbuilder-adapters-slack/setup.py +++ b/libraries/botbuilder-adapters-slack/setup.py @@ -5,9 +5,11 @@ from setuptools import setup REQUIRES = [ - "botbuilder-schema>=4.7.0", - "botframework-connector>=4.7.0", - "botbuilder-core>=4.7.0", + "botbuilder-schema==4.17.0", + "botframework-connector==4.17.0", + "botbuilder-core==4.17.0", + "pyslack", + "slackclient", ] TEST_REQUIRES = ["aiounittest==1.3.0"] @@ -32,7 +34,7 @@ long_description=long_description, long_description_content_type="text/x-rst", license=package_info["__license__"], - packages=["botbuilder.adapters", "botbuilder.adapters.slack",], + packages=["botbuilder.adapters.slack"], install_requires=REQUIRES + TEST_REQUIRES, tests_require=TEST_REQUIRES, include_package_data=True, diff --git a/libraries/botbuilder-adapters-slack/tests/test_slack_client.py b/libraries/botbuilder-adapters-slack/tests/test_slack_client.py new file mode 100644 index 000000000..1f13c19b0 --- /dev/null +++ b/libraries/botbuilder-adapters-slack/tests/test_slack_client.py @@ -0,0 +1,119 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import hashlib +import hmac +import json +import os +import uuid +import datetime +import time +import aiounittest +import requests +import pytest + +SKIP = os.getenv("SlackChannel") == "" + + +class SlackClient(aiounittest.AsyncTestCase): + @pytest.mark.skipif(not SKIP, reason="Needs the env.SlackChannel to run.") + async def test_send_and_receive_slack_message(self): + # Arrange + echo_guid = str(uuid.uuid4()) + + # Act + await self._send_message_async(echo_guid) + response = await self._receive_message_async() + + # Assert + self.assertEqual(f"Echo: {echo_guid}", response) + + async def _receive_message_async(self) -> str: + last_message = "" + i = 0 + + while "Echo" not in last_message and i < 60: + url = ( + f"{self._slack_url_base}/conversations.history?token=" + f"{self._slack_bot_token}&channel={self._slack_channel}" + ) + response = requests.get( + url, + ) + last_message = response.json()["messages"][0]["text"] + + time.sleep(1) + i += 1 + + return last_message + + async def _send_message_async(self, echo_guid: str) -> None: + timestamp = str(int(datetime.datetime.utcnow().timestamp())) + message = self._create_message(echo_guid) + hub_signature = self._create_hub_signature(message, timestamp) + headers = { + "X-Slack-Request-Timestamp": timestamp, + "X-Slack-Signature": hub_signature, + "Content-type": "application/json", + } + url = f"https://{self._bot_name}.azurewebsites.net/api/messages" + + requests.post(url, headers=headers, data=message) + + def _create_message(self, echo_guid: str) -> str: + slack_event = { + "client_msg_id": "client_msg_id", + "type": "message", + "text": echo_guid, + "user": "userId", + "channel": self._slack_channel, + "channel_type": "im", + } + + message = { + "token": self._slack_verification_token, + "team_id": "team_id", + "api_app_id": "apiAppId", + "event": slack_event, + "type": "event_callback", + } + + return json.dumps(message) + + def _create_hub_signature(self, message: str, timestamp: str) -> str: + signature = ["v0", timestamp, message] + base_string = ":".join(signature) + + computed_signature = "V0=" + hmac.new( + bytes(self._slack_client_signing_secret, encoding="utf8"), + msg=bytes(base_string, "utf-8"), + digestmod=hashlib.sha256, + ).hexdigest().upper().replace("-", "") + + return computed_signature + + @classmethod + def setUpClass(cls) -> None: + cls._slack_url_base: str = "https://slack.com/api" + + cls._slack_channel = os.getenv("SlackChannel") + if not cls._slack_channel: + raise Exception('Environment variable "SlackChannel" not found.') + + cls._slack_bot_token = os.getenv("SlackBotToken") + if not cls._slack_bot_token: + raise Exception('Environment variable "SlackBotToken" not found.') + + cls._slack_client_signing_secret = os.getenv("SlackClientSigningSecret") + if not cls._slack_client_signing_secret: + raise Exception( + 'Environment variable "SlackClientSigningSecret" not found.' + ) + + cls._slack_verification_token = os.getenv("SlackVerificationToken") + if not cls._slack_verification_token: + raise Exception('Environment variable "SlackVerificationToken" not found.') + + cls._bot_name = os.getenv("BotName") + if not cls._bot_name: + raise Exception('Environment variable "BotName" not found.') diff --git a/libraries/botbuilder-ai/README.rst b/libraries/botbuilder-ai/README.rst index aef9094cc..c4a4269b9 100644 --- a/libraries/botbuilder-ai/README.rst +++ b/libraries/botbuilder-ai/README.rst @@ -3,8 +3,8 @@ BotBuilder-AI SDK for Python ============================ -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch .. image:: https://badge.fury.io/py/botbuilder-ai.svg diff --git a/libraries/botbuilder-ai/botbuilder/ai/about.py b/libraries/botbuilder-ai/botbuilder/ai/about.py index 2fe559dac..e063c5499 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/about.py +++ b/libraries/botbuilder-ai/botbuilder/ai/about.py @@ -5,7 +5,7 @@ __title__ = "botbuilder-ai" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/__init__.py b/libraries/botbuilder-ai/botbuilder/ai/luis/__init__.py index cbee8bdc2..823d15dd9 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/luis/__init__.py +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/__init__.py @@ -2,12 +2,14 @@ # Licensed under the MIT License. from .luis_application import LuisApplication +from .luis_recognizer_options_v3 import LuisRecognizerOptionsV3 from .luis_prediction_options import LuisPredictionOptions from .luis_telemetry_constants import LuisTelemetryConstants from .luis_recognizer import LuisRecognizer __all__ = [ "LuisApplication", + "LuisRecognizerOptionsV3", "LuisPredictionOptions", "LuisRecognizer", "LuisTelemetryConstants", diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/activity_util.py b/libraries/botbuilder-ai/botbuilder/ai/luis/activity_util.py index d2656a3ba..303917fbb 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/luis/activity_util.py +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/activity_util.py @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from datetime import datetime +from datetime import datetime, timezone from botbuilder.schema import ( Activity, @@ -51,7 +51,7 @@ def create_trace( reply = Activity( type=ActivityTypes.trace, - timestamp=datetime.utcnow(), + timestamp=datetime.now(timezone.utc), from_property=from_property, recipient=ChannelAccount( id=turn_activity.from_property.id, name=turn_activity.from_property.name diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_application.py b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_application.py index 8d8f8e09d..3351b5882 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_application.py +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_application.py @@ -13,12 +13,13 @@ class LuisApplication: """ def __init__(self, application_id: str, endpoint_key: str, endpoint: str): - """Initializes a new instance of the class. + """Initializes a new instance of the :class:`LuisApplication` class. + :param application_id: LUIS application ID. :type application_id: str :param endpoint_key: LUIS subscription or endpoint key. :type endpoint_key: str - :param endpoint: LUIS endpoint to use like https://westus.api.cognitive.microsoft.com. + :param endpoint: LUIS endpoint to use, like https://westus.api.cognitive.microsoft.com. :type endpoint: str :raises ValueError: :raises ValueError: @@ -46,7 +47,8 @@ def __init__(self, application_id: str, endpoint_key: str, endpoint: str): @classmethod def from_application_endpoint(cls, application_endpoint: str): - """Initializes a new instance of the class. + """Initializes a new instance of the :class:`LuisApplication` class. + :param application_endpoint: LUIS application endpoint. :type application_endpoint: str :return: diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer.py b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer.py index 6733e8a1c..bf6e15bfe 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer.py +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer.py @@ -3,11 +3,6 @@ import json from typing import Dict, List, Tuple, Union - -from azure.cognitiveservices.language.luis.runtime import LUISRuntimeClient -from azure.cognitiveservices.language.luis.runtime.models import LuisResult -from msrest.authentication import CognitiveServicesCredentials - from botbuilder.core import ( BotAssert, IntentScore, @@ -16,15 +11,16 @@ TurnContext, ) from botbuilder.schema import ActivityTypes - from . import LuisApplication, LuisPredictionOptions, LuisTelemetryConstants -from .activity_util import ActivityUtil -from .luis_util import LuisUtil +from .luis_recognizer_v3 import LuisRecognizerV3 +from .luis_recognizer_v2 import LuisRecognizerV2 +from .luis_recognizer_options_v2 import LuisRecognizerOptionsV2 +from .luis_recognizer_options_v3 import LuisRecognizerOptionsV3 class LuisRecognizer(Recognizer): """ - A LUIS based implementation of . + A LUIS based implementation of :class:`botbuilder.core.Recognizer`. """ # The value type for a LUIS trace activity. @@ -36,17 +32,20 @@ class LuisRecognizer(Recognizer): def __init__( self, application: Union[LuisApplication, str], - prediction_options: LuisPredictionOptions = None, + prediction_options: Union[ + LuisRecognizerOptionsV2, LuisRecognizerOptionsV3, LuisPredictionOptions + ] = None, include_api_results: bool = False, ): - """Initializes a new instance of the class. + """Initializes a new instance of the :class:`LuisRecognizer` class. + :param application: The LUIS application to use to recognize text. - :type application: LuisApplication - :param prediction_options: The LUIS prediction options to use, defaults to None - :param prediction_options: LuisPredictionOptions, optional - :param include_api_results: True to include raw LUIS API response, defaults to False - :param include_api_results: bool, optional - :raises TypeError: + :type application: :class:`LuisApplication` + :param prediction_options: The LUIS prediction options to use, defaults to None. + :type prediction_options: :class:`LuisPredictionOptions`, optional + :param include_api_results: True to include raw LUIS API response, defaults to False. + :type include_api_results: bool, optional + :raises: TypeError """ if isinstance(application, LuisApplication): @@ -59,30 +58,31 @@ def __init__( ) self._options = prediction_options or LuisPredictionOptions() - - self._include_api_results = include_api_results + self._include_api_results = include_api_results or ( + prediction_options.include_api_results + if isinstance( + prediction_options, (LuisRecognizerOptionsV3, LuisRecognizerOptionsV2) + ) + else False + ) self.telemetry_client = self._options.telemetry_client self.log_personal_information = self._options.log_personal_information - credentials = CognitiveServicesCredentials(self._application.endpoint_key) - self._runtime = LUISRuntimeClient(self._application.endpoint, credentials) - self._runtime.config.add_user_agent(LuisUtil.get_user_agent()) - self._runtime.config.connection.timeout = self._options.timeout // 1000 - @staticmethod def top_intent( results: RecognizerResult, default_intent: str = "None", min_score: float = 0.0 ) -> str: """Returns the name of the top scoring intent from a set of LUIS results. + :param results: Result set to be searched. - :type results: RecognizerResult - :param default_intent: Intent name to return should a top intent be found, defaults to "None" - :param default_intent: str, optional - :param min_score: Minimum score needed for an intent to be considered as a top intent. If all intents in the - set are below this threshold then the `defaultIntent` will be returned, defaults to 0.0 - :param min_score: float, optional - :raises TypeError: + :type results: :class:`botbuilder.core.RecognizerResult` + :param default_intent: Intent name to return should a top intent be found, defaults to None. + :type default_intent: str, optional + :param min_score: Minimum score needed for an intent to be considered as a top intent. If all intents in the set + are below this threshold then the `defaultIntent` is returned, defaults to 0.0. + :type min_score: float, optional + :raises: TypeError :return: The top scoring intent name. :rtype: str """ @@ -108,17 +108,18 @@ async def recognize( # pylint: disable=arguments-differ telemetry_metrics: Dict[str, float] = None, luis_prediction_options: LuisPredictionOptions = None, ) -> RecognizerResult: - """Return results of the analysis (Suggested actions and intents). - :param turn_context: Context object containing information for a single turn of conversation with a user. - :type turn_context: TurnContext + """Return results of the analysis (suggested actions and intents). + + :param turn_context: Context object containing information for a single conversation turn with a user. + :type turn_context: :class:`botbuilder.core.TurnContext` :param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event, defaults - to None - :param telemetry_properties: Dict[str, str], optional + to None. + :type telemetry_properties: :class:`typing.Dict[str, str]`, optional :param telemetry_metrics: Additional metrics to be logged to telemetry with the LuisResult event, defaults to - None - :param telemetry_metrics: Dict[str, float], optional + None. + :type telemetry_metrics: :class:`typing.Dict[str, float]`, optional :return: The LUIS results of the analysis of the current message text in the current turn's context activity. - :rtype: RecognizerResult + :rtype: :class:`botbuilder.core.RecognizerResult` """ return await self._recognize_internal( @@ -136,16 +137,17 @@ def on_recognizer_result( telemetry_metrics: Dict[str, float] = None, ): """Invoked prior to a LuisResult being logged. - :param recognizer_result: The Luis Results for the call. - :type recognizer_result: RecognizerResult + + :param recognizer_result: The LuisResult for the call. + :type recognizer_result: :class:`botbuilder.core.RecognizerResult` :param turn_context: Context object containing information for a single turn of conversation with a user. - :type turn_context: TurnContext + :type turn_context: :class:`botbuilder.core.TurnContext` :param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event, defaults - to None - :param telemetry_properties: Dict[str, str], optional - :param telemetry_metrics: Additional metrics to be logged to telemetry with the LuisResult event, defaults to - None - :param telemetry_metrics: Dict[str, float], optional + to None. + :type telemetry_properties: :class:`typing.Dict[str, str]`, optional + :param telemetry_metrics: Additional metrics to be logged to telemetry with the LuisResult event, defaults + to None. + :type telemetry_metrics: :class:`typing.Dict[str, float]`, optional """ properties = self.fill_luis_event_properties( @@ -180,16 +182,17 @@ def fill_luis_event_properties( ) -> Dict[str, str]: """Fills the event properties for LuisResult event for telemetry. These properties are logged when the recognizer is called. + :param recognizer_result: Last activity sent from user. - :type recognizer_result: RecognizerResult + :type recognizer_result: :class:`botbuilder.core.RecognizerResult` :param turn_context: Context object containing information for a single turn of conversation with a user. - :type turn_context: TurnContext - :param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event, - defaults to None - :param telemetry_properties: Dict[str, str], optional - :return: A dictionary that is sent as "Properties" to IBotTelemetryClient.TrackEvent method for the + :type turn_context: :class:`botbuilder.core.TurnContext` + :param telemetry_properties: Additional properties to be logged to telemetry with the LuisResult event, defaults + to None. + :type telemetry_properties: :class:`typing.Dict[str, str]`, optional + :return: A dictionary sent as "Properties" to :func:`botbuilder.core.BotTelemetryClient.track_event` for the BotMessageSend event. - :rtype: Dict[str, str] + :rtype: `typing.Dict[str, str]` """ intents = recognizer_result.intents @@ -233,9 +236,9 @@ def fill_luis_event_properties( # Use the LogPersonalInformation flag to toggle logging PII data, text is a common example if self.log_personal_information and turn_context.activity.text: - properties[ - LuisTelemetryConstants.question_property - ] = turn_context.activity.text + properties[LuisTelemetryConstants.question_property] = ( + turn_context.activity.text + ) # Additional Properties can override "stock" properties. if telemetry_properties is not None: @@ -249,20 +252,22 @@ async def _recognize_internal( turn_context: TurnContext, telemetry_properties: Dict[str, str], telemetry_metrics: Dict[str, float], - luis_prediction_options: LuisPredictionOptions = None, + luis_prediction_options: Union[ + LuisPredictionOptions, LuisRecognizerOptionsV2, LuisRecognizerOptionsV3 + ] = None, ) -> RecognizerResult: - BotAssert.context_not_none(turn_context) if turn_context.activity.type != ActivityTypes.message: return None - utterance: str = turn_context.activity.text if turn_context.activity is not None else None + utterance: str = ( + turn_context.activity.text if turn_context.activity is not None else None + ) recognizer_result: RecognizerResult = None - luis_result: LuisResult = None if luis_prediction_options: - options = self._merge_options(luis_prediction_options) + options = luis_prediction_options else: options = self._options @@ -271,71 +276,48 @@ async def _recognize_internal( text=utterance, intents={"": IntentScore(score=1.0)}, entities={} ) else: - luis_result = self._runtime.prediction.resolve( - self._application.application_id, - utterance, - timezone_offset=options.timezone_offset, - verbose=options.include_all_intents, - staging=options.staging, - spell_check=options.spell_check, - bing_spell_check_subscription_key=options.bing_spell_check_subscription_key, - log=options.log if options.log is not None else True, - ) - - recognizer_result = RecognizerResult( - text=utterance, - altered_text=luis_result.altered_query, - intents=LuisUtil.get_intents(luis_result), - entities=LuisUtil.extract_entities_and_metadata( - luis_result.entities, - luis_result.composite_entities, - options.include_instance_data - if options.include_instance_data is not None - else True, - ), - ) - LuisUtil.add_properties(luis_result, recognizer_result) - if self._include_api_results: - recognizer_result.properties["luisResult"] = luis_result + luis_recognizer = self._build_recognizer(options) + recognizer_result = await luis_recognizer.recognizer_internal(turn_context) # Log telemetry self.on_recognizer_result( recognizer_result, turn_context, telemetry_properties, telemetry_metrics ) - await self._emit_trace_info( - turn_context, luis_result, recognizer_result, options - ) - return recognizer_result - async def _emit_trace_info( - self, - turn_context: TurnContext, - luis_result: LuisResult, - recognizer_result: RecognizerResult, - options: LuisPredictionOptions, - ) -> None: - trace_info: Dict[str, object] = { - "recognizerResult": LuisUtil.recognizer_result_as_dict(recognizer_result), - "luisModel": {"ModelID": self._application.application_id}, - "luisOptions": {"Staging": options.staging}, - "luisResult": LuisUtil.luis_result_as_dict(luis_result), - } - - trace_activity = ActivityUtil.create_trace( - turn_context.activity, - "LuisRecognizer", - trace_info, - LuisRecognizer.luis_trace_type, - LuisRecognizer.luis_trace_label, - ) - - await turn_context.send_activity(trace_activity) - def _merge_options( - self, user_defined_options: LuisPredictionOptions + self, + user_defined_options: Union[ + LuisRecognizerOptionsV3, LuisRecognizerOptionsV2, LuisPredictionOptions + ], ) -> LuisPredictionOptions: merged_options = LuisPredictionOptions() merged_options.__dict__.update(user_defined_options.__dict__) return merged_options + + def _build_recognizer( + self, + luis_prediction_options: Union[ + LuisRecognizerOptionsV3, LuisRecognizerOptionsV2, LuisPredictionOptions + ], + ): + if isinstance(luis_prediction_options, LuisRecognizerOptionsV3): + return LuisRecognizerV3(self._application, luis_prediction_options) + if isinstance(luis_prediction_options, LuisRecognizerOptionsV2): + return LuisRecognizerV3(self._application, luis_prediction_options) + + recognizer_options = LuisRecognizerOptionsV2( + luis_prediction_options.bing_spell_check_subscription_key, + luis_prediction_options.include_all_intents, + luis_prediction_options.include_instance_data, + luis_prediction_options.log, + luis_prediction_options.spell_check, + luis_prediction_options.staging, + luis_prediction_options.timeout, + luis_prediction_options.timezone_offset, + self._include_api_results, + luis_prediction_options.telemetry_client, + luis_prediction_options.log_personal_information, + ) + return LuisRecognizerV2(self._application, recognizer_options) diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_internal.py b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_internal.py new file mode 100644 index 000000000..66ec5a4ce --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_internal.py @@ -0,0 +1,18 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod +from botbuilder.core import TurnContext +from .luis_application import LuisApplication + + +class LuisRecognizerInternal(ABC): + def __init__(self, luis_application: LuisApplication): + if luis_application is None: + raise TypeError(luis_application.__class__.__name__) + + self.luis_application = luis_application + + @abstractmethod + async def recognizer_internal(self, turn_context: TurnContext): + raise NotImplementedError() diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_options.py b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_options.py new file mode 100644 index 000000000..4368aa443 --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_options.py @@ -0,0 +1,16 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import BotTelemetryClient, NullTelemetryClient + + +class LuisRecognizerOptions: + def __init__( + self, + include_api_results: bool = None, + telemetry_client: BotTelemetryClient = NullTelemetryClient(), + log_personal_information: bool = False, + ): + self.include_api_results = include_api_results + self.telemetry_client = telemetry_client + self.log_personal_information = log_personal_information diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_options_v2.py b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_options_v2.py new file mode 100644 index 000000000..a06c6c5cc --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_options_v2.py @@ -0,0 +1,33 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import BotTelemetryClient, NullTelemetryClient +from .luis_recognizer_options import LuisRecognizerOptions + + +class LuisRecognizerOptionsV2(LuisRecognizerOptions): + def __init__( + self, + bing_spell_check_subscription_key: str = None, + include_all_intents: bool = None, + include_instance_data: bool = True, + log: bool = True, + spell_check: bool = None, + staging: bool = None, + timeout: float = 100000, + timezone_offset: float = None, + include_api_results: bool = True, + telemetry_client: BotTelemetryClient = NullTelemetryClient(), + log_personal_information: bool = False, + ): + super().__init__( + include_api_results, telemetry_client, log_personal_information + ) + self.bing_spell_check_subscription_key = bing_spell_check_subscription_key + self.include_all_intents = include_all_intents + self.include_instance_data = include_instance_data + self.log = log + self.spell_check = spell_check + self.staging = staging + self.timeout = timeout + self.timezone_offset = timezone_offset diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_options_v3.py b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_options_v3.py new file mode 100644 index 000000000..4793e36f8 --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_options_v3.py @@ -0,0 +1,37 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List + +from botbuilder.core import BotTelemetryClient, NullTelemetryClient +from .luis_recognizer_options import LuisRecognizerOptions + + +class LuisRecognizerOptionsV3(LuisRecognizerOptions): + def __init__( + self, + include_all_intents: bool = False, + include_instance_data: bool = True, + log: bool = True, + prefer_external_entities: bool = True, + datetime_reference: str = None, + dynamic_lists: List = None, + external_entities: List = None, + slot: str = "production", + version: str = None, + include_api_results: bool = True, + telemetry_client: BotTelemetryClient = NullTelemetryClient(), + log_personal_information: bool = False, + ): + super().__init__( + include_api_results, telemetry_client, log_personal_information + ) + self.include_all_intents = include_all_intents + self.include_instance_data = include_instance_data + self.log = log + self.prefer_external_entities = prefer_external_entities + self.datetime_reference = datetime_reference + self.dynamic_lists = dynamic_lists + self.external_entities = external_entities + self.slot = slot + self.version: str = version diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_v2.py b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_v2.py new file mode 100644 index 000000000..507b10774 --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_v2.py @@ -0,0 +1,113 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Dict +from azure.cognitiveservices.language.luis.runtime import LUISRuntimeClient +from azure.cognitiveservices.language.luis.runtime.models import LuisResult +from msrest.authentication import CognitiveServicesCredentials +from botbuilder.core import ( + TurnContext, + RecognizerResult, +) +from .luis_recognizer_internal import LuisRecognizerInternal +from .luis_recognizer_options_v2 import LuisRecognizerOptionsV2 +from .luis_application import LuisApplication +from .luis_util import LuisUtil + +from .activity_util import ActivityUtil + + +class LuisRecognizerV2(LuisRecognizerInternal): + # The value type for a LUIS trace activity. + luis_trace_type: str = "https://www.luis.ai/schemas/trace" + + # The context label for a LUIS trace activity. + luis_trace_label: str = "Luis Trace" + + def __init__( + self, + luis_application: LuisApplication, + luis_recognizer_options_v2: LuisRecognizerOptionsV2 = None, + ): + super().__init__(luis_application) + credentials = CognitiveServicesCredentials(luis_application.endpoint_key) + self._runtime = LUISRuntimeClient(luis_application.endpoint, credentials) + self._runtime.config.add_user_agent(LuisUtil.get_user_agent()) + self._runtime.config.connection.timeout = ( + luis_recognizer_options_v2.timeout // 1000 + ) + self.luis_recognizer_options_v2 = ( + luis_recognizer_options_v2 or LuisRecognizerOptionsV2() + ) + self._application = luis_application + + async def recognizer_internal(self, turn_context: TurnContext): + utterance: str = ( + turn_context.activity.text if turn_context.activity is not None else None + ) + luis_result: LuisResult = self._runtime.prediction.resolve( + self._application.application_id, + utterance, + timezone_offset=self.luis_recognizer_options_v2.timezone_offset, + verbose=self.luis_recognizer_options_v2.include_all_intents, + staging=self.luis_recognizer_options_v2.staging, + spell_check=self.luis_recognizer_options_v2.spell_check, + bing_spell_check_subscription_key=self.luis_recognizer_options_v2.bing_spell_check_subscription_key, + log=( + self.luis_recognizer_options_v2.log + if self.luis_recognizer_options_v2.log is not None + else True + ), + ) + + recognizer_result: RecognizerResult = RecognizerResult( + text=utterance, + altered_text=luis_result.altered_query, + intents=LuisUtil.get_intents(luis_result), + entities=LuisUtil.extract_entities_and_metadata( + luis_result.entities, + luis_result.composite_entities, + ( + self.luis_recognizer_options_v2.include_instance_data + if self.luis_recognizer_options_v2.include_instance_data is not None + else True + ), + ), + ) + + LuisUtil.add_properties(luis_result, recognizer_result) + if self.luis_recognizer_options_v2.include_api_results: + recognizer_result.properties["luisResult"] = luis_result + + await self._emit_trace_info( + turn_context, + luis_result, + recognizer_result, + self.luis_recognizer_options_v2, + ) + + return recognizer_result + + async def _emit_trace_info( + self, + turn_context: TurnContext, + luis_result: LuisResult, + recognizer_result: RecognizerResult, + options: LuisRecognizerOptionsV2, + ) -> None: + trace_info: Dict[str, object] = { + "recognizerResult": LuisUtil.recognizer_result_as_dict(recognizer_result), + "luisModel": {"ModelID": self._application.application_id}, + "luisOptions": {"Staging": options.staging}, + "luisResult": LuisUtil.luis_result_as_dict(luis_result), + } + + trace_activity = ActivityUtil.create_trace( + turn_context.activity, + "LuisRecognizer", + trace_info, + LuisRecognizerV2.luis_trace_type, + LuisRecognizerV2.luis_trace_label, + ) + + await turn_context.send_activity(trace_activity) diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_v3.py b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_v3.py new file mode 100644 index 000000000..4e373023e --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_recognizer_v3.py @@ -0,0 +1,288 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import re +from typing import Dict + +import aiohttp +from botbuilder.ai.luis.activity_util import ActivityUtil +from botbuilder.ai.luis.luis_util import LuisUtil +from botbuilder.core import ( + IntentScore, + RecognizerResult, + TurnContext, +) +from .luis_recognizer_internal import LuisRecognizerInternal +from .luis_recognizer_options_v3 import LuisRecognizerOptionsV3 +from .luis_application import LuisApplication + + +# from .activity_util import ActivityUtil + + +class LuisRecognizerV3(LuisRecognizerInternal): + _dateSubtypes = [ + "date", + "daterange", + "datetime", + "datetimerange", + "duration", + "set", + "time", + "timerange", + ] + _geographySubtypes = ["poi", "city", "countryRegion", "continent", "state"] + _metadata_key = "$instance" + + # The value type for a LUIS trace activity. + luis_trace_type: str = "https://www.luis.ai/schemas/trace" + + # The context label for a LUIS trace activity. + luis_trace_label: str = "Luis Trace" + + def __init__( + self, + luis_application: LuisApplication, + luis_recognizer_options_v3: LuisRecognizerOptionsV3 = None, + ): + super().__init__(luis_application) + + self.luis_recognizer_options_v3 = ( + luis_recognizer_options_v3 or LuisRecognizerOptionsV3() + ) + self._application = luis_application + + async def recognizer_internal(self, turn_context: TurnContext): + recognizer_result: RecognizerResult = None + + utterance: str = ( + turn_context.activity.text if turn_context.activity is not None else None + ) + + url = self._build_url() + body = self._build_request(utterance) + headers = { + "Ocp-Apim-Subscription-Key": self.luis_application.endpoint_key, + "Content-Type": "application/json", + } + + async with aiohttp.ClientSession() as session: + async with session.post( + url, json=body, headers=headers, ssl=False + ) as result: + luis_result = await result.json() + + recognizer_result = RecognizerResult( + text=utterance, + intents=self._get_intents(luis_result["prediction"]), + entities=self._extract_entities_and_metadata( + luis_result["prediction"] + ), + ) + + if self.luis_recognizer_options_v3.include_instance_data: + recognizer_result.entities[self._metadata_key] = ( + recognizer_result.entities[self._metadata_key] + if self._metadata_key in recognizer_result.entities + else {} + ) + + if "sentiment" in luis_result["prediction"]: + recognizer_result.properties["sentiment"] = self._get_sentiment( + luis_result["prediction"] + ) + + await self._emit_trace_info( + turn_context, + luis_result, + recognizer_result, + self.luis_recognizer_options_v3, + ) + + return recognizer_result + + def _build_url(self): + base_uri = ( + self._application.endpoint or "https://westus.api.cognitive.microsoft.com" + ) + uri = "%s/luis/prediction/v3.0/apps/%s" % ( + base_uri, + self._application.application_id, + ) + + if self.luis_recognizer_options_v3.version: + uri += "/versions/%s/predict" % (self.luis_recognizer_options_v3.version) + else: + uri += "/slots/%s/predict" % (self.luis_recognizer_options_v3.slot) + + params = "?verbose=%s&show-all-intents=%s&log=%s" % ( + ( + "true" + if self.luis_recognizer_options_v3.include_instance_data + else "false" + ), + "true" if self.luis_recognizer_options_v3.include_all_intents else "false", + "true" if self.luis_recognizer_options_v3.log else "false", + ) + + return uri + params + + def _build_request(self, utterance: str): + body = { + "query": utterance, + "options": { + "preferExternalEntities": self.luis_recognizer_options_v3.prefer_external_entities, + }, + } + + if self.luis_recognizer_options_v3.datetime_reference: + body["options"][ + "datetimeReference" + ] = self.luis_recognizer_options_v3.datetime_reference + + if self.luis_recognizer_options_v3.dynamic_lists: + body["dynamicLists"] = self.luis_recognizer_options_v3.dynamic_lists + + if self.luis_recognizer_options_v3.external_entities: + body["externalEntities"] = self.luis_recognizer_options_v3.external_entities + + return body + + def _get_intents(self, luis_result): + intents = {} + if not luis_result["intents"]: + return intents + + for intent in luis_result["intents"]: + intents[self._normalize_name(intent)] = IntentScore( + luis_result["intents"][intent]["score"] + ) + + return intents + + def _normalize_name(self, name): + return re.sub(r"\.", "_", name) + + def _normalize(self, entity): + split_entity = entity.split(":") + entity_name = split_entity[-1] + return self._normalize_name(entity_name) + + def _extract_entities_and_metadata(self, luis_result): + entities = luis_result["entities"] + return self._map_properties(entities, False) + + def _map_properties(self, source, in_instance): + if isinstance(source, (int, float, bool, str)): + return source + + result = source + if isinstance(source, list): + narr = [] + for item in source: + is_geography_v2 = "" + if ( + isinstance(item, dict) + and "type" in item + and item["type"] in self._geographySubtypes + ): + is_geography_v2 = item["type"] + + if not in_instance and is_geography_v2: + geo_entity = {} + for item_props in item: + if item_props == "value": + geo_entity["location"] = item[item_props] + + geo_entity["type"] = is_geography_v2 + narr.append(geo_entity) + else: + narr.append(self._map_properties(item, in_instance)) + + result = narr + + elif not isinstance(source, str): + nobj = {} + if ( + not in_instance + and isinstance(source, dict) + and "type" in source + and isinstance(source["type"], str) + and source["type"] in self._dateSubtypes + ): + timexs = source["values"] + arr = [] + if timexs: + unique = [] + for elt in timexs: + if elt["timex"] and elt["timex"] not in unique: + unique.append(elt["timex"]) + + for timex in unique: + arr.append(timex) + + nobj["timex"] = arr + + nobj["type"] = source["type"] + + else: + for property in source: + name = self._normalize(property) + is_array = isinstance(source[property], list) + is_string = isinstance(source[property], str) + is_int = isinstance(source[property], (int, float)) + val = self._map_properties( + source[property], in_instance or property == self._metadata_key + ) + if name == "datetime" and is_array: + nobj["datetimeV1"] = val + + elif name == "datetimeV2" and is_array: + nobj["datetime"] = val + + elif in_instance: + if name == "length" and is_int: + nobj["endIndex"] = source[name] + source["startIndex"] + elif not ( + (is_int and name == "modelTypeId") + or (is_string and name == "role") + ): + nobj[name] = val + else: + if name == "unit" and is_string: + nobj["units"] = val + else: + nobj[name] = val + + result = nobj + return result + + def _get_sentiment(self, luis_result): + return { + "label": luis_result["sentiment"]["label"], + "score": luis_result["sentiment"]["score"], + } + + async def _emit_trace_info( + self, + turn_context: TurnContext, + luis_result, + recognizer_result: RecognizerResult, + options: LuisRecognizerOptionsV3, + ) -> None: + trace_info: Dict[str, object] = { + "recognizerResult": LuisUtil.recognizer_result_as_dict(recognizer_result), + "luisModel": {"ModelID": self._application.application_id}, + "luisOptions": {"Slot": options.slot}, + "luisResult": luis_result, + } + + trace_activity = ActivityUtil.create_trace( + turn_context.activity, + "LuisRecognizer", + trace_info, + LuisRecognizerV3.luis_trace_type, + LuisRecognizerV3.luis_trace_label, + ) + + await turn_context.send_activity(trace_activity) diff --git a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_util.py b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_util.py index 9f620be34..c3dc2f106 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/luis/luis_util.py +++ b/libraries/botbuilder-ai/botbuilder/ai/luis/luis_util.py @@ -303,10 +303,14 @@ def recognizer_result_as_dict( ) -> Dict[str, object]: # an internal method that returns a dict for json serialization. - intents: Dict[str, Dict[str, float]] = { - name: LuisUtil.intent_score_as_dict(intent_score) - for name, intent_score in recognizer_result.intents.items() - } if recognizer_result.intents is not None else None + intents: Dict[str, Dict[str, float]] = ( + { + name: LuisUtil.intent_score_as_dict(intent_score) + for name, intent_score in recognizer_result.intents.items() + } + if recognizer_result.intents is not None + else None + ) dictionary: Dict[str, object] = { "text": recognizer_result.text, diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/__init__.py b/libraries/botbuilder-ai/botbuilder/ai/qna/__init__.py index dc3f3ccba..938010a71 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/__init__.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/__init__.py @@ -1,39 +1,41 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .qnamaker import QnAMaker -from .qnamaker_endpoint import QnAMakerEndpoint -from .qnamaker_options import QnAMakerOptions -from .qnamaker_telemetry_client import QnAMakerTelemetryClient -from .utils import ( - ActiveLearningUtils, - GenerateAnswerUtils, - HttpRequestUtils, - QnATelemetryConstants, -) - -from .models import ( - FeedbackRecord, - FeedbackRecords, - Metadata, - QnAMakerTraceInfo, - QueryResult, - QueryResults, -) - -__all__ = [ - "ActiveLearningUtils", - "FeedbackRecord", - "FeedbackRecords", - "GenerateAnswerUtils", - "HttpRequestUtils", - "Metadata", - "QueryResult", - "QueryResults", - "QnAMaker", - "QnAMakerEndpoint", - "QnAMakerOptions", - "QnAMakerTelemetryClient", - "QnAMakerTraceInfo", - "QnATelemetryConstants", -] +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .qnamaker import QnAMaker +from .qnamaker_endpoint import QnAMakerEndpoint +from .qnamaker_options import QnAMakerOptions +from .qnamaker_telemetry_client import QnAMakerTelemetryClient +from .qna_dialog_response_options import QnADialogResponseOptions +from .utils import ( + ActiveLearningUtils, + GenerateAnswerUtils, + HttpRequestUtils, + QnATelemetryConstants, +) + +from .models import ( + FeedbackRecord, + FeedbackRecords, + Metadata, + QnAMakerTraceInfo, + QueryResult, + QueryResults, +) + +__all__ = [ + "ActiveLearningUtils", + "FeedbackRecord", + "FeedbackRecords", + "GenerateAnswerUtils", + "HttpRequestUtils", + "Metadata", + "QueryResult", + "QueryResults", + "QnAMaker", + "QnAMakerEndpoint", + "QnAMakerOptions", + "QnAMakerTelemetryClient", + "QnAMakerTraceInfo", + "QnATelemetryConstants", + "QnADialogResponseOptions", +] diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/dialogs/__init__.py b/libraries/botbuilder-ai/botbuilder/ai/qna/dialogs/__init__.py new file mode 100644 index 000000000..a6fb238d1 --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/dialogs/__init__.py @@ -0,0 +1,14 @@ +# coding=utf-8 +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +from .qnamaker_dialog import QnAMakerDialog +from .qnamaker_dialog_options import QnAMakerDialogOptions + +__all__ = [ + "QnAMakerDialogOptions", + "QnAMakerDialog", +] diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/dialogs/qnamaker_dialog.py b/libraries/botbuilder-ai/botbuilder/ai/qna/dialogs/qnamaker_dialog.py new file mode 100644 index 000000000..4bcceebaa --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/dialogs/qnamaker_dialog.py @@ -0,0 +1,467 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +from typing import List + +from botbuilder.dialogs import ( + WaterfallDialog, + WaterfallStepContext, + DialogContext, + DialogTurnResult, + Dialog, + ObjectPath, + DialogTurnStatus, + DialogReason, +) +from botbuilder.schema import Activity, ActivityTypes + +from .qnamaker_dialog_options import QnAMakerDialogOptions +from .. import ( + QnAMakerOptions, + QnADialogResponseOptions, + QnAMaker, + QnAMakerEndpoint, +) +from ..models import QnARequestContext, Metadata, QueryResult, FeedbackRecord +from ..models.ranker_types import RankerTypes +from ..utils import QnACardBuilder + + +class QnAMakerDialog(WaterfallDialog): + """ + A dialog that supports multi-step and adaptive-learning QnA Maker services. + + .. remarks:: + An instance of this class targets a specific QnA Maker knowledge base. + It supports knowledge bases that include follow-up prompt and active learning features. + """ + + KEY_QNA_CONTEXT_DATA = "qnaContextData" + """ + The path for storing and retrieving QnA Maker context data. + + .. remarks: + This represents context about the current or previous call to QnA Maker. + It is stored within the current step's :class:'botbuilder.dialogs.WaterfallStepContext'. + It supports QnA Maker's follow-up prompt and active learning features. + """ + + KEY_PREVIOUS_QNA_ID = "prevQnAId" + """ + The path for storing and retrieving the previous question ID. + + .. remarks: + This represents the QnA question ID from the previous turn. + It is stored within the current step's :class:'botbuilder.dialogs.WaterfallStepContext'. + It supports QnA Maker's follow-up prompt and active learning features. + """ + + KEY_OPTIONS = "options" + """ + The path for storing and retrieving the options for this instance of the dialog. + + .. remarks: + This includes the options with which the dialog was started and options + expected by the QnA Maker service. + It is stored within the current step's :class:'botbuilder.dialogs.WaterfallStepContext'. + It supports QnA Maker and the dialog system. + """ + + # Dialog Options parameters + DEFAULT_THRESHOLD = 0.3 + """ The default threshold for answers returned, based on score. """ + + DEFAULT_TOP_N = 3 + """ The default maximum number of answers to be returned for the question. """ + + DEFAULT_NO_ANSWER = "No QnAMaker answers found." + """ The default no answer text sent to the user. """ + + # Card parameters + DEFAULT_CARD_TITLE = "Did you mean:" + """ The default active learning card title. """ + + DEFAULT_CARD_NO_MATCH_TEXT = "None of the above." + """ The default active learning no match text. """ + + DEFAULT_CARD_NO_MATCH_RESPONSE = "Thanks for the feedback." + """ The default active learning response text. """ + + # Value Properties + PROPERTY_CURRENT_QUERY = "currentQuery" + PROPERTY_QNA_DATA = "qnaData" + + def __init__( + self, + knowledgebase_id: str, + endpoint_key: str, + hostname: str, + no_answer: Activity = None, + threshold: float = DEFAULT_THRESHOLD, + active_learning_card_title: str = DEFAULT_CARD_TITLE, + card_no_match_text: str = DEFAULT_CARD_NO_MATCH_TEXT, + top: int = DEFAULT_TOP_N, + card_no_match_response: Activity = None, + strict_filters: [Metadata] = None, + dialog_id: str = "QnAMakerDialog", + ): + """ + Initializes a new instance of the QnAMakerDialog class. + + :param knowledgebase_id: The ID of the QnA Maker knowledge base to query. + :param endpoint_key: The QnA Maker endpoint key to use to query the knowledge base. + :param hostname: The QnA Maker host URL for the knowledge base, starting with "https://" and + ending with "/qnamaker". + :param no_answer: The activity to send the user when QnA Maker does not find an answer. + :param threshold: The threshold for answers returned, based on score. + :param active_learning_card_title: The card title to use when showing active learning options + to the user, if active learning is enabled. + :param card_no_match_text: The button text to use with active learning options, + allowing a user to indicate none of the options are applicable. + :param top: The maximum number of answers to return from the knowledge base. + :param card_no_match_response: The activity to send the user if they select the no match option + on an active learning card. + :param strict_filters: QnA Maker metadata with which to filter or boost queries to the + knowledge base; or null to apply none. + :param dialog_id: The ID of this dialog. + """ + super().__init__(dialog_id) + + self.knowledgebase_id = knowledgebase_id + self.endpoint_key = endpoint_key + self.hostname = hostname + self.no_answer = no_answer + self.threshold = threshold + self.active_learning_card_title = active_learning_card_title + self.card_no_match_text = card_no_match_text + self.top = top + self.card_no_match_response = card_no_match_response + self.strict_filters = strict_filters + + self.maximum_score_for_low_score_variation = 0.95 + + self.add_step(self.__call_generate_answer) + self.add_step(self.__call_train) + self.add_step(self.__check_for_multiturn_prompt) + self.add_step(self.__display_qna_result) + + async def begin_dialog( + self, dialog_context: DialogContext, options: object = None + ) -> DialogTurnResult: + """ + Called when the dialog is started and pushed onto the dialog stack. + + .. remarks: + If the task is successful, the result indicates whether the dialog is still + active after the turn has been processed by the dialog. + + :param dialog_context: The :class:'botbuilder.dialogs.DialogContext' for the current turn of conversation. + :param options: Optional, initial information to pass to the dialog. + """ + + if not dialog_context: + raise TypeError("DialogContext is required") + + if ( + dialog_context.context + and dialog_context.context.activity + and dialog_context.context.activity.type != ActivityTypes.message + ): + return Dialog.end_of_turn + + dialog_options = QnAMakerDialogOptions( + options=self._get_qnamaker_options(dialog_context), + response_options=self._get_qna_response_options(dialog_context), + ) + + if options: + dialog_options = ObjectPath.assign(dialog_options, options) + + ObjectPath.set_path_value( + dialog_context.active_dialog.state, + QnAMakerDialog.KEY_OPTIONS, + dialog_options, + ) + + return await super().begin_dialog(dialog_context, dialog_options) + + def _get_qnamaker_client(self, dialog_context: DialogContext) -> QnAMaker: + """ + Gets a :class:'botbuilder.ai.qna.QnAMaker' to use to access the QnA Maker knowledge base. + + :param dialog_context: The :class:'botbuilder.dialogs.DialogContext' for the current turn of conversation. + """ + + endpoint = QnAMakerEndpoint( + endpoint_key=self.endpoint_key, + host=self.hostname, + knowledge_base_id=self.knowledgebase_id, + ) + + options = self._get_qnamaker_options(dialog_context) + + return QnAMaker(endpoint, options) + + def _get_qnamaker_options( # pylint: disable=unused-argument + self, dialog_context: DialogContext + ) -> QnAMakerOptions: + """ + Gets the options for the QnAMaker client that the dialog will use to query the knowledge base. + + :param dialog_context: The :class:'botbuilder.dialogs.DialogContext' for the current turn of conversation. + """ + + return QnAMakerOptions( + score_threshold=self.threshold, + strict_filters=self.strict_filters, + top=self.top, + context=QnARequestContext(), + qna_id=0, + ranker_type=RankerTypes.DEFAULT, + is_test=False, + ) + + def _get_qna_response_options( # pylint: disable=unused-argument + self, dialog_context: DialogContext + ) -> QnADialogResponseOptions: + """ + Gets the options the dialog will use to display query results to the user. + + :param dialog_context: The :class:'botbuilder.dialogs.DialogContext' for the current turn of conversation. + """ + + return QnADialogResponseOptions( + no_answer=self.no_answer, + active_learning_card_title=self.active_learning_card_title + or QnAMakerDialog.DEFAULT_CARD_TITLE, + card_no_match_text=self.card_no_match_text + or QnAMakerDialog.DEFAULT_CARD_NO_MATCH_TEXT, + card_no_match_response=self.card_no_match_response, + ) + + async def __call_generate_answer(self, step_context: WaterfallStepContext): + dialog_options: QnAMakerDialogOptions = ObjectPath.get_path_value( + step_context.active_dialog.state, QnAMakerDialog.KEY_OPTIONS + ) + + # Resetting context and QnAId + dialog_options.options.qna_id = 0 + dialog_options.options.context = QnARequestContext() + + # Storing the context info + step_context.values[QnAMakerDialog.PROPERTY_CURRENT_QUERY] = ( + step_context.context.activity.text + ) + + # -Check if previous context is present, if yes then put it with the query + # -Check for id if query is present in reverse index. + previous_context_data = ObjectPath.get_path_value( + step_context.active_dialog.state, QnAMakerDialog.KEY_QNA_CONTEXT_DATA, {} + ) + previous_qna_id = ObjectPath.get_path_value( + step_context.active_dialog.state, QnAMakerDialog.KEY_PREVIOUS_QNA_ID, 0 + ) + + if previous_qna_id > 0: + dialog_options.options.context = QnARequestContext( + previous_qna_id=previous_qna_id + ) + + current_qna_id = previous_context_data.get( + step_context.context.activity.text + ) + if current_qna_id: + dialog_options.options.qna_id = current_qna_id + + # Calling QnAMaker to get response. + qna_client = self._get_qnamaker_client(step_context) + response = await qna_client.get_answers_raw( + step_context.context, dialog_options.options + ) + + is_active_learning_enabled = response.active_learning_enabled + step_context.values[QnAMakerDialog.PROPERTY_QNA_DATA] = response.answers + + # Resetting previous query. + previous_qna_id = -1 + ObjectPath.set_path_value( + step_context.active_dialog.state, + QnAMakerDialog.KEY_PREVIOUS_QNA_ID, + previous_qna_id, + ) + + # Check if active learning is enabled and send card + # maximum_score_for_low_score_variation is the score above which no need to check for feedback. + if ( + response.answers + and response.answers[0].score <= self.maximum_score_for_low_score_variation + ): + # Get filtered list of the response that support low score variation criteria. + response.answers = qna_client.get_low_score_variation(response.answers) + if len(response.answers) > 1 and is_active_learning_enabled: + suggested_questions = [qna.questions[0] for qna in response.answers] + message = QnACardBuilder.get_suggestions_card( + suggested_questions, + dialog_options.response_options.active_learning_card_title, + dialog_options.response_options.card_no_match_text, + ) + await step_context.context.send_activity(message) + + ObjectPath.set_path_value( + step_context.active_dialog.state, + QnAMakerDialog.KEY_OPTIONS, + dialog_options, + ) + + await qna_client.close() + + return DialogTurnResult(DialogTurnStatus.Waiting) + + # If card is not shown, move to next step with top qna response. + result = [response.answers[0]] if response.answers else [] + step_context.values[QnAMakerDialog.PROPERTY_QNA_DATA] = result + ObjectPath.set_path_value( + step_context.active_dialog.state, QnAMakerDialog.KEY_OPTIONS, dialog_options + ) + + await qna_client.close() + + return await step_context.next(result) + + async def __call_train(self, step_context: WaterfallStepContext): + dialog_options: QnAMakerDialogOptions = ObjectPath.get_path_value( + step_context.active_dialog.state, QnAMakerDialog.KEY_OPTIONS + ) + train_responses: [QueryResult] = step_context.values[ + QnAMakerDialog.PROPERTY_QNA_DATA + ] + current_query = step_context.values[QnAMakerDialog.PROPERTY_CURRENT_QUERY] + + reply = step_context.context.activity.text + + if len(train_responses) > 1: + qna_results = [ + result for result in train_responses if result.questions[0] == reply + ] + + if qna_results: + qna_result = qna_results[0] + step_context.values[QnAMakerDialog.PROPERTY_QNA_DATA] = [qna_result] + + feedback_records = [ + FeedbackRecord( + user_id=step_context.context.activity.id, + user_question=current_query, + qna_id=qna_result.id, + ) + ] + + # Call Active Learning Train API + qna_client = self._get_qnamaker_client(step_context) + await qna_client.call_train(feedback_records) + await qna_client.close() + + return await step_context.next([qna_result]) + + if ( + reply.lower() + == dialog_options.response_options.card_no_match_text.lower() + ): + activity = dialog_options.response_options.card_no_match_response + if not activity: + await step_context.context.send_activity( + QnAMakerDialog.DEFAULT_CARD_NO_MATCH_RESPONSE + ) + else: + await step_context.context.send_activity(activity) + + return await step_context.end_dialog() + + return await super().run_step( + step_context, index=0, reason=DialogReason.BeginCalled, result=None + ) + + return await step_context.next(step_context.result) + + async def __check_for_multiturn_prompt(self, step_context: WaterfallStepContext): + dialog_options: QnAMakerDialogOptions = ObjectPath.get_path_value( + step_context.active_dialog.state, QnAMakerDialog.KEY_OPTIONS + ) + + response = step_context.result + if response and isinstance(response, List): + answer = response[0] + if answer.context and answer.context.prompts: + previous_context_data = ObjectPath.get_path_value( + step_context.active_dialog.state, + QnAMakerDialog.KEY_QNA_CONTEXT_DATA, + {}, + ) + for prompt in answer.context.prompts: + previous_context_data[prompt.display_text] = prompt.qna_id + + ObjectPath.set_path_value( + step_context.active_dialog.state, + QnAMakerDialog.KEY_QNA_CONTEXT_DATA, + previous_context_data, + ) + ObjectPath.set_path_value( + step_context.active_dialog.state, + QnAMakerDialog.KEY_PREVIOUS_QNA_ID, + answer.id, + ) + ObjectPath.set_path_value( + step_context.active_dialog.state, + QnAMakerDialog.KEY_OPTIONS, + dialog_options, + ) + + # Get multi-turn prompts card activity. + message = QnACardBuilder.get_qna_prompts_card( + answer, dialog_options.response_options.card_no_match_text + ) + await step_context.context.send_activity(message) + + return DialogTurnResult(DialogTurnStatus.Waiting) + + return await step_context.next(step_context.result) + + async def __display_qna_result(self, step_context: WaterfallStepContext): + dialog_options: QnAMakerDialogOptions = ObjectPath.get_path_value( + step_context.active_dialog.state, QnAMakerDialog.KEY_OPTIONS + ) + + reply = step_context.context.activity.text + if reply.lower() == dialog_options.response_options.card_no_match_text.lower(): + activity = dialog_options.response_options.card_no_match_response + if not activity: + await step_context.context.send_activity( + QnAMakerDialog.DEFAULT_CARD_NO_MATCH_RESPONSE + ) + else: + await step_context.context.send_activity(activity) + + return await step_context.end_dialog() + + # If previous QnAId is present, replace the dialog + previous_qna_id = ObjectPath.get_path_value( + step_context.active_dialog.state, QnAMakerDialog.KEY_PREVIOUS_QNA_ID, 0 + ) + if previous_qna_id > 0: + return await super().run_step( + step_context, index=0, reason=DialogReason.BeginCalled, result=None + ) + + # If response is present then show that response, else default answer. + response = step_context.result + if response and isinstance(response, List): + await step_context.context.send_activity(response[0].answer) + else: + activity = dialog_options.response_options.no_answer + if not activity: + await step_context.context.send_activity( + QnAMakerDialog.DEFAULT_NO_ANSWER + ) + else: + await step_context.context.send_activity(activity) + + return await step_context.end_dialog() diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/dialogs/qnamaker_dialog_options.py b/libraries/botbuilder-ai/botbuilder/ai/qna/dialogs/qnamaker_dialog_options.py new file mode 100644 index 000000000..99d0e15cf --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/dialogs/qnamaker_dialog_options.py @@ -0,0 +1,18 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .. import QnAMakerOptions, QnADialogResponseOptions + + +class QnAMakerDialogOptions: + """ + Defines Dialog Options for QnAMakerDialog. + """ + + def __init__( + self, + options: QnAMakerOptions = None, + response_options: QnADialogResponseOptions = None, + ): + self.options = options or QnAMakerOptions() + self.response_options = response_options or QnADialogResponseOptions() diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/__init__.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/__init__.py index 018d40c95..608ffeef1 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/__init__.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/__init__.py @@ -8,6 +8,7 @@ from .feedback_record import FeedbackRecord from .feedback_records import FeedbackRecords from .generate_answer_request_body import GenerateAnswerRequestBody +from .join_operator import JoinOperator from .metadata import Metadata from .prompt import Prompt from .qnamaker_trace_info import QnAMakerTraceInfo @@ -21,6 +22,7 @@ "FeedbackRecord", "FeedbackRecords", "GenerateAnswerRequestBody", + "JoinOperator", "Metadata", "Prompt", "QnAMakerTraceInfo", diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/feedback_record.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/feedback_record.py index 74a78d5d0..1a268b2d7 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/feedback_record.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/feedback_record.py @@ -5,7 +5,7 @@ class FeedbackRecord(Model): - """ Active learning feedback record. """ + """Active learning feedback record.""" _attribute_map = { "user_id": {"key": "userId", "type": "str"}, @@ -13,20 +13,8 @@ class FeedbackRecord(Model): "qna_id": {"key": "qnaId", "type": "int"}, } - def __init__(self, user_id: str, user_question: str, qna_id: int, **kwargs): - """ - Parameters: - ----------- - - user_id: ID of the user. - - user_question: User question. - - qna_id: QnA ID. - """ - + def __init__(self, **kwargs): super().__init__(**kwargs) - - self.user_id = user_id - self.user_question = user_question - self.qna_id = qna_id + self.user_id = kwargs.get("user_id", None) + self.user_question = kwargs.get("user_question", None) + self.qna_id = kwargs.get("qna_id", None) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/feedback_records.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/feedback_records.py index 62f3983c4..c9bf67ae8 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/feedback_records.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/feedback_records.py @@ -1,26 +1,14 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from typing import List - from msrest.serialization import Model -from .feedback_record import FeedbackRecord - class FeedbackRecords(Model): - """ Active learning feedback records. """ + """Active learning feedback records.""" _attribute_map = {"records": {"key": "records", "type": "[FeedbackRecord]"}} - def __init__(self, records: List[FeedbackRecord], **kwargs): - """ - Parameter(s): - ------------- - - records: List of feedback records. - """ - + def __init__(self, **kwargs): super().__init__(**kwargs) - - self.records = records + self.records = kwargs.get("records", None) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/generate_answer_request_body.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/generate_answer_request_body.py index 20162a08f..473fc2bc2 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/generate_answer_request_body.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/generate_answer_request_body.py @@ -1,17 +1,11 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from typing import List - from msrest.serialization import Model -from .metadata import Metadata -from .qna_request_context import QnARequestContext -from .ranker_types import RankerTypes - class GenerateAnswerRequestBody(Model): - """ Question used as the payload body for QnA Maker's Generate Answer API. """ + """Question used as the payload body for QnA Maker's Generate Answer API.""" _attribute_map = { "question": {"key": "question", "type": "str"}, @@ -22,49 +16,22 @@ class GenerateAnswerRequestBody(Model): "qna_id": {"key": "qnaId", "type": "int"}, "is_test": {"key": "isTest", "type": "bool"}, "ranker_type": {"key": "rankerType", "type": "RankerTypes"}, + "strict_filters_join_operator": { + "key": "strictFiltersCompoundOperationType", + "type": "str", + }, } - def __init__( - self, - question: str, - top: int, - score_threshold: float, - strict_filters: List[Metadata], - context: QnARequestContext = None, - qna_id: int = None, - is_test: bool = False, - ranker_type: str = RankerTypes.DEFAULT, - **kwargs - ): - """ - Parameters: - ----------- - - question: The user question to query against the knowledge base. - - top: Max number of answers to be returned for the question. - - score_threshold: Threshold for answers returned based on score. - - strict_filters: Find answers that contains these metadata. - - context: The context from which the QnA was extracted. - - qna_id: Id of the current question asked. - - is_test: (Optional) A value indicating whether to call test or prod environment of knowledgebase. - - ranker_types: (Optional) Ranker types. - - """ - + def __init__(self, **kwargs): super().__init__(**kwargs) - - self.question = question - self.top = top - self.score_threshold = score_threshold - self.strict_filters = strict_filters - self.context = context - self.qna_id = qna_id - self.is_test = is_test - self.ranker_type = ranker_type + self.question = kwargs.get("question", None) + self.top = kwargs.get("top", None) + self.score_threshold = kwargs.get("score_threshold", None) + self.strict_filters = kwargs.get("strict_filters", None) + self.context = kwargs.get("context", None) + self.qna_id = kwargs.get("qna_id", None) + self.is_test = kwargs.get("is_test", None) + self.ranker_type = kwargs.get("ranker_type", None) + self.strict_filters_join_operator = kwargs.get( + "strict_filters_join_operator", None + ) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/join_operator.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/join_operator.py new file mode 100644 index 000000000..67b7ba1bd --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/join_operator.py @@ -0,0 +1,21 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from enum import Enum + + +class JoinOperator(str, Enum): + """ + Join Operator for Strict Filters. + + remarks + -------- + For example, when using multiple filters in a query, if you want results that + have metadata that matches all filters, then use `AND` operator. + + If instead you only wish that the results from knowledge base match + at least one of the filters, then use `OR` operator. + """ + + AND = "AND" + OR = "OR" diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/metadata.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/metadata.py index af0f1f00b..8f93fff76 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/metadata.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/metadata.py @@ -5,24 +5,14 @@ class Metadata(Model): - """ Metadata associated with the answer. """ + """Metadata associated with the answer.""" _attribute_map = { "name": {"key": "name", "type": "str"}, "value": {"key": "value", "type": "str"}, } - def __init__(self, name: str, value: str, **kwargs): - """ - Parameters: - ----------- - - name: Metadata name. Max length: 100. - - value: Metadata value. Max length: 100. - """ - + def __init__(self, **kwargs): super().__init__(**kwargs) - - self.name = name - self.value = value + self.name = kwargs.get("name", None) + self.value = kwargs.get("value", None) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/prompt.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/prompt.py index d7f090c87..6a347ce14 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/prompt.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/prompt.py @@ -5,7 +5,7 @@ class Prompt(Model): - """ Prompt Object. """ + """Prompt Object.""" _attribute_map = { "display_order": {"key": "displayOrder", "type": "int"}, @@ -14,32 +14,9 @@ class Prompt(Model): "display_text": {"key": "displayText", "type": "str"}, } - def __init__( - self, - *, - display_order: int, - qna_id: int, - display_text: str, - qna: object = None, - **kwargs - ): - """ - Parameters: - ----------- - - display_order: Index of the prompt - used in ordering of the prompts. - - qna_id: QnA ID. - - display_text: Text displayed to represent a follow up question prompt. - - qna: The QnA object returned from the API (Optional). - - """ - - super(Prompt, self).__init__(**kwargs) - - self.display_order = display_order - self.qna_id = qna_id - self.display_text = display_text - self.qna = qna + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.display_order = kwargs.get("display_order", None) + self.qna_id = kwargs.get("qna_id", None) + self.display_text = kwargs.get("display_text", None) + self.qna = kwargs.get("qna", None) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/qna_request_context.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/qna_request_context.py index ae3342a76..ff85afc99 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/qna_request_context.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/qna_request_context.py @@ -1,31 +1,21 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from msrest.serialization import Model - - -class QnARequestContext(Model): - """ - The context associated with QnA. - Used to mark if the current prompt is relevant with a previous question or not. - """ - - _attribute_map = { - "previous_qna_id": {"key": "previousQnAId", "type": "int"}, - "prvious_user_query": {"key": "previousUserQuery", "type": "string"}, - } - - def __init__(self, previous_qna_id: int, prvious_user_query: str, **kwargs): - """ - Parameters: - ----------- - - previous_qna_id: The previous QnA Id that was returned. - - prvious_user_query: The previous user query/question. - """ - - super().__init__(**kwargs) - - self.previous_qna_id = previous_qna_id - self.prvious_user_query = prvious_user_query +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from msrest.serialization import Model + + +class QnARequestContext(Model): + """ + The context associated with QnA. + Used to mark if the current prompt is relevant with a previous question or not. + """ + + _attribute_map = { + "previous_qna_id": {"key": "previousQnAId", "type": "int"}, + "previous_user_query": {"key": "previousUserQuery", "type": "string"}, + } + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.previous_qna_id = kwargs.get("previous_qna_id", None) + self.previous_user_query = kwargs.get("previous_user_query", None) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/qna_response_context.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/qna_response_context.py index 537bf09db..643180779 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/qna_response_context.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/qna_response_context.py @@ -1,9 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from typing import List from msrest.serialization import Model -from .prompt import Prompt class QnAResponseContext(Model): @@ -17,11 +15,9 @@ class QnAResponseContext(Model): "prompts": {"key": "prompts", "type": "[Prompt]"}, } - def __init__( - self, *, is_context_only: bool = False, prompts: List[Prompt] = None, **kwargs - ): + def __init__(self, **kwargs): """ - Parameters: + Parameters ----------- is_context_only: Whether this prompt is context only. @@ -30,6 +26,6 @@ def __init__( """ - super(QnAResponseContext, self).__init__(**kwargs) - self.is_context_only = is_context_only - self.prompts = prompts + super().__init__(**kwargs) + self.is_context_only = kwargs.get("is_context_only", None) + self.prompts = kwargs.get("prompts", None) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/qnamaker_trace_info.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/qnamaker_trace_info.py index f585e5c26..1d15a93ed 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/qnamaker_trace_info.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/qnamaker_trace_info.py @@ -11,7 +11,7 @@ class QnAMakerTraceInfo: - """ Represents all the trace info that we collect from the QnAMaker Middleware. """ + """Represents all the trace info that we collect from the QnAMaker Middleware.""" def __init__( self, @@ -27,7 +27,7 @@ def __init__( ranker_type: str = RankerTypes.DEFAULT, ): """ - Parameters: + Parameters ----------- message: Message which instigated the query to QnA Maker. diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/query_result.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/query_result.py index 321ea64cf..c0ca858f1 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/query_result.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/query_result.py @@ -1,60 +1,28 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from typing import List from msrest.serialization import Model -from .metadata import Metadata -from .qna_response_context import QnAResponseContext class QueryResult(Model): - """ Represents an individual result from a knowledge base query. """ + """Represents an individual result from a knowledge base query.""" _attribute_map = { "questions": {"key": "questions", "type": "[str]"}, "answer": {"key": "answer", "type": "str"}, "score": {"key": "score", "type": "float"}, - "metadata": {"key": "metadata", "type": "object"}, + "metadata": {"key": "metadata", "type": "[Metadata]"}, "source": {"key": "source", "type": "str"}, "id": {"key": "id", "type": "int"}, - "context": {"key": "context", "type": "object"}, + "context": {"key": "context", "type": "QnAResponseContext"}, } - def __init__( - self, - *, - questions: List[str], - answer: str, - score: float, - metadata: object = None, - source: str = None, - id: int = None, # pylint: disable=invalid-name - context: QnAResponseContext = None, - **kwargs - ): - """ - Parameters: - ----------- - - questions: The list of questions indexed in the QnA Service for the given answer (if any). - - answer: Answer from the knowledge base. - - score: Confidence on a scale from 0.0 to 1.0 that the answer matches the user's intent. - - metadata: Metadata associated with the answer (if any). - - source: The source from which the QnA was extracted (if any). - - id: The index of the answer in the knowledge base. V3 uses 'qnaId', V4 uses 'id' (if any). - - context: The context from which the QnA was extracted. - """ - super(QueryResult, self).__init__(**kwargs) - self.questions = questions - self.answer = answer - self.score = score - self.metadata = list(map(lambda meta: Metadata(**meta), metadata)) - self.source = source - self.context = QnAResponseContext(**context) if context is not None else None - self.id = id # pylint: disable=invalid-name + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.questions = kwargs.get("questions", None) + self.answer = kwargs.get("answer", None) + self.score = kwargs.get("score", None) + self.metadata = kwargs.get("metadata", None) + self.source = kwargs.get("source", None) + self.context = kwargs.get("context", None) + self.id = kwargs.get("id", None) # pylint: disable=invalid-name diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/query_results.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/query_results.py index 17fd2a2c8..46d2cfa93 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/query_results.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/query_results.py @@ -7,7 +7,7 @@ class QueryResults(Model): - """ Contains answers for a user query. """ + """Contains answers for a user query.""" _attribute_map = { "answers": {"key": "answers", "type": "[QueryResult]"}, @@ -18,13 +18,13 @@ def __init__( self, answers: List[QueryResult], active_learning_enabled: bool = None, **kwargs ): """ - Parameters: + Parameters ----------- answers: The answers for a user query. active_learning_enabled: The active learning enable flag. """ - super(QueryResults, self).__init__(**kwargs) + super().__init__(**kwargs) self.answers = answers self.active_learning_enabled = active_learning_enabled diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/ranker_types.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/ranker_types.py index a3f0463ca..811d61623 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/ranker_types.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/ranker_types.py @@ -3,8 +3,7 @@ class RankerTypes: - - """ Default Ranker Behaviour. i.e. Ranking based on Questions and Answer. """ + """Default Ranker Behaviour. i.e. Ranking based on Questions and Answer.""" DEFAULT = "Default" diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/models/train_request_body.py b/libraries/botbuilder-ai/botbuilder/ai/qna/models/train_request_body.py index 2ce267831..29b2a0258 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/models/train_request_body.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/models/train_request_body.py @@ -1,27 +1,16 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from typing import List from msrest.serialization import Model -from .feedback_record import FeedbackRecord - class TrainRequestBody(Model): - """ Class the models the request body that is sent as feedback to the Train API. """ + """Class the models the request body that is sent as feedback to the Train API.""" _attribute_map = { "feedback_records": {"key": "feedbackRecords", "type": "[FeedbackRecord]"} } - def __init__(self, feedback_records: List[FeedbackRecord], **kwargs): - """ - Parameters: - ----------- - - feedback_records: List of feedback records. - """ - + def __init__(self, **kwargs): super().__init__(**kwargs) - - self.feedback_records = feedback_records + self.feedback_records = kwargs.get("feedback_records", None) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/qna_dialog_response_options.py b/libraries/botbuilder-ai/botbuilder/ai/qna/qna_dialog_response_options.py new file mode 100644 index 000000000..5490f7727 --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/qna_dialog_response_options.py @@ -0,0 +1,18 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.schema import Activity + + +class QnADialogResponseOptions: + def __init__( + self, + active_learning_card_title: str = None, + card_no_match_text: str = None, + no_answer: Activity = None, + card_no_match_response: Activity = None, + ): + self.active_learning_card_title = active_learning_card_title + self.card_no_match_text = card_no_match_text + self.no_answer = no_answer + self.card_no_match_response = card_no_match_response diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker.py b/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker.py index f01aeb453..773c487e6 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker.py @@ -1,271 +1,268 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import json -from typing import Dict, List, NamedTuple, Union -from aiohttp import ClientSession, ClientTimeout - -from botbuilder.schema import Activity -from botbuilder.core import BotTelemetryClient, NullTelemetryClient, TurnContext - -from .models import FeedbackRecord, QueryResult, QueryResults -from .utils import ( - ActiveLearningUtils, - GenerateAnswerUtils, - QnATelemetryConstants, - TrainUtils, -) -from .qnamaker_endpoint import QnAMakerEndpoint -from .qnamaker_options import QnAMakerOptions -from .qnamaker_telemetry_client import QnAMakerTelemetryClient - -from .. import __title__, __version__ - - -class EventData(NamedTuple): - properties: Dict[str, str] - metrics: Dict[str, float] - - -class QnAMaker(QnAMakerTelemetryClient): - """ - Class used to query a QnA Maker knowledge base for answers. - """ - - def __init__( - self, - endpoint: QnAMakerEndpoint, - options: QnAMakerOptions = None, - http_client: ClientSession = None, - telemetry_client: BotTelemetryClient = None, - log_personal_information: bool = None, - ): - super().__init__(log_personal_information, telemetry_client) - - if not isinstance(endpoint, QnAMakerEndpoint): - raise TypeError( - "QnAMaker.__init__(): endpoint is not an instance of QnAMakerEndpoint" - ) - - self._endpoint: str = endpoint - - opt = options or QnAMakerOptions() - self._validate_options(opt) - - instance_timeout = ClientTimeout(total=opt.timeout / 1000) - self._http_client = http_client or ClientSession(timeout=instance_timeout) - - self.telemetry_client: Union[ - BotTelemetryClient, NullTelemetryClient - ] = telemetry_client or NullTelemetryClient() - - self.log_personal_information = log_personal_information or False - - self._generate_answer_helper = GenerateAnswerUtils( - self.telemetry_client, self._endpoint, options, self._http_client - ) - self._active_learning_train_helper = TrainUtils( - self._endpoint, self._http_client - ) - - async def get_answers( - self, - context: TurnContext, - options: QnAMakerOptions = None, - telemetry_properties: Dict[str, str] = None, - telemetry_metrics: Dict[str, int] = None, - ) -> [QueryResult]: - """ - Generates answers from the knowledge base. - - return: - ------- - A list of answers for the user's query, sorted in decreasing order of ranking score. - - rtype: - ------ - List[QueryResult] - """ - result = await self.get_answers_raw( - context, options, telemetry_properties, telemetry_metrics - ) - - return result.answers - - async def get_answers_raw( - self, - context: TurnContext, - options: QnAMakerOptions = None, - telemetry_properties: Dict[str, str] = None, - telemetry_metrics: Dict[str, int] = None, - ) -> QueryResults: - """ - Generates raw answers from the knowledge base. - - return: - ------- - A list of answers for the user's query, sorted in decreasing order of ranking score. - - rtype: - ------ - QueryResults - """ - if not context: - raise TypeError("QnAMaker.get_answers(): context cannot be None.") - - if not isinstance(context.activity, Activity): - raise TypeError( - "QnAMaker.get_answers(): TurnContext's activity must be an Activity instance." - ) - - result = await self._generate_answer_helper.get_answers_raw(context, options) - - await self.on_qna_result( - result.answers, context, telemetry_properties, telemetry_metrics - ) - - return result - - def get_low_score_variation(self, query_result: QueryResult) -> List[QueryResult]: - """ - Filters the ambiguous question for active learning. - - Parameters: - ----------- - query_result: User query output. - - Return: - ------- - Filtered aray of ambigous questions. - """ - return ActiveLearningUtils.get_low_score_variation(query_result) - - async def call_train(self, feedback_records: List[FeedbackRecord]): - """ - Sends feedback to the knowledge base. - - Parameters: - ----------- - feedback_records - """ - return await self._active_learning_train_helper.call_train(feedback_records) - - async def on_qna_result( - self, - query_results: [QueryResult], - turn_context: TurnContext, - telemetry_properties: Dict[str, str] = None, - telemetry_metrics: Dict[str, float] = None, - ): - event_data = await self.fill_qna_event( - query_results, turn_context, telemetry_properties, telemetry_metrics - ) - - # Track the event - self.telemetry_client.track_event( - name=QnATelemetryConstants.qna_message_event, - properties=event_data.properties, - measurements=event_data.metrics, - ) - - async def fill_qna_event( - self, - query_results: [QueryResult], - turn_context: TurnContext, - telemetry_properties: Dict[str, str] = None, - telemetry_metrics: Dict[str, float] = None, - ) -> EventData: - """ - Fills the event properties and metrics for the QnaMessage event for telemetry. - - return: - ------- - A tuple of event data properties and metrics that will be sent to the - BotTelemetryClient.track_event() method for the QnAMessage event. - The properties and metrics returned the standard properties logged - with any properties passed from the get_answers() method. - - rtype: - ------ - EventData - """ - - properties: Dict[str, str] = dict() - metrics: Dict[str, float] = dict() - - properties[ - QnATelemetryConstants.knowledge_base_id_property - ] = self._endpoint.knowledge_base_id - - text: str = turn_context.activity.text - user_name: str = turn_context.activity.from_property.name - - # Use the LogPersonalInformation flag to toggle logging PII data; text and username are common examples. - if self.log_personal_information: - if text: - properties[QnATelemetryConstants.question_property] = text - - if user_name: - properties[QnATelemetryConstants.username_property] = user_name - - # Fill in Qna Results (found or not). - if self._has_matched_answer_in_kb(query_results): - query_result = query_results[0] - - result_properties = { - QnATelemetryConstants.matched_question_property: json.dumps( - query_result.questions - ), - QnATelemetryConstants.question_id_property: str(query_result.id), - QnATelemetryConstants.answer_property: query_result.answer, - QnATelemetryConstants.article_found_property: "true", - } - properties.update(result_properties) - - metrics[QnATelemetryConstants.score_metric] = query_result.score - else: - no_match_properties = { - QnATelemetryConstants.matched_question_property: "No Qna Question matched", - QnATelemetryConstants.question_id_property: "No Qna Question Id matched", - QnATelemetryConstants.answer_property: "No Qna Answer matched", - QnATelemetryConstants.article_found_property: "false", - } - - properties.update(no_match_properties) - - # Additional Properties can override "stock" properties. - if telemetry_properties: - properties.update(telemetry_properties) - - # Additional Metrics can override "stock" metrics. - if telemetry_metrics: - metrics.update(telemetry_metrics) - - return EventData(properties=properties, metrics=metrics) - - def _validate_options(self, options: QnAMakerOptions): - if not options.score_threshold: - options.score_threshold = 0.3 - - if not options.top: - options.top = 1 - - if options.score_threshold < 0 or options.score_threshold > 1: - raise ValueError("Score threshold should be a value between 0 and 1") - - if options.top < 1: - raise ValueError("QnAMakerOptions.top should be an integer greater than 0") - - if not options.strict_filters: - options.strict_filters = [] - - if not options.timeout: - options.timeout = 100000 - - def _has_matched_answer_in_kb(self, query_results: [QueryResult]) -> bool: - if query_results: - if query_results[0].id != -1: - - return True - - return False +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import asyncio +import json +from typing import Dict, List, NamedTuple, Union +from aiohttp import ClientSession, ClientTimeout + +from botbuilder.schema import Activity +from botbuilder.core import BotTelemetryClient, NullTelemetryClient, TurnContext + +from .models import FeedbackRecord, QueryResult, QueryResults +from .utils import ( + ActiveLearningUtils, + GenerateAnswerUtils, + QnATelemetryConstants, + TrainUtils, +) +from .qnamaker_endpoint import QnAMakerEndpoint +from .qnamaker_options import QnAMakerOptions +from .qnamaker_telemetry_client import QnAMakerTelemetryClient + +from .. import __title__, __version__ + + +class EventData(NamedTuple): + properties: Dict[str, str] + metrics: Dict[str, float] + + +class QnAMaker(QnAMakerTelemetryClient): + """ + Class used to query a QnA Maker knowledge base for answers. + """ + + def __init__( + self, + endpoint: QnAMakerEndpoint, + options: QnAMakerOptions = None, + http_client: ClientSession = None, + telemetry_client: BotTelemetryClient = None, + log_personal_information: bool = None, + ): + super().__init__(log_personal_information, telemetry_client) + + if not isinstance(endpoint, QnAMakerEndpoint): + raise TypeError( + "QnAMaker.__init__(): endpoint is not an instance of QnAMakerEndpoint" + ) + + self._endpoint: str = endpoint + + opt = options or QnAMakerOptions() + self._validate_options(opt) + + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + + instance_timeout = ClientTimeout(total=opt.timeout / 1000) + self._http_client = http_client or ClientSession( + timeout=instance_timeout, loop=loop + ) + + self.telemetry_client: Union[BotTelemetryClient, NullTelemetryClient] = ( + telemetry_client or NullTelemetryClient() + ) + + self.log_personal_information = log_personal_information or False + + self._generate_answer_helper = GenerateAnswerUtils( + self.telemetry_client, self._endpoint, options, self._http_client + ) + self._active_learning_train_helper = TrainUtils( + self._endpoint, self._http_client + ) + + async def close(self): + await self._http_client.close() + + async def get_answers( + self, + context: TurnContext, + options: QnAMakerOptions = None, + telemetry_properties: Dict[str, str] = None, + telemetry_metrics: Dict[str, int] = None, + ) -> [QueryResult]: + """ + Generates answers from the knowledge base. + + :return: A list of answers for the user's query, sorted in decreasing order of ranking score. + :rtype: :class:`typing.List[QueryResult]` + """ + result = await self.get_answers_raw( + context, options, telemetry_properties, telemetry_metrics + ) + + return result.answers + + async def get_answers_raw( + self, + context: TurnContext, + options: QnAMakerOptions = None, + telemetry_properties: Dict[str, str] = None, + telemetry_metrics: Dict[str, int] = None, + ) -> QueryResults: + """ + Generates raw answers from the knowledge base. + + :return: A list of answers for the user's query, sorted in decreasing order of ranking score. + :rtype: :class:`QueryResult` + """ + if not context: + raise TypeError("QnAMaker.get_answers(): context cannot be None.") + + if not isinstance(context.activity, Activity): + raise TypeError( + "QnAMaker.get_answers(): TurnContext's activity must be an Activity instance." + ) + + result = await self._generate_answer_helper.get_answers_raw(context, options) + + await self.on_qna_result( + result.answers, context, telemetry_properties, telemetry_metrics + ) + + return result + + def get_low_score_variation(self, query_result: QueryResult) -> List[QueryResult]: + """ + Filters the ambiguous question for active learning. + + :param query_result: User query output. + :type query_result: :class:`QueryResult` + :return: Filtered array of ambiguous questions. + :rtype: :class:`typing.List[QueryResult]` + """ + return ActiveLearningUtils.get_low_score_variation(query_result) + + async def call_train(self, feedback_records: List[FeedbackRecord]): + """ + Sends feedback to the knowledge base. + + :param feedback_records: Feedback records. + :type feedback_records: :class:`typing.List[FeedbackRecord]` + """ + return await self._active_learning_train_helper.call_train(feedback_records) + + async def on_qna_result( + self, + query_results: [QueryResult], + turn_context: TurnContext, + telemetry_properties: Dict[str, str] = None, + telemetry_metrics: Dict[str, float] = None, + ): + event_data = await self.fill_qna_event( + query_results, turn_context, telemetry_properties, telemetry_metrics + ) + + # Track the event + self.telemetry_client.track_event( + name=QnATelemetryConstants.qna_message_event, + properties=event_data.properties, + measurements=event_data.metrics, + ) + + async def fill_qna_event( + self, + query_results: [QueryResult], + turn_context: TurnContext, + telemetry_properties: Dict[str, str] = None, + telemetry_metrics: Dict[str, float] = None, + ) -> EventData: + """ + Fills the event properties and metrics for the QnaMessage event for telemetry. + + :param query_results: QnA service results. + :type quert_results: :class:`QueryResult` + :param turn_context: Context object containing information for a single turn of conversation with a user. + :type turn_context: :class:`botbuilder.core.TurnContext` + :param telemetry_properties: Properties to add/override for the event. + :type telemetry_properties: :class:`typing.Dict[str, str]` + :param telemetry_metrics: Metrics to add/override for the event. + :type telemetry_metrics: :class:`typing.Dict[str, float]` + :return: Event properties and metrics for the QnaMessage event for telemetry. + :rtype: :class:`EventData` + """ + + properties: Dict[str, str] = dict() + metrics: Dict[str, float] = dict() + + properties[QnATelemetryConstants.knowledge_base_id_property] = ( + self._endpoint.knowledge_base_id + ) + + text: str = turn_context.activity.text + user_name: str = turn_context.activity.from_property.name + + # Use the LogPersonalInformation flag to toggle logging PII data; text and username are common examples. + if self.log_personal_information: + if text: + properties[QnATelemetryConstants.question_property] = text + + if user_name: + properties[QnATelemetryConstants.username_property] = user_name + + # Fill in Qna Results (found or not). + if self._has_matched_answer_in_kb(query_results): + query_result = query_results[0] + + result_properties = { + QnATelemetryConstants.matched_question_property: json.dumps( + query_result.questions + ), + QnATelemetryConstants.question_id_property: str(query_result.id), + QnATelemetryConstants.answer_property: query_result.answer, + QnATelemetryConstants.article_found_property: "true", + } + properties.update(result_properties) + + metrics[QnATelemetryConstants.score_metric] = query_result.score + else: + no_match_properties = { + QnATelemetryConstants.matched_question_property: "No Qna Question matched", + QnATelemetryConstants.question_id_property: "No Qna Question Id matched", + QnATelemetryConstants.answer_property: "No Qna Answer matched", + QnATelemetryConstants.article_found_property: "false", + } + + properties.update(no_match_properties) + + # Additional Properties can override "stock" properties. + if telemetry_properties: + properties.update(telemetry_properties) + + # Additional Metrics can override "stock" metrics. + if telemetry_metrics: + metrics.update(telemetry_metrics) + + return EventData(properties=properties, metrics=metrics) + + def _validate_options(self, options: QnAMakerOptions): + if not options.score_threshold: + options.score_threshold = 0.3 + + if not options.top: + options.top = 1 + + if options.score_threshold < 0 or options.score_threshold > 1: + raise ValueError("Score threshold should be a value between 0 and 1") + + if options.top < 1: + raise ValueError("QnAMakerOptions.top should be an integer greater than 0") + + if not options.strict_filters: + options.strict_filters = [] + + if not options.timeout: + options.timeout = 100000 + + def _has_matched_answer_in_kb(self, query_results: [QueryResult]) -> bool: + if query_results: + if query_results[0].id != -1: + return True + + return False diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker_options.py b/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker_options.py index d93b1cd1f..72dfe4e9d 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker_options.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/qnamaker_options.py @@ -1,27 +1,64 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .models import Metadata, QnARequestContext -from .models.ranker_types import RankerTypes - -# figure out if 300 milliseconds is ok for python requests library...or 100000 -class QnAMakerOptions: - def __init__( - self, - score_threshold: float = 0.0, - timeout: int = 0, - top: int = 0, - strict_filters: [Metadata] = None, - context: [QnARequestContext] = None, - qna_id: int = None, - is_test: bool = False, - ranker_type: bool = RankerTypes.DEFAULT, - ): - self.score_threshold = score_threshold - self.timeout = timeout - self.top = top - self.strict_filters = strict_filters or [] - self.context = context - self.qna_id = qna_id - self.is_test = is_test - self.ranker_type = ranker_type +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .models import Metadata, QnARequestContext +from .models.ranker_types import RankerTypes +from .models.join_operator import JoinOperator + + +class QnAMakerOptions: + """ + Defines options used to configure a `QnAMaker` instance. + + remarks + -------- + All parameters are optional. + """ + + def __init__( + self, + score_threshold: float = 0.0, + timeout: int = 0, + top: int = 0, + strict_filters: [Metadata] = None, + context: [QnARequestContext] = None, + qna_id: int = None, + is_test: bool = False, + ranker_type: str = RankerTypes.DEFAULT, + strict_filters_join_operator: str = JoinOperator.AND, + ): + """ + Parameters + ----------- + score_threshold (float): + The minimum score threshold, used to filter returned results. + Values range from score of 0.0 to 1.0. + timeout (int): + The time in milliseconds to wait before the request times out. + top (int): + The number of ranked results to return. + strict_filters ([Metadata]): + Filters to use on queries to a QnA knowledge base, based on a + QnA pair's metadata. + context ([QnARequestContext]): + The context of the previous turn. + qna_id (int): + Id of the current question asked (if available). + is_test (bool): + A value indicating whether to call test or prod environment of a knowledge base. + ranker_type (str): + The QnA ranker type to use. + strict_filters_join_operator (str): + A value indicating how strictly you want to apply strict_filters on QnA pairs' metadata. + For example, when combining several metadata filters, you can determine if you are + concerned with all filters matching or just at least one filter matching. + """ + self.score_threshold = score_threshold + self.timeout = timeout + self.top = top + self.strict_filters = strict_filters or [] + self.context = context + self.qna_id = qna_id + self.is_test = is_test + self.ranker_type = ranker_type + self.strict_filters_join_operator = strict_filters_join_operator diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/__init__.py b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/__init__.py index 58d8575e0..e4669b2aa 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/__init__.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/__init__.py @@ -1,20 +1,22 @@ -# coding=utf-8 -# -------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -------------------------------------------------------------------------- - -from .active_learning_utils import ActiveLearningUtils -from .generate_answer_utils import GenerateAnswerUtils -from .http_request_utils import HttpRequestUtils -from .qna_telemetry_constants import QnATelemetryConstants -from .train_utils import TrainUtils - -__all__ = [ - "ActiveLearningUtils", - "GenerateAnswerUtils", - "HttpRequestUtils", - "QnATelemetryConstants", - "TrainUtils", -] +# coding=utf-8 +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +from .active_learning_utils import ActiveLearningUtils +from .generate_answer_utils import GenerateAnswerUtils +from .http_request_utils import HttpRequestUtils +from .qna_telemetry_constants import QnATelemetryConstants +from .train_utils import TrainUtils +from .qna_card_builder import QnACardBuilder + +__all__ = [ + "ActiveLearningUtils", + "GenerateAnswerUtils", + "HttpRequestUtils", + "QnATelemetryConstants", + "TrainUtils", + "QnACardBuilder", +] diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/active_learning_utils.py b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/active_learning_utils.py index 625d77dbc..3b549ce1c 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/active_learning_utils.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/active_learning_utils.py @@ -13,7 +13,7 @@ class ActiveLearningUtils: - """ Active learning helper class """ + """Active learning helper class""" @staticmethod def get_low_score_variation( @@ -22,7 +22,7 @@ def get_low_score_variation( """ Returns a list of QnA search results, which have low score variation. - Parameters: + Parameters ----------- qna_serach_results: A list of QnA QueryResults returned from the QnA GenerateAnswer API call. diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/generate_answer_utils.py b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/generate_answer_utils.py index 3852f1365..2a8209ec5 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/generate_answer_utils.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/generate_answer_utils.py @@ -2,7 +2,9 @@ # Licensed under the MIT License. from copy import copy -from typing import List, Union +from typing import Any, List, Union +import json +import requests from aiohttp import ClientResponse, ClientSession @@ -39,7 +41,7 @@ def __init__( http_client: ClientSession, ): """ - Parameters: + Parameters ----------- telemetry_client: Telemetry client. @@ -109,7 +111,8 @@ def _hydrate_options(self, query_options: QnAMakerOptions) -> QnAMakerOptions: with the options passed as arguments into get_answers(). Return: ------- - QnAMakerOptions with options passed into constructor overwritten by new options passed into get_answers() + QnAMakerOptions with options passed into constructor overwritten + by new options passed into get_answers() rtype: ------ @@ -141,6 +144,9 @@ def _hydrate_options(self, query_options: QnAMakerOptions) -> QnAMakerOptions: hydrated_options.qna_id = query_options.qna_id hydrated_options.is_test = query_options.is_test hydrated_options.ranker_type = query_options.ranker_type + hydrated_options.strict_filters_join_operator = ( + query_options.strict_filters_join_operator + ) return hydrated_options @@ -158,11 +164,12 @@ async def _query_qna_service( qna_id=options.qna_id, is_test=options.is_test, ranker_type=options.ranker_type, + strict_filters_join_operator=options.strict_filters_join_operator, ) http_request_helper = HttpRequestUtils(self._http_client) - response: ClientResponse = await http_request_helper.execute_http_request( + response: Any = await http_request_helper.execute_http_request( url, question, self._endpoint, options.timeout ) @@ -200,21 +207,26 @@ async def _format_qna_result( self, result, options: QnAMakerOptions ) -> QueryResults: json_res = result + if isinstance(result, ClientResponse): json_res = await result.json() + if isinstance(result, requests.Response): + json_res = json.loads(result.text) + answers_within_threshold = [ {**answer, "score": answer["score"] / 100} for answer in json_res["answers"] if answer["score"] / 100 > options.score_threshold ] + sorted_answers = sorted( answers_within_threshold, key=lambda ans: ans["score"], reverse=True ) - answers_as_query_results = list( - map(lambda answer: QueryResult(**answer), sorted_answers) - ) + answers_as_query_results = [ + QueryResult().deserialize(answer) for answer in sorted_answers + ] active_learning_enabled = ( json_res["activeLearningEnabled"] diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/http_request_utils.py b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/http_request_utils.py index d550f8ad0..8251471c7 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/http_request_utils.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/http_request_utils.py @@ -1,95 +1,140 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import json -import platform - -from aiohttp import ClientResponse, ClientSession, ClientTimeout - -from ... import __title__, __version__ - -from ..qnamaker_endpoint import QnAMakerEndpoint - - -class HttpRequestUtils: - """ HTTP request utils class. """ - - def __init__(self, http_client: ClientSession): - self._http_client = http_client - - async def execute_http_request( - self, - request_url: str, - payload_body: object, - endpoint: QnAMakerEndpoint, - timeout: float = None, - ) -> ClientResponse: - """ - Execute HTTP request. - - Parameters: - ----------- - - request_url: HTTP request URL. - - payload_body: HTTP request body. - - endpoint: QnA Maker endpoint details. - - timeout: Timeout for HTTP call (milliseconds). - """ - if not request_url: - raise TypeError( - "HttpRequestUtils.execute_http_request(): request_url cannot be None." - ) - - if not payload_body: - raise TypeError( - "HttpRequestUtils.execute_http_request(): question cannot be None." - ) - - if not endpoint: - raise TypeError( - "HttpRequestUtils.execute_http_request(): endpoint cannot be None." - ) - - serialized_payload_body = json.dumps(payload_body.serialize()) - - headers = self._get_headers(endpoint) - - if timeout: - # Convert miliseconds to seconds (as other BotBuilder SDKs accept timeout value in miliseconds) - # aiohttp.ClientSession units are in seconds - request_timeout = ClientTimeout(total=timeout / 1000) - - response: ClientResponse = await self._http_client.post( - request_url, - data=serialized_payload_body, - headers=headers, - timeout=request_timeout, - ) - else: - response: ClientResponse = await self._http_client.post( - request_url, data=serialized_payload_body, headers=headers - ) - - return response - - def _get_headers(self, endpoint: QnAMakerEndpoint): - headers = { - "Content-Type": "application/json", - "User-Agent": self._get_user_agent(), - "Authorization": f"EndpointKey {endpoint.endpoint_key}", - } - - return headers - - def _get_user_agent(self): - package_user_agent = f"{__title__}/{__version__}" - uname = platform.uname() - os_version = f"{uname.machine}-{uname.system}-{uname.version}" - py_version = f"Python,Version={platform.python_version()}" - platform_user_agent = f"({os_version}; {py_version})" - user_agent = f"{package_user_agent} {platform_user_agent}" - - return user_agent +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +import platform +from typing import Any +import requests + +from aiohttp import ClientResponse, ClientSession, ClientTimeout + +from ... import __title__, __version__ + +from ..qnamaker_endpoint import QnAMakerEndpoint + + +class HttpRequestUtils: + """HTTP request utils class. + + Parameters + ----------- + + http_client: Client to make HTTP requests with. Default client used in the SDK is `aiohttp.ClientSession`. + """ + + def __init__(self, http_client: Any): + self._http_client = http_client + + async def execute_http_request( + self, + request_url: str, + payload_body: object, + endpoint: QnAMakerEndpoint, + timeout: float = None, + ) -> Any: + """ + Execute HTTP request. + + Parameters + ----------- + + request_url: HTTP request URL. + + payload_body: HTTP request body. + + endpoint: QnA Maker endpoint details. + + timeout: Timeout for HTTP call (milliseconds). + """ + if not request_url: + raise TypeError( + "HttpRequestUtils.execute_http_request(): request_url cannot be None." + ) + + if not payload_body: + raise TypeError( + "HttpRequestUtils.execute_http_request(): question cannot be None." + ) + + if not endpoint: + raise TypeError( + "HttpRequestUtils.execute_http_request(): endpoint cannot be None." + ) + + serialized_payload_body = json.dumps(payload_body.serialize()) + + headers = self._get_headers(endpoint) + + if isinstance(self._http_client, ClientSession): + response: ClientResponse = await self._make_request_with_aiohttp( + request_url, serialized_payload_body, headers, timeout + ) + elif self._is_using_requests_module(): + response: requests.Response = self._make_request_with_requests( + request_url, serialized_payload_body, headers, timeout + ) + else: + response = await self._http_client.post( + request_url, data=serialized_payload_body, headers=headers + ) + + return response + + def _get_headers(self, endpoint: QnAMakerEndpoint): + headers = { + "Content-Type": "application/json", + "User-Agent": self._get_user_agent(), + "Authorization": f"EndpointKey {endpoint.endpoint_key}", + "Ocp-Apim-Subscription-Key": f"EndpointKey {endpoint.endpoint_key}", + } + + return headers + + def _get_user_agent(self): + package_user_agent = f"{__title__}/{__version__}" + uname = platform.uname() + os_version = f"{uname.machine}-{uname.system}-{uname.version}" + py_version = f"Python,Version={platform.python_version()}" + platform_user_agent = f"({os_version}; {py_version})" + user_agent = f"{package_user_agent} {platform_user_agent}" + + return user_agent + + def _is_using_requests_module(self) -> bool: + return (type(self._http_client).__name__ == "module") and ( + self._http_client.__name__ == "requests" + ) + + async def _make_request_with_aiohttp( + self, request_url: str, payload_body: str, headers: dict, timeout: float + ) -> ClientResponse: + if timeout: + # aiohttp.ClientSession's timeouts are in seconds + timeout_in_seconds = ClientTimeout(total=timeout / 1000) + + return await self._http_client.post( + request_url, + data=payload_body, + headers=headers, + timeout=timeout_in_seconds, + ) + + return await self._http_client.post( + request_url, data=payload_body, headers=headers + ) + + def _make_request_with_requests( + self, request_url: str, payload_body: str, headers: dict, timeout: float + ) -> requests.Response: + if timeout: + # requests' timeouts are in seconds + timeout_in_seconds = timeout / 1000 + + return self._http_client.post( + request_url, + data=payload_body, + headers=headers, + timeout=timeout_in_seconds, + ) + + return self._http_client.post(request_url, data=payload_body, headers=headers) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/qna_card_builder.py b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/qna_card_builder.py new file mode 100644 index 000000000..fe70b6c73 --- /dev/null +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/qna_card_builder.py @@ -0,0 +1,76 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List +from botbuilder.core import CardFactory +from botbuilder.schema import Activity, ActivityTypes, CardAction, HeroCard + +from ..models import QueryResult + + +class QnACardBuilder: + """ + Message activity card builder for QnAMaker dialogs. + """ + + @staticmethod + def get_suggestions_card( + suggestions: List[str], card_title: str, card_no_match: str + ) -> Activity: + """ + Get active learning suggestions card. + """ + + if not suggestions: + raise TypeError("suggestions list is required") + + if not card_title: + raise TypeError("card_title is required") + + if not card_no_match: + raise TypeError("card_no_match is required") + + # Add all suggestions + button_list = [ + CardAction(value=suggestion, type="imBack", title=suggestion) + for suggestion in suggestions + ] + + # Add No match text + button_list.append( + CardAction(value=card_no_match, type="imBack", title=card_no_match) + ) + + attachment = CardFactory.hero_card(HeroCard(buttons=button_list)) + + return Activity( + type=ActivityTypes.message, text=card_title, attachments=[attachment] + ) + + @staticmethod + def get_qna_prompts_card(result: QueryResult, card_no_match_text: str) -> Activity: + """ + Get active learning suggestions card. + """ + + if not result: + raise TypeError("result is required") + + if not card_no_match_text: + raise TypeError("card_no_match_text is required") + + # Add all prompts + button_list = [ + CardAction( + value=prompt.display_text, + type="imBack", + title=prompt.display_text, + ) + for prompt in result.context.prompts + ] + + attachment = CardFactory.hero_card(HeroCard(buttons=button_list)) + + return Activity( + type=ActivityTypes.message, text=result.answer, attachments=[attachment] + ) diff --git a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/train_utils.py b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/train_utils.py index ec2586bd4..c803d79eb 100644 --- a/libraries/botbuilder-ai/botbuilder/ai/qna/utils/train_utils.py +++ b/libraries/botbuilder-ai/botbuilder/ai/qna/utils/train_utils.py @@ -11,13 +11,13 @@ class TrainUtils: - """ Class for Train API, used in active learning to add suggestions to the knowledge base """ + """Class for Train API, used in active learning to add suggestions to the knowledge base""" def __init__(self, endpoint: QnAMakerEndpoint, http_client: ClientSession): """ Initializes a new instance for active learning train utils. - Parameters: + Parameters ----------- endpoint: QnA Maker Endpoint of the knowledge base to query. @@ -45,7 +45,9 @@ async def call_train(self, feedback_records: List[FeedbackRecord]): await self._query_train(feedback_records) async def _query_train(self, feedback_records: List[FeedbackRecord]): - url: str = f"{ self._endpoint.host }/knowledgebases/{ self._endpoint.knowledge_base_id }/train" + url: str = ( + f"{ self._endpoint.host }/knowledgebases/{ self._endpoint.knowledge_base_id }/train" + ) payload_body = TrainRequestBody(feedback_records=feedback_records) http_request_helper = HttpRequestUtils(self._http_client) diff --git a/libraries/botbuilder-ai/requirements.txt b/libraries/botbuilder-ai/requirements.txt index dc2867a87..232724deb 100644 --- a/libraries/botbuilder-ai/requirements.txt +++ b/libraries/botbuilder-ai/requirements.txt @@ -1,6 +1,6 @@ -msrest==0.6.10 -botbuilder-schema>=4.7.1 -botbuilder-core>=4.7.1 -requests==2.22.0 +msrest== 0.7.* +botbuilder-schema==4.17.0 +botbuilder-core==4.17.0 +requests==2.32.0 aiounittest==1.3.0 azure-cognitiveservices-language-luis==0.2.0 \ No newline at end of file diff --git a/libraries/botbuilder-ai/setup.py b/libraries/botbuilder-ai/setup.py index 72f112a5a..10bc3ee5c 100644 --- a/libraries/botbuilder-ai/setup.py +++ b/libraries/botbuilder-ai/setup.py @@ -6,9 +6,9 @@ REQUIRES = [ "azure-cognitiveservices-language-luis==0.2.0", - "botbuilder-schema>=4.7.1", - "botbuilder-core>=4.7.1", - "aiohttp==3.6.2", + "botbuilder-schema==4.17.0", + "botbuilder-core==4.17.0", + "aiohttp>=3.10,<4.0", ] TESTS_REQUIRES = ["aiounittest>=1.1.0"] @@ -39,6 +39,7 @@ "botbuilder.ai.luis", "botbuilder.ai.qna.models", "botbuilder.ai.qna.utils", + "botbuilder.ai.qna.dialogs", ], install_requires=REQUIRES + TESTS_REQUIRES, tests_require=TESTS_REQUIRES, diff --git a/libraries/botbuilder-ai/tests/luis/luis_recognizer_test.py b/libraries/botbuilder-ai/tests/luis/luis_recognizer_test.py index d6bc0c4df..5c55b9951 100644 --- a/libraries/botbuilder-ai/tests/luis/luis_recognizer_test.py +++ b/libraries/botbuilder-ai/tests/luis/luis_recognizer_test.py @@ -10,10 +10,6 @@ from unittest.mock import MagicMock, Mock from aiounittest import AsyncTestCase -from azure.cognitiveservices.language.luis.runtime import LUISRuntimeClient -from azure.cognitiveservices.language.luis.runtime.luis_runtime_client import ( - LUISRuntimeClientConfiguration, -) from msrest import Deserializer from requests import Session from requests.models import Response @@ -70,21 +66,6 @@ def test_luis_recognizer_construction(self): self.assertEqual("048ec46dc58e495482b0c447cfdbd291", app.endpoint_key) self.assertEqual("https://westus.api.cognitive.microsoft.com", app.endpoint) - def test_luis_recognizer_timeout(self): - endpoint = ( - "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" - "b31aeaf3-3511-495b-a07f-571fc873214b?verbose=true&timezoneOffset=-360" - "&subscription-key=048ec46dc58e495482b0c447cfdbd291&q=" - ) - expected_timeout = 300 - options_with_timeout = LuisPredictionOptions(timeout=expected_timeout * 1000) - - recognizer_with_timeout = LuisRecognizer(endpoint, options_with_timeout) - - self.assertEqual( - expected_timeout, recognizer_with_timeout._runtime.config.connection.timeout - ) - def test_none_endpoint(self): # Arrange my_app = LuisApplication( @@ -145,7 +126,9 @@ async def test_single_intent_simply_entity(self): async def test_null_utterance(self): utterance: str = None - response_path: str = "SingleIntent_SimplyEntity.json" # The path is irrelevant in this case + response_path: str = ( + "SingleIntent_SimplyEntity.json" # The path is irrelevant in this case + ) _, result = await LuisRecognizerTest._get_recognizer_result( utterance, response_path @@ -418,24 +401,6 @@ def test_top_intent_returns_top_intent_if_score_equals_min_score(self): ) self.assertEqual(default_intent, "Greeting") - async def test_user_agent_contains_product_version(self): - utterance: str = "please book from May 5 to June 6" - response_path: str = "MultipleDateTimeEntities.json" # it doesn't matter to use which file. - - recognizer, _ = await LuisRecognizerTest._get_recognizer_result( - utterance, response_path, bot_adapter=NullAdapter() - ) - - runtime: LUISRuntimeClient = recognizer._runtime - config: LUISRuntimeClientConfiguration = runtime.config - user_agent = config.user_agent - - # Verify we didn't unintentionally stamp on the user-agent from the client. - self.assertTrue("azure-cognitiveservices-language-luis" in user_agent) - - # And that we added the bot.builder package details. - self.assertTrue("botbuilder-ai/4" in user_agent) - def test_telemetry_construction(self): # Arrange # Note this is NOT a real LUIS application ID nor a real LUIS subscription-key diff --git a/libraries/botbuilder-ai/tests/luis/luis_recognizer_v3_test.py b/libraries/botbuilder-ai/tests/luis/luis_recognizer_v3_test.py new file mode 100644 index 000000000..b87252deb --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/luis_recognizer_v3_test.py @@ -0,0 +1,251 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +# pylint: disable=no-value-for-parameter + +import json +from os import path +from typing import Dict, Tuple, Union + +import re +from unittest import mock +from unittest.mock import MagicMock +from aioresponses import aioresponses +from aiounittest import AsyncTestCase +from botbuilder.ai.luis import LuisRecognizerOptionsV3 +from botbuilder.ai.luis import LuisApplication, LuisPredictionOptions, LuisRecognizer +from botbuilder.ai.luis.luis_util import LuisUtil +from botbuilder.core import ( + BotAdapter, + IntentScore, + RecognizerResult, + TurnContext, +) +from botbuilder.core.adapters import TestAdapter +from botbuilder.schema import ( + Activity, + ActivityTypes, + ChannelAccount, + ConversationAccount, +) + + +class LuisRecognizerV3Test(AsyncTestCase): + _luisAppId: str = "b31aeaf3-3511-495b-a07f-571fc873214b" + _subscriptionKey: str = "048ec46dc58e495482b0c447cfdbd291" + _endpoint: str = "https://westus.api.cognitive.microsoft.com" + + def __init__(self, *args, **kwargs): + super(LuisRecognizerV3Test, self).__init__(*args, **kwargs) + self._mocked_results: RecognizerResult = RecognizerResult( + intents={"Test": IntentScore(score=0.2), "Greeting": IntentScore(score=0.4)} + ) + self._empty_luis_response: Dict[str, object] = json.loads( + '{ "query": null, "intents": [], "entities": [] }' + ) + + @staticmethod + def _remove_none_property(dictionary: Dict[str, object]) -> Dict[str, object]: + for key, value in list(dictionary.items()): + if value is None: + del dictionary[key] + elif isinstance(value, dict): + LuisRecognizerV3Test._remove_none_property(value) + return dictionary + + @classmethod + @aioresponses() + async def _get_recognizer_result( + cls, + utterance: str, + response_json: Union[str, Dict[str, object]], + mock_get, + bot_adapter: BotAdapter = TestAdapter(), + options: Union[LuisRecognizerOptionsV3, LuisPredictionOptions] = None, + include_api_results: bool = False, + telemetry_properties: Dict[str, str] = None, + telemetry_metrics: Dict[str, float] = None, + recognizer_class: type = LuisRecognizer, + ) -> Tuple[LuisRecognizer, RecognizerResult]: + if isinstance(response_json, str): + response_json = LuisRecognizerV3Test._get_json_for_file( + response_file=response_json + ) + + recognizer = LuisRecognizerV3Test._get_luis_recognizer( + recognizer_class, include_api_results=include_api_results, options=options + ) + context = LuisRecognizerV3Test._get_context(utterance, bot_adapter) + # mock_get.return_value.__aenter__.return_value.json = CoroutineMock(side_effect=[response_json]) + + pattern = re.compile(r"^https://westus.api.cognitive.microsoft.com.*$") + mock_get.post(pattern, payload=response_json, status=200) + + result = await recognizer.recognize( + context, telemetry_properties, telemetry_metrics + ) + return recognizer, result + + @classmethod + def _get_json_for_file(cls, response_file: str) -> Dict[str, object]: + curr_dir = path.dirname(path.abspath(__file__)) + response_path = path.join(curr_dir, "test_data", response_file) + + with open(response_path, "r", encoding="utf-8-sig") as file: + response_str = file.read() + response_json = json.loads(response_str) + return response_json + + @classmethod + def _get_luis_recognizer( + cls, + recognizer_class: type, + options: Union[LuisPredictionOptions, LuisRecognizerOptionsV3] = None, + include_api_results: bool = False, + ) -> LuisRecognizer: + luis_app = LuisApplication(cls._luisAppId, cls._subscriptionKey, cls._endpoint) + + if isinstance(options, LuisRecognizerOptionsV3): + LuisRecognizerOptionsV3.include_api_results = include_api_results + + return recognizer_class( + luis_app, + prediction_options=options, + include_api_results=include_api_results, + ) + + @staticmethod + def _get_context(utterance: str, bot_adapter: BotAdapter) -> TurnContext: + activity = Activity( + type=ActivityTypes.message, + text=utterance, + conversation=ConversationAccount(), + recipient=ChannelAccount(), + from_property=ChannelAccount(), + ) + return TurnContext(bot_adapter, activity) + + # Luis V3 endpoint tests begin here + async def _test_json_v3(self, response_file: str) -> None: + # Arrange + expected_json = LuisRecognizerV3Test._get_json_for_file(response_file) + response_json = expected_json["v3"]["response"] + utterance = expected_json.get("text") + if utterance is None: + utterance = expected_json.get("Text") + + test_options = expected_json["v3"]["options"] + + options = LuisRecognizerOptionsV3( + include_all_intents=test_options["includeAllIntents"], + include_instance_data=test_options["includeInstanceData"], + log=test_options["log"], + prefer_external_entities=test_options["preferExternalEntities"], + slot=test_options["slot"], + include_api_results=test_options["includeAPIResults"], + ) + + if "version" in test_options: + options.version = test_options["version"] + + if "externalEntities" in test_options: + options.external_entities = test_options["externalEntities"] + + # dynamic_lists: List = None, + # external_entities: List = None, + # telemetry_client: BotTelemetryClient = NullTelemetryClient(), + # log_personal_information: bool = False,) + # , + + # Act + _, result = await LuisRecognizerV3Test._get_recognizer_result( + utterance, response_json, options=options, include_api_results=True + ) + + # Assert + actual_result_json = LuisUtil.recognizer_result_as_dict(result) + del expected_json["v3"] + trimmed_expected = LuisRecognizerV3Test._remove_none_property(expected_json) + trimmed_actual = LuisRecognizerV3Test._remove_none_property(actual_result_json) + + self.assertEqual(trimmed_expected, trimmed_actual) + + async def test_composite1_v3(self): + await self._test_json_v3("Composite1_v3.json") + + async def test_composite2_v3(self): + await self._test_json_v3("Composite2_v3.json") + + async def test_composite3_v3(self): + await self._test_json_v3("Composite3_v3.json") + + async def test_external_entities_and_built_in_v3(self): + await self._test_json_v3("ExternalEntitiesAndBuiltIn_v3.json") + + async def test_external_entities_and_composite_v3(self): + await self._test_json_v3("ExternalEntitiesAndComposite_v3.json") + + async def test_external_entities_and_list_v3(self): + await self._test_json_v3("ExternalEntitiesAndList_v3.json") + + async def test_external_entities_and_regex_v3(self): + await self._test_json_v3("ExternalEntitiesAndRegex_v3.json") + + async def test_external_entities_and_simple_v3(self): + await self._test_json_v3("ExternalEntitiesAndSimple_v3.json") + + async def test_geo_people_ordinal_v3(self): + await self._test_json_v3("GeoPeopleOrdinal_v3.json") + + async def test_minimal_v3(self): + await self._test_json_v3("Minimal_v3.json") + + async def test_no_entities_instance_true_v3(self): + await self._test_json_v3("NoEntitiesInstanceTrue_v3.json") + + async def test_patterns_v3(self): + await self._test_json_v3("Patterns_v3.json") + + async def test_prebuilt_v3(self): + await self._test_json_v3("Prebuilt_v3.json") + + async def test_roles_v3(self): + await self._test_json_v3("roles_v3.json") + + async def test_trace_activity(self): + # Arrange + utterance: str = "fly on delta at 3pm" + expected_json = LuisRecognizerV3Test._get_json_for_file("Minimal_v3.json") + response_json = expected_json["v3"]["response"] + + # add async support to magic mock. + async def async_magic(): + pass + + MagicMock.__await__ = lambda x: async_magic().__await__() + + # Act + with mock.patch.object(TurnContext, "send_activity") as mock_send_activity: + await LuisRecognizerV3Test._get_recognizer_result( + utterance, response_json, options=LuisRecognizerOptionsV3() + ) + trace_activity: Activity = mock_send_activity.call_args[0][0] + + # Assert + self.assertIsNotNone(trace_activity) + self.assertEqual(LuisRecognizer.luis_trace_type, trace_activity.value_type) + self.assertEqual(LuisRecognizer.luis_trace_label, trace_activity.label) + + luis_trace_info = trace_activity.value + self.assertIsNotNone(luis_trace_info) + self.assertIsNotNone(luis_trace_info["recognizerResult"]) + self.assertIsNotNone(luis_trace_info["luisResult"]) + self.assertIsNotNone(luis_trace_info["luisOptions"]) + self.assertIsNotNone(luis_trace_info["luisModel"]) + + recognizer_result: RecognizerResult = luis_trace_info["recognizerResult"] + self.assertEqual(utterance, recognizer_result["text"]) + self.assertIsNotNone(recognizer_result["intents"]["Roles"]) + self.assertEqual( + LuisRecognizerV3Test._luisAppId, luis_trace_info["luisModel"]["ModelID"] + ) diff --git a/libraries/botbuilder-ai/tests/luis/test_data/Composite1_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/Composite1_v3.json new file mode 100644 index 000000000..5d1266497 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/Composite1_v3.json @@ -0,0 +1,1285 @@ +{ + "text": "12 years old and 3 days old and monday july 3rd, 2019 and every monday and between 3am and 5:30am and 4 acres and 4 pico meters and chrimc@hotmail.com and $4 and $4.25 and also 32 and 210.4 and first and 10% and 10.5% and 425-555-1234 and 3 degrees and -27.5 degrees c and the next one and the previous one", + "intents": { + "Cancel": { + "score": 0.00000156337478 + }, + "Delivery": { + "score": 0.0002846266 + }, + "EntityTests": { + "score": 0.953405857 + }, + "Greeting": { + "score": 8.20979437e-7 + }, + "Help": { + "score": 0.00000481870757 + }, + "None": { + "score": 0.01040122 + }, + "Roles": { + "score": 0.197366714 + }, + "search": { + "score": 0.14049834 + }, + "SpecifyName": { + "score": 0.000137732946 + }, + "Travel": { + "score": 0.0100996653 + }, + "Weather_GetForecast": { + "score": 0.0143940123 + } + }, + "entities": { + "$instance": { + "Composite1": [ + { + "endIndex": 306, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.880988955, + "startIndex": 0, + "text": "12 years old and 3 days old and monday july 3rd, 2019 and every monday and between 3am and 5:30am and 4 acres and 4 pico meters and chrimc@hotmail.com and $4 and $4.25 and also 32 and 210.4 and first and 10% and 10.5% and 425-555-1234 and 3 degrees and -27.5 degrees c and the next one and the previous one", + "type": "Composite1" + } + ], + "ordinalV2": [ + { + "endIndex": 47, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 44, + "text": "3rd", + "type": "builtin.ordinalV2" + }, + { + "endIndex": 199, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 194, + "text": "first", + "type": "builtin.ordinalV2" + }, + { + "endIndex": 285, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 277, + "text": "next one", + "type": "builtin.ordinalV2.relative" + }, + { + "endIndex": 306, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 294, + "text": "previous one", + "type": "builtin.ordinalV2.relative" + } + ] + }, + "Composite1": [ + { + "$instance": { + "age": [ + { + "endIndex": 12, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "12 years old", + "type": "builtin.age" + }, + { + "endIndex": 27, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "3 days old", + "type": "builtin.age" + } + ], + "datetime": [ + { + "endIndex": 8, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "12 years", + "type": "builtin.datetimeV2.duration" + }, + { + "endIndex": 23, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "3 days", + "type": "builtin.datetimeV2.duration" + }, + { + "endIndex": 53, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 32, + "text": "monday july 3rd, 2019", + "type": "builtin.datetimeV2.date" + }, + { + "endIndex": 70, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 58, + "text": "every monday", + "type": "builtin.datetimeV2.set" + }, + { + "endIndex": 97, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 75, + "text": "between 3am and 5:30am", + "type": "builtin.datetimeV2.timerange" + } + ], + "dimension": [ + { + "endIndex": 109, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 102, + "text": "4 acres", + "type": "builtin.dimension" + }, + { + "endIndex": 127, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 114, + "text": "4 pico meters", + "type": "builtin.dimension" + } + ], + "email": [ + { + "endIndex": 150, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 132, + "text": "chrimc@hotmail.com", + "type": "builtin.email" + } + ], + "money": [ + { + "endIndex": 157, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 155, + "text": "$4", + "type": "builtin.currency" + }, + { + "endIndex": 167, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 162, + "text": "$4.25", + "type": "builtin.currency" + } + ], + "number": [ + { + "endIndex": 2, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "12", + "type": "builtin.number" + }, + { + "endIndex": 18, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "3", + "type": "builtin.number" + }, + { + "endIndex": 53, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 49, + "text": "2019", + "type": "builtin.number" + }, + { + "endIndex": 92, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 91, + "text": "5", + "type": "builtin.number" + }, + { + "endIndex": 103, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 102, + "text": "4", + "type": "builtin.number" + }, + { + "endIndex": 115, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 114, + "text": "4", + "type": "builtin.number" + }, + { + "endIndex": 157, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 156, + "text": "4", + "type": "builtin.number" + }, + { + "endIndex": 167, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 163, + "text": "4.25", + "type": "builtin.number" + }, + { + "endIndex": 179, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 177, + "text": "32", + "type": "builtin.number" + }, + { + "endIndex": 189, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 184, + "text": "210.4", + "type": "builtin.number" + }, + { + "endIndex": 206, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 204, + "text": "10", + "type": "builtin.number" + }, + { + "endIndex": 216, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 212, + "text": "10.5", + "type": "builtin.number" + }, + { + "endIndex": 225, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 222, + "text": "425", + "type": "builtin.number" + }, + { + "endIndex": 229, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 226, + "text": "555", + "type": "builtin.number" + }, + { + "endIndex": 234, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 230, + "text": "1234", + "type": "builtin.number" + }, + { + "endIndex": 240, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 239, + "text": "3", + "type": "builtin.number" + }, + { + "endIndex": 258, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 253, + "text": "-27.5", + "type": "builtin.number" + }, + { + "endIndex": 285, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 282, + "text": "one", + "type": "builtin.number" + }, + { + "endIndex": 306, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 303, + "text": "one", + "type": "builtin.number" + } + ], + "percentage": [ + { + "endIndex": 207, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 204, + "text": "10%", + "type": "builtin.percentage" + }, + { + "endIndex": 217, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 212, + "text": "10.5%", + "type": "builtin.percentage" + } + ], + "phonenumber": [ + { + "endIndex": 234, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.9, + "startIndex": 222, + "text": "425-555-1234", + "type": "builtin.phonenumber" + } + ], + "temperature": [ + { + "endIndex": 248, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 239, + "text": "3 degrees", + "type": "builtin.temperature" + }, + { + "endIndex": 268, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 253, + "text": "-27.5 degrees c", + "type": "builtin.temperature" + } + ] + }, + "age": [ + { + "number": 12, + "units": "Year" + }, + { + "number": 3, + "units": "Day" + } + ], + "datetime": [ + { + "timex": [ + "P12Y" + ], + "type": "duration" + }, + { + "timex": [ + "P3D" + ], + "type": "duration" + }, + { + "timex": [ + "2019-07-03" + ], + "type": "date" + }, + { + "timex": [ + "XXXX-WXX-1" + ], + "type": "set" + }, + { + "timex": [ + "(T03,T05:30,PT2H30M)" + ], + "type": "timerange" + } + ], + "dimension": [ + { + "number": 4, + "units": "Acre" + }, + { + "number": 4, + "units": "Picometer" + } + ], + "email": [ + "chrimc@hotmail.com" + ], + "money": [ + { + "number": 4, + "units": "Dollar" + }, + { + "number": 4.25, + "units": "Dollar" + } + ], + "number": [ + 12, + 3, + 2019, + 5, + 4, + 4, + 4, + 4.25, + 32, + 210.4, + 10, + 10.5, + 425, + 555, + 1234, + 3, + -27.5, + 1, + 1 + ], + "percentage": [ + 10, + 10.5 + ], + "phonenumber": [ + "425-555-1234" + ], + "temperature": [ + { + "number": 3, + "units": "Degree" + }, + { + "number": -27.5, + "units": "C" + } + ] + } + ], + "ordinalV2": [ + { + "offset": 3, + "relativeTo": "start" + }, + { + "offset": 1, + "relativeTo": "start" + }, + { + "offset": 1, + "relativeTo": "current" + }, + { + "offset": -1, + "relativeTo": "current" + } + ] + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "v3": { + "response": { + "prediction": { + "entities": { + "$instance": { + "Composite1": [ + { + "length": 306, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "score": 0.880988955, + "startIndex": 0, + "text": "12 years old and 3 days old and monday july 3rd, 2019 and every monday and between 3am and 5:30am and 4 acres and 4 pico meters and chrimc@hotmail.com and $4 and $4.25 and also 32 and 210.4 and first and 10% and 10.5% and 425-555-1234 and 3 degrees and -27.5 degrees c and the next one and the previous one", + "type": "Composite1" + } + ], + "ordinalV2": [ + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 44, + "text": "3rd", + "type": "builtin.ordinalV2" + }, + { + "length": 5, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 194, + "text": "first", + "type": "builtin.ordinalV2" + }, + { + "length": 8, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 277, + "text": "next one", + "type": "builtin.ordinalV2.relative" + }, + { + "length": 12, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 294, + "text": "previous one", + "type": "builtin.ordinalV2.relative" + } + ] + }, + "Composite1": [ + { + "$instance": { + "age": [ + { + "length": 12, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "12 years old", + "type": "builtin.age" + }, + { + "length": 10, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "3 days old", + "type": "builtin.age" + } + ], + "datetimeV2": [ + { + "length": 8, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "12 years", + "type": "builtin.datetimeV2.duration" + }, + { + "length": 6, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "3 days", + "type": "builtin.datetimeV2.duration" + }, + { + "length": 21, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 32, + "text": "monday july 3rd, 2019", + "type": "builtin.datetimeV2.date" + }, + { + "length": 12, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 58, + "text": "every monday", + "type": "builtin.datetimeV2.set" + }, + { + "length": 22, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 75, + "text": "between 3am and 5:30am", + "type": "builtin.datetimeV2.timerange" + } + ], + "dimension": [ + { + "length": 7, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 102, + "text": "4 acres", + "type": "builtin.dimension" + }, + { + "length": 13, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 114, + "text": "4 pico meters", + "type": "builtin.dimension" + } + ], + "email": [ + { + "length": 18, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 132, + "text": "chrimc@hotmail.com", + "type": "builtin.email" + } + ], + "money": [ + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 155, + "text": "$4", + "type": "builtin.currency" + }, + { + "length": 5, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 162, + "text": "$4.25", + "type": "builtin.currency" + } + ], + "number": [ + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "12", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "3", + "type": "builtin.number" + }, + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 49, + "text": "2019", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 91, + "text": "5", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 102, + "text": "4", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 114, + "text": "4", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 156, + "text": "4", + "type": "builtin.number" + }, + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 163, + "text": "4.25", + "type": "builtin.number" + }, + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 177, + "text": "32", + "type": "builtin.number" + }, + { + "length": 5, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 184, + "text": "210.4", + "type": "builtin.number" + }, + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 204, + "text": "10", + "type": "builtin.number" + }, + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 212, + "text": "10.5", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 222, + "text": "425", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 226, + "text": "555", + "type": "builtin.number" + }, + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 230, + "text": "1234", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 239, + "text": "3", + "type": "builtin.number" + }, + { + "length": 5, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 253, + "text": "-27.5", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 282, + "text": "one", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 303, + "text": "one", + "type": "builtin.number" + } + ], + "percentage": [ + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 204, + "text": "10%", + "type": "builtin.percentage" + }, + { + "length": 5, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 212, + "text": "10.5%", + "type": "builtin.percentage" + } + ], + "phonenumber": [ + { + "length": 12, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "score": 0.9, + "startIndex": 222, + "text": "425-555-1234", + "type": "builtin.phonenumber" + } + ], + "temperature": [ + { + "length": 9, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 239, + "text": "3 degrees", + "type": "builtin.temperature" + }, + { + "length": 15, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 253, + "text": "-27.5 degrees c", + "type": "builtin.temperature" + } + ] + }, + "age": [ + { + "number": 12, + "unit": "Year" + }, + { + "number": 3, + "unit": "Day" + } + ], + "datetimeV2": [ + { + "type": "duration", + "values": [ + { + "timex": "P12Y", + "value": "378432000" + } + ] + }, + { + "type": "duration", + "values": [ + { + "timex": "P3D", + "value": "259200" + } + ] + }, + { + "type": "date", + "values": [ + { + "timex": "2019-07-03", + "value": "2019-07-03" + } + ] + }, + { + "type": "set", + "values": [ + { + "timex": "XXXX-WXX-1", + "value": "not resolved" + } + ] + }, + { + "type": "timerange", + "values": [ + { + "end": "05:30:00", + "start": "03:00:00", + "timex": "(T03,T05:30,PT2H30M)" + } + ] + } + ], + "dimension": [ + { + "number": 4, + "unit": "Acre" + }, + { + "number": 4, + "unit": "Picometer" + } + ], + "email": [ + "chrimc@hotmail.com" + ], + "money": [ + { + "number": 4, + "unit": "Dollar" + }, + { + "number": 4.25, + "unit": "Dollar" + } + ], + "number": [ + 12, + 3, + 2019, + 5, + 4, + 4, + 4, + 4.25, + 32, + 210.4, + 10, + 10.5, + 425, + 555, + 1234, + 3, + -27.5, + 1, + 1 + ], + "percentage": [ + 10, + 10.5 + ], + "phonenumber": [ + "425-555-1234" + ], + "temperature": [ + { + "number": 3, + "unit": "Degree" + }, + { + "number": -27.5, + "unit": "C" + } + ] + } + ], + "ordinalV2": [ + { + "offset": 3, + "relativeTo": "start" + }, + { + "offset": 1, + "relativeTo": "start" + }, + { + "offset": 1, + "relativeTo": "current" + }, + { + "offset": -1, + "relativeTo": "current" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.00000156337478 + }, + "Delivery": { + "score": 0.0002846266 + }, + "EntityTests": { + "score": 0.953405857 + }, + "Greeting": { + "score": 8.20979437e-7 + }, + "Help": { + "score": 0.00000481870757 + }, + "None": { + "score": 0.01040122 + }, + "Roles": { + "score": 0.197366714 + }, + "search": { + "score": 0.14049834 + }, + "SpecifyName": { + "score": 0.000137732946 + }, + "Travel": { + "score": 0.0100996653 + }, + "Weather_GetForecast": { + "score": 0.0143940123 + } + }, + "normalizedQuery": "12 years old and 3 days old and monday july 3rd, 2019 and every monday and between 3am and 5:30am and 4 acres and 4 pico meters and chrimc@hotmail.com and $4 and $4.25 and also 32 and 210.4 and first and 10% and 10.5% and 425-555-1234 and 3 degrees and -27.5 degrees c and the next one and the previous one", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "EntityTests" + }, + "query": "12 years old and 3 days old and monday july 3rd, 2019 and every monday and between 3am and 5:30am and 4 acres and 4 pico meters and chrimc@hotmail.com and $4 and $4.25 and also 32 and 210.4 and first and 10% and 10.5% and 425-555-1234 and 3 degrees and -27.5 degrees c and the next one and the previous one" + }, + "options": { + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + } + } +} \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/Composite2_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/Composite2_v3.json new file mode 100644 index 000000000..11fc7bb89 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/Composite2_v3.json @@ -0,0 +1,312 @@ +{ + "entities": { + "$instance": { + "Composite2": [ + { + "endIndex": 69, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.97076714, + "startIndex": 0, + "text": "http://foo.com is where you can fly from seattle to dallas via denver", + "type": "Composite2" + } + ], + "geographyV2": [ + { + "endIndex": 48, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 41, + "text": "seattle", + "type": "builtin.geographyV2.city" + } + ] + }, + "Composite2": [ + { + "$instance": { + "City": [ + { + "endIndex": 69, + "modelType": "Hierarchical Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.984581649, + "startIndex": 63, + "text": "denver", + "type": "City" + } + ], + "From": [ + { + "endIndex": 48, + "modelType": "Hierarchical Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.999511, + "startIndex": 41, + "text": "seattle", + "type": "City::From" + } + ], + "To": [ + { + "endIndex": 58, + "modelType": "Hierarchical Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.9984612, + "startIndex": 52, + "text": "dallas", + "type": "City::To" + } + ], + "url": [ + { + "endIndex": 14, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "http://foo.com", + "type": "builtin.url" + } + ] + }, + "City": [ + "denver" + ], + "From": [ + "seattle" + ], + "To": [ + "dallas" + ], + "url": [ + "http://foo.com" + ] + } + ], + "geographyV2": [ + { + "location": "seattle", + "type": "city" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.000227437369 + }, + "Delivery": { + "score": 0.001310123 + }, + "EntityTests": { + "score": 0.94500196 + }, + "Greeting": { + "score": 0.000152356763 + }, + "Help": { + "score": 0.000547201431 + }, + "None": { + "score": 0.004187195 + }, + "Roles": { + "score": 0.0300086979 + }, + "search": { + "score": 0.0108942846 + }, + "SpecifyName": { + "score": 0.00168467627 + }, + "Travel": { + "score": 0.0154484725 + }, + "Weather_GetForecast": { + "score": 0.0237181056 + } + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "text": "http://foo.com is where you can fly from seattle to dallas via denver", + "v3": { + "options": { + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "$instance": { + "Composite2": [ + { + "length": 69, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "score": 0.97076714, + "startIndex": 0, + "text": "http://foo.com is where you can fly from seattle to dallas via denver", + "type": "Composite2" + } + ], + "geographyV2": [ + { + "length": 7, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 41, + "text": "seattle", + "type": "builtin.geographyV2.city" + } + ] + }, + "Composite2": [ + { + "$instance": { + "City": [ + { + "length": 6, + "modelType": "Hierarchical Entity Extractor", + "modelTypeId": 3, + "recognitionSources": [ + "model" + ], + "score": 0.984581649, + "startIndex": 63, + "text": "denver", + "type": "City" + } + ], + "City::From": [ + { + "length": 7, + "modelType": "Hierarchical Entity Extractor", + "modelTypeId": 3, + "recognitionSources": [ + "model" + ], + "score": 0.999511, + "startIndex": 41, + "text": "seattle", + "type": "City::From" + } + ], + "City::To": [ + { + "length": 6, + "modelType": "Hierarchical Entity Extractor", + "modelTypeId": 3, + "recognitionSources": [ + "model" + ], + "score": 0.9984612, + "startIndex": 52, + "text": "dallas", + "type": "City::To" + } + ], + "url": [ + { + "length": 14, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "http://foo.com", + "type": "builtin.url" + } + ] + }, + "City": [ + "denver" + ], + "City::From": [ + "seattle" + ], + "City::To": [ + "dallas" + ], + "url": [ + "http://foo.com" + ] + } + ], + "geographyV2": [ + { + "type": "city", + "value": "seattle" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.000227437369 + }, + "Delivery": { + "score": 0.001310123 + }, + "EntityTests": { + "score": 0.94500196 + }, + "Greeting": { + "score": 0.000152356763 + }, + "Help": { + "score": 0.000547201431 + }, + "None": { + "score": 0.004187195 + }, + "Roles": { + "score": 0.0300086979 + }, + "search": { + "score": 0.0108942846 + }, + "SpecifyName": { + "score": 0.00168467627 + }, + "Travel": { + "score": 0.0154484725 + }, + "Weather.GetForecast": { + "score": 0.0237181056 + } + }, + "normalizedQuery": "http://foo.com is where you can fly from seattle to dallas via denver", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "EntityTests" + }, + "query": "http://foo.com is where you can fly from seattle to dallas via denver" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/Composite3_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/Composite3_v3.json new file mode 100644 index 000000000..fe55aba56 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/Composite3_v3.json @@ -0,0 +1,315 @@ +{ + "text": "Deliver from 12345 VA to 12346 WA", + "intents": { + "Cancel": { + "score": 1.01764708e-9 + }, + "Delivery": { + "score": 0.00238572317 + }, + "EntityTests": { + "score": 4.757576e-10 + }, + "Greeting": { + "score": 1.0875e-9 + }, + "Help": { + "score": 1.01764708e-9 + }, + "None": { + "score": 0.00000117844979 + }, + "Roles": { + "score": 0.999911964 + }, + "search": { + "score": 0.000009494859 + }, + "SpecifyName": { + "score": 3.0666667e-9 + }, + "Travel": { + "score": 0.00000309763345 + }, + "Weather_GetForecast": { + "score": 0.00000102792524 + } + }, + "entities": { + "$instance": { + "Destination": [ + { + "endIndex": 33, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.9818366, + "startIndex": 25, + "text": "12346 WA", + "type": "Address" + } + ], + "Source": [ + { + "endIndex": 21, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.9345161, + "startIndex": 13, + "text": "12345 VA", + "type": "Address" + } + ] + }, + "Destination": [ + { + "$instance": { + "number": [ + { + "endIndex": 30, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 25, + "text": "12346", + "type": "builtin.number" + } + ], + "State": [ + { + "endIndex": 33, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.9893861, + "startIndex": 31, + "text": "WA", + "type": "State" + } + ] + }, + "number": [ + 12346 + ], + "State": [ + "WA" + ] + } + ], + "Source": [ + { + "$instance": { + "number": [ + { + "endIndex": 18, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 13, + "text": "12345", + "type": "builtin.number" + } + ], + "State": [ + { + "endIndex": 21, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.941649556, + "startIndex": 19, + "text": "VA", + "type": "State" + } + ] + }, + "number": [ + 12345 + ], + "State": [ + "VA" + ] + } + ] + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "v3": { + "response": { + "prediction": { + "entities": { + "$instance": { + "Destination": [ + { + "length": 8, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "role": "Destination", + "score": 0.9818366, + "startIndex": 25, + "text": "12346 WA", + "type": "Address" + } + ], + "Source": [ + { + "length": 8, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "role": "Source", + "score": 0.9345161, + "startIndex": 13, + "text": "12345 VA", + "type": "Address" + } + ] + }, + "Destination": [ + { + "$instance": { + "number": [ + { + "length": 5, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 25, + "text": "12346", + "type": "builtin.number" + } + ], + "State": [ + { + "length": 2, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model" + ], + "score": 0.9893861, + "startIndex": 31, + "text": "WA", + "type": "State" + } + ] + }, + "number": [ + 12346 + ], + "State": [ + "WA" + ] + } + ], + "Source": [ + { + "$instance": { + "number": [ + { + "length": 5, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 13, + "text": "12345", + "type": "builtin.number" + } + ], + "State": [ + { + "length": 2, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model" + ], + "score": 0.941649556, + "startIndex": 19, + "text": "VA", + "type": "State" + } + ] + }, + "number": [ + 12345 + ], + "State": [ + "VA" + ] + } + ] + }, + "intents": { + "Cancel": { + "score": 1.01764708e-9 + }, + "Delivery": { + "score": 0.00238572317 + }, + "EntityTests": { + "score": 4.757576e-10 + }, + "Greeting": { + "score": 1.0875e-9 + }, + "Help": { + "score": 1.01764708e-9 + }, + "None": { + "score": 0.00000117844979 + }, + "Roles": { + "score": 0.999911964 + }, + "search": { + "score": 0.000009494859 + }, + "SpecifyName": { + "score": 3.0666667e-9 + }, + "Travel": { + "score": 0.00000309763345 + }, + "Weather_GetForecast": { + "score": 0.00000102792524 + } + }, + "normalizedQuery": "deliver from 12345 va to 12346 wa", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "Roles" + }, + "query": "Deliver from 12345 VA to 12346 WA" + }, + "options": { + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production", + "version": "GeoPeople" + } + } +} \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndBuiltIn_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndBuiltIn_v3.json new file mode 100644 index 000000000..a451ebbb2 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndBuiltIn_v3.json @@ -0,0 +1,168 @@ +{ + "text": "buy hul and 2 items", + "intents": { + "Cancel": { + "score": 0.006906527 + }, + "Delivery": { + "score": 0.00567273 + }, + "EntityTests": { + "score": 0.128755629 + }, + "Greeting": { + "score": 0.00450348156 + }, + "Help": { + "score": 0.00583425 + }, + "None": { + "score": 0.0135525977 + }, + "Roles": { + "score": 0.04635598 + }, + "search": { + "score": 0.008885799 + }, + "SpecifyName": { + "score": 0.00721160974 + }, + "Travel": { + "score": 0.005146626 + }, + "Weather_GetForecast": { + "score": 0.00913477 + } + }, + "entities": { + "$instance": { + "number": [ + { + "endIndex": 7, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 4, + "text": "hul", + "type": "builtin.number" + }, + { + "endIndex": 13, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 12, + "text": "2", + "type": "builtin.number" + } + ] + }, + "number": [ + 8, + 2 + ] + }, + "sentiment": { + "label": "positive", + "score": 0.7149857 + }, + "v3": { + "response": { + "prediction": { + "entities": { + "$instance": { + "number": [ + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 4, + "text": "hul", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 12, + "text": "2", + "type": "builtin.number" + } + ] + }, + "number": [ + 8, + 2 + ] + }, + "intents": { + "Cancel": { + "score": 0.006906527 + }, + "Delivery": { + "score": 0.00567273 + }, + "EntityTests": { + "score": 0.128755629 + }, + "Greeting": { + "score": 0.00450348156 + }, + "Help": { + "score": 0.00583425 + }, + "None": { + "score": 0.0135525977 + }, + "Roles": { + "score": 0.04635598 + }, + "search": { + "score": 0.008885799 + }, + "SpecifyName": { + "score": 0.00721160974 + }, + "Travel": { + "score": 0.005146626 + }, + "Weather.GetForecast": { + "score": 0.00913477 + } + }, + "normalizedQuery": "buy hul and 2 items", + "sentiment": { + "label": "positive", + "score": 0.7149857 + }, + "topIntent": "EntityTests" + }, + "query": "buy hul and 2 items" + }, + "options": { + "externalEntities": [ + { + "entityLength": 3, + "entityName": "number", + "resolution": 8, + "startIndex": 4 + } + ], + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + } + } +} \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndComposite_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndComposite_v3.json new file mode 100644 index 000000000..33c5d7342 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndComposite_v3.json @@ -0,0 +1,261 @@ +{ + "entities": { + "$instance": { + "Address": [ + { + "endIndex": 13, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.7160641, + "startIndex": 8, + "text": "35 WA", + "type": "Address" + }, + { + "endIndex": 33, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 17, + "text": "repent harelquin", + "type": "Address" + } + ] + }, + "Address": [ + { + "$instance": { + "number": [ + { + "endIndex": 10, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 8, + "text": "35", + "type": "builtin.number" + } + ], + "State": [ + { + "endIndex": 13, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.614376, + "startIndex": 11, + "text": "WA", + "type": "State" + } + ] + }, + "number": [ + 35 + ], + "State": [ + "WA" + ] + }, + { + "number": [ + 3 + ], + "State": [ + "France" + ] + } + ] + }, + "intents": { + "Cancel": { + "score": 0.00325984019 + }, + "Delivery": { + "score": 0.482009649 + }, + "EntityTests": { + "score": 0.00372873852 + }, + "Greeting": { + "score": 0.00283122621 + }, + "Help": { + "score": 0.00292110164 + }, + "None": { + "score": 0.0208108239 + }, + "Roles": { + "score": 0.069060266 + }, + "search": { + "score": 0.009682492 + }, + "SpecifyName": { + "score": 0.00586992875 + }, + "Travel": { + "score": 0.007831623 + }, + "Weather_GetForecast": { + "score": 0.009580207 + } + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "text": "deliver 35 WA to repent harelquin", + "v3": { + "options": { + "externalEntities": [ + { + "entityLength": 16, + "entityName": "Address", + "resolution": { + "number": [ + 3 + ], + "State": [ + "France" + ] + }, + "startIndex": 17 + } + ], + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "$instance": { + "Address": [ + { + "length": 5, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "score": 0.7160641, + "startIndex": 8, + "text": "35 WA", + "type": "Address" + }, + { + "length": 16, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 17, + "text": "repent harelquin", + "type": "Address" + } + ] + }, + "Address": [ + { + "$instance": { + "number": [ + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 8, + "text": "35", + "type": "builtin.number" + } + ], + "State": [ + { + "length": 2, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model" + ], + "score": 0.614376, + "startIndex": 11, + "text": "WA", + "type": "State" + } + ] + }, + "number": [ + 35 + ], + "State": [ + "WA" + ] + }, + { + "number": [ + 3 + ], + "State": [ + "France" + ] + } + ] + }, + "intents": { + "Cancel": { + "score": 0.00325984019 + }, + "Delivery": { + "score": 0.482009649 + }, + "EntityTests": { + "score": 0.00372873852 + }, + "Greeting": { + "score": 0.00283122621 + }, + "Help": { + "score": 0.00292110164 + }, + "None": { + "score": 0.0208108239 + }, + "Roles": { + "score": 0.069060266 + }, + "search": { + "score": 0.009682492 + }, + "SpecifyName": { + "score": 0.00586992875 + }, + "Travel": { + "score": 0.007831623 + }, + "Weather.GetForecast": { + "score": 0.009580207 + } + }, + "normalizedQuery": "deliver 35 wa to repent harelquin", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "Delivery" + }, + "query": "deliver 35 WA to repent harelquin" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndList_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndList_v3.json new file mode 100644 index 000000000..e2cf8eb63 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndList_v3.json @@ -0,0 +1,178 @@ +{ + "entities": { + "$instance": { + "Airline": [ + { + "endIndex": 23, + "modelType": "List Entity Extractor", + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 7, + "text": "humberg airlines", + "type": "Airline" + }, + { + "endIndex": 32, + "modelType": "List Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 27, + "text": "Delta", + "type": "Airline" + } + ] + }, + "Airline": [ + [ + "HumAir" + ], + [ + "Delta" + ] + ] + }, + "intents": { + "Cancel": { + "score": 0.00330878259 + }, + "Delivery": { + "score": 0.00452178251 + }, + "EntityTests": { + "score": 0.052175343 + }, + "Greeting": { + "score": 0.002769983 + }, + "Help": { + "score": 0.002995687 + }, + "None": { + "score": 0.0302589461 + }, + "Roles": { + "score": 0.132316783 + }, + "search": { + "score": 0.007362695 + }, + "SpecifyName": { + "score": 0.00500302855 + }, + "Travel": { + "score": 0.0146034053 + }, + "Weather_GetForecast": { + "score": 0.005048246 + } + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "text": "fly on humberg airlines or Delta", + "v3": { + "options": { + "externalEntities": [ + { + "entityLength": 16, + "entityName": "Airline", + "resolution": [ + "HumAir" + ], + "startIndex": 7 + } + ], + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "$instance": { + "Airline": [ + { + "length": 16, + "modelType": "List Entity Extractor", + "modelTypeId": 5, + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 7, + "text": "humberg airlines", + "type": "Airline" + }, + { + "length": 5, + "modelType": "List Entity Extractor", + "modelTypeId": 5, + "recognitionSources": [ + "model" + ], + "startIndex": 27, + "text": "Delta", + "type": "Airline" + } + ] + }, + "Airline": [ + [ + "HumAir" + ], + [ + "Delta" + ] + ] + }, + "intents": { + "Cancel": { + "score": 0.00330878259 + }, + "Delivery": { + "score": 0.00452178251 + }, + "EntityTests": { + "score": 0.052175343 + }, + "Greeting": { + "score": 0.002769983 + }, + "Help": { + "score": 0.002995687 + }, + "None": { + "score": 0.0302589461 + }, + "Roles": { + "score": 0.132316783 + }, + "search": { + "score": 0.007362695 + }, + "SpecifyName": { + "score": 0.00500302855 + }, + "Travel": { + "score": 0.0146034053 + }, + "Weather.GetForecast": { + "score": 0.005048246 + } + }, + "normalizedQuery": "fly on humberg airlines or delta", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "Roles" + }, + "query": "fly on humberg airlines or Delta" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndRegex_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndRegex_v3.json new file mode 100644 index 000000000..fa8566eb3 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndRegex_v3.json @@ -0,0 +1,167 @@ +{ + "entities": { + "$instance": { + "Part": [ + { + "endIndex": 5, + "modelType": "Regex Entity Extractor", + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 0, + "text": "42ski", + "type": "Part" + }, + { + "endIndex": 26, + "modelType": "Regex Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 21, + "text": "kb423", + "type": "Part" + } + ] + }, + "Part": [ + "42ski", + "kb423" + ] + }, + "intents": { + "Cancel": { + "score": 0.0127721056 + }, + "Delivery": { + "score": 0.004578639 + }, + "EntityTests": { + "score": 0.008811761 + }, + "Greeting": { + "score": 0.00256775436 + }, + "Help": { + "score": 0.00214677141 + }, + "None": { + "score": 0.27875194 + }, + "Roles": { + "score": 0.0273685548 + }, + "search": { + "score": 0.0084077 + }, + "SpecifyName": { + "score": 0.0148377549 + }, + "Travel": { + "score": 0.0039825947 + }, + "Weather_GetForecast": { + "score": 0.009611839 + } + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "text": "42ski is a part like kb423", + "v3": { + "options": { + "externalEntities": [ + { + "entityLength": 5, + "entityName": "Part", + "startIndex": 0 + } + ], + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "$instance": { + "Part": [ + { + "length": 5, + "modelType": "Regex Entity Extractor", + "modelTypeId": 8, + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 0, + "text": "42ski", + "type": "Part" + }, + { + "length": 5, + "modelType": "Regex Entity Extractor", + "modelTypeId": 8, + "recognitionSources": [ + "model" + ], + "startIndex": 21, + "text": "kb423", + "type": "Part" + } + ] + }, + "Part": [ + "42ski", + "kb423" + ] + }, + "intents": { + "Cancel": { + "score": 0.0127721056 + }, + "Delivery": { + "score": 0.004578639 + }, + "EntityTests": { + "score": 0.008811761 + }, + "Greeting": { + "score": 0.00256775436 + }, + "Help": { + "score": 0.00214677141 + }, + "None": { + "score": 0.27875194 + }, + "Roles": { + "score": 0.0273685548 + }, + "search": { + "score": 0.0084077 + }, + "SpecifyName": { + "score": 0.0148377549 + }, + "Travel": { + "score": 0.0039825947 + }, + "Weather.GetForecast": { + "score": 0.009611839 + } + }, + "normalizedQuery": "42ski is a part like kb423", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "None" + }, + "query": "42ski is a part like kb423" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndSimpleOverride_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndSimpleOverride_v3.json new file mode 100644 index 000000000..8f48817dd --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndSimpleOverride_v3.json @@ -0,0 +1,299 @@ +{ + "entities": { + "$instance": { + "Address": [ + { + "endIndex": 13, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.7033113, + "startIndex": 8, + "text": "37 wa", + "type": "Address" + } + ], + "number": [ + { + "endIndex": 19, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "82", + "type": "builtin.number" + } + ], + "State": [ + { + "endIndex": 22, + "modelType": "Entity Extractor", + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 20, + "text": "co", + "type": "State" + } + ] + }, + "Address": [ + { + "$instance": { + "number": [ + { + "endIndex": 10, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 8, + "text": "37", + "type": "builtin.number" + } + ], + "State": [ + { + "endIndex": 13, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model", + "externalEntities" + ], + "score": 0.5987082, + "startIndex": 11, + "text": "wa", + "type": "State" + } + ] + }, + "number": [ + 37 + ], + "State": [ + { + "state": "Washington" + } + ] + } + ], + "number": [ + 82 + ], + "State": [ + { + "state": "Colorado" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.004045653 + }, + "Delivery": { + "score": 0.511144161 + }, + "EntityTests": { + "score": 0.004197402 + }, + "Greeting": { + "score": 0.00286332145 + }, + "Help": { + "score": 0.00351834856 + }, + "None": { + "score": 0.01229356 + }, + "Roles": { + "score": 0.08465987 + }, + "search": { + "score": 0.009909824 + }, + "SpecifyName": { + "score": 0.006426142 + }, + "Travel": { + "score": 0.008369388 + }, + "Weather_GetForecast": { + "score": 0.0112502193 + } + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "text": "deliver 37 wa to 82 co", + "v3": { + "options": { + "externalEntities": [ + { + "entityLength": 2, + "entityName": "State", + "resolution": { + "state": "Washington" + }, + "startIndex": 11 + }, + { + "entityLength": 2, + "entityName": "State", + "resolution": { + "state": "Colorado" + }, + "startIndex": 20 + } + ], + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "$instance": { + "Address": [ + { + "length": 5, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "score": 0.7033113, + "startIndex": 8, + "text": "37 wa", + "type": "Address" + } + ], + "number": [ + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "82", + "type": "builtin.number" + } + ], + "State": [ + { + "length": 2, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 20, + "text": "co", + "type": "State" + } + ] + }, + "Address": [ + { + "$instance": { + "number": [ + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 8, + "text": "37", + "type": "builtin.number" + } + ], + "State": [ + { + "length": 2, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model", + "externalEntities" + ], + "score": 0.5987082, + "startIndex": 11, + "text": "wa", + "type": "State" + } + ] + }, + "number": [ + 37 + ], + "State": [ + { + "state": "Washington" + } + ] + } + ], + "number": [ + 82 + ], + "State": [ + { + "state": "Colorado" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.004045653 + }, + "Delivery": { + "score": 0.511144161 + }, + "EntityTests": { + "score": 0.004197402 + }, + "Greeting": { + "score": 0.00286332145 + }, + "Help": { + "score": 0.00351834856 + }, + "None": { + "score": 0.01229356 + }, + "Roles": { + "score": 0.08465987 + }, + "search": { + "score": 0.009909824 + }, + "SpecifyName": { + "score": 0.006426142 + }, + "Travel": { + "score": 0.008369388 + }, + "Weather.GetForecast": { + "score": 0.0112502193 + } + }, + "normalizedQuery": "deliver 37 wa to 82 co", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "Delivery" + }, + "query": "deliver 37 wa to 82 co" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndSimple_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndSimple_v3.json new file mode 100644 index 000000000..e7073627d --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/ExternalEntitiesAndSimple_v3.json @@ -0,0 +1,292 @@ +{ + "entities": { + "$instance": { + "Address": [ + { + "endIndex": 13, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.7033113, + "startIndex": 8, + "text": "37 wa", + "type": "Address" + } + ], + "number": [ + { + "endIndex": 19, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "82", + "type": "builtin.number" + } + ], + "State": [ + { + "endIndex": 22, + "modelType": "Entity Extractor", + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 20, + "text": "co", + "type": "State" + } + ] + }, + "Address": [ + { + "$instance": { + "number": [ + { + "endIndex": 10, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 8, + "text": "37", + "type": "builtin.number" + } + ], + "State": [ + { + "endIndex": 13, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model", + "externalEntities" + ], + "score": 0.5987082, + "startIndex": 11, + "text": "wa", + "type": "State" + } + ] + }, + "number": [ + 37 + ], + "State": [ + "wa" + ] + } + ], + "number": [ + 82 + ], + "State": [ + { + "state": "Colorado" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.004045653 + }, + "Delivery": { + "score": 0.511144161 + }, + "EntityTests": { + "score": 0.004197402 + }, + "Greeting": { + "score": 0.00286332145 + }, + "Help": { + "score": 0.00351834856 + }, + "None": { + "score": 0.01229356 + }, + "Roles": { + "score": 0.08465987 + }, + "search": { + "score": 0.009909824 + }, + "SpecifyName": { + "score": 0.006426142 + }, + "Travel": { + "score": 0.008369388 + }, + "Weather_GetForecast": { + "score": 0.0112502193 + } + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "text": "deliver 37 wa to 82 co", + "v3": { + "options": { + "externalEntities": [ + { + "entityLength": 2, + "entityName": "State", + "startIndex": 11 + }, + { + "entityLength": 2, + "entityName": "State", + "resolution": { + "state": "Colorado" + }, + "startIndex": 20 + } + ], + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "$instance": { + "Address": [ + { + "length": 5, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "score": 0.7033113, + "startIndex": 8, + "text": "37 wa", + "type": "Address" + } + ], + "number": [ + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "82", + "type": "builtin.number" + } + ], + "State": [ + { + "length": 2, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "externalEntities" + ], + "startIndex": 20, + "text": "co", + "type": "State" + } + ] + }, + "Address": [ + { + "$instance": { + "number": [ + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 8, + "text": "37", + "type": "builtin.number" + } + ], + "State": [ + { + "length": 2, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model", + "externalEntities" + ], + "score": 0.5987082, + "startIndex": 11, + "text": "wa", + "type": "State" + } + ] + }, + "number": [ + 37 + ], + "State": [ + "wa" + ] + } + ], + "number": [ + 82 + ], + "State": [ + { + "state": "Colorado" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.004045653 + }, + "Delivery": { + "score": 0.511144161 + }, + "EntityTests": { + "score": 0.004197402 + }, + "Greeting": { + "score": 0.00286332145 + }, + "Help": { + "score": 0.00351834856 + }, + "None": { + "score": 0.01229356 + }, + "Roles": { + "score": 0.08465987 + }, + "search": { + "score": 0.009909824 + }, + "SpecifyName": { + "score": 0.006426142 + }, + "Travel": { + "score": 0.008369388 + }, + "Weather.GetForecast": { + "score": 0.0112502193 + } + }, + "normalizedQuery": "deliver 37 wa to 82 co", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "Delivery" + }, + "query": "deliver 37 wa to 82 co" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/GeoPeopleOrdinal_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/GeoPeopleOrdinal_v3.json new file mode 100644 index 000000000..4ac3ed4ff --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/GeoPeopleOrdinal_v3.json @@ -0,0 +1,321 @@ +{ + "entities": { + "$instance": { + "child": [ + { + "endIndex": 99, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 87, + "text": "lisa simpson", + "type": "builtin.personName" + } + ], + "endloc": [ + { + "endIndex": 51, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 44, + "text": "jakarta", + "type": "builtin.geographyV2.city" + } + ], + "ordinalV2": [ + { + "endIndex": 28, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 24, + "text": "last", + "type": "builtin.ordinalV2.relative" + } + ], + "parent": [ + { + "endIndex": 69, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 56, + "text": "homer simpson", + "type": "builtin.personName" + } + ], + "startloc": [ + { + "endIndex": 40, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 34, + "text": "london", + "type": "builtin.geographyV2.city" + } + ], + "startpos": [ + { + "endIndex": 20, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 8, + "text": "next to last", + "type": "builtin.ordinalV2.relative" + } + ] + }, + "child": [ + "lisa simpson" + ], + "endloc": [ + { + "location": "jakarta", + "type": "city" + } + ], + "ordinalV2": [ + { + "offset": 0, + "relativeTo": "end" + } + ], + "parent": [ + "homer simpson" + ], + "startloc": [ + { + "location": "london", + "type": "city" + } + ], + "startpos": [ + { + "offset": -1, + "relativeTo": "end" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.000107549029 + }, + "Delivery": { + "score": 0.00123035291 + }, + "EntityTests": { + "score": 0.0009487789 + }, + "Greeting": { + "score": 5.293933E-05 + }, + "Help": { + "score": 0.0001358991 + }, + "None": { + "score": 0.0109820236 + }, + "Roles": { + "score": 0.999204934 + }, + "search": { + "score": 0.0263254233 + }, + "SpecifyName": { + "score": 0.00104324089 + }, + "Travel": { + "score": 0.01043327 + }, + "Weather_GetForecast": { + "score": 0.0106523167 + } + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "text": "go from next to last to last move london to jakarta and homer simpson is the parent of lisa simpson", + "v3": { + "options": { + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "$instance": { + "child": [ + { + "length": 12, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "child", + "startIndex": 87, + "text": "lisa simpson", + "type": "builtin.personName" + } + ], + "endloc": [ + { + "length": 7, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "endloc", + "startIndex": 44, + "text": "jakarta", + "type": "builtin.geographyV2.city" + } + ], + "ordinalV2": [ + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 24, + "text": "last", + "type": "builtin.ordinalV2.relative" + } + ], + "parent": [ + { + "length": 13, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "parent", + "startIndex": 56, + "text": "homer simpson", + "type": "builtin.personName" + } + ], + "startloc": [ + { + "length": 6, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "startloc", + "startIndex": 34, + "text": "london", + "type": "builtin.geographyV2.city" + } + ], + "startpos": [ + { + "length": 12, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "startpos", + "startIndex": 8, + "text": "next to last", + "type": "builtin.ordinalV2.relative" + } + ] + }, + "child": [ + "lisa simpson" + ], + "endloc": [ + { + "value": "jakarta", + "type": "city" + } + ], + "ordinalV2": [ + { + "offset": 0, + "relativeTo": "end" + } + ], + "parent": [ + "homer simpson" + ], + "startloc": [ + { + "value": "london", + "type": "city" + } + ], + "startpos": [ + { + "offset": -1, + "relativeTo": "end" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.000107549029 + }, + "Delivery": { + "score": 0.00123035291 + }, + "EntityTests": { + "score": 0.0009487789 + }, + "Greeting": { + "score": 5.293933E-05 + }, + "Help": { + "score": 0.0001358991 + }, + "None": { + "score": 0.0109820236 + }, + "Roles": { + "score": 0.999204934 + }, + "search": { + "score": 0.0263254233 + }, + "SpecifyName": { + "score": 0.00104324089 + }, + "Travel": { + "score": 0.01043327 + }, + "Weather.GetForecast": { + "score": 0.0106523167 + } + }, + "normalizedQuery": "go from next to last to last move london to jakarta and homer simpson is the parent of lisa simpson", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "Roles" + }, + "query": "go from next to last to last move london to jakarta and homer simpson is the parent of lisa simpson" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/Minimal_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/Minimal_v3.json new file mode 100644 index 000000000..b810446ad --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/Minimal_v3.json @@ -0,0 +1,83 @@ +{ + "entities": { + "Airline": [ + [ + "Delta" + ] + ], + "datetime": [ + { + "timex": [ + "T15" + ], + "type": "time" + } + ], + "dimension": [ + { + "number": 3, + "units": "Picometer" + } + ] + }, + "intents": { + "Roles": { + "score": 0.446264923 + } + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "text": "fly on delta at 3pm", + "v3": { + "options": { + "includeAllIntents": false, + "includeAPIResults": true, + "includeInstanceData": false, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "Airline": [ + [ + "Delta" + ] + ], + "datetimeV2": [ + { + "type": "time", + "values": [ + { + "timex": "T15", + "value": "15:00:00" + } + ] + } + ], + "dimension": [ + { + "number": 3, + "unit": "Picometer" + } + ] + }, + "intents": { + "Roles": { + "score": 0.446264923 + } + }, + "normalizedQuery": "fly on delta at 3pm", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "Roles" + }, + "query": "fly on delta at 3pm" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/NoEntitiesInstanceTrue_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/NoEntitiesInstanceTrue_v3.json new file mode 100644 index 000000000..10a268338 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/NoEntitiesInstanceTrue_v3.json @@ -0,0 +1,33 @@ +{ + "entities": { + "$instance": {} + }, + "intents": { + "Greeting": { + "score": 0.9589885 + } + }, + "text": "Hi", + "v3": { + "options": { + "includeAllIntents": false, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "query": "Hi", + "prediction": { + "topIntent": "Greeting", + "intents": { + "Greeting": { + "score": 0.9589885 + } + }, + "entities": {} + } + } + } + } diff --git a/libraries/botbuilder-ai/tests/luis/test_data/Patterns_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/Patterns_v3.json new file mode 100644 index 000000000..824bf5f54 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/Patterns_v3.json @@ -0,0 +1,262 @@ +{ + "entities": { + "$instance": { + "extra": [ + { + "endIndex": 76, + "modelType": "Pattern.Any Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 71, + "text": "kb435", + "type": "subject" + } + ], + "Part": [ + { + "endIndex": 76, + "modelType": "Regex Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 71, + "text": "kb435", + "type": "Part" + } + ], + "person": [ + { + "endIndex": 61, + "modelType": "Pattern.Any Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 49, + "text": "bart simpson", + "type": "person" + } + ], + "personName": [ + { + "endIndex": 61, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 49, + "text": "bart simpson", + "type": "builtin.personName" + } + ], + "subject": [ + { + "endIndex": 43, + "modelType": "Pattern.Any Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 12, + "text": "something wicked this way comes", + "type": "subject" + } + ] + }, + "extra": [ + "kb435" + ], + "Part": [ + "kb435" + ], + "person": [ + "bart simpson" + ], + "personName": [ + "bart simpson" + ], + "subject": [ + "something wicked this way comes" + ] + }, + "intents": { + "Cancel": { + "score": 1.01764708E-09 + }, + "Delivery": { + "score": 1.8E-09 + }, + "EntityTests": { + "score": 1.044335E-05 + }, + "Greeting": { + "score": 1.0875E-09 + }, + "Help": { + "score": 1.01764708E-09 + }, + "None": { + "score": 2.38094663E-06 + }, + "Roles": { + "score": 5.98274755E-06 + }, + "search": { + "score": 0.9999993 + }, + "SpecifyName": { + "score": 3.0666667E-09 + }, + "Travel": { + "score": 3.09763345E-06 + }, + "Weather_GetForecast": { + "score": 1.02792524E-06 + } + }, + "sentiment": { + "label": "negative", + "score": 0.210341513 + }, + "text": "email about something wicked this way comes from bart simpson and also kb435", + "v3": { + "options": { + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "$instance": { + "extra": [ + { + "length": 5, + "modelType": "Pattern.Any Entity Extractor", + "modelTypeId": 7, + "recognitionSources": [ + "model" + ], + "role": "extra", + "startIndex": 71, + "text": "kb435", + "type": "subject" + } + ], + "Part": [ + { + "length": 5, + "modelType": "Regex Entity Extractor", + "modelTypeId": 8, + "recognitionSources": [ + "model" + ], + "startIndex": 71, + "text": "kb435", + "type": "Part" + } + ], + "person": [ + { + "length": 12, + "modelType": "Pattern.Any Entity Extractor", + "modelTypeId": 7, + "recognitionSources": [ + "model" + ], + "startIndex": 49, + "text": "bart simpson", + "type": "person" + } + ], + "personName": [ + { + "length": 12, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 49, + "text": "bart simpson", + "type": "builtin.personName" + } + ], + "subject": [ + { + "length": 31, + "modelType": "Pattern.Any Entity Extractor", + "modelTypeId": 7, + "recognitionSources": [ + "model" + ], + "startIndex": 12, + "text": "something wicked this way comes", + "type": "subject" + } + ] + }, + "extra": [ + "kb435" + ], + "Part": [ + "kb435" + ], + "person": [ + "bart simpson" + ], + "personName": [ + "bart simpson" + ], + "subject": [ + "something wicked this way comes" + ] + }, + "intents": { + "Cancel": { + "score": 1.01764708E-09 + }, + "Delivery": { + "score": 1.8E-09 + }, + "EntityTests": { + "score": 1.044335E-05 + }, + "Greeting": { + "score": 1.0875E-09 + }, + "Help": { + "score": 1.01764708E-09 + }, + "None": { + "score": 2.38094663E-06 + }, + "Roles": { + "score": 5.98274755E-06 + }, + "search": { + "score": 0.9999993 + }, + "SpecifyName": { + "score": 3.0666667E-09 + }, + "Travel": { + "score": 3.09763345E-06 + }, + "Weather.GetForecast": { + "score": 1.02792524E-06 + } + }, + "normalizedQuery": "email about something wicked this way comes from bart simpson and also kb435", + "sentiment": { + "label": "negative", + "score": 0.210341513 + }, + "topIntent": "search" + }, + "query": "email about something wicked this way comes from bart simpson and also kb435" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/Prebuilt_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/Prebuilt_v3.json new file mode 100644 index 000000000..9cb4ab134 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/Prebuilt_v3.json @@ -0,0 +1,246 @@ +{ + "entities": { + "$instance": { + "Composite2": [ + { + "endIndex": 66, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.7077416, + "startIndex": 0, + "text": "http://foo.com is where you can get a weather forecast for seattle", + "type": "Composite2" + } + ], + "geographyV2": [ + { + "endIndex": 66, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 59, + "text": "seattle", + "type": "builtin.geographyV2.city" + } + ] + }, + "Composite2": [ + { + "$instance": { + "url": [ + { + "endIndex": 14, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "http://foo.com", + "type": "builtin.url" + } + ], + "Weather_Location": [ + { + "endIndex": 66, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.76184386, + "startIndex": 59, + "text": "seattle", + "type": "Weather.Location" + } + ] + }, + "url": [ + "http://foo.com" + ], + "Weather_Location": [ + "seattle" + ] + } + ], + "geographyV2": [ + { + "location": "seattle", + "type": "city" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.000171828113 + }, + "Delivery": { + "score": 0.0011408634 + }, + "EntityTests": { + "score": 0.342939854 + }, + "Greeting": { + "score": 0.0001518702 + }, + "Help": { + "score": 0.0005502715 + }, + "None": { + "score": 0.0175834317 + }, + "Roles": { + "score": 0.0432791822 + }, + "search": { + "score": 0.01050759 + }, + "SpecifyName": { + "score": 0.001833231 + }, + "Travel": { + "score": 0.004430798 + }, + "Weather_GetForecast": { + "score": 0.669524968 + } + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "text": "http://foo.com is where you can get a weather forecast for seattle", + "v3": { + "options": { + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + }, + "response": { + "prediction": { + "entities": { + "$instance": { + "Composite2": [ + { + "length": 66, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "score": 0.7077416, + "startIndex": 0, + "text": "http://foo.com is where you can get a weather forecast for seattle", + "type": "Composite2" + } + ], + "geographyV2": [ + { + "length": 7, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 59, + "text": "seattle", + "type": "builtin.geographyV2.city" + } + ] + }, + "Composite2": [ + { + "$instance": { + "url": [ + { + "length": 14, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "http://foo.com", + "type": "builtin.url" + } + ], + "Weather.Location": [ + { + "length": 7, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model" + ], + "score": 0.76184386, + "startIndex": 59, + "text": "seattle", + "type": "Weather.Location" + } + ] + }, + "url": [ + "http://foo.com" + ], + "Weather.Location": [ + "seattle" + ] + } + ], + "geographyV2": [ + { + "type": "city", + "value": "seattle" + } + ] + }, + "intents": { + "Cancel": { + "score": 0.000171828113 + }, + "Delivery": { + "score": 0.0011408634 + }, + "EntityTests": { + "score": 0.342939854 + }, + "Greeting": { + "score": 0.0001518702 + }, + "Help": { + "score": 0.0005502715 + }, + "None": { + "score": 0.0175834317 + }, + "Roles": { + "score": 0.0432791822 + }, + "search": { + "score": 0.01050759 + }, + "SpecifyName": { + "score": 0.001833231 + }, + "Travel": { + "score": 0.004430798 + }, + "Weather.GetForecast": { + "score": 0.669524968 + } + }, + "normalizedQuery": "http://foo.com is where you can get a weather forecast for seattle", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "Weather.GetForecast" + }, + "query": "http://foo.com is where you can get a weather forecast for seattle" + } + } + } \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/luis/test_data/roles_v3.json b/libraries/botbuilder-ai/tests/luis/test_data/roles_v3.json new file mode 100644 index 000000000..15ee58ac4 --- /dev/null +++ b/libraries/botbuilder-ai/tests/luis/test_data/roles_v3.json @@ -0,0 +1,1759 @@ +{ + "text": "3 inches long by 2 inches wide and 5% to 10% and are you between 6 years old and 8 years old and can i trade kb457 for kb922 and change 425-777-1212 to 206-666-4123 and did delta buy virgin and did the rain from hawaii get to redmond and http://foo.com changed to http://blah.com and i like between 68 degrees and 72 degrees and john likes mary and leave 3pm and arrive 5pm and pay between $400 and $500 and send chrimc@hotmail.com from emad@gmail.com", + "intents": { + "Cancel": { + "score": 4.50860341e-7 + }, + "Delivery": { + "score": 0.00007978094 + }, + "EntityTests": { + "score": 0.0046325135 + }, + "Greeting": { + "score": 4.73494453e-7 + }, + "Help": { + "score": 7.622754e-7 + }, + "None": { + "score": 0.00093744183 + }, + "Roles": { + "score": 1 + }, + "search": { + "score": 0.07635335 + }, + "SpecifyName": { + "score": 0.00009136085 + }, + "Travel": { + "score": 0.00771805458 + }, + "Weather_GetForecast": { + "score": 0.0100867962 + } + }, + "entities": { + "$instance": { + "a": [ + { + "endIndex": 309, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 299, + "text": "68 degrees", + "type": "builtin.temperature" + } + ], + "arrive": [ + { + "endIndex": 373, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 370, + "text": "5pm", + "type": "builtin.datetimeV2.time" + } + ], + "b": [ + { + "endIndex": 324, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 314, + "text": "72 degrees", + "type": "builtin.temperature" + } + ], + "begin": [ + { + "endIndex": 76, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 65, + "text": "6 years old", + "type": "builtin.age" + } + ], + "buy": [ + { + "endIndex": 124, + "modelType": "Regex Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 119, + "text": "kb922", + "type": "Part" + } + ], + "Buyer": [ + { + "endIndex": 178, + "modelType": "List Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 173, + "text": "delta", + "type": "Airline" + } + ], + "Composite1": [ + { + "endIndex": 172, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.01107535, + "startIndex": 0, + "text": "3 inches long by 2 inches wide and 5% to 10% and are you between 6 years old and 8 years old and can i trade kb457 for kb922 and change 425-777-1212 to 206-666-4123 and did", + "type": "Composite1" + } + ], + "Composite2": [ + { + "endIndex": 283, + "modelType": "Composite Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.15191336, + "startIndex": 238, + "text": "http://foo.com changed to http://blah.com and", + "type": "Composite2" + } + ], + "destination": [ + { + "endIndex": 233, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.985884964, + "startIndex": 226, + "text": "redmond", + "type": "Weather.Location" + } + ], + "dimension": [ + { + "endIndex": 358, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 355, + "text": "3pm", + "type": "builtin.dimension" + }, + { + "endIndex": 373, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 370, + "text": "5pm", + "type": "builtin.dimension" + } + ], + "end": [ + { + "endIndex": 92, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 81, + "text": "8 years old", + "type": "builtin.age" + } + ], + "geographyV2": [ + { + "endIndex": 218, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 212, + "text": "hawaii", + "type": "builtin.geographyV2.state" + }, + { + "endIndex": 233, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 226, + "text": "redmond", + "type": "builtin.geographyV2.city" + } + ], + "leave": [ + { + "endIndex": 358, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 355, + "text": "3pm", + "type": "builtin.datetimeV2.time" + } + ], + "length": [ + { + "endIndex": 8, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "3 inches", + "type": "builtin.dimension" + } + ], + "likee": [ + { + "endIndex": 344, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.9900547, + "startIndex": 340, + "text": "mary", + "type": "Name" + } + ], + "liker": [ + { + "endIndex": 333, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.992201567, + "startIndex": 329, + "text": "john", + "type": "Name" + } + ], + "max": [ + { + "endIndex": 403, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 399, + "text": "$500", + "type": "builtin.currency" + } + ], + "maximum": [ + { + "endIndex": 44, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 41, + "text": "10%", + "type": "builtin.percentage" + } + ], + "min": [ + { + "endIndex": 394, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 390, + "text": "$400", + "type": "builtin.currency" + } + ], + "minimum": [ + { + "endIndex": 37, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 35, + "text": "5%", + "type": "builtin.percentage" + } + ], + "newPhone": [ + { + "endIndex": 164, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.9, + "startIndex": 152, + "text": "206-666-4123", + "type": "builtin.phonenumber" + } + ], + "number": [ + { + "endIndex": 301, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 299, + "text": "68", + "type": "builtin.number" + }, + { + "endIndex": 316, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 314, + "text": "72", + "type": "builtin.number" + }, + { + "endIndex": 394, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 391, + "text": "400", + "type": "builtin.number" + }, + { + "endIndex": 403, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 400, + "text": "500", + "type": "builtin.number" + } + ], + "old": [ + { + "endIndex": 148, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.9, + "startIndex": 136, + "text": "425-777-1212", + "type": "builtin.phonenumber" + } + ], + "oldURL": [ + { + "endIndex": 252, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 238, + "text": "http://foo.com", + "type": "builtin.url" + } + ], + "personName": [ + { + "endIndex": 333, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 329, + "text": "john", + "type": "builtin.personName" + }, + { + "endIndex": 344, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 340, + "text": "mary", + "type": "builtin.personName" + } + ], + "receiver": [ + { + "endIndex": 431, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 413, + "text": "chrimc@hotmail.com", + "type": "builtin.email" + } + ], + "sell": [ + { + "endIndex": 114, + "modelType": "Regex Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 109, + "text": "kb457", + "type": "Part" + } + ], + "Seller": [ + { + "endIndex": 189, + "modelType": "List Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 183, + "text": "virgin", + "type": "Airline" + } + ], + "sender": [ + { + "endIndex": 451, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 437, + "text": "emad@gmail.com", + "type": "builtin.email" + } + ], + "source": [ + { + "endIndex": 218, + "modelType": "Entity Extractor", + "recognitionSources": [ + "model" + ], + "score": 0.9713092, + "startIndex": 212, + "text": "hawaii", + "type": "Weather.Location" + } + ], + "width": [ + { + "endIndex": 25, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "2 inches", + "type": "builtin.dimension" + } + ] + }, + "a": [ + { + "number": 68, + "units": "Degree" + } + ], + "arrive": [ + { + "timex": [ + "T17" + ], + "type": "time" + } + ], + "b": [ + { + "number": 72, + "units": "Degree" + } + ], + "begin": [ + { + "number": 6, + "units": "Year" + } + ], + "buy": [ + "kb922" + ], + "Buyer": [ + [ + "Delta" + ] + ], + "Composite1": [ + { + "$instance": { + "datetime": [ + { + "endIndex": 72, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 65, + "text": "6 years", + "type": "builtin.datetimeV2.duration" + }, + { + "endIndex": 88, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 81, + "text": "8 years", + "type": "builtin.datetimeV2.duration" + } + ], + "number": [ + { + "endIndex": 1, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "3", + "type": "builtin.number" + }, + { + "endIndex": 18, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "2", + "type": "builtin.number" + }, + { + "endIndex": 36, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 35, + "text": "5", + "type": "builtin.number" + }, + { + "endIndex": 43, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 41, + "text": "10", + "type": "builtin.number" + }, + { + "endIndex": 66, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 65, + "text": "6", + "type": "builtin.number" + }, + { + "endIndex": 82, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 81, + "text": "8", + "type": "builtin.number" + }, + { + "endIndex": 139, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 136, + "text": "425", + "type": "builtin.number" + }, + { + "endIndex": 143, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 140, + "text": "777", + "type": "builtin.number" + }, + { + "endIndex": 148, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 144, + "text": "1212", + "type": "builtin.number" + }, + { + "endIndex": 155, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 152, + "text": "206", + "type": "builtin.number" + }, + { + "endIndex": 159, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 156, + "text": "666", + "type": "builtin.number" + }, + { + "endIndex": 164, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 160, + "text": "4123", + "type": "builtin.number" + } + ] + }, + "datetime": [ + { + "timex": [ + "P6Y" + ], + "type": "duration" + }, + { + "timex": [ + "P8Y" + ], + "type": "duration" + } + ], + "number": [ + 3, + 2, + 5, + 10, + 6, + 8, + 425, + 777, + 1212, + 206, + 666, + 4123 + ] + } + ], + "Composite2": [ + { + "$instance": { + "url": [ + { + "endIndex": 279, + "modelType": "Prebuilt Entity Extractor", + "recognitionSources": [ + "model" + ], + "startIndex": 264, + "text": "http://blah.com", + "type": "builtin.url" + } + ] + }, + "url": [ + "http://blah.com" + ] + } + ], + "destination": [ + "redmond" + ], + "dimension": [ + { + "number": 3, + "units": "Picometer" + }, + { + "number": 5, + "units": "Picometer" + } + ], + "end": [ + { + "number": 8, + "units": "Year" + } + ], + "geographyV2": [ + { + "location": "hawaii", + "type": "state" + }, + { + "location": "redmond", + "type": "city" + } + ], + "leave": [ + { + "timex": [ + "T15" + ], + "type": "time" + } + ], + "length": [ + { + "number": 3, + "units": "Inch" + } + ], + "likee": [ + "mary" + ], + "liker": [ + "john" + ], + "max": [ + { + "number": 500, + "units": "Dollar" + } + ], + "maximum": [ + 10 + ], + "min": [ + { + "number": 400, + "units": "Dollar" + } + ], + "minimum": [ + 5 + ], + "newPhone": [ + "206-666-4123" + ], + "number": [ + 68, + 72, + 400, + 500 + ], + "old": [ + "425-777-1212" + ], + "oldURL": [ + "http://foo.com" + ], + "personName": [ + "john", + "mary" + ], + "receiver": [ + "chrimc@hotmail.com" + ], + "sell": [ + "kb457" + ], + "Seller": [ + [ + "Virgin" + ] + ], + "sender": [ + "emad@gmail.com" + ], + "source": [ + "hawaii" + ], + "width": [ + { + "number": 2, + "units": "Inch" + } + ] + }, + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "v3": { + "response": { + "prediction": { + "entities": { + "$instance": { + "a": [ + { + "length": 10, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "a", + "startIndex": 299, + "text": "68 degrees", + "type": "builtin.temperature" + } + ], + "arrive": [ + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "arrive", + "startIndex": 370, + "text": "5pm", + "type": "builtin.datetimeV2.time" + } + ], + "b": [ + { + "length": 10, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "b", + "startIndex": 314, + "text": "72 degrees", + "type": "builtin.temperature" + } + ], + "begin": [ + { + "length": 11, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "begin", + "startIndex": 65, + "text": "6 years old", + "type": "builtin.age" + } + ], + "buy": [ + { + "length": 5, + "modelType": "Regex Entity Extractor", + "modelTypeId": 8, + "recognitionSources": [ + "model" + ], + "role": "buy", + "startIndex": 119, + "text": "kb922", + "type": "Part" + } + ], + "Buyer": [ + { + "length": 5, + "modelType": "List Entity Extractor", + "modelTypeId": 5, + "recognitionSources": [ + "model" + ], + "role": "Buyer", + "startIndex": 173, + "text": "delta", + "type": "Airline" + } + ], + "Composite1": [ + { + "length": 172, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "score": 0.01107535, + "startIndex": 0, + "text": "3 inches long by 2 inches wide and 5% to 10% and are you between 6 years old and 8 years old and can i trade kb457 for kb922 and change 425-777-1212 to 206-666-4123 and did", + "type": "Composite1" + } + ], + "Composite2": [ + { + "length": 45, + "modelType": "Composite Entity Extractor", + "modelTypeId": 4, + "recognitionSources": [ + "model" + ], + "score": 0.15191336, + "startIndex": 238, + "text": "http://foo.com changed to http://blah.com and", + "type": "Composite2" + } + ], + "destination": [ + { + "length": 7, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model" + ], + "role": "destination", + "score": 0.985884964, + "startIndex": 226, + "text": "redmond", + "type": "Weather.Location" + } + ], + "dimension": [ + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 355, + "text": "3pm", + "type": "builtin.dimension" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 370, + "text": "5pm", + "type": "builtin.dimension" + } + ], + "end": [ + { + "length": 11, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "end", + "startIndex": 81, + "text": "8 years old", + "type": "builtin.age" + } + ], + "geographyV2": [ + { + "length": 6, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 212, + "text": "hawaii", + "type": "builtin.geographyV2.state" + }, + { + "length": 7, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 226, + "text": "redmond", + "type": "builtin.geographyV2.city" + } + ], + "leave": [ + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "leave", + "startIndex": 355, + "text": "3pm", + "type": "builtin.datetimeV2.time" + } + ], + "length": [ + { + "length": 8, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "length", + "startIndex": 0, + "text": "3 inches", + "type": "builtin.dimension" + } + ], + "likee": [ + { + "length": 4, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model" + ], + "role": "likee", + "score": 0.9900547, + "startIndex": 340, + "text": "mary", + "type": "Name" + } + ], + "liker": [ + { + "length": 4, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model" + ], + "role": "liker", + "score": 0.992201567, + "startIndex": 329, + "text": "john", + "type": "Name" + } + ], + "max": [ + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "max", + "startIndex": 399, + "text": "$500", + "type": "builtin.currency" + } + ], + "maximum": [ + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "maximum", + "startIndex": 41, + "text": "10%", + "type": "builtin.percentage" + } + ], + "min": [ + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "min", + "startIndex": 390, + "text": "$400", + "type": "builtin.currency" + } + ], + "minimum": [ + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "minimum", + "startIndex": 35, + "text": "5%", + "type": "builtin.percentage" + } + ], + "newPhone": [ + { + "length": 12, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "newPhone", + "score": 0.9, + "startIndex": 152, + "text": "206-666-4123", + "type": "builtin.phonenumber" + } + ], + "number": [ + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 299, + "text": "68", + "type": "builtin.number" + }, + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 314, + "text": "72", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 391, + "text": "400", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 400, + "text": "500", + "type": "builtin.number" + } + ], + "old": [ + { + "length": 12, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "old", + "score": 0.9, + "startIndex": 136, + "text": "425-777-1212", + "type": "builtin.phonenumber" + } + ], + "oldURL": [ + { + "length": 14, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "oldURL", + "startIndex": 238, + "text": "http://foo.com", + "type": "builtin.url" + } + ], + "personName": [ + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 329, + "text": "john", + "type": "builtin.personName" + }, + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 340, + "text": "mary", + "type": "builtin.personName" + } + ], + "receiver": [ + { + "length": 18, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "receiver", + "startIndex": 413, + "text": "chrimc@hotmail.com", + "type": "builtin.email" + } + ], + "sell": [ + { + "length": 5, + "modelType": "Regex Entity Extractor", + "modelTypeId": 8, + "recognitionSources": [ + "model" + ], + "role": "sell", + "startIndex": 109, + "text": "kb457", + "type": "Part" + } + ], + "Seller": [ + { + "length": 6, + "modelType": "List Entity Extractor", + "modelTypeId": 5, + "recognitionSources": [ + "model" + ], + "role": "Seller", + "startIndex": 183, + "text": "virgin", + "type": "Airline" + } + ], + "sender": [ + { + "length": 14, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "sender", + "startIndex": 437, + "text": "emad@gmail.com", + "type": "builtin.email" + } + ], + "source": [ + { + "length": 6, + "modelType": "Entity Extractor", + "modelTypeId": 1, + "recognitionSources": [ + "model" + ], + "role": "source", + "score": 0.9713092, + "startIndex": 212, + "text": "hawaii", + "type": "Weather.Location" + } + ], + "width": [ + { + "length": 8, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "role": "width", + "startIndex": 17, + "text": "2 inches", + "type": "builtin.dimension" + } + ] + }, + "a": [ + { + "number": 68, + "unit": "Degree" + } + ], + "arrive": [ + { + "type": "time", + "values": [ + { + "timex": "T17", + "value": "17:00:00" + } + ] + } + ], + "b": [ + { + "number": 72, + "unit": "Degree" + } + ], + "begin": [ + { + "number": 6, + "unit": "Year" + } + ], + "buy": [ + "kb922" + ], + "Buyer": [ + [ + "Delta" + ] + ], + "Composite1": [ + { + "$instance": { + "datetimeV2": [ + { + "length": 7, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 65, + "text": "6 years", + "type": "builtin.datetimeV2.duration" + }, + { + "length": 7, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 81, + "text": "8 years", + "type": "builtin.datetimeV2.duration" + } + ], + "number": [ + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 0, + "text": "3", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 17, + "text": "2", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 35, + "text": "5", + "type": "builtin.number" + }, + { + "length": 2, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 41, + "text": "10", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 65, + "text": "6", + "type": "builtin.number" + }, + { + "length": 1, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 81, + "text": "8", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 136, + "text": "425", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 140, + "text": "777", + "type": "builtin.number" + }, + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 144, + "text": "1212", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 152, + "text": "206", + "type": "builtin.number" + }, + { + "length": 3, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 156, + "text": "666", + "type": "builtin.number" + }, + { + "length": 4, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 160, + "text": "4123", + "type": "builtin.number" + } + ] + }, + "datetimeV2": [ + { + "type": "duration", + "values": [ + { + "timex": "P6Y", + "value": "189216000" + } + ] + }, + { + "type": "duration", + "values": [ + { + "timex": "P8Y", + "value": "252288000" + } + ] + } + ], + "number": [ + 3, + 2, + 5, + 10, + 6, + 8, + 425, + 777, + 1212, + 206, + 666, + 4123 + ] + } + ], + "Composite2": [ + { + "$instance": { + "url": [ + { + "length": 15, + "modelType": "Prebuilt Entity Extractor", + "modelTypeId": 2, + "recognitionSources": [ + "model" + ], + "startIndex": 264, + "text": "http://blah.com", + "type": "builtin.url" + } + ] + }, + "url": [ + "http://blah.com" + ] + } + ], + "destination": [ + "redmond" + ], + "dimension": [ + { + "number": 3, + "unit": "Picometer" + }, + { + "number": 5, + "unit": "Picometer" + } + ], + "end": [ + { + "number": 8, + "unit": "Year" + } + ], + "geographyV2": [ + { + "type": "state", + "value": "hawaii" + }, + { + "type": "city", + "value": "redmond" + } + ], + "leave": [ + { + "type": "time", + "values": [ + { + "timex": "T15", + "value": "15:00:00" + } + ] + } + ], + "length": [ + { + "number": 3, + "unit": "Inch" + } + ], + "likee": [ + "mary" + ], + "liker": [ + "john" + ], + "max": [ + { + "number": 500, + "unit": "Dollar" + } + ], + "maximum": [ + 10 + ], + "min": [ + { + "number": 400, + "unit": "Dollar" + } + ], + "minimum": [ + 5 + ], + "newPhone": [ + "206-666-4123" + ], + "number": [ + 68, + 72, + 400, + 500 + ], + "old": [ + "425-777-1212" + ], + "oldURL": [ + "http://foo.com" + ], + "personName": [ + "john", + "mary" + ], + "receiver": [ + "chrimc@hotmail.com" + ], + "sell": [ + "kb457" + ], + "Seller": [ + [ + "Virgin" + ] + ], + "sender": [ + "emad@gmail.com" + ], + "source": [ + "hawaii" + ], + "width": [ + { + "number": 2, + "unit": "Inch" + } + ] + }, + "intents": { + "Cancel": { + "score": 4.50860341e-7 + }, + "Delivery": { + "score": 0.00007978094 + }, + "EntityTests": { + "score": 0.0046325135 + }, + "Greeting": { + "score": 4.73494453e-7 + }, + "Help": { + "score": 7.622754e-7 + }, + "None": { + "score": 0.00093744183 + }, + "Roles": { + "score": 1 + }, + "search": { + "score": 0.07635335 + }, + "SpecifyName": { + "score": 0.00009136085 + }, + "Travel": { + "score": 0.00771805458 + }, + "Weather.GetForecast": { + "score": 0.0100867962 + } + }, + "normalizedQuery": "3 inches long by 2 inches wide and 5% to 10% and are you between 6 years old and 8 years old and can i trade kb457 for kb922 and change 425-777-1212 to 206-666-4123 and did delta buy virgin and did the rain from hawaii get to redmond and http://foo.com changed to http://blah.com and i like between 68 degrees and 72 degrees and john likes mary and leave 3pm and arrive 5pm and pay between $400 and $500 and send chrimc@hotmail.com from emad@gmail.com", + "sentiment": { + "label": "neutral", + "score": 0.5 + }, + "topIntent": "Roles" + }, + "query": "3 inches long by 2 inches wide and 5% to 10% and are you between 6 years old and 8 years old and can i trade kb457 for kb922 and change 425-777-1212 to 206-666-4123 and did delta buy virgin and did the rain from hawaii get to redmond and http://foo.com changed to http://blah.com and i like between 68 degrees and 72 degrees and john likes mary and leave 3pm and arrive 5pm and pay between $400 and $500 and send chrimc@hotmail.com from emad@gmail.com" + }, + "options": { + "includeAllIntents": true, + "includeAPIResults": true, + "includeInstanceData": true, + "log": true, + "preferExternalEntities": true, + "slot": "production" + } + } +} \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/qna/test_data/QnAMakerDialog_ActiveLearning.json b/libraries/botbuilder-ai/tests/qna/test_data/QnAMakerDialog_ActiveLearning.json new file mode 100644 index 000000000..33c82e70d --- /dev/null +++ b/libraries/botbuilder-ai/tests/qna/test_data/QnAMakerDialog_ActiveLearning.json @@ -0,0 +1,65 @@ +{ + "answers": [ + { + "questions": [ + "Esper seeks" + ], + "answer": "Esper seeks. She's a curious little explorer. Young toddlers seek out new adventures, expanding their knowledge base. It's their job to test limits, to learn about them. It's the adult's job to enforce the limits, while also allowing room for exploration", + "score": 79.65, + "id": 35, + "source": "Editorial", + "isDocumentText": false, + "metadata": [], + "context": { + "isContextOnly": false, + "prompts": [] + } + }, + { + "questions": [ + "Esper sups" + ], + "answer": "Esper sups. She eats just about anything. She loves her broccoli. Anything that she sees her parents eating, she wants to part take in herself.\n\nCaution though. If she spots you eating dessert, you best be prepared to share with her. Best to wait until she goes down for bed and then sneak your favorite snack in, without her prying eyes.", + "score": 79.65, + "id": 36, + "source": "Editorial", + "isDocumentText": false, + "metadata": [], + "context": { + "isContextOnly": false, + "prompts": [] + } + }, + { + "questions": [ + "Esper screams" + ], + "answer": "Esper screams. The currently 1-year old toddler has a brain that's rapidly developing, expanding to new abilities at an alarming rate. With it may come fright or possibly frustration as they understand what could be done, however they need to master how to do a task themselves", + "score": 66.89, + "id": 34, + "source": "Editorial", + "isDocumentText": false, + "metadata": [], + "context": { + "isContextOnly": false, + "prompts": [] + } + }, + { + "questions": [ + "Esper sleeps" + ], + "answer": "Esper sleeps. Esper sleeps on her floor bed. She never had a crib, as her parents placed her directly on the floor bed since birth. With this comes the benefit of not having to have an awkward transition period from crib to bed, when she gets old enough.\n\nThe idea of using the bed is that it offers the child more freedom to move about--more autonomy. Downside is, they will definitely wander off the bed, when they don't want to sleep", + "score": 65.71, + "id": 33, + "source": "Editorial", + "isDocumentText": false, + "metadata": [], + "context": { + "isContextOnly": false, + "prompts": [] + } + } + ], + "activeLearningEnabled": true +} \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/qna/test_data/QnAMakerDialog_MultiTurn_Answer1.json b/libraries/botbuilder-ai/tests/qna/test_data/QnAMakerDialog_MultiTurn_Answer1.json new file mode 100644 index 000000000..29119ff67 --- /dev/null +++ b/libraries/botbuilder-ai/tests/qna/test_data/QnAMakerDialog_MultiTurn_Answer1.json @@ -0,0 +1,32 @@ +{ + "answers": [ + { + "questions": [ + "Tell me about birds", + "What do you know about birds" + ], + "answer": "Choose one of the following birds to get more info", + "score": 100.0, + "id": 37, + "source": "Editorial", + "isDocumentText": false, + "metadata": [], + "context": { + "isContextOnly": false, + "prompts": [ + { + "displayOrder": 1, + "qnaId": 38, + "displayText": "Bald Eagle" + }, + { + "displayOrder": 2, + "qnaId": 39, + "displayText": "Hummingbird" + } + ] + } + } + ], + "activeLearningEnabled": true +} \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/qna/test_data/QnAMakerDialog_MultiTurn_Answer2.json b/libraries/botbuilder-ai/tests/qna/test_data/QnAMakerDialog_MultiTurn_Answer2.json new file mode 100644 index 000000000..5c72329f5 --- /dev/null +++ b/libraries/botbuilder-ai/tests/qna/test_data/QnAMakerDialog_MultiTurn_Answer2.json @@ -0,0 +1,20 @@ +{ + "answers": [ + { + "questions": [ + "Bald Eagle" + ], + "answer": "Apparently these guys aren't actually bald!", + "score": 100.0, + "id": 38, + "source": "Editorial", + "isDocumentText": false, + "metadata": [], + "context": { + "isContextOnly": true, + "prompts": [] + } + } + ], + "activeLearningEnabled": true +} \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/qna/test_data/QnaMaker_TopNAnswer.json b/libraries/botbuilder-ai/tests/qna/test_data/QnaMaker_TopNAnswer.json new file mode 100644 index 000000000..6c09c9b8d --- /dev/null +++ b/libraries/botbuilder-ai/tests/qna/test_data/QnaMaker_TopNAnswer.json @@ -0,0 +1,65 @@ +{ + "activeLearningEnabled": true, + "answers": [ + { + "questions": [ + "Q1" + ], + "answer": "A1", + "score": 80, + "id": 15, + "source": "Editorial", + "metadata": [ + { + "name": "topic", + "value": "value" + } + ] + }, + { + "questions": [ + "Q2" + ], + "answer": "A2", + "score": 78, + "id": 16, + "source": "Editorial", + "metadata": [ + { + "name": "topic", + "value": "value" + } + ] + }, + { + "questions": [ + "Q3" + ], + "answer": "A3", + "score": 75, + "id": 17, + "source": "Editorial", + "metadata": [ + { + "name": "topic", + "value": "value" + } + ] + }, + { + "questions": [ + "Q4" + ], + "answer": "A4", + "score": 50, + "id": 18, + "source": "Editorial", + "metadata": [ + { + "name": "topic", + "value": "value" + } + ] + } + ] +} \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/qna/test_data/QnaMaker_TopNAnswer_DisableActiveLearning.json b/libraries/botbuilder-ai/tests/qna/test_data/QnaMaker_TopNAnswer_DisableActiveLearning.json new file mode 100644 index 000000000..f4fa91d57 --- /dev/null +++ b/libraries/botbuilder-ai/tests/qna/test_data/QnaMaker_TopNAnswer_DisableActiveLearning.json @@ -0,0 +1,65 @@ +{ + "activeLearningEnabled": false, + "answers": [ + { + "questions": [ + "Q1" + ], + "answer": "A1", + "score": 80, + "id": 15, + "source": "Editorial", + "metadata": [ + { + "name": "topic", + "value": "value" + } + ] + }, + { + "questions": [ + "Q2" + ], + "answer": "A2", + "score": 78, + "id": 16, + "source": "Editorial", + "metadata": [ + { + "name": "topic", + "value": "value" + } + ] + }, + { + "questions": [ + "Q3" + ], + "answer": "A3", + "score": 75, + "id": 17, + "source": "Editorial", + "metadata": [ + { + "name": "topic", + "value": "value" + } + ] + }, + { + "questions": [ + "Q4" + ], + "answer": "A4", + "score": 50, + "id": 18, + "source": "Editorial", + "metadata": [ + { + "name": "topic", + "value": "value" + } + ] + } + ] +} \ No newline at end of file diff --git a/libraries/botbuilder-ai/tests/qna/test_data/RetrunsAnswer_WithStrictFilter_And_Operator.json b/libraries/botbuilder-ai/tests/qna/test_data/RetrunsAnswer_WithStrictFilter_And_Operator.json new file mode 100644 index 000000000..1bb54754a --- /dev/null +++ b/libraries/botbuilder-ai/tests/qna/test_data/RetrunsAnswer_WithStrictFilter_And_Operator.json @@ -0,0 +1,29 @@ +{ + "answers": [ + { + "questions": [ + "Where can you find Misty", + "Misty" + ], + "answer": "Wherever people are having a swimming good time", + "score": 74.51, + "id": 27, + "source": "Editorial", + "metadata": [ + { + "name": "species", + "value": "human" + }, + { + "name": "type", + "value": "water" + } + ], + "context": { + "isContextOnly": false, + "prompts": [] + } + } + ], + "activeLearningEnabled": true +} diff --git a/libraries/botbuilder-ai/tests/qna/test_data/RetrunsAnswer_WithStrictFilter_Or_Operator.json b/libraries/botbuilder-ai/tests/qna/test_data/RetrunsAnswer_WithStrictFilter_Or_Operator.json new file mode 100644 index 000000000..3346464fc --- /dev/null +++ b/libraries/botbuilder-ai/tests/qna/test_data/RetrunsAnswer_WithStrictFilter_Or_Operator.json @@ -0,0 +1,76 @@ +{ + "answers": [ + { + "questions": [ + "Where can you find Squirtle" + ], + "answer": "Did you not see him in the first three balls?", + "score": 80.22, + "id": 28, + "source": "Editorial", + "metadata": [ + { + "name": "species", + "value": "turtle" + }, + { + "name": "type", + "value": "water" + } + ], + "context": { + "isContextOnly": false, + "prompts": [] + } + }, + { + "questions": [ + "Where can you find Ash", + "Ash" + ], + "answer": "I don't know. Maybe ask your little electric mouse friend?", + "score": 63.74, + "id": 26, + "source": "Editorial", + "metadata": [ + { + "name": "species", + "value": "human" + }, + { + "name": "type", + "value": "miscellaneous" + } + ], + "context": { + "isContextOnly": false, + "prompts": [] + } + }, + { + "questions": [ + "Where can you find Misty", + "Misty" + ], + "answer": "Wherever people are having a swimming good time", + "score": 31.13, + "id": 27, + "source": "Editorial", + "metadata": [ + { + "name": "species", + "value": "human" + }, + { + "name": "type", + "value": "water" + } + ], + "context": { + "isContextOnly": false, + "prompts": [] + } + } + ], + "activeLearningEnabled": true +} diff --git a/libraries/botbuilder-ai/tests/qna/test_qna.py b/libraries/botbuilder-ai/tests/qna/test_qna.py index cae752861..8a3f595ed 100644 --- a/libraries/botbuilder-ai/tests/qna/test_qna.py +++ b/libraries/botbuilder-ai/tests/qna/test_qna.py @@ -2,23 +2,28 @@ # Licensed under the MIT License. # pylint: disable=protected-access +# pylint: disable=too-many-lines -import json +import unittest from os import path from typing import List, Dict -import unittest from unittest.mock import patch + +import json +import requests from aiohttp import ClientSession import aiounittest from botbuilder.ai.qna import QnAMakerEndpoint, QnAMaker, QnAMakerOptions from botbuilder.ai.qna.models import ( FeedbackRecord, + JoinOperator, Metadata, QueryResult, QnARequestContext, ) -from botbuilder.ai.qna.utils import QnATelemetryConstants +from botbuilder.ai.qna.utils import HttpRequestUtils, QnATelemetryConstants +from botbuilder.ai.qna.models import GenerateAnswerRequestBody from botbuilder.core import BotAdapter, BotTelemetryClient, TurnContext from botbuilder.core.adapters import TestAdapter from botbuilder.schema import ( @@ -109,7 +114,7 @@ def test_options_passed_to_ctor(self): score_threshold=0.8, timeout=9000, top=5, - strict_filters=[Metadata("movie", "disney")], + strict_filters=[Metadata(**{"movie": "disney"})], ) qna_with_options = QnAMaker(self.tests_endpoint, options) @@ -118,7 +123,7 @@ def test_options_passed_to_ctor(self): expected_threshold = 0.8 expected_timeout = 9000 expected_top = 5 - expected_strict_filters = [Metadata("movie", "disney")] + expected_strict_filters = [Metadata(**{"movie": "disney"})] self.assertEqual(expected_threshold, actual_options.score_threshold) self.assertEqual(expected_timeout, actual_options.timeout) @@ -163,12 +168,121 @@ async def test_active_learning_enabled_status(self): self.assertEqual(1, len(result.answers)) self.assertFalse(result.active_learning_enabled) + async def test_returns_answer_with_strict_filters_with_or_operator(self): + # Arrange + question: str = "Where can you find" + response_path: str = "RetrunsAnswer_WithStrictFilter_Or_Operator.json" + response_json = QnaApplicationTest._get_json_for_file(response_path) + + strict_filters = [ + Metadata(name="species", value="human"), + Metadata(name="type", value="water"), + ] + options = QnAMakerOptions( + top=5, + strict_filters=strict_filters, + strict_filters_join_operator=JoinOperator.OR, + ) + qna = QnAMaker(endpoint=QnaApplicationTest.tests_endpoint) + context = QnaApplicationTest._get_context(question, TestAdapter()) + + # Act + with patch( + "aiohttp.ClientSession.post", + return_value=aiounittest.futurized(response_json), + ) as mock_http_client: + result = await qna.get_answers_raw(context, options) + + serialized_http_req_args = mock_http_client.call_args[1]["data"] + req_args = json.loads(serialized_http_req_args) + + # Assert + self.assertIsNotNone(result) + self.assertEqual(3, len(result.answers)) + self.assertEqual( + JoinOperator.OR, req_args["strictFiltersCompoundOperationType"] + ) + + req_args_strict_filters = req_args["strictFilters"] + + first_filter = strict_filters[0] + self.assertEqual(first_filter.name, req_args_strict_filters[0]["name"]) + self.assertEqual(first_filter.value, req_args_strict_filters[0]["value"]) + + second_filter = strict_filters[1] + self.assertEqual(second_filter.name, req_args_strict_filters[1]["name"]) + self.assertEqual(second_filter.value, req_args_strict_filters[1]["value"]) + + async def test_returns_answer_with_strict_filters_with_and_operator(self): + # Arrange + question: str = "Where can you find" + response_path: str = "RetrunsAnswer_WithStrictFilter_And_Operator.json" + response_json = QnaApplicationTest._get_json_for_file(response_path) + + strict_filters = [ + Metadata(name="species", value="human"), + Metadata(name="type", value="water"), + ] + options = QnAMakerOptions( + top=5, + strict_filters=strict_filters, + strict_filters_join_operator=JoinOperator.AND, + ) + qna = QnAMaker(endpoint=QnaApplicationTest.tests_endpoint) + context = QnaApplicationTest._get_context(question, TestAdapter()) + + # Act + with patch( + "aiohttp.ClientSession.post", + return_value=aiounittest.futurized(response_json), + ) as mock_http_client: + result = await qna.get_answers_raw(context, options) + + serialized_http_req_args = mock_http_client.call_args[1]["data"] + req_args = json.loads(serialized_http_req_args) + + # Assert + self.assertIsNotNone(result) + self.assertEqual(1, len(result.answers)) + self.assertEqual( + JoinOperator.AND, req_args["strictFiltersCompoundOperationType"] + ) + + req_args_strict_filters = req_args["strictFilters"] + + first_filter = strict_filters[0] + self.assertEqual(first_filter.name, req_args_strict_filters[0]["name"]) + self.assertEqual(first_filter.value, req_args_strict_filters[0]["value"]) + + second_filter = strict_filters[1] + self.assertEqual(second_filter.name, req_args_strict_filters[1]["name"]) + self.assertEqual(second_filter.value, req_args_strict_filters[1]["value"]) + + async def test_returns_answer_using_requests_module(self): + question: str = "how do I clean the stove?" + response_path: str = "ReturnsAnswer.json" + response_json = QnaApplicationTest._get_json_for_file(response_path) + + qna = QnAMaker(endpoint=QnaApplicationTest.tests_endpoint, http_client=requests) + context = QnaApplicationTest._get_context(question, TestAdapter()) + + with patch("requests.post", return_value=response_json): + result = await qna.get_answers_raw(context) + answers = result.answers + + self.assertIsNotNone(result) + self.assertEqual(1, len(answers)) + self.assertEqual( + "BaseCamp: You can use a damp rag to clean around the Power Pack", + answers[0].answer, + ) + async def test_returns_answer_using_options(self): # Arrange question: str = "up" response_path: str = "AnswerWithOptions.json" options = QnAMakerOptions( - score_threshold=0.8, top=5, strict_filters=[Metadata("movie", "disney")] + score_threshold=0.8, top=5, strict_filters=[Metadata(**{"movie": "disney"})] ) # Act @@ -233,8 +347,6 @@ async def test_trace_test(self): self._knowledge_base_id, trace_activity.value.knowledge_base_id ) - return result - async def test_returns_answer_with_timeout(self): question: str = "how do I clean the stove?" options = QnAMakerOptions(timeout=999999) @@ -253,6 +365,36 @@ async def test_returns_answer_with_timeout(self): options.timeout, qna._generate_answer_helper.options.timeout ) + async def test_returns_answer_using_requests_module_with_no_timeout(self): + url = f"{QnaApplicationTest._host}/knowledgebases/{QnaApplicationTest._knowledge_base_id}/generateAnswer" + question = GenerateAnswerRequestBody( + question="how do I clean the stove?", + top=1, + score_threshold=0.3, + strict_filters=[], + context=None, + qna_id=None, + is_test=False, + ranker_type="Default", + ) + response_path = "ReturnsAnswer.json" + response_json = QnaApplicationTest._get_json_for_file(response_path) + + http_request_helper = HttpRequestUtils(requests) + + with patch("requests.post", return_value=response_json): + result = await http_request_helper.execute_http_request( + url, question, QnaApplicationTest.tests_endpoint, timeout=None + ) + answers = result["answers"] + + self.assertIsNotNone(result) + self.assertEqual(1, len(answers)) + self.assertEqual( + "BaseCamp: You can use a damp rag to clean around the Power Pack", + answers[0]["answer"], + ) + async def test_telemetry_returns_answer(self): # Arrange question: str = "how do I clean the stove?" @@ -679,7 +821,7 @@ async def test_call_train(self): QnAMaker, "call_train", return_value=None ) as mocked_call_train: qna = QnAMaker(QnaApplicationTest.tests_endpoint) - qna.call_train(feedback_records) + await qna.call_train(feedback_records) mocked_call_train.assert_called_once_with(feedback_records) @@ -757,7 +899,7 @@ async def test_should_answer_with_high_score_provided_context(self): qna = QnAMaker(QnaApplicationTest.tests_endpoint) question: str = "where can I buy?" context = QnARequestContext( - previous_qna_id=5, prvious_user_query="how do I clean the stove?" + previous_qna_id=5, previous_user_query="how do I clean the stove?" ) options = QnAMakerOptions(top=2, qna_id=55, context=context) turn_context = QnaApplicationTest._get_context(question, TestAdapter()) @@ -811,6 +953,46 @@ async def test_should_answer_with_low_score_without_provided_context(self): ) self.assertEqual(True, results[0].score < 1, "Score should be low.") + async def test_low_score_variation(self): + qna = QnAMaker(QnaApplicationTest.tests_endpoint) + options = QnAMakerOptions(top=5, context=None) + + turn_context = QnaApplicationTest._get_context("Q11", TestAdapter()) + response_json = QnaApplicationTest._get_json_for_file( + "QnaMaker_TopNAnswer.json" + ) + + # active learning enabled + with patch( + "aiohttp.ClientSession.post", + return_value=aiounittest.futurized(response_json), + ): + results = await qna.get_answers(turn_context, options) + self.assertIsNotNone(results) + self.assertEqual(4, len(results), "should get four results") + + filtered_results = qna.get_low_score_variation(results) + self.assertIsNotNone(filtered_results) + self.assertEqual(3, len(filtered_results), "should get three results") + + # active learning disabled + turn_context = QnaApplicationTest._get_context("Q11", TestAdapter()) + response_json = QnaApplicationTest._get_json_for_file( + "QnaMaker_TopNAnswer_DisableActiveLearning.json" + ) + + with patch( + "aiohttp.ClientSession.post", + return_value=aiounittest.futurized(response_json), + ): + results = await qna.get_answers(turn_context, options) + self.assertIsNotNone(results) + self.assertEqual(4, len(results), "should get four results") + + filtered_results = qna.get_low_score_variation(results) + self.assertIsNotNone(filtered_results) + self.assertEqual(3, len(filtered_results), "should get three results") + @classmethod async def _get_service_result( cls, diff --git a/libraries/botbuilder-ai/tests/qna/test_qna_dialog.py b/libraries/botbuilder-ai/tests/qna/test_qna_dialog.py new file mode 100644 index 000000000..5da180bb5 --- /dev/null +++ b/libraries/botbuilder-ai/tests/qna/test_qna_dialog.py @@ -0,0 +1,165 @@ +import json +from os import path +from unittest.mock import patch +import aiounittest + +# from botbuilder.ai.qna import QnAMakerEndpoint, QnAMaker, QnAMakerOptions +from botbuilder.ai.qna.dialogs import QnAMakerDialog +from botbuilder.schema import Activity, ActivityTypes +from botbuilder.core import ConversationState, MemoryStorage, TurnContext +from botbuilder.core.adapters import TestAdapter, TestFlow +from botbuilder.dialogs import DialogSet, DialogTurnStatus + + +class QnaMakerDialogTest(aiounittest.AsyncTestCase): + # Note this is NOT a real QnA Maker application ID nor a real QnA Maker subscription-key + # theses are GUIDs edited to look right to the parsing and validation code. + + _knowledge_base_id: str = "f028d9k3-7g9z-11d3-d300-2b8x98227q8w" + _endpoint_key: str = "1k997n7w-207z-36p3-j2u1-09tas20ci6011" + _host: str = "https://dummyqnahost.azurewebsites.net/qnamaker" + + _tell_me_about_birds: str = "Tell me about birds" + _choose_bird: str = "Choose one of the following birds to get more info" + _bald_eagle: str = "Bald Eagle" + _esper: str = "Esper" + + DEFAULT_ACTIVE_LEARNING_TITLE: str = "Did you mean:" + DEFAULT_NO_MATCH_TEXT: str = "None of the above." + DEFAULT_CARD_NO_MATCH_RESPONSE: str = "Thanks for the feedback." + + async def test_multiturn_dialog(self): + # Set Up QnAMakerDialog + convo_state = ConversationState(MemoryStorage()) + dialog_state = convo_state.create_property("dialogState") + dialogs = DialogSet(dialog_state) + + qna_dialog = QnAMakerDialog( + self._knowledge_base_id, self._endpoint_key, self._host + ) + dialogs.add(qna_dialog) + + # Callback that runs the dialog + async def execute_qna_dialog(turn_context: TurnContext) -> None: + if turn_context.activity.type != ActivityTypes.message: + raise TypeError( + "Failed to execute QnA dialog. Should have received a message activity." + ) + + response_json = self._get_json_res(turn_context.activity.text) + dialog_context = await dialogs.create_context(turn_context) + with patch( + "aiohttp.ClientSession.post", + return_value=aiounittest.futurized(response_json), + ): + results = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + await dialog_context.begin_dialog("QnAMakerDialog") + + await convo_state.save_changes(turn_context) + + # Send and receive messages from QnA dialog + test_adapter = TestAdapter(execute_qna_dialog) + test_flow = TestFlow(None, test_adapter) + tf2 = await test_flow.send(self._tell_me_about_birds) + dialog_reply: Activity = tf2.adapter.activity_buffer[0] + self._assert_has_valid_hero_card_buttons(dialog_reply, button_count=2) + tf3 = await tf2.assert_reply(self._choose_bird) + tf4 = await tf3.send(self._bald_eagle) + await tf4.assert_reply("Apparently these guys aren't actually bald!") + + async def test_active_learning(self): + # Set Up QnAMakerDialog + convo_state = ConversationState(MemoryStorage()) + dialog_state = convo_state.create_property("dialogState") + dialogs = DialogSet(dialog_state) + + qna_dialog = QnAMakerDialog( + self._knowledge_base_id, self._endpoint_key, self._host + ) + dialogs.add(qna_dialog) + + # Callback that runs the dialog + async def execute_qna_dialog(turn_context: TurnContext) -> None: + if turn_context.activity.type != ActivityTypes.message: + raise TypeError( + "Failed to execute QnA dialog. Should have received a message activity." + ) + + response_json = self._get_json_res(turn_context.activity.text) + dialog_context = await dialogs.create_context(turn_context) + with patch( + "aiohttp.ClientSession.post", + return_value=aiounittest.futurized(response_json), + ): + results = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + await dialog_context.begin_dialog("QnAMakerDialog") + + await convo_state.save_changes(turn_context) + + # Send and receive messages from QnA dialog + test_adapter = TestAdapter(execute_qna_dialog) + test_flow = TestFlow(None, test_adapter) + tf2 = await test_flow.send(self._esper) + dialog_reply: Activity = tf2.adapter.activity_buffer[0] + self._assert_has_valid_hero_card_buttons(dialog_reply, button_count=3) + tf3 = await tf2.assert_reply(self.DEFAULT_ACTIVE_LEARNING_TITLE) + tf4 = await tf3.send(self.DEFAULT_NO_MATCH_TEXT) + await tf4.assert_reply(self.DEFAULT_CARD_NO_MATCH_RESPONSE) + + print(tf2) + + def _assert_has_valid_hero_card_buttons( + self, activity: Activity, button_count: int + ): + self.assertIsInstance(activity, Activity) + attachments = activity.attachments + self.assertTrue(attachments) + self.assertEqual(len(attachments), 1) + buttons = attachments[0].content.buttons + button_count_err = ( + f"Should have only received {button_count} buttons in multi-turn prompt" + ) + + if activity.text == self._choose_bird: + self.assertEqual(len(buttons), button_count, button_count_err) + self.assertEqual(buttons[0].value, self._bald_eagle) + self.assertEqual(buttons[1].value, "Hummingbird") + + if activity.text == self.DEFAULT_ACTIVE_LEARNING_TITLE: + self.assertEqual(len(buttons), button_count, button_count_err) + self.assertEqual(buttons[0].value, "Esper seeks") + self.assertEqual(buttons[1].value, "Esper sups") + self.assertEqual(buttons[2].value, self.DEFAULT_NO_MATCH_TEXT) + + def _get_json_res(self, text: str) -> object: + if text == self._tell_me_about_birds: + return QnaMakerDialogTest._get_json_for_file( + "QnAMakerDialog_MultiTurn_Answer1.json" + ) + + if text == self._bald_eagle: + return QnaMakerDialogTest._get_json_for_file( + "QnAMakerDialog_MultiTurn_Answer2.json" + ) + + if text == self._esper: + return QnaMakerDialogTest._get_json_for_file( + "QnAMakerDialog_ActiveLearning.json" + ) + + return None + + @staticmethod + def _get_json_for_file(response_file: str) -> object: + curr_dir = path.dirname(path.abspath(__file__)) + response_path = path.join(curr_dir, "test_data", response_file) + + with open(response_path, "r", encoding="utf-8-sig") as file: + response_str = file.read() + response_json = json.loads(response_str) + + return response_json diff --git a/libraries/botbuilder-ai/tests/requirements.txt b/libraries/botbuilder-ai/tests/requirements.txt new file mode 100644 index 000000000..676c72001 --- /dev/null +++ b/libraries/botbuilder-ai/tests/requirements.txt @@ -0,0 +1 @@ +aioresponses~=0.7.2 \ No newline at end of file diff --git a/libraries/botbuilder-applicationinsights/README.rst b/libraries/botbuilder-applicationinsights/README.rst index 43f6046da..6e5c9c0df 100644 --- a/libraries/botbuilder-applicationinsights/README.rst +++ b/libraries/botbuilder-applicationinsights/README.rst @@ -3,8 +3,8 @@ BotBuilder-ApplicationInsights SDK for Python ============================================= -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch .. image:: https://badge.fury.io/py/botbuilder-applicationinsights.svg diff --git a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/about.py b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/about.py index 4c5006f5f..b36e7c9b3 100644 --- a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/about.py +++ b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/about.py @@ -6,7 +6,7 @@ __title__ = "botbuilder-applicationinsights" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/application_insights_telemetry_client.py b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/application_insights_telemetry_client.py index ae660eb7b..39b1eac3a 100644 --- a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/application_insights_telemetry_client.py +++ b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/application_insights_telemetry_client.py @@ -38,13 +38,18 @@ def __init__( instrumentation_key: str, telemetry_client: TelemetryClient = None, telemetry_processor: Callable[[object, object], bool] = None, + client_queue_size: int = None, ): self._instrumentation_key = instrumentation_key + self._client = ( telemetry_client if telemetry_client is not None else TelemetryClient(self._instrumentation_key) ) + if client_queue_size: + self._client.channel.queue.max_queue_length = client_queue_size + # Telemetry Processor processor = ( telemetry_processor @@ -63,13 +68,18 @@ def track_pageview( ) -> None: """ Send information about the page viewed in the application (a web page for instance). + :param name: the name of the page that was viewed. + :type name: str :param url: the URL of the page that was viewed. + :type url: str :param duration: the duration of the page view in milliseconds. (defaults to: 0) - :param properties: the set of custom properties the client wants attached to this data item. - (defaults to: None) - :param measurements: the set of custom measurements the client wants to attach to this data item. - (defaults to: None) + :duration: int + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) + :type properties: :class:`typing.Dict[str, object]` + :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: + None) + :type measurements: :class:`typing.Dict[str, object]` """ self._client.track_pageview(name, url, duration, properties, measurements) @@ -83,13 +93,15 @@ def track_exception( ) -> None: """ Send information about a single exception that occurred in the application. + :param exception_type: the type of the exception that was thrown. :param value: the exception that the client wants to send. :param trace: the traceback information as returned by :func:`sys.exc_info`. - :param properties: the set of custom properties the client wants attached to this data item. - (defaults to: None) - :param measurements: the set of custom measurements the client wants to attach to this data item. - (defaults to: None) + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) + :type properties: :class:`typing.Dict[str, object]` + :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: + None) + :type measurements: :class:`typing.Dict[str, object]` """ self._client.track_exception( exception_type, value, trace, properties, measurements @@ -103,11 +115,14 @@ def track_event( ) -> None: """ Send information about a single event that has occurred in the context of the application. + :param name: the data to associate to this event. - :param properties: the set of custom properties the client wants attached to this data item. - (defaults to: None) - :param measurements: the set of custom measurements the client wants to attach to this data item. - (defaults to: None) + :type name: str + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) + :type properties: :class:`typing.Dict[str, object]` + :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: + None) + :type measurements: :class:`typing.Dict[str, object]` """ self._client.track_event(name, properties=properties, measurements=measurements) @@ -124,18 +139,25 @@ def track_metric( ) -> NotImplemented: """ Send information about a single metric data point that was captured for the application. + :param name: The name of the metric that was captured. + :type name: str :param value: The value of the metric that was captured. + :type value: float :param tel_type: The type of the metric. (defaults to: TelemetryDataPointType.aggregation`) :param count: the number of metrics that were aggregated into this data point. (defaults to: None) - :param min_val: the minimum of all metrics collected that were aggregated into this data point. - (defaults to: None) - :param max_val: the maximum of all metrics collected that were aggregated into this data point. - (defaults to: None) + :type count: int + :param min_val: the minimum of all metrics collected that were aggregated into this data point. (defaults to: + None) + :type min_val: float + :param max_val: the maximum of all metrics collected that were aggregated into this data point. (defaults to: + None) + :type max_val: float :param std_dev: the standard deviation of all metrics collected that were aggregated into this data point. (defaults to: None) - :param properties: the set of custom properties the client wants attached to this data item. - (defaults to: None) + :type std_dev: float + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) + :type properties: :class:`typing.Dict[str, object]` """ self._client.track_metric( name, value, tel_type, count, min_val, max_val, std_dev, properties @@ -146,8 +168,11 @@ def track_trace( ): """ Sends a single trace statement. + :param name: the trace statement. + :type name: str :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) + :type properties: :class:`typing.Dict[str, object]` :param severity: the severity level of this trace, one of DEBUG, INFO, WARNING, ERROR, CRITICAL """ self._client.track_trace(name, properties, severity) @@ -167,19 +192,29 @@ def track_request( ): """ Sends a single request that was captured for the application. + :param name: The name for this request. All requests with the same name will be grouped together. + :type name: str :param url: The actual URL for this request (to show in individual request instances). + :type url: str :param success: True if the request ended in success, False otherwise. + :type success: bool :param start_time: the start time of the request. The value should look the same as the one returned by - :func:`datetime.isoformat()` (defaults to: None) + :func:`datetime.isoformat`. (defaults to: None) + :type start_time: str :param duration: the number of milliseconds that this request lasted. (defaults to: None) + :type duration: int :param response_code: the response code that this request returned. (defaults to: None) + :type response_code: str :param http_method: the HTTP method that triggered this request. (defaults to: None) - :param properties: the set of custom properties the client wants attached to this data item. - (defaults to: None) - :param measurements: the set of custom measurements the client wants to attach to this data item. - (defaults to: None) + :type http_method: str + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) + :type properties: :class:`typing.Dict[str, object]` + :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: + None) + :type measurements: :class:`typing.Dict[str, object]` :param request_id: the id for this request. If None, a new uuid will be generated. (defaults to: None) + :type request_id: str """ self._client.track_request( name, @@ -209,25 +244,35 @@ def track_dependency( ): """ Sends a single dependency telemetry that was captured for the application. + :param name: the name of the command initiated with this dependency call. Low cardinality value. Examples are stored procedure name and URL path template. - :param data: the command initiated by this dependency call. - Examples are SQL statement and HTTP URL with all query parameters. + :type name: str + :param data: the command initiated by this dependency call. Examples are SQL statement and HTTP URL with all + query parameters. + :type data: str :param type_name: the dependency type name. Low cardinality value for logical grouping of dependencies and interpretation of other fields like commandName and resultCode. Examples are SQL, Azure table, and HTTP. (default to: None) - :param target: the target site of a dependency call. Examples are server name, host address. - (default to: None) - :param duration: the number of milliseconds that this dependency call lasted. - (defaults to: None) - :param success: true if the dependency call ended in success, false otherwise. - (defaults to: None) + :type type_name: str + :param target: the target site of a dependency call. Examples are server name, host address. (default to: None) + :type target: str + :param duration: the number of milliseconds that this dependency call lasted. (defaults to: None) + :type duration: int + :param success: true if the dependency call ended in success, false otherwise. (defaults to: None) + :type success: bool :param result_code: the result code of a dependency call. Examples are SQL error code and HTTP status code. (defaults to: None) - :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) + :type result_code: str + :param properties: the set of custom properties the client wants attached to this data item. + (defaults to: None) + :type properties: :class:`typing.Dict[str, object]` :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None) - :param id: the id for this dependency call. If None, a new uuid will be generated. (defaults to: None) + :type measurements: :class:`typing.Dict[str, object]` + :param dependency_id: the id for this dependency call. If None, a new uuid will be generated. + (defaults to: None) + :type dependency_id: str """ self._client.track_dependency( name, diff --git a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/bot_telemetry_processor.py b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/bot_telemetry_processor.py index fa15a3225..bc6c2918a 100644 --- a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/bot_telemetry_processor.py +++ b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/bot_telemetry_processor.py @@ -12,10 +12,14 @@ class BotTelemetryProcessor(TelemetryProcessor): """Application Insights Telemetry Processor for Bot""" def __init__(self, processors: List[TelemetryProcessor] = None): - self._processors: List[TelemetryProcessor] = [ - DjangoTelemetryProcessor(), - FlaskTelemetryProcessor(), - ] if processors is None else processors + self._processors: List[TelemetryProcessor] = ( + [ + DjangoTelemetryProcessor(), + FlaskTelemetryProcessor(), + ] + if processors is None + else processors + ) def can_process(self) -> bool: for processor in self._processors: diff --git a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/django/bot_telemetry_middleware.py b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/django/bot_telemetry_middleware.py index 10b4b9b20..4508dcef1 100644 --- a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/django/bot_telemetry_middleware.py +++ b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/django/bot_telemetry_middleware.py @@ -10,10 +10,10 @@ def retrieve_bot_body(): - """ retrieve_bot_body + """ Retrieve the POST body text from temporary cache. - The POST body corresponds with the thread id and should resides in - cache just for lifetime of request. + + The POST body corresponds to the thread ID and must reside in the cache just for the lifetime of the request. """ result = _REQUEST_BODIES.get(current_thread().ident, None) @@ -22,15 +22,17 @@ def retrieve_bot_body(): class BotTelemetryMiddleware: """ - Save off the POST body to later populate bot-specific properties to - add to Application Insights. + Save off the POST body to later populate bot-specific properties to add to Application Insights. Example activating MIDDLEWARE in Django settings: - MIDDLEWARE = [ - # Ideally add somewhere near top - 'botbuilder.applicationinsights.django.BotTelemetryMiddleware', - ... - ] + + .. code-block:: python + + MIDDLEWARE = [ + # Ideally add somewhere near top + 'botbuilder.applicationinsights.django.BotTelemetryMiddleware', + ... + ] """ def __init__(self, get_response): diff --git a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/django/logging.py b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/django/logging.py index dc36a362b..78e651aa7 100644 --- a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/django/logging.py +++ b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/django/logging.py @@ -8,7 +8,7 @@ class LoggingHandler(logging.LoggingHandler): """This class is a LoggingHandler that uses the same settings as the Django middleware to configure the telemetry client. This can be referenced from LOGGING in your Django settings.py file. As an - example, this code would send all Django log messages--WARNING and up--to Application Insights: + example, this code would send all Django log messages, WARNING and up, to Application Insights: .. code:: python diff --git a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/flask/flask_telemetry_middleware.py b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/flask/flask_telemetry_middleware.py index b125c8997..5cc2676f2 100644 --- a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/flask/flask_telemetry_middleware.py +++ b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/flask/flask_telemetry_middleware.py @@ -10,7 +10,7 @@ def retrieve_flask_body(): - """ retrieve_flask_body + """retrieve_flask_body Retrieve the POST body text from temporary cache. The POST body corresponds with the thread id and should resides in cache just for lifetime of request. @@ -38,6 +38,8 @@ def __call__(self, environ, start_response): def process_request(self, environ) -> bool: """Process the incoming Flask request.""" + body_unicode = None + # Bot Service doesn't handle anything over 256k length = int(environ.get("CONTENT_LENGTH", "0")) if length > 256 * 1024: diff --git a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/processor/telemetry_processor.py b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/processor/telemetry_processor.py index 7a15acb16..0802f3cdf 100644 --- a/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/processor/telemetry_processor.py +++ b/libraries/botbuilder-applicationinsights/botbuilder/applicationinsights/processor/telemetry_processor.py @@ -1,7 +1,9 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +import base64 import json from abc import ABC, abstractmethod +from hashlib import sha256 class TelemetryProcessor(ABC): @@ -11,8 +13,9 @@ class TelemetryProcessor(ABC): def activity_json(self) -> json: """Retrieve the request body as json (Activity).""" body_text = self.get_request_body() - body = json.loads(body_text) if body_text is not None else None - return body + if body_text: + return body_text if isinstance(body_text, dict) else json.loads(body_text) + return None @abstractmethod def can_process(self) -> bool: @@ -28,7 +31,7 @@ def get_request_body(self) -> str: # pylint: disable=inconsistent-return-statem raise NotImplementedError() def __call__(self, data, context) -> bool: - """ Traditional Web user and session ID's don't apply for Bots. This processor + """Traditional Web user and session ID's don't apply for Bots. This processor replaces the identifiers to be consistent with Bot Framework's notion of user and session id's. @@ -67,15 +70,34 @@ def __call__(self, data, context) -> bool: conversation = ( post_data["conversation"] if "conversation" in post_data else None ) - conversation_id = conversation["id"] if "id" in conversation else None + + session_id = "" + if "id" in conversation: + conversation_id = conversation["id"] + session_id = base64.b64encode( + sha256(conversation_id.encode("utf-8")).digest() + ).decode() + + # Set the user id on the Application Insights telemetry item. context.user.id = channel_id + user_id - context.session.id = conversation_id - # Additional bot-specific properties + # Set the session id on the Application Insights telemetry item. + # Hashed ID is used due to max session ID length for App Insights session Id + context.session.id = session_id + + # Set the activity id: + # https://github.com/Microsoft/botframework-obi/blob/master/botframework-activity/botframework-activity.md#id if "id" in post_data: data.properties["activityId"] = post_data["id"] + + # Set the channel id: + # https://github.com/Microsoft/botframework-obi/blob/master/botframework-activity/botframework-activity.md#channel-id if "channelId" in post_data: data.properties["channelId"] = post_data["channelId"] + + # Set the activity type: + # https://github.com/Microsoft/botframework-obi/blob/master/botframework-activity/botframework-activity.md#type if "type" in post_data: data.properties["activityType"] = post_data["type"] + return True diff --git a/libraries/botbuilder-applicationinsights/django_tests/.gitignore b/libraries/botbuilder-applicationinsights/django_tests/.gitignore deleted file mode 100644 index e84001e2c..000000000 --- a/libraries/botbuilder-applicationinsights/django_tests/.gitignore +++ /dev/null @@ -1 +0,0 @@ -aitest diff --git a/libraries/botbuilder-applicationinsights/django_tests/README.md b/libraries/botbuilder-applicationinsights/django_tests/README.md deleted file mode 100644 index bbc0b9db3..000000000 --- a/libraries/botbuilder-applicationinsights/django_tests/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# DJANGO-specific tests -Django generates *code* to create projects (`django-admin startproject`) and apps. For testing, we test the generated code. The tests are bare-bones to be compatible across different versions of django. - -- This project contains a script to execute tests against currently supported version(s) of python and django. -- Assume latest version of Application Insights. -- Relies on virtualenv to run all tests. -- Uses django commands to generate new project and execute django tests. -- To run, first `cd django_tests` and then `bash .\all_tests.sh` (ie, in Powershell) to run all permutations. - -File | | Description ---- | --- -all_tests.sh | Runs our current test matrix of python/django versions. Current matrix is python (3.7) and django (2.2). -README.md | This file. -run_test.sh | Runs specific python/django version to create project, copy replacement files and runs tests. -template.html | Template file -tests.py | Django tests. -urls.py | url paths called by tests -views.py | paths that are called - - - - - diff --git a/libraries/botbuilder-applicationinsights/django_tests/all_tests.sh b/libraries/botbuilder-applicationinsights/django_tests/all_tests.sh deleted file mode 100644 index 562785cf2..000000000 --- a/libraries/botbuilder-applicationinsights/django_tests/all_tests.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -if [ -z $PYTHON ]; then - PYTHON=$(which python) -fi - -cd $(dirname $0) -BASEDIR=$(pwd) - -# Django/python compatibility matrix... -if $PYTHON -c "import sys; sys.exit(1 if (sys.version_info.major == 3 and sys.version_info.minor == 6) else 0)"; then - echo "[Error] Environment should be configured with Python 3.7!" 1>&2 - exit 2 -fi -# Add more versions here (space delimited). -DJANGO_VERSIONS='2.2' - -# For each Django version... -for v in $DJANGO_VERSIONS -do - echo "" - echo "***" - echo "*** Running tests for Django $v" - echo "***" - echo "" - - # Create new directory - TMPDIR=$(mktemp -d) - function cleanup - { - rm -rf $TMPDIR - exit $1 - } - - trap cleanup EXIT SIGINT - - # Create virtual environment - $PYTHON -m venv $TMPDIR/env - - # Install Django version + application insights - . $TMPDIR/env/bin/activate - pip install Django==$v || exit $? - cd $BASEDIR/.. - pip install . || exit $? - - # Run tests - cd $BASEDIR - bash ./run_test.sh || exit $? - - # Deactivate - # (Windows may complain since doesn't add deactivate to path properly) - deactivate - - # Remove venv - rm -rf $TMPDIR -done \ No newline at end of file diff --git a/libraries/botbuilder-applicationinsights/django_tests/run_test.sh b/libraries/botbuilder-applicationinsights/django_tests/run_test.sh deleted file mode 100644 index 3144a2684..000000000 --- a/libraries/botbuilder-applicationinsights/django_tests/run_test.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash - -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -# It is expected at this point that django and applicationinsights are both installed into a -# virtualenv. -django_version=$(python -c "import django ; print('.'.join(map(str, django.VERSION[0:2])))") -test $? -eq 0 || exit 1 - -# Create a new temporary work directory -TMPDIR=$(mktemp -d) -SRCDIR=$(pwd) -function cleanup -{ - cd $SRCDIR - rm -rf $TMPDIR - exit $1 -} -trap cleanup EXIT SIGINT -cd $TMPDIR - -# Set up Django project -django-admin startproject aitest -cd aitest -cp $SRCDIR/views.py aitest/views.py -cp $SRCDIR/tests.py aitest/tests.py -cp $SRCDIR/urls.py aitest/urls.py -cp $SRCDIR/template.html aitest/template.html - -./manage.py test -exit $? \ No newline at end of file diff --git a/libraries/botbuilder-applicationinsights/django_tests/template.html b/libraries/botbuilder-applicationinsights/django_tests/template.html deleted file mode 100644 index 0ce23e725..000000000 --- a/libraries/botbuilder-applicationinsights/django_tests/template.html +++ /dev/null @@ -1 +0,0 @@ -Test django template: {{ context }} \ No newline at end of file diff --git a/libraries/botbuilder-applicationinsights/django_tests/tests.py b/libraries/botbuilder-applicationinsights/django_tests/tests.py deleted file mode 100644 index 180aa72b2..000000000 --- a/libraries/botbuilder-applicationinsights/django_tests/tests.py +++ /dev/null @@ -1,586 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import logging -import os - -import django -from applicationinsights.channel import ( - AsynchronousSender, - NullSender, - SenderBase, - SynchronousQueue, - TelemetryChannel, -) -from applicationinsights.channel.SenderBase import ( - DEFAULT_ENDPOINT_URL as DEFAULT_ENDPOINT, -) -from botbuilder.applicationinsights.django import common -from django.test import TestCase, modify_settings, override_settings -from rest_framework.test import RequestsClient - - -# Note: MIDDLEWARE_CLASSES for django.VERSION <= (1, 10) -MIDDLEWARE_NAME = "MIDDLEWARE" -TEST_IKEY = "12345678-1234-5678-9012-123456789abc" -TEST_ENDPOINT = "https://test.endpoint/v2/track" -PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) - - -class AITestCase(TestCase): - def plug_sender(self): - # Reset saved objects - common.saved_clients = {} - common.saved_channels = {} - - # Create a client and mock out the sender - client = common.create_client() - sender = MockSender() - client._channel = TelemetryChannel(None, SynchronousQueue(sender)) - # client.add_telemetry_processor(bot_telemetry_processor) - self.events = sender.events - self.channel = client.channel - - def get_events(self, count): - self.channel.flush() - self.assertEqual( - len(self.events), - count, - "Expected %d event(s) in queue (%d actual)" % (count, len(self.events)), - ) - if count == 1: - return self.events[0] - return self.events - - -@modify_settings( - **{ - MIDDLEWARE_NAME: { - "append": "botbuilder.applicationinsights.django.ApplicationInsightsMiddleware", - "prepend": "botbuilder.applicationinsights.django.BotTelemetryMiddleware", - } - } -) -@override_settings( - APPLICATION_INSIGHTS={"ikey": TEST_IKEY}, - # Templates for 1.7 - TEMPLATE_DIRS=(PROJECT_ROOT,), - TEMPLATE_LOADERS=("django.template.loaders.filesystem.Loader",), - # Templates for 1.8 and up - TEMPLATES=[ - { - "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [PROJECT_ROOT], - } - ], -) -class MiddlewareTests(AITestCase): - def setUp(self): - self.plug_sender() - - def test_basic_request(self): - """Tests that hitting a simple view generates a telemetry item with the correct properties""" - response = self.invoke_post("") - assert response.status_code == 200 - - event = self.get_events(1) - tags = event["tags"] - data = event["data"]["baseData"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual(event["iKey"], TEST_IKEY) - self.assertEqual(tags["ai.operation.name"], "POST /", "Operation name") - self.assertEqual(data["name"], "POST /", "Request name") - self.assertEqual(data["responseCode"], 200, "Status code") - self.assertEqual(data["success"], True, "Success value") - self.assertEqual(data["url"], "http://testserver/", "Request url") - self.assertEqual(tags["ai.user.id"], "SLACKFROMID") - self.assertEqual(tags["ai.session.id"], "CONVERSATIONID") - self.assertEqual(data["properties"]["channelId"], "SLACK", "channelId=SLACK") - self.assertEqual( - data["properties"]["activityId"], "bf3cc9a2f5de...", "activityID is set" - ) - self.assertEqual( - data["properties"]["activityType"], "message", "activityType == message" - ) - - def test_bot_event(self): - """Tests that traces logged from inside of a view are submitted and parented to the request telemetry item""" - response = self.invoke_post("botlog_event") - assert response.status_code == 200 - - logev, reqev = self.get_events(2) - - # Check request event (minimal, since we validate this elsewhere) - tags = reqev["tags"] - data = reqev["data"]["baseData"] - reqid = tags["ai.operation.id"] - self.assertEqual( - reqev["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual(data["id"], reqid, "Request id") - self.assertEqual(data["name"], "POST /botlog_event", "Operation name") - self.assertEqual(data["url"], "http://testserver/botlog_event", "Request url") - - self.assertTrue(reqid, "Request id not empty") - - # Check log event - tags = logev["tags"] - data = logev["data"]["baseData"] - self.assertEqual( - logev["name"], "Microsoft.ApplicationInsights.Event", "Event type" - ) - self.assertEqual(logev["iKey"], TEST_IKEY) - self.assertEqual(data["name"], "botevent", "validate event name") - self.assertEqual(data["properties"]["foo"], "bar", "foo=bar") - self.assertEqual(data["properties"]["moo"], "cow", "moo=cow") - # Test TelemetryProcessor properties - self.assertEqual(tags["ai.user.id"], "SLACKFROMID") - self.assertEqual(tags["ai.session.id"], "CONVERSATIONID") - self.assertEqual(data["properties"]["channelId"], "SLACK", "channelId=SLACK") - self.assertEqual( - data["properties"]["activityId"], "bf3cc9a2f5de...", "activityID is set" - ) - self.assertEqual( - data["properties"]["activityType"], "message", "activityType == message" - ) - - def test_logger(self): - """Tests that traces logged from inside of a view are submitted and parented to the request telemetry item""" - response = self.invoke_post("logger") - self.assertEqual(response.status_code, 200) - - logev, reqev = self.get_events(2) - - # Check request event (minimal, since we validate this elsewhere) - tags = reqev["tags"] - data = reqev["data"]["baseData"] - reqid = tags["ai.operation.id"] - self.assertEqual( - reqev["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual(data["id"], reqid, "Request id") - self.assertEqual(data["name"], "POST /logger", "Operation name") - self.assertEqual(data["url"], "http://testserver/logger", "Request url") - self.assertTrue(reqid, "Request id not empty") - self.assertEqual(tags["ai.user.id"], "SLACKFROMID") - self.assertEqual(tags["ai.session.id"], "CONVERSATIONID") - self.assertEqual(data["properties"]["channelId"], "SLACK", "channelId=SLACK") - self.assertEqual( - data["properties"]["activityId"], "bf3cc9a2f5de...", "activityID is set" - ) - self.assertEqual( - data["properties"]["activityType"], "message", "activityType == message" - ) - - # Check log event - tags = logev["tags"] - data = logev["data"]["baseData"] - self.assertEqual( - logev["name"], "Microsoft.ApplicationInsights.Message", "Event type" - ) - self.assertEqual(logev["iKey"], TEST_IKEY) - self.assertEqual(tags["ai.operation.parentId"], reqid, "Parent id") - self.assertEqual(data["message"], "Logger message", "Log message") - self.assertEqual(data["properties"]["property"], "value", "Property=value") - self.assertEqual(tags["ai.user.id"], "SLACKFROMID") - self.assertEqual(tags["ai.session.id"], "CONVERSATIONID") - self.assertEqual(data["properties"]["channelId"], "SLACK", "channelId=SLACK") - self.assertEqual( - data["properties"]["activityId"], "bf3cc9a2f5de...", "activityID is set" - ) - self.assertEqual( - data["properties"]["activityType"], "message", "activityType == message" - ) - - def test_thrower(self): - """Tests that unhandled exceptions generate an exception telemetry item parented to the request telemetry item""" - response = self.invoke_post("thrower") - self.assertEqual(response.status_code, 500) - - errev, reqev = self.get_events(2) - # reqev = self.get_events(1) - - # Check request event - tags = reqev["tags"] - data = reqev["data"]["baseData"] - reqid = tags["ai.operation.id"] - self.assertEqual( - reqev["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual(reqev["iKey"], TEST_IKEY) - self.assertEqual(data["id"], reqid, "Request id") - self.assertEqual(data["responseCode"], 500, "Response code") - self.assertEqual(data["success"], False, "Success value") - self.assertEqual(data["name"], "POST /thrower", "Request name") - self.assertEqual(data["url"], "http://testserver/thrower", "Request url") - self.assertTrue(reqid, "Request id not empty") - self.assertEqual(tags["ai.user.id"], "SLACKFROMID") - self.assertEqual(tags["ai.session.id"], "CONVERSATIONID") - self.assertEqual(data["properties"]["channelId"], "SLACK", "channelId=SLACK") - self.assertEqual( - data["properties"]["activityId"], "bf3cc9a2f5de...", "activityID is set" - ) - self.assertEqual( - data["properties"]["activityType"], "message", "activityType == message" - ) - - # Check exception event - tags = errev["tags"] - data = errev["data"]["baseData"] - self.assertEqual( - errev["name"], "Microsoft.ApplicationInsights.Exception", "Event type" - ) - self.assertEqual(tags["ai.operation.parentId"], reqid, "Exception parent id") - self.assertEqual(len(data["exceptions"]), 1, "Exception count") - exc = data["exceptions"][0] - self.assertEqual(exc["typeName"], "ValueError", "Exception type") - self.assertEqual(exc["hasFullStack"], True, "Has full stack") - self.assertEqual( - exc["parsedStack"][0]["method"], "thrower", "Stack frame method name" - ) - self.assertEqual(tags["ai.user.id"], "SLACKFROMID") - self.assertEqual(tags["ai.session.id"], "CONVERSATIONID") - self.assertEqual(data["properties"]["channelId"], "SLACK", "channelId=SLACK") - self.assertEqual( - data["properties"]["activityId"], "bf3cc9a2f5de...", "activityID is set" - ) - self.assertEqual( - data["properties"]["activityType"], "message", "activityType == message" - ) - - def test_error(self): - """Tests that Http404 exception does not generate an exception event - and the request telemetry item properly logs the failure""" - - response = self.invoke_post("errorer") - self.assertEqual(response.status_code, 404) - - event = self.get_events(1) - tags = event["tags"] - data = event["data"]["baseData"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual(tags["ai.operation.name"], "POST /errorer", "Operation name") - self.assertEqual(data["responseCode"], 404, "Status code") - self.assertEqual(data["success"], False, "Success value") - self.assertEqual(data["url"], "http://testserver/errorer", "Request url") - - def test_template(self): - """Tests that views using templates operate correctly and that template data is logged""" - response = self.invoke_post("templater/ctx") - self.assertEqual(response.status_code, 200) - - event = self.get_events(1) - data = event["data"]["baseData"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual(data["success"], True, "Success value") - self.assertEqual(data["responseCode"], 200, "Status code") - self.assertEqual( - data["properties"]["template_name"], "template.html", "Template name" - ) - - def test_no_view_arguments(self): - """Tests that view id logging is off by default""" - self.plug_sender() - # response = self.client.get('/getid/24') - response = self.invoke_post("getid/24") - self.assertEqual(response.status_code, 200) - - event = self.get_events(1) - data = event["data"]["baseData"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertTrue( - "properties" not in data or "view_arg_0" not in data["properties"] - ) - - def test_no_view(self): - """Tests that requests to URLs not backed by views are still logged""" - # response = self.client.get('/this/view/does/not/exist') - response = self.invoke_post("this/view/does/not/exist") - self.assertEqual(response.status_code, 404) - - event = self.get_events(1) - tags = event["tags"] - data = event["data"]["baseData"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual(data["responseCode"], 404, "Status code") - self.assertEqual(data["success"], False, "Success value") - self.assertEqual(data["name"], "POST /this/view/does/not/exist", "Request name") - self.assertEqual( - data["url"], "http://testserver/this/view/does/not/exist", "Request url" - ) - - def test_401_success(self): - """Tests that a 401 status code is considered successful""" - # response = self.client.get("/returncode/401") - response = self.invoke_post("returncode/405") - self.assertEqual(response.status_code, 405) - - event = self.get_events(1) - tags = event["tags"] - data = event["data"]["baseData"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual( - tags["ai.operation.name"], "POST /returncode/405", "Operation name" - ) - self.assertEqual(data["responseCode"], 405, "Status code") - self.assertEqual(data["success"], False, "Success value") - self.assertEqual(data["url"], "http://testserver/returncode/405", "Request url") - - def invoke_post(self, first_level_directory: str): - client = RequestsClient() - return client.post( - f"http://localhost/{first_level_directory}", - json={ - "type": "message", - "id": "bf3cc9a2f5de...", - "timestamp": "2016-10-19T20:17:52.2891902Z", - "serviceUrl": "https://smba.trafficmanager.net/apis", - "channelId": "SLACK", - "from": {"id": "FROMID", "name": "user's name"}, - "conversation": {"id": "CONVERSATIONID", "name": "conversation's name"}, - "recipient": {"id": "RECIPIENTID", "name": "bot's name"}, - "text": "Haircut on Saturday", - }, - ) - - -@modify_settings( - **{ - MIDDLEWARE_NAME: { - "append": "botbuilder.applicationinsights.django.ApplicationInsightsMiddleware" - } - } -) -class RequestSettingsTests(AITestCase): - # This type needs to plug the sender during the test -- doing it in setUp would have nil effect - # because each method's override_settings wouldn't have happened by then. - - @override_settings(APPLICATION_INSIGHTS={"ikey": TEST_IKEY, "use_view_name": True}) - def test_use_view_name(self): - """Tests that request names are set to view names when use_view_name=True""" - self.plug_sender() - self.client.get("/") - event = self.get_events(1) - self.assertEqual( - event["data"]["baseData"]["name"], "GET aitest.views.home", "Request name" - ) - self.assertEqual( - event["tags"]["ai.operation.name"], - "GET aitest.views.home", - "Operation name", - ) - - @override_settings(APPLICATION_INSIGHTS={"ikey": TEST_IKEY, "use_view_name": False}) - def test_use_view_name_off(self): - """Tests that request names are set to URLs when use_view_name=False""" - self.plug_sender() - self.client.get("/") - event = self.get_events(1) - self.assertEqual(event["data"]["baseData"]["name"], "GET /", "Request name") - self.assertEqual(event["tags"]["ai.operation.name"], "GET /", "Operation name") - - @override_settings(APPLICATION_INSIGHTS={"ikey": TEST_IKEY, "use_view_name": True}) - def test_view_name_class(self): - """Tests that classes can be correctly identified when use_view_name=True""" - self.plug_sender() - self.client.get("/class") - event = self.get_events(1) - self.assertEqual( - event["data"]["baseData"]["name"], - "GET aitest.views.classview", - "Request name", - ) - self.assertEqual( - event["tags"]["ai.operation.name"], - "GET aitest.views.classview", - "Operation name", - ) - - @override_settings(APPLICATION_INSIGHTS=None) - def test_appinsights_still_supplied(self): - """Tests that appinsights is still added to requests even if APPLICATION_INSIGHTS is unspecified""" - # This uses request.appinsights -- it will crash if it's not there. - response = self.invoke_post("logger") - self.assertEqual(response.status_code, 200) - - @override_settings( - APPLICATION_INSIGHTS={"ikey": TEST_IKEY, "record_view_arguments": True} - ) - def test_view_id(self): - """Tests that view arguments are logged when record_view_arguments=True""" - self.plug_sender() - response = self.invoke_post("getid/24") - self.assertEqual(response.status_code, 200) - - event = self.get_events(1) - props = event["data"]["baseData"]["properties"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual(props["view_arg_0"], "24", "View argument") - - @override_settings( - APPLICATION_INSIGHTS={"ikey": TEST_IKEY, "log_exceptions": False} - ) - def test_log_exceptions_off(self): - """Tests that exceptions are not logged when log_exceptions=False""" - self.plug_sender() - response = self.invoke_post("thrower") - self.assertEqual(response.status_code, 500) - - event = self.get_events(1) - data = event["data"]["baseData"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Request", "Event type" - ) - self.assertEqual(data["responseCode"], 500, "Response code") - - def invoke_post(self, first_level_directory: str): - client = RequestsClient() - return client.post( - f"http://localhost/{first_level_directory}", - json={ - "type": "message", - "id": "bf3cc9a2f5de...", - "timestamp": "2016-10-19T20:17:52.2891902Z", - "serviceUrl": "https://smba.trafficmanager.net/apis", - "channelId": "SLACK", - "from": {"id": "FROMID", "name": "user's name"}, - "conversation": {"id": "CONVERSATIONID", "name": "conversation's name"}, - "recipient": {"id": "RECIPIENTID", "name": "bot's name"}, - "text": "Haircut on Saturday", - }, - ) - - -class SettingsTests(TestCase): - def setUp(self): - # Just clear out any cached objects - common.saved_clients = {} - common.saved_channels = {} - - def test_no_app_insights(self): - """Tests that events are swallowed when APPLICATION_INSIGHTS is unspecified""" - client = common.create_client() - self.assertTrue(type(client.channel.sender) is NullSender) - - @override_settings(APPLICATION_INSIGHTS={"ikey": TEST_IKEY}) - def test_default_endpoint(self): - """Tests that the default endpoint is used when endpoint is unspecified""" - client = common.create_client() - self.assertEqual(client.channel.sender.service_endpoint_uri, DEFAULT_ENDPOINT) - - @override_settings( - APPLICATION_INSIGHTS={"ikey": TEST_IKEY, "endpoint": TEST_ENDPOINT} - ) - def test_overridden_endpoint(self): - """Tests that the endpoint is used when specified""" - client = common.create_client() - self.assertEqual(client.channel.sender.service_endpoint_uri, TEST_ENDPOINT) - - @override_settings(APPLICATION_INSIGHTS={"ikey": TEST_IKEY, "send_time": 999}) - def test_send_time(self): - """Tests that send_time is propagated to sender""" - client = common.create_client() - self.assertEqual(client.channel.sender.send_time, 999) - - @override_settings(APPLICATION_INSIGHTS={"ikey": TEST_IKEY, "send_interval": 999}) - def test_send_interval(self): - """Tests that send_interval is propagated to sender""" - client = common.create_client() - self.assertEqual(client.channel.sender.send_interval, 999) - - @override_settings(APPLICATION_INSIGHTS={"ikey": TEST_IKEY}) - def test_default_send_time(self): - """Tests that send_time is equal to the default when it is unspecified""" - client = common.create_client() - self.assertEqual( - client.channel.sender.send_time, AsynchronousSender().send_time - ) - - @override_settings(APPLICATION_INSIGHTS={"ikey": TEST_IKEY}) - def test_default_send_interval(self): - """Tests that send_interval is equal to the default when it is unspecified""" - client = common.create_client() - self.assertEqual( - client.channel.sender.send_interval, AsynchronousSender().send_interval - ) - - -@override_settings( - LOGGING={ - "version": 1, - "handlers": { - "appinsights": { - "class": "botbuilder.applicationinsights.django.LoggingHandler", - "level": "INFO", - } - }, - "loggers": {__name__: {"handlers": ["appinsights"], "level": "INFO"}}, - }, - APPLICATION_INSIGHTS={"ikey": TEST_IKEY}, -) -class LoggerTests(AITestCase): - def setUp(self): - self.plug_sender() - - def test_log_error(self): - """Tests an error trace telemetry is properly sent""" - django.setup() - logger = logging.getLogger(__name__) - msg = "An error log message" - logger.error(msg) - - event = self.get_events(1) - data = event["data"]["baseData"] - props = data["properties"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Message", "Event type" - ) - self.assertEqual(event["iKey"], TEST_IKEY) - self.assertEqual(data["message"], msg, "Log message") - self.assertEqual(data["severityLevel"], 3, "Severity level") - self.assertEqual(props["fileName"], "tests.py", "Filename property") - self.assertEqual(props["level"], "ERROR", "Level property") - self.assertEqual(props["module"], "tests", "Module property") - - def test_log_info(self): - """Tests an info trace telemetry is properly sent""" - django.setup() - logger = logging.getLogger(__name__) - msg = "An info message" - logger.info(msg) - - event = self.get_events(1) - data = event["data"]["baseData"] - props = data["properties"] - self.assertEqual( - event["name"], "Microsoft.ApplicationInsights.Message", "Event type" - ) - self.assertEqual(event["iKey"], TEST_IKEY) - self.assertEqual(data["message"], msg, "Log message") - self.assertEqual(data["severityLevel"], 1, "Severity level") - self.assertEqual(props["fileName"], "tests.py", "Filename property") - self.assertEqual(props["level"], "INFO", "Level property") - self.assertEqual(props["module"], "tests", "Module property") - - -class MockSender(SenderBase): - def __init__(self): - SenderBase.__init__(self, DEFAULT_ENDPOINT) - self.events = [] - - def send(self, data): - self.events.extend(a.write() for a in data) diff --git a/libraries/botbuilder-applicationinsights/django_tests/urls.py b/libraries/botbuilder-applicationinsights/django_tests/urls.py deleted file mode 100644 index f544461c1..000000000 --- a/libraries/botbuilder-applicationinsights/django_tests/urls.py +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from django.conf.urls import include, url -from django.contrib import admin - -from . import views - -urlpatterns = [ - url(r"^$", views.home, name="home"), - url(r"^logger$", views.logger, name="logger"), - url(r"^botlog_event$", views.botlog_event, name="botlog_event"), - url(r"^thrower$", views.thrower, name="thrower"), - url(r"^errorer$", views.errorer, name="errorer"), - url(r"^getid/([0-9]+)$", views.getid, name="getid"), - url(r"^returncode/([0-9]+)$", views.returncode, name="returncode"), - url(r"^templater/([^/]*)$", views.templater, name="templater"), - url(r"^class$", views.classview(), name="class"), -] diff --git a/libraries/botbuilder-applicationinsights/django_tests/views.py b/libraries/botbuilder-applicationinsights/django_tests/views.py deleted file mode 100644 index 181ca847c..000000000 --- a/libraries/botbuilder-applicationinsights/django_tests/views.py +++ /dev/null @@ -1,65 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from rest_framework.decorators import api_view -from botbuilder.applicationinsights.django import common -from botbuilder.applicationinsights import ApplicationInsightsTelemetryClient -from django.http import HttpResponse, Http404 -from django.template.response import TemplateResponse - - -@api_view(["POST"]) -def home(request): - # Basic request, no logging. Check BOT properties added. - return HttpResponse("Welcome home") - - -@api_view(["POST"]) -def botlog_event(request): - # Simulates a bot. - telemetry = ApplicationInsightsTelemetryClient( - None, common.create_client() - ) # Used shared client AppInsights uses. - telemetry.track_event("botevent", {"foo": "bar", "moo": "cow"}) - return HttpResponse("We logged a bot event") - - -@api_view(["POST"]) -def logger(request): - # Log with Application Insights - request.appinsights.client.track_trace("Logger message", {"property": "value"}) - return HttpResponse("We logged a message") - - -@api_view(["POST"]) -def thrower(request): - raise ValueError("This is an unexpected exception") - - -@api_view(["POST"]) -def errorer(request): - raise Http404("This is a 404 error") - - -def echoer(request): - return HttpResponse(request.appinsights.request.id) - - -@api_view(["POST"]) -def getid(request, id): - return HttpResponse(str(id)) - - -@api_view(["POST"]) -def returncode(request, id): - return HttpResponse("Status code set to %s" % id, status=int(id)) - - -@api_view(["POST"]) -def templater(request, data): - return TemplateResponse(request, "template.html", {"context": data}) - - -class classview: - def __call__(self, request): - return HttpResponse("You called a class.") diff --git a/libraries/botbuilder-applicationinsights/requirements.txt b/libraries/botbuilder-applicationinsights/requirements.txt index e89251ff0..dcdbb2ecb 100644 --- a/libraries/botbuilder-applicationinsights/requirements.txt +++ b/libraries/botbuilder-applicationinsights/requirements.txt @@ -1,3 +1,3 @@ -msrest==0.6.10 -botbuilder-core>=4.7.1 +msrest== 0.7.* +botbuilder-core==4.17.0 aiounittest==1.3.0 \ No newline at end of file diff --git a/libraries/botbuilder-applicationinsights/setup.py b/libraries/botbuilder-applicationinsights/setup.py index 0e4429065..9573e27f2 100644 --- a/libraries/botbuilder-applicationinsights/setup.py +++ b/libraries/botbuilder-applicationinsights/setup.py @@ -5,16 +5,16 @@ from setuptools import setup REQUIRES = [ - "applicationinsights>=0.11.9", - "botbuilder-schema>=4.7.1", - "botframework-connector>=4.7.1", - "botbuilder-core>=4.7.1", + "applicationinsights==0.11.9", + "botbuilder-schema==4.17.0", + "botframework-connector==4.17.0", + "botbuilder-core==4.17.0", ] TESTS_REQUIRES = [ "aiounittest==1.3.0", - "django==2.2.6", # For samples - "djangorestframework==3.10.3", # For samples - "flask==1.1.1", # For samples + "django==4.2.15", # For samples + "djangorestframework==3.14.0", # For samples + "flask==2.2.5", # For samples ] root = os.path.abspath(os.path.dirname(__file__)) diff --git a/libraries/botbuilder-applicationinsights/tests/test_telemetry_waterfall.py b/libraries/botbuilder-applicationinsights/tests/test_telemetry_waterfall.py index c1ab6e261..a0952907a 100644 --- a/libraries/botbuilder-applicationinsights/tests/test_telemetry_waterfall.py +++ b/libraries/botbuilder-applicationinsights/tests/test_telemetry_waterfall.py @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from unittest.mock import MagicMock +from unittest.mock import create_autospec, MagicMock from typing import Dict import aiounittest from botbuilder.core.adapters import TestAdapter, TestFlow @@ -14,6 +14,8 @@ ) from botbuilder.dialogs import ( Dialog, + DialogInstance, + DialogReason, DialogSet, WaterfallDialog, DialogTurnResult, @@ -63,7 +65,6 @@ async def step2(step) -> DialogTurnResult: # Initialize TestAdapter async def exec_test(turn_context: TurnContext) -> None: - dialog_context = await dialogs.create_context(turn_context) results = await dialog_context.continue_dialog() if results.status == DialogTurnStatus.Empty: @@ -83,11 +84,10 @@ async def exec_test(turn_context: TurnContext) -> None: await tf4.assert_reply("ending WaterfallDialog.") # assert - telemetry_calls = [ ("WaterfallStart", {"DialogId": "test"}), - ("WaterfallStep", {"DialogId": "test", "StepName": "Step1of2"}), - ("WaterfallStep", {"DialogId": "test", "StepName": "Step2of2"}), + ("WaterfallStep", {"DialogId": "test", "StepName": step1.__qualname__}), + ("WaterfallStep", {"DialogId": "test", "StepName": step2.__qualname__}), ] self.assert_telemetry_calls(telemetry, telemetry_calls) @@ -118,7 +118,6 @@ async def step2(step) -> DialogTurnResult: # Initialize TestAdapter async def exec_test(turn_context: TurnContext) -> None: - dialog_context = await dialogs.create_context(turn_context) await dialog_context.continue_dialog() if not turn_context.responded: @@ -138,15 +137,49 @@ async def exec_test(turn_context: TurnContext) -> None: # assert telemetry_calls = [ ("WaterfallStart", {"DialogId": "test"}), - ("WaterfallStep", {"DialogId": "test", "StepName": "Step1of2"}), - ("WaterfallStep", {"DialogId": "test", "StepName": "Step2of2"}), + ("WaterfallStep", {"DialogId": "test", "StepName": step1.__qualname__}), + ("WaterfallStep", {"DialogId": "test", "StepName": step2.__qualname__}), ("WaterfallComplete", {"DialogId": "test"}), ("WaterfallStart", {"DialogId": "test"}), - ("WaterfallStep", {"DialogId": "test", "StepName": "Step1of2"}), + ("WaterfallStep", {"DialogId": "test", "StepName": step1.__qualname__}), ] - print(str(telemetry.track_event.call_args_list)) self.assert_telemetry_calls(telemetry, telemetry_calls) + async def test_cancelling_waterfall_telemetry(self): + # Arrange + dialog_id = "waterfall" + index = 0 + guid = "(guid)" + + async def my_waterfall_step(step) -> DialogTurnResult: + await step.context.send_activity("step1 response") + return Dialog.end_of_turn + + dialog = WaterfallDialog(dialog_id, [my_waterfall_step]) + + telemetry_client = create_autospec(NullTelemetryClient) + dialog.telemetry_client = telemetry_client + + dialog_instance = DialogInstance() + dialog_instance.id = dialog_id + dialog_instance.state = {"instanceId": guid, "stepIndex": index} + + # Act + await dialog.end_dialog( + TurnContext(TestAdapter(), Activity()), + dialog_instance, + DialogReason.CancelCalled, + ) + + # Assert + telemetry_props = telemetry_client.track_event.call_args_list[0][0][1] + + self.assertEqual(3, len(telemetry_props)) + self.assertEqual(dialog_id, telemetry_props["DialogId"]) + self.assertEqual(my_waterfall_step.__qualname__, telemetry_props["StepName"]) + self.assertEqual(guid, telemetry_props["InstanceId"]) + telemetry_client.track_event.assert_called_once() + def assert_telemetry_call( self, telemetry_mock, index: int, event_name: str, props: Dict[str, str] ) -> None: diff --git a/libraries/botbuilder-azure/README.rst b/libraries/botbuilder-azure/README.rst index 04af9dacc..ab937de70 100644 --- a/libraries/botbuilder-azure/README.rst +++ b/libraries/botbuilder-azure/README.rst @@ -3,12 +3,12 @@ BotBuilder-Azure SDK for Python =============================== -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch -.. image:: https://badge.fury.io/py/botbuilder-dialogs.svg - :target: https://badge.fury.io/py/botbuilder-dialogs +.. image:: https://badge.fury.io/py/botbuilder-azure.svg + :target: https://badge.fury.io/py/botbuilder-azure :alt: Latest PyPI package version Azure extensions for Microsoft BotBuilder. diff --git a/libraries/botbuilder-azure/botbuilder/azure/__init__.py b/libraries/botbuilder-azure/botbuilder/azure/__init__.py index 9980f8aa4..e6c70e7fc 100644 --- a/libraries/botbuilder-azure/botbuilder/azure/__init__.py +++ b/libraries/botbuilder-azure/botbuilder/azure/__init__.py @@ -6,18 +6,18 @@ # -------------------------------------------------------------------------- from .about import __version__ -from .cosmosdb_storage import CosmosDbStorage, CosmosDbConfig, CosmosDbKeyEscape +from .azure_queue_storage import AzureQueueStorage from .cosmosdb_partitioned_storage import ( CosmosDbPartitionedStorage, CosmosDbPartitionedConfig, + CosmosDbKeyEscape, ) from .blob_storage import BlobStorage, BlobStorageSettings __all__ = [ + "AzureQueueStorage", "BlobStorage", "BlobStorageSettings", - "CosmosDbStorage", - "CosmosDbConfig", "CosmosDbKeyEscape", "CosmosDbPartitionedStorage", "CosmosDbPartitionedConfig", diff --git a/libraries/botbuilder-azure/botbuilder/azure/about.py b/libraries/botbuilder-azure/botbuilder/azure/about.py index a2276b583..2b8d35387 100644 --- a/libraries/botbuilder-azure/botbuilder/azure/about.py +++ b/libraries/botbuilder-azure/botbuilder/azure/about.py @@ -5,7 +5,7 @@ __title__ = "botbuilder-azure" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botbuilder-azure/botbuilder/azure/azure_queue_storage.py b/libraries/botbuilder-azure/botbuilder/azure/azure_queue_storage.py new file mode 100644 index 000000000..34fc5ebe7 --- /dev/null +++ b/libraries/botbuilder-azure/botbuilder/azure/azure_queue_storage.py @@ -0,0 +1,67 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from azure.core.exceptions import ResourceExistsError +from azure.storage.queue.aio import QueueClient +from jsonpickle import encode + +from botbuilder.core import QueueStorage +from botbuilder.schema import Activity + + +class AzureQueueStorage(QueueStorage): + def __init__(self, queues_storage_connection_string: str, queue_name: str): + if not queues_storage_connection_string: + raise Exception("queues_storage_connection_string cannot be empty.") + if not queue_name: + raise Exception("queue_name cannot be empty.") + + self.__queue_client = QueueClient.from_connection_string( + queues_storage_connection_string, queue_name + ) + + self.__initialized = False + + async def _initialize(self): + if self.__initialized is False: + # This should only happen once - assuming this is a singleton. + # There is no `create_queue_if_exists` or `exists` method, so we need to catch the ResourceExistsError. + try: + await self.__queue_client.create_queue() + except ResourceExistsError: + pass + self.__initialized = True + return self.__initialized + + async def queue_activity( + self, + activity: Activity, + visibility_timeout: int = None, + time_to_live: int = None, + ) -> str: + """ + Enqueues an Activity for later processing. The visibility timeout specifies how long the message should be + visible to Dequeue and Peek operations. + + :param activity: The activity to be queued for later processing. + :type activity: :class:`botbuilder.schema.Activity` + :param visibility_timeout: Visibility timeout in seconds. Optional with a default value of 0. + Cannot be larger than 7 days. + :type visibility_timeout: int + :param time_to_live: Specifies the time-to-live interval for the message in seconds. + :type time_to_live: int + + :returns: QueueMessage as a JSON string. + :rtype: :class:`azure.storage.queue.QueueMessage` + """ + await self._initialize() + + # Encode the activity as a JSON string. + message = encode(activity) + + receipt = await self.__queue_client.send_message( + message, visibility_timeout=visibility_timeout, time_to_live=time_to_live + ) + + # Encode the QueueMessage receipt as a JSON string. + return encode(receipt) diff --git a/libraries/botbuilder-azure/botbuilder/azure/blob_storage.py b/libraries/botbuilder-azure/botbuilder/azure/blob_storage.py index b69217680..4ed6793e4 100644 --- a/libraries/botbuilder-azure/botbuilder/azure/blob_storage.py +++ b/libraries/botbuilder-azure/botbuilder/azure/blob_storage.py @@ -1,15 +1,39 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import json from typing import Dict, List from jsonpickle import encode from jsonpickle.unpickler import Unpickler -from azure.storage.blob import BlockBlobService, Blob, PublicAccess +from azure.core import MatchConditions +from azure.core.exceptions import ( + HttpResponseError, + ResourceExistsError, + ResourceNotFoundError, +) +from azure.storage.blob.aio import ( + BlobServiceClient, + BlobClient, + StorageStreamDownloader, +) from botbuilder.core import Storage -# TODO: sanitize_blob_name - class BlobStorageSettings: + """The class for Azure Blob configuration for the Azure Bot Framework. + + :param container_name: Name of the Blob container. + :type container_name: str + :param account_name: Name of the Blob Storage account. Required if not using connection_string. + :type account_name: str + :param account_key: Key of the Blob Storage account. Required if not using connection_string. + :type account_key: str + :param connection_string: Connection string of the Blob Storage account. + Required if not using account_name and account_key. + :type connection_string: str + """ + def __init__( self, container_name: str, @@ -23,56 +47,105 @@ def __init__( self.connection_string = connection_string +# New Azure Blob SDK only allows connection strings, but our SDK allows key+name. +# This is here for backwards compatibility. +def convert_account_name_and_key_to_connection_string(settings: BlobStorageSettings): + if not settings.account_name or not settings.account_key: + raise Exception( + "account_name and account_key are both required for BlobStorageSettings if not using a connections string." + ) + return ( + f"DefaultEndpointsProtocol=https;AccountName={settings.account_name};" + f"AccountKey={settings.account_key};EndpointSuffix=core.windows.net" + ) + + class BlobStorage(Storage): + """An Azure Blob based storage provider for a bot. + + This class uses a single Azure Storage Blob Container. + Each entity or StoreItem is serialized into a JSON string and stored in an individual text blob. + Each blob is named after the store item key, which is encoded so that it conforms a valid blob name. + If an entity is an StoreItem, the storage object will set the entity's e_tag + property value to the blob's e_tag upon read. Afterward, an match_condition with the ETag value + will be generated during Write. New entities start with a null e_tag. + + :param settings: Settings used to instantiate the Blob service. + :type settings: :class:`botbuilder.azure.BlobStorageSettings` + """ + def __init__(self, settings: BlobStorageSettings): + if not settings.container_name: + raise Exception("Container name is required.") + if settings.connection_string: - client = BlockBlobService(connection_string=settings.connection_string) - elif settings.account_name and settings.account_key: - client = BlockBlobService( - account_name=settings.account_name, account_key=settings.account_key + blob_service_client = BlobServiceClient.from_connection_string( + settings.connection_string ) else: - raise Exception( - "Connection string should be provided if there are no account name and key" + blob_service_client = BlobServiceClient.from_connection_string( + convert_account_name_and_key_to_connection_string(settings) ) - self.client = client - self.settings = settings + self.__container_client = blob_service_client.get_container_client( + settings.container_name + ) + + self.__initialized = False + + async def _initialize(self): + if self.__initialized is False: + # This should only happen once - assuming this is a singleton. + # ContainerClient.exists() method is available in an unreleased version of the SDK. Until then, we use: + try: + await self.__container_client.create_container() + except ResourceExistsError: + pass + self.__initialized = True + return self.__initialized async def read(self, keys: List[str]) -> Dict[str, object]: + """Retrieve entities from the configured blob container. + + :param keys: An array of entity keys. + :type keys: Dict[str, object] + :return dict: + """ if not keys: raise Exception("Keys are required when reading") - self.client.create_container(self.settings.container_name) - self.client.set_container_acl( - self.settings.container_name, public_access=PublicAccess.Container - ) + await self._initialize() + items = {} for key in keys: - if self.client.exists( - container_name=self.settings.container_name, blob_name=key - ): - items[key] = self._blob_to_store_item( - self.client.get_blob_to_text( - container_name=self.settings.container_name, blob_name=key - ) - ) + blob_client = self.__container_client.get_blob_client(key) + + try: + items[key] = await self._inner_read_blob(blob_client) + except HttpResponseError as err: + if err.status_code == 404: + continue return items async def write(self, changes: Dict[str, object]): + """Stores a new entity in the configured blob container. + + :param changes: The changes to write to storage. + :type changes: Dict[str, object] + :return: + """ if changes is None: raise Exception("Changes are required when writing") if not changes: return - self.client.create_container(self.settings.container_name) - self.client.set_container_acl( - self.settings.container_name, public_access=PublicAccess.Container - ) + await self._initialize() + + for name, item in changes.items(): + blob_reference = self.__container_client.get_blob_client(name) - for (name, item) in changes.items(): e_tag = None if isinstance(item, dict): e_tag = item.get("e_tag", None) @@ -81,39 +154,46 @@ async def write(self, changes: Dict[str, object]): e_tag = None if e_tag == "*" else e_tag if e_tag == "": raise Exception("blob_storage.write(): etag missing") + item_str = self._store_item_to_str(item) - try: - self.client.create_blob_from_text( - container_name=self.settings.container_name, - blob_name=name, - text=item_str, - if_match=e_tag, + + if e_tag: + await blob_reference.upload_blob( + item_str, match_condition=MatchConditions.IfNotModified, etag=e_tag ) - except Exception as error: - raise error + else: + await blob_reference.upload_blob(item_str, overwrite=True) async def delete(self, keys: List[str]): + """Deletes entity blobs from the configured container. + + :param keys: An array of entity keys. + :type keys: Dict[str, object] + """ if keys is None: raise Exception("BlobStorage.delete: keys parameter can't be null") - self.client.create_container(self.settings.container_name) - self.client.set_container_acl( - self.settings.container_name, public_access=PublicAccess.Container - ) + await self._initialize() for key in keys: - if self.client.exists( - container_name=self.settings.container_name, blob_name=key - ): - self.client.delete_blob( - container_name=self.settings.container_name, blob_name=key - ) - - def _blob_to_store_item(self, blob: Blob) -> object: - item = json.loads(blob.content) - item["e_tag"] = blob.properties.etag - result = Unpickler().restore(item) - return result + blob_client = self.__container_client.get_blob_client(key) + try: + await blob_client.delete_blob() + # We can't delete what's already gone. + except ResourceNotFoundError: + pass def _store_item_to_str(self, item: object) -> str: return encode(item) + + async def _inner_read_blob(self, blob_client: BlobClient): + blob = await blob_client.download_blob() + + return await self._blob_to_store_item(blob) + + @staticmethod + async def _blob_to_store_item(blob: StorageStreamDownloader) -> object: + item = json.loads(await blob.content_as_text()) + item["e_tag"] = blob.properties.etag.replace('"', "") + result = Unpickler().restore(item) + return result diff --git a/libraries/botbuilder-azure/botbuilder/azure/cosmosdb_partitioned_storage.py b/libraries/botbuilder-azure/botbuilder/azure/cosmosdb_partitioned_storage.py index 93657bbed..cfe66f8d8 100644 --- a/libraries/botbuilder-azure/botbuilder/azure/cosmosdb_partitioned_storage.py +++ b/libraries/botbuilder-azure/botbuilder/azure/cosmosdb_partitioned_storage.py @@ -1,7 +1,4 @@ -"""CosmosDB Middleware for Python Bot Framework. - -This is middleware to store items in CosmosDB. -Part of the Azure Bot Framework in Python. +"""Implements a CosmosDB based storage provider using partitioning for a bot. """ # Copyright (c) Microsoft Corporation. All rights reserved. @@ -9,14 +6,14 @@ from typing import Dict, List from threading import Lock import json - +from hashlib import sha256 +from azure.core import MatchConditions from azure.cosmos import documents, http_constants from jsonpickle.pickler import Pickler from jsonpickle.unpickler import Unpickler import azure.cosmos.cosmos_client as cosmos_client # pylint: disable=no-name-in-module,import-error -import azure.cosmos.errors as cosmos_errors # pylint: disable=no-name-in-module,import-error +import azure.cosmos.exceptions as cosmos_exceptions from botbuilder.core.storage import Storage -from botbuilder.azure import CosmosDbKeyEscape class CosmosDbPartitionedConfig: @@ -66,8 +63,51 @@ def __init__( self.compatibility_mode = compatibility_mode or kwargs.get("compatibility_mode") +class CosmosDbKeyEscape: + @staticmethod + def sanitize_key( + key: str, key_suffix: str = "", compatibility_mode: bool = True + ) -> str: + """Return the sanitized key. + + Replace characters that are not allowed in keys in Cosmos. + + :param key: The provided key to be escaped. + :param key_suffix: The string to add a the end of all RowKeys. + :param compatibility_mode: True if keys should be truncated in order to support previous CosmosDb + max key length of 255. This behavior can be overridden by setting + cosmosdb_partitioned_config.compatibility_mode to False. + :return str: + """ + # forbidden characters + bad_chars = ["\\", "?", "/", "#", "\t", "\n", "\r", "*"] + # replace those with with '*' and the + # Unicode code point of the character and return the new string + key = "".join(map(lambda x: "*" + str(ord(x)) if x in bad_chars else x, key)) + + if key_suffix is None: + key_suffix = "" + + return CosmosDbKeyEscape.truncate_key(f"{key}{key_suffix}", compatibility_mode) + + @staticmethod + def truncate_key(key: str, compatibility_mode: bool = True) -> str: + max_key_len = 255 + + if not compatibility_mode: + return key + + if len(key) > max_key_len: + aux_hash = sha256(key.encode("utf-8")) + aux_hex = aux_hash.hexdigest() + + key = key[0 : max_key_len - len(aux_hex)] + aux_hex + + return key + + class CosmosDbPartitionedStorage(Storage): - """The class for partitioned CosmosDB middleware for the Azure Bot Framework.""" + """A CosmosDB based storage provider using partitioning for a bot.""" def __init__(self, config: CosmosDbPartitionedConfig): """Create the storage object. @@ -102,7 +142,8 @@ async def read(self, keys: List[str]) -> Dict[str, object]: :return dict: """ if not keys: - raise Exception("Keys are required when reading") + # No keys passed in, no result to return. Back-compat with original CosmosDBStorage. + return {} await self.initialize() @@ -114,8 +155,8 @@ async def read(self, keys: List[str]) -> Dict[str, object]: key, self.config.key_suffix, self.config.compatibility_mode ) - read_item_response = self.client.ReadItem( - self.__item_link(escaped_key), self.__get_partition_key(escaped_key) + read_item_response = self.container.read_item( + escaped_key, self.__get_partition_key(escaped_key) ) document_store_item = read_item_response if document_store_item: @@ -125,13 +166,8 @@ async def read(self, keys: List[str]) -> Dict[str, object]: # When an item is not found a CosmosException is thrown, but we want to # return an empty collection so in this instance we catch and do not rethrow. # Throw for any other exception. - except cosmos_errors.HTTPFailure as err: - if ( - err.status_code - == cosmos_errors.http_constants.StatusCodes.NOT_FOUND - ): - continue - raise err + except cosmos_exceptions.CosmosResourceNotFoundError: + continue except Exception as err: raise err return store_items @@ -149,7 +185,7 @@ async def write(self, changes: Dict[str, object]): await self.initialize() - for (key, change) in changes.items(): + for key, change in changes.items(): e_tag = None if isinstance(change, dict): e_tag = change.get("e_tag", None) @@ -165,20 +201,16 @@ async def write(self, changes: Dict[str, object]): if e_tag == "": raise Exception("cosmosdb_storage.write(): etag missing") - access_condition = { - "accessCondition": {"type": "IfMatch", "condition": e_tag} - } - options = ( - access_condition if e_tag != "*" and e_tag and e_tag != "" else None - ) + access_condition = e_tag != "*" and e_tag and e_tag != "" + try: - self.client.UpsertItem( - database_or_Container_link=self.__container_link, - document=doc, - options=options, + self.container.upsert_item( + body=doc, + etag=e_tag if access_condition else None, + match_condition=( + MatchConditions.IfNotModified if access_condition else None + ), ) - except cosmos_errors.HTTPFailure as err: - raise err except Exception as err: raise err @@ -195,69 +227,66 @@ async def delete(self, keys: List[str]): key, self.config.key_suffix, self.config.compatibility_mode ) try: - self.client.DeleteItem( - document_link=self.__item_link(escaped_key), - options=self.__get_partition_key(escaped_key), + self.container.delete_item( + escaped_key, + self.__get_partition_key(escaped_key), ) - except cosmos_errors.HTTPFailure as err: - if ( - err.status_code - == cosmos_errors.http_constants.StatusCodes.NOT_FOUND - ): - continue - raise err + except cosmos_exceptions.CosmosResourceNotFoundError: + continue except Exception as err: raise err async def initialize(self): if not self.container: if not self.client: + connection_policy = self.config.cosmos_client_options.get( + "connection_policy", documents.ConnectionPolicy() + ) + + # kwargs 'connection_verify' is to handle CosmosClient overwriting the + # ConnectionPolicy.DisableSSLVerification value. self.client = cosmos_client.CosmosClient( self.config.cosmos_db_endpoint, - {"masterKey": self.config.auth_key}, - self.config.cosmos_client_options.get("connection_policy", None), + self.config.auth_key, self.config.cosmos_client_options.get("consistency_level", None), + **{ + "connection_policy": connection_policy, + "connection_verify": not connection_policy.DisableSSLVerification, + }, ) if not self.database: with self.__lock: - try: - if not self.database: - self.database = self.client.CreateDatabase( - {"id": self.config.database_id} - ) - except cosmos_errors.HTTPFailure: - self.database = self.client.ReadDatabase( - "dbs/" + self.config.database_id + if not self.database: + self.database = self.client.create_database_if_not_exists( + self.config.database_id ) self.__get_or_create_container() def __get_or_create_container(self): with self.__lock: - container_def = { - "id": self.config.container_id, - "partitionKey": { - "paths": ["/id"], - "kind": documents.PartitionKind.Hash, - }, + partition_key = { + "paths": ["/id"], + "kind": documents.PartitionKind.Hash, } try: if not self.container: - self.container = self.client.CreateContainer( - "dbs/" + self.database["id"], - container_def, - {"offerThroughput": self.config.container_throughput}, + self.container = self.database.create_container( + self.config.container_id, + partition_key, + offer_throughput=self.config.container_throughput, ) - except cosmos_errors.HTTPFailure as err: + except cosmos_exceptions.CosmosHttpResponseError as err: if err.status_code == http_constants.StatusCodes.CONFLICT: - self.container = self.client.ReadContainer( - "dbs/" + self.database["id"] + "/colls/" + container_def["id"] + self.container = self.database.get_container_client( + self.config.container_id ) - if "partitionKey" not in self.container: + properties = self.container.read() + if "partitionKey" not in properties: self.compatability_mode_partition_key = True else: - paths = self.container["partitionKey"]["paths"] + paths = properties["partitionKey"]["paths"] if "/partitionKey" in paths: self.compatability_mode_partition_key = True elif "/id" not in paths: @@ -270,7 +299,7 @@ def __get_or_create_container(self): raise err def __get_partition_key(self, key: str) -> str: - return None if self.compatability_mode_partition_key else {"partitionKey": key} + return None if self.compatability_mode_partition_key else key @staticmethod def __create_si(result) -> object: @@ -306,28 +335,3 @@ def __create_dict(store_item: object) -> Dict: # loop through attributes and write and return a dict return json_dict - - def __item_link(self, identifier) -> str: - """Return the item link of a item in the container. - - :param identifier: - :return str: - """ - return self.__container_link + "/docs/" + identifier - - @property - def __container_link(self) -> str: - """Return the container link in the database. - - :param: - :return str: - """ - return self.__database_link + "/colls/" + self.config.container_id - - @property - def __database_link(self) -> str: - """Return the database link. - - :return str: - """ - return "dbs/" + self.config.database_id diff --git a/libraries/botbuilder-azure/botbuilder/azure/cosmosdb_storage.py b/libraries/botbuilder-azure/botbuilder/azure/cosmosdb_storage.py deleted file mode 100644 index a5d01eea5..000000000 --- a/libraries/botbuilder-azure/botbuilder/azure/cosmosdb_storage.py +++ /dev/null @@ -1,377 +0,0 @@ -"""CosmosDB Middleware for Python Bot Framework. - -This is middleware to store items in CosmosDB. -Part of the Azure Bot Framework in Python. -""" - -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -from hashlib import sha256 -from typing import Dict, List -from threading import Semaphore -import json -from jsonpickle.pickler import Pickler -from jsonpickle.unpickler import Unpickler -import azure.cosmos.cosmos_client as cosmos_client # pylint: disable=no-name-in-module,import-error -import azure.cosmos.errors as cosmos_errors # pylint: disable=no-name-in-module,import-error -from botbuilder.core.storage import Storage - - -class CosmosDbConfig: - """The class for CosmosDB configuration for the Azure Bot Framework.""" - - def __init__( - self, - endpoint: str = None, - masterkey: str = None, - database: str = None, - container: str = None, - partition_key: str = None, - database_creation_options: dict = None, - container_creation_options: dict = None, - **kwargs, - ): - """Create the Config object. - - :param endpoint: - :param masterkey: - :param database: - :param container: - :param filename: - :return CosmosDbConfig: - """ - self.__config_file = kwargs.get("filename") - if self.__config_file: - kwargs = json.load(open(self.__config_file)) - self.endpoint = endpoint or kwargs.get("endpoint") - self.masterkey = masterkey or kwargs.get("masterkey") - self.database = database or kwargs.get("database", "bot_db") - self.container = container or kwargs.get("container", "bot_container") - self.partition_key = partition_key or kwargs.get("partition_key") - self.database_creation_options = database_creation_options or kwargs.get( - "database_creation_options" - ) - self.container_creation_options = container_creation_options or kwargs.get( - "container_creation_options" - ) - - -class CosmosDbKeyEscape: - @staticmethod - def sanitize_key( - key: str, key_suffix: str = "", compatibility_mode: bool = True - ) -> str: - """Return the sanitized key. - - Replace characters that are not allowed in keys in Cosmos. - - :param key: The provided key to be escaped. - :param key_suffix: The string to add a the end of all RowKeys. - :param compatibility_mode: True if keys should be truncated in order to support previous CosmosDb - max key length of 255. This behavior can be overridden by setting - cosmosdb_partitioned_config.compatibility_mode to False. - :return str: - """ - # forbidden characters - bad_chars = ["\\", "?", "/", "#", "\t", "\n", "\r", "*"] - # replace those with with '*' and the - # Unicode code point of the character and return the new string - key = "".join(map(lambda x: "*" + str(ord(x)) if x in bad_chars else x, key)) - - if key_suffix is None: - key_suffix = "" - - return CosmosDbKeyEscape.truncate_key(f"{key}{key_suffix}", compatibility_mode) - - @staticmethod - def truncate_key(key: str, compatibility_mode: bool = True) -> str: - max_key_len = 255 - - if not compatibility_mode: - return key - - if len(key) > max_key_len: - aux_hash = sha256(key.encode("utf-8")) - aux_hex = aux_hash.hexdigest() - - key = key[0 : max_key_len - len(aux_hex)] + aux_hex - - return key - - -class CosmosDbStorage(Storage): - """The class for CosmosDB middleware for the Azure Bot Framework.""" - - def __init__( - self, config: CosmosDbConfig, client: cosmos_client.CosmosClient = None - ): - """Create the storage object. - - :param config: - """ - super(CosmosDbStorage, self).__init__() - self.config = config - self.client = client or cosmos_client.CosmosClient( - self.config.endpoint, {"masterKey": self.config.masterkey} - ) - # these are set by the functions that check - # the presence of the database and container or creates them - self.database = None - self.container = None - self._database_creation_options = config.database_creation_options - self._container_creation_options = config.container_creation_options - self.__semaphore = Semaphore() - - async def read(self, keys: List[str]) -> Dict[str, object]: - """Read storeitems from storage. - - :param keys: - :return dict: - """ - try: - # check if the database and container exists and if not create - if not self.__container_exists: - self.__create_db_and_container() - if keys: - # create the parameters object - parameters = [ - { - "name": f"@id{i}", - "value": f"{CosmosDbKeyEscape.sanitize_key(key)}", - } - for i, key in enumerate(keys) - ] - # get the names of the params - parameter_sequence = ",".join(param.get("name") for param in parameters) - # create the query - query = { - "query": f"SELECT c.id, c.realId, c.document, c._etag FROM c WHERE c.id in ({parameter_sequence})", - "parameters": parameters, - } - - if self.config.partition_key: - options = {"partitionKey": self.config.partition_key} - else: - options = {"enableCrossPartitionQuery": True} - - # run the query and store the results as a list - results = list( - self.client.QueryItems(self.__container_link, query, options) - ) - # return a dict with a key and an object - return {r.get("realId"): self.__create_si(r) for r in results} - - # No keys passed in, no result to return. - return {} - except TypeError as error: - raise error - - async def write(self, changes: Dict[str, object]): - """Save storeitems to storage. - - :param changes: - :return: - """ - if changes is None: - raise Exception("Changes are required when writing") - if not changes: - return - try: - # check if the database and container exists and if not create - if not self.__container_exists: - self.__create_db_and_container() - # iterate over the changes - for (key, change) in changes.items(): - # store the e_tag - e_tag = None - if isinstance(change, dict): - e_tag = change.get("e_tag", None) - elif hasattr(change, "e_tag"): - e_tag = change.e_tag - # create the new document - doc = { - "id": CosmosDbKeyEscape.sanitize_key(key), - "realId": key, - "document": self.__create_dict(change), - } - if e_tag == "": - raise Exception("cosmosdb_storage.write(): etag missing") - # the e_tag will be * for new docs so do an insert - if e_tag == "*" or not e_tag: - self.client.UpsertItem( - database_or_Container_link=self.__container_link, - document=doc, - options={"disableAutomaticIdGeneration": True}, - ) - # if we have an etag, do opt. concurrency replace - elif e_tag: - access_condition = {"type": "IfMatch", "condition": e_tag} - self.client.ReplaceItem( - document_link=self.__item_link( - CosmosDbKeyEscape.sanitize_key(key) - ), - new_document=doc, - options={"accessCondition": access_condition}, - ) - except Exception as error: - raise error - - async def delete(self, keys: List[str]): - """Remove storeitems from storage. - - :param keys: - :return: - """ - try: - # check if the database and container exists and if not create - if not self.__container_exists: - self.__create_db_and_container() - - options = {} - if self.config.partition_key: - options["partitionKey"] = self.config.partition_key - - # call the function for each key - for key in keys: - self.client.DeleteItem( - document_link=self.__item_link(CosmosDbKeyEscape.sanitize_key(key)), - options=options, - ) - # print(res) - except cosmos_errors.HTTPFailure as http_failure: - # print(h.status_code) - if http_failure.status_code != 404: - raise http_failure - except TypeError as error: - raise error - - def __create_si(self, result) -> object: - """Create an object from a result out of CosmosDB. - - :param result: - :return object: - """ - # get the document item from the result and turn into a dict - doc = result.get("document") - # read the e_tag from Cosmos - if result.get("_etag"): - doc["e_tag"] = result["_etag"] - - result_obj = Unpickler().restore(doc) - - # create and return the object - return result_obj - - def __create_dict(self, store_item: object) -> Dict: - """Return the dict of an object. - - This eliminates non_magic attributes and the e_tag. - - :param store_item: - :return dict: - """ - # read the content - json_dict = Pickler().flatten(store_item) - if "e_tag" in json_dict: - del json_dict["e_tag"] - - # loop through attributes and write and return a dict - return json_dict - - def __item_link(self, identifier) -> str: - """Return the item link of a item in the container. - - :param identifier: - :return str: - """ - return self.__container_link + "/docs/" + identifier - - @property - def __container_link(self) -> str: - """Return the container link in the database. - - :param: - :return str: - """ - return self.__database_link + "/colls/" + self.container - - @property - def __database_link(self) -> str: - """Return the database link. - - :return str: - """ - return "dbs/" + self.database - - @property - def __container_exists(self) -> bool: - """Return whether the database and container have been created. - - :return bool: - """ - return self.database and self.container - - def __create_db_and_container(self): - """Call the get or create methods.""" - with self.__semaphore: - db_id = self.config.database - container_name = self.config.container - self.database = self._get_or_create_database(self.client, db_id) - self.container = self._get_or_create_container(self.client, container_name) - - def _get_or_create_database( # pylint: disable=invalid-name - self, doc_client, id - ) -> str: - """Return the database link. - - Check if the database exists or create the database. - - :param doc_client: - :param id: - :return str: - """ - # query CosmosDB for a database with that name/id - dbs = list( - doc_client.QueryDatabases( - { - "query": "SELECT * FROM r WHERE r.id=@id", - "parameters": [{"name": "@id", "value": id}], - } - ) - ) - # if there are results, return the first (database names are unique) - if dbs: - return dbs[0]["id"] - - # create the database if it didn't exist - res = doc_client.CreateDatabase({"id": id}, self._database_creation_options) - return res["id"] - - def _get_or_create_container(self, doc_client, container) -> str: - """Return the container link. - - Check if the container exists or create the container. - - :param doc_client: - :param container: - :return str: - """ - # query CosmosDB for a container in the database with that name - containers = list( - doc_client.QueryContainers( - self.__database_link, - { - "query": "SELECT * FROM r WHERE r.id=@id", - "parameters": [{"name": "@id", "value": container}], - }, - ) - ) - # if there are results, return the first (container names are unique) - if containers: - return containers[0]["id"] - - # Create a container if it didn't exist - res = doc_client.CreateContainer( - self.__database_link, {"id": container}, self._container_creation_options - ) - return res["id"] diff --git a/libraries/botbuilder-azure/setup.py b/libraries/botbuilder-azure/setup.py index 2245c3bbc..7ff214d2e 100644 --- a/libraries/botbuilder-azure/setup.py +++ b/libraries/botbuilder-azure/setup.py @@ -5,11 +5,12 @@ from setuptools import setup REQUIRES = [ - "azure-cosmos==3.1.2", - "azure-storage-blob==2.1.0", - "botbuilder-schema>=4.7.1", - "botframework-connector>=4.7.1", - "jsonpickle==1.2", + "azure-cosmos==4.7.0", + "azure-storage-blob==12.7.0", + "azure-storage-queue==12.4.0", + "botbuilder-schema==4.17.0", + "botframework-connector==4.17.0", + "jsonpickle>=1.2,<4", ] TEST_REQUIRES = ["aiounittest==1.3.0"] diff --git a/libraries/botbuilder-azure/tests/test_blob_storage.py b/libraries/botbuilder-azure/tests/test_blob_storage.py index 31f54a231..6357d31d6 100644 --- a/libraries/botbuilder-azure/tests/test_blob_storage.py +++ b/libraries/botbuilder-azure/tests/test_blob_storage.py @@ -2,6 +2,8 @@ # Licensed under the MIT License. import pytest +from azure.core.exceptions import ResourceNotFoundError +from azure.storage.blob.aio import BlobServiceClient from botbuilder.core import StoreItem from botbuilder.azure import BlobStorage, BlobStorageSettings from botbuilder.testing import StorageBaseTests @@ -26,12 +28,12 @@ def get_storage(): async def reset(): - storage = get_storage() + storage = BlobServiceClient.from_connection_string( + BLOB_STORAGE_SETTINGS.connection_string + ) try: - await storage.client.delete_container( - container_name=BLOB_STORAGE_SETTINGS.container_name - ) - except Exception: + await storage.delete_container(BLOB_STORAGE_SETTINGS.container_name) + except ResourceNotFoundError: pass @@ -44,7 +46,7 @@ def __init__(self, counter=1, e_tag="*"): class TestBlobStorageConstructor: @pytest.mark.asyncio - async def test_blob_storage_init_should_error_without_cosmos_db_config(self): + async def test_blob_storage_init_should_error_without_blob_config(self): try: BlobStorage(BlobStorageSettings()) # pylint: disable=no-value-for-parameter except Exception as error: diff --git a/libraries/botbuilder-azure/tests/test_cosmos_partitioned_storage.py b/libraries/botbuilder-azure/tests/test_cosmos_partitioned_storage.py index cb6dd0822..d52733fd9 100644 --- a/libraries/botbuilder-azure/tests/test_cosmos_partitioned_storage.py +++ b/libraries/botbuilder-azure/tests/test_cosmos_partitioned_storage.py @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -import azure.cosmos.errors as cosmos_errors +import azure.cosmos.exceptions as cosmos_exceptions from azure.cosmos import documents import pytest from botbuilder.azure import CosmosDbPartitionedStorage, CosmosDbPartitionedConfig @@ -27,8 +27,8 @@ async def reset(): storage = CosmosDbPartitionedStorage(get_settings()) await storage.initialize() try: - storage.client.DeleteDatabase(database_link="dbs/" + get_settings().database_id) - except cosmos_errors.HTTPFailure: + storage.client.delete_database(get_settings().database_id) + except cosmos_exceptions.HttpResponseError: pass @@ -99,9 +99,12 @@ async def test_passes_cosmos_client_options(self): client = CosmosDbPartitionedStorage(settings_with_options) await client.initialize() - assert client.client.connection_policy.DisableSSLVerification is True assert ( - client.client.default_headers["x-ms-consistency-level"] + client.client.client_connection.connection_policy.DisableSSLVerification + is True + ) + assert ( + client.client.client_connection.default_headers["x-ms-consistency-level"] == documents.ConsistencyLevel.Eventual ) diff --git a/libraries/botbuilder-azure/tests/test_cosmos_storage.py b/libraries/botbuilder-azure/tests/test_cosmos_storage.py deleted file mode 100644 index c66660857..000000000 --- a/libraries/botbuilder-azure/tests/test_cosmos_storage.py +++ /dev/null @@ -1,300 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from unittest.mock import Mock -import azure.cosmos.errors as cosmos_errors -from azure.cosmos.cosmos_client import CosmosClient -import pytest -from botbuilder.core import StoreItem -from botbuilder.azure import CosmosDbStorage, CosmosDbConfig -from botbuilder.testing import StorageBaseTests - -# local cosmosdb emulator instance cosmos_db_config -COSMOS_DB_CONFIG = CosmosDbConfig( - endpoint="https://localhost:8081", - masterkey="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", - database="test-db", - container="bot-storage", -) -EMULATOR_RUNNING = False - - -def get_storage(): - return CosmosDbStorage(COSMOS_DB_CONFIG) - - -async def reset(): - storage = CosmosDbStorage(COSMOS_DB_CONFIG) - try: - storage.client.DeleteDatabase(database_link="dbs/" + COSMOS_DB_CONFIG.database) - except cosmos_errors.HTTPFailure: - pass - - -def get_mock_client(identifier: str = "1"): - # pylint: disable=attribute-defined-outside-init, invalid-name - mock = MockClient() - - mock.QueryDatabases = Mock(return_value=[]) - mock.QueryContainers = Mock(return_value=[]) - mock.CreateDatabase = Mock(return_value={"id": identifier}) - mock.CreateContainer = Mock(return_value={"id": identifier}) - - return mock - - -class MockClient(CosmosClient): - def __init__(self): # pylint: disable=super-init-not-called - pass - - -class SimpleStoreItem(StoreItem): - def __init__(self, counter=1, e_tag="*"): - super(SimpleStoreItem, self).__init__() - self.counter = counter - self.e_tag = e_tag - - -class TestCosmosDbStorageConstructor: - @pytest.mark.asyncio - async def test_cosmos_storage_init_should_error_without_cosmos_db_config(self): - try: - CosmosDbStorage(CosmosDbConfig()) - except Exception as error: - assert error - - @pytest.mark.asyncio - async def test_creation_request_options_are_being_called(self): - # pylint: disable=protected-access - test_config = CosmosDbConfig( - endpoint="https://localhost:8081", - masterkey="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", - database="test-db", - container="bot-storage", - database_creation_options={"OfferThroughput": 1000}, - container_creation_options={"OfferThroughput": 500}, - ) - - test_id = "1" - client = get_mock_client(identifier=test_id) - storage = CosmosDbStorage(test_config, client) - storage.database = test_id - - assert storage._get_or_create_database(doc_client=client, id=test_id), test_id - client.CreateDatabase.assert_called_with( - {"id": test_id}, test_config.database_creation_options - ) - assert storage._get_or_create_container( - doc_client=client, container=test_id - ), test_id - client.CreateContainer.assert_called_with( - "dbs/" + test_id, {"id": test_id}, test_config.container_creation_options - ) - - -class TestCosmosDbStorageBaseStorageTests: - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_return_empty_object_when_reading_unknown_key(self): - await reset() - - test_ran = await StorageBaseTests.return_empty_object_when_reading_unknown_key( - get_storage() - ) - - assert test_ran - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_handle_null_keys_when_reading(self): - await reset() - - test_ran = await StorageBaseTests.handle_null_keys_when_reading(get_storage()) - - assert test_ran - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_handle_null_keys_when_writing(self): - await reset() - - test_ran = await StorageBaseTests.handle_null_keys_when_writing(get_storage()) - - assert test_ran - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_does_not_raise_when_writing_no_items(self): - await reset() - - test_ran = await StorageBaseTests.does_not_raise_when_writing_no_items( - get_storage() - ) - - assert test_ran - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_create_object(self): - await reset() - - test_ran = await StorageBaseTests.create_object(get_storage()) - - assert test_ran - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_handle_crazy_keys(self): - await reset() - - test_ran = await StorageBaseTests.handle_crazy_keys(get_storage()) - - assert test_ran - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_update_object(self): - await reset() - - test_ran = await StorageBaseTests.update_object(get_storage()) - - assert test_ran - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_delete_object(self): - await reset() - - test_ran = await StorageBaseTests.delete_object(get_storage()) - - assert test_ran - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_perform_batch_operations(self): - await reset() - - test_ran = await StorageBaseTests.perform_batch_operations(get_storage()) - - assert test_ran - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_proceeds_through_waterfall(self): - await reset() - - test_ran = await StorageBaseTests.proceeds_through_waterfall(get_storage()) - - assert test_ran - - -class TestCosmosDbStorage: - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_cosmos_storage_init_should_work_with_just_endpoint_and_key(self): - storage = CosmosDbStorage( - CosmosDbConfig( - endpoint=COSMOS_DB_CONFIG.endpoint, masterkey=COSMOS_DB_CONFIG.masterkey - ) - ) - await storage.write({"user": SimpleStoreItem()}) - data = await storage.read(["user"]) - assert "user" in data - assert data["user"].counter == 1 - assert len(data.keys()) == 1 - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_cosmos_storage_read_update_should_return_new_etag(self): - await reset() - storage = CosmosDbStorage(COSMOS_DB_CONFIG) - await storage.write({"test": SimpleStoreItem(counter=1)}) - data_result = await storage.read(["test"]) - data_result["test"].counter = 2 - await storage.write(data_result) - data_updated = await storage.read(["test"]) - assert data_updated["test"].counter == 2 - assert data_updated["test"].e_tag != data_result["test"].e_tag - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_cosmos_storage_read_with_invalid_key_should_return_empty_dict(self): - await reset() - storage = CosmosDbStorage(COSMOS_DB_CONFIG) - data = await storage.read(["test"]) - - assert isinstance(data, dict) - assert not data.keys() - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_cosmos_storage_write_should_overwrite_when_new_e_tag_is_an_asterisk( - self, - ): - await reset() - storage = CosmosDbStorage(COSMOS_DB_CONFIG) - await storage.write({"user": SimpleStoreItem()}) - - await storage.write({"user": SimpleStoreItem(counter=10, e_tag="*")}) - data = await storage.read(["user"]) - assert data["user"].counter == 10 - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_cosmos_storage_delete_should_delete_multiple_values_when_given_multiple_valid_keys( - self, - ): - await reset() - storage = CosmosDbStorage(COSMOS_DB_CONFIG) - await storage.write({"test": SimpleStoreItem(), "test2": SimpleStoreItem(2)}) - - await storage.delete(["test", "test2"]) - data = await storage.read(["test", "test2"]) - assert not data.keys() - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_cosmos_storage_delete_should_delete_values_when_given_multiple_valid_keys_and_ignore_other_data( - self, - ): - await reset() - storage = CosmosDbStorage(COSMOS_DB_CONFIG) - await storage.write( - { - "test": SimpleStoreItem(), - "test2": SimpleStoreItem(counter=2), - "test3": SimpleStoreItem(counter=3), - } - ) - - await storage.delete(["test", "test2"]) - data = await storage.read(["test", "test2", "test3"]) - assert len(data.keys()) == 1 - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_cosmos_storage_delete_invalid_key_should_do_nothing_and_not_affect_cached_data( - self, - ): - await reset() - storage = CosmosDbStorage(COSMOS_DB_CONFIG) - await storage.write({"test": SimpleStoreItem()}) - - await storage.delete(["foo"]) - data = await storage.read(["test"]) - assert len(data.keys()) == 1 - data = await storage.read(["foo"]) - assert not data.keys() - - @pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.") - @pytest.mark.asyncio - async def test_cosmos_storage_delete_invalid_keys_should_do_nothing_and_not_affect_cached_data( - self, - ): - await reset() - storage = CosmosDbStorage(COSMOS_DB_CONFIG) - await storage.write({"test": SimpleStoreItem()}) - - await storage.delete(["foo", "bar"]) - data = await storage.read(["test"]) - assert len(data.keys()) == 1 diff --git a/libraries/botbuilder-azure/tests/test_queue_storage.py b/libraries/botbuilder-azure/tests/test_queue_storage.py new file mode 100644 index 000000000..17c6631cc --- /dev/null +++ b/libraries/botbuilder-azure/tests/test_queue_storage.py @@ -0,0 +1,50 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import unittest +import aiounittest +from jsonpickle import decode + +from botbuilder.azure import AzureQueueStorage + +EMULATOR_RUNNING = False + +# This connection string is to connect to local Azure Storage Emulator. +CONNECTION_STRING = ( + "AccountName=devstoreaccount1;" + "AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr" + "/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;" + "BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;" + "QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;" + "TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;" +) +QUEUE_NAME = "queue" + + +class TestAzureQueueStorageConstructor: + def test_queue_storage_init_should_error_without_connection_string(self): + try: + # pylint: disable=no-value-for-parameter + AzureQueueStorage() + except Exception as error: + assert error + + def test_queue_storage_init_should_error_without_queue_name(self): + try: + # pylint: disable=no-value-for-parameter + AzureQueueStorage(queues_storage_connection_string="somestring") + except Exception as error: + assert error + + +class TestAzureQueueStorage(aiounittest.AsyncTestCase): + @unittest.skipIf(not EMULATOR_RUNNING, reason="Needs the emulator to run.") + async def test_returns_read_receipt(self): + message = {"string": "test", "object": {"string2": "test2"}, "number": 99} + queue = AzureQueueStorage(CONNECTION_STRING, QUEUE_NAME) + + receipt = await queue.queue_activity(message) + decoded = decode(receipt) + + assert decoded.id is not None + assert decode(decoded.content) == message diff --git a/libraries/botbuilder-core/README.rst b/libraries/botbuilder-core/README.rst index 2fc7b5fe5..7cfe8e3f0 100644 --- a/libraries/botbuilder-core/README.rst +++ b/libraries/botbuilder-core/README.rst @@ -3,8 +3,8 @@ BotBuilder-Core SDK for Python ============================== -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch .. image:: https://badge.fury.io/py/botbuilder-core.svg diff --git a/libraries/botbuilder-core/botbuilder/core/__init__.py b/libraries/botbuilder-core/botbuilder/core/__init__.py index f9a846ea5..0769d9100 100644 --- a/libraries/botbuilder-core/botbuilder/core/__init__.py +++ b/libraries/botbuilder-core/botbuilder/core/__init__.py @@ -4,6 +4,8 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- +from botbuilder.schema import InvokeResponse + from . import conversation_reference_extension from .about import __version__ @@ -18,16 +20,20 @@ from .bot_telemetry_client import BotTelemetryClient, Severity from .card_factory import CardFactory from .channel_service_handler import BotActionNotImplementedError, ChannelServiceHandler +from .cloud_adapter_base import CloudAdapterBase +from .cloud_channel_service_handler import CloudChannelServiceHandler +from .component_registration import ComponentRegistration from .conversation_state import ConversationState -from .extended_user_token_provider import ExtendedUserTokenProvider +from .oauth.extended_user_token_provider import ExtendedUserTokenProvider +from .oauth.user_token_provider import UserTokenProvider from .intent_score import IntentScore -from .invoke_response import InvokeResponse from .memory_storage import MemoryStorage from .memory_transcript_store import MemoryTranscriptStore from .message_factory import MessageFactory from .middleware_set import AnonymousReceiveMiddleware, Middleware, MiddlewareSet from .null_telemetry_client import NullTelemetryClient from .private_conversation_state import PrivateConversationState +from .queue_storage import QueueStorage from .recognizer import Recognizer from .recognizer_result import RecognizerResult, TopIntent from .show_typing_middleware import ShowTypingMiddleware @@ -38,11 +44,15 @@ from .telemetry_logger_constants import TelemetryLoggerConstants from .telemetry_logger_middleware import TelemetryLoggerMiddleware from .turn_context import TurnContext +from .transcript_logger import TranscriptLogger, TranscriptLoggerMiddleware from .user_state import UserState -from .user_token_provider import UserTokenProvider +from .register_class_middleware import RegisterClassMiddleware +from .adapter_extensions import AdapterExtensions +from .serializer_helper import serializer_helper __all__ = [ "ActivityHandler", + "AdapterExtensions", "AnonymousReceiveMiddleware", "AutoSaveStateMiddleware", "Bot", @@ -57,6 +67,9 @@ "calculate_change_hash", "CardFactory", "ChannelServiceHandler", + "CloudAdapterBase", + "CloudChannelServiceHandler", + "ComponentRegistration", "ConversationState", "conversation_reference_extension", "ExtendedUserTokenProvider", @@ -69,6 +82,8 @@ "MiddlewareSet", "NullTelemetryClient", "PrivateConversationState", + "QueueStorage", + "RegisterClassMiddleware", "Recognizer", "RecognizerResult", "Severity", @@ -81,8 +96,11 @@ "TelemetryLoggerConstants", "TelemetryLoggerMiddleware", "TopIntent", + "TranscriptLogger", + "TranscriptLoggerMiddleware", "TurnContext", "UserState", "UserTokenProvider", + "serializer_helper", "__version__", ] diff --git a/libraries/botbuilder-core/botbuilder/core/about.py b/libraries/botbuilder-core/botbuilder/core/about.py index f47c7fd78..5220c09e6 100644 --- a/libraries/botbuilder-core/botbuilder/core/about.py +++ b/libraries/botbuilder-core/botbuilder/core/about.py @@ -5,7 +5,7 @@ __title__ = "botbuilder-core" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botbuilder-core/botbuilder/core/activity_handler.py b/libraries/botbuilder-core/botbuilder/core/activity_handler.py index f88012ede..4dbf04f0b 100644 --- a/libraries/botbuilder-core/botbuilder/core/activity_handler.py +++ b/libraries/botbuilder-core/botbuilder/core/activity_handler.py @@ -6,17 +6,21 @@ from botbuilder.schema import ( Activity, ActivityTypes, + AdaptiveCardInvokeResponse, + AdaptiveCardInvokeValue, ChannelAccount, + InvokeResponse, MessageReaction, SignInConstants, ) + +from .bot import Bot from .serializer_helper import serializer_helper from .bot_framework_adapter import BotFrameworkAdapter -from .invoke_response import InvokeResponse from .turn_context import TurnContext -class ActivityHandler: +class ActivityHandler(Bot): """ Handles activities and should be subclassed. @@ -26,7 +30,9 @@ class ActivityHandler: in the derived class. """ - async def on_turn(self, turn_context: TurnContext): + async def on_turn( + self, turn_context: TurnContext + ): # pylint: disable=arguments-differ """ Called by the adapter (for example, :class:`BotFrameworkAdapter`) at runtime in order to process an inbound :class:`botbuilder.schema.Activity`. @@ -62,6 +68,10 @@ async def on_turn(self, turn_context: TurnContext): if turn_context.activity.type == ActivityTypes.message: await self.on_message_activity(turn_context) + elif turn_context.activity.type == ActivityTypes.message_update: + await self.on_message_update_activity(turn_context) + elif turn_context.activity.type == ActivityTypes.message_delete: + await self.on_message_delete_activity(turn_context) elif turn_context.activity.type == ActivityTypes.conversation_update: await self.on_conversation_update_activity(turn_context) elif turn_context.activity.type == ActivityTypes.message_reaction: @@ -82,6 +92,8 @@ async def on_turn(self, turn_context: TurnContext): await self.on_end_of_conversation_activity(turn_context) elif turn_context.activity.type == ActivityTypes.typing: await self.on_typing_activity(turn_context) + elif turn_context.activity.type == ActivityTypes.installation_update: + await self.on_installation_update(turn_context) else: await self.on_unrecognized_activity_type(turn_context) @@ -99,6 +111,34 @@ async def on_message_activity( # pylint: disable=unused-argument """ return + async def on_message_update_activity( # pylint: disable=unused-argument + self, turn_context: TurnContext + ): + """ + Override this method in a derived class to provide logic specific to activities, + such as the conversational logic. + + :param turn_context: The context object for this turn + :type turn_context: :class:`botbuilder.core.TurnContext` + + :returns: A task that represents the work queued to execute + """ + return + + async def on_message_delete_activity( # pylint: disable=unused-argument + self, turn_context: TurnContext + ): + """ + Override this method in a derived class to provide logic specific to activities, + such as the conversational logic. + + :param turn_context: The context object for this turn + :type turn_context: :class:`botbuilder.core.TurnContext` + + :returns: A task that represents the work queued to execute + """ + return + async def on_conversation_update_activity(self, turn_context: TurnContext): """ Invoked when a conversation update activity is received from the channel when the base behavior of @@ -361,6 +401,49 @@ async def on_typing_activity( # pylint: disable=unused-argument """ return + async def on_installation_update( # pylint: disable=unused-argument + self, turn_context: TurnContext + ): + """ + Override this in a derived class to provide logic specific to + ActivityTypes.InstallationUpdate activities. + + :param turn_context: The context object for this turn + :type turn_context: :class:`botbuilder.core.TurnContext` + :returns: A task that represents the work queued to execute + """ + if turn_context.activity.action in ("add", "add-upgrade"): + return await self.on_installation_update_add(turn_context) + if turn_context.activity.action in ("remove", "remove-upgrade"): + return await self.on_installation_update_remove(turn_context) + return + + async def on_installation_update_add( # pylint: disable=unused-argument + self, turn_context: TurnContext + ): + """ + Override this in a derived class to provide logic specific to + ActivityTypes.InstallationUpdate activities with 'action' set to 'add'. + + :param turn_context: The context object for this turn + :type turn_context: :class:`botbuilder.core.TurnContext` + :returns: A task that represents the work queued to execute + """ + return + + async def on_installation_update_remove( # pylint: disable=unused-argument + self, turn_context: TurnContext + ): + """ + Override this in a derived class to provide logic specific to + ActivityTypes.InstallationUpdate activities with 'action' set to 'remove'. + + :param turn_context: The context object for this turn + :type turn_context: :class:`botbuilder.core.TurnContext` + :returns: A task that represents the work queued to execute + """ + return + async def on_unrecognized_activity_type( # pylint: disable=unused-argument self, turn_context: TurnContext ): @@ -395,12 +478,27 @@ async def on_invoke_activity( # pylint: disable=unused-argument if ( turn_context.activity.name == SignInConstants.verify_state_operation_name - or turn_context.activity.name - == SignInConstants.token_exchange_operation_name ): await self.on_sign_in_invoke(turn_context) return self._create_invoke_response() + # This is for back-compat with previous versions of Python SDK. This method does not + # exist in the C# SDK, and is not used in the Python SDK. + if ( + turn_context.activity.name + == SignInConstants.token_exchange_operation_name + ): + await self.on_teams_signin_token_exchange(turn_context) + return self._create_invoke_response() + + if turn_context.activity.name == "adaptiveCard/action": + invoke_value = self._get_adaptive_card_invoke_value( + turn_context.activity + ) + return self._create_invoke_response( + await self.on_adaptive_card_invoke(turn_context, invoke_value) + ) + raise _InvokeResponseException(HTTPStatus.NOT_IMPLEMENTED) except _InvokeResponseException as invoke_exception: return invoke_exception.create_invoke_response() @@ -421,10 +519,70 @@ async def on_sign_in_invoke( # pylint: disable=unused-argument """ raise _InvokeResponseException(HTTPStatus.NOT_IMPLEMENTED) + async def on_adaptive_card_invoke( + self, turn_context: TurnContext, invoke_value: AdaptiveCardInvokeValue + ) -> AdaptiveCardInvokeResponse: + """ + Invoked when the bot is sent an Adaptive Card Action Execute. + + When the on_invoke_activity method receives an Invoke with a Activity.name of `adaptiveCard/action`, it + calls this method. + + :param turn_context: A context object for this turn. + :type turn_context: :class:`botbuilder.core.TurnContext` + :param invoke_value: A string-typed object from the incoming activity's value. + :type invoke_value: :class:`botframework.schema.models.AdaptiveCardInvokeValue` + :return: The HealthCheckResponse object + """ + raise _InvokeResponseException(HTTPStatus.NOT_IMPLEMENTED) + @staticmethod def _create_invoke_response(body: object = None) -> InvokeResponse: return InvokeResponse(status=int(HTTPStatus.OK), body=serializer_helper(body)) + def _get_adaptive_card_invoke_value(self, activity: Activity): + if activity.value is None: + response = self._create_adaptive_card_invoke_error_response( + HTTPStatus.BAD_REQUEST, "BadRequest", "Missing value property" + ) + raise _InvokeResponseException(HTTPStatus.BAD_REQUEST, response) + + invoke_value = None + try: + invoke_value = AdaptiveCardInvokeValue(**activity.value) + except: + response = self._create_adaptive_card_invoke_error_response( + HTTPStatus.BAD_REQUEST, + "BadRequest", + "Value property is not properly formed", + ) + raise _InvokeResponseException(HTTPStatus.BAD_REQUEST, response) + + if invoke_value.action is None: + response = self._create_adaptive_card_invoke_error_response( + HTTPStatus.BAD_REQUEST, "BadRequest", "Missing action property" + ) + raise _InvokeResponseException(HTTPStatus.BAD_REQUEST, response) + + if invoke_value.action.get("type") != "Action.Execute": + response = self._create_adaptive_card_invoke_error_response( + HTTPStatus.BAD_REQUEST, + "NotSupported", + f"The action '{invoke_value.action.get('type')}' is not supported.", + ) + raise _InvokeResponseException(HTTPStatus.BAD_REQUEST, response) + + return invoke_value + + def _create_adaptive_card_invoke_error_response( + self, status_code: HTTPStatus, code: str, message: str + ): + return AdaptiveCardInvokeResponse( + status_code=status_code, + type="application/vnd.microsoft.error", + value=Exception(code, message), + ) + class _InvokeResponseException(Exception): def __init__(self, status_code: HTTPStatus, body: object = None): diff --git a/libraries/botbuilder-core/botbuilder/core/adapter_extensions.py b/libraries/botbuilder-core/botbuilder/core/adapter_extensions.py new file mode 100644 index 000000000..db13d74b5 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/adapter_extensions.py @@ -0,0 +1,99 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +from warnings import warn + +from botbuilder.core import ( + BotAdapter, + BotState, + Storage, + RegisterClassMiddleware, + UserState, + ConversationState, + AutoSaveStateMiddleware, +) + + +class AdapterExtensions: + @staticmethod + def use_storage(adapter: BotAdapter, storage: Storage) -> BotAdapter: + """ + Registers a storage layer with the adapter. The storage object will be available via the turn context's + `turn_state` property. + + :param adapter: The BotAdapter on which to register the storage object. + :param storage: The Storage object to register. + :return: The BotAdapter + """ + return adapter.use(RegisterClassMiddleware(storage)) + + @staticmethod + def use_bot_state( + bot_adapter: BotAdapter, *bot_states: BotState, auto: bool = True + ) -> BotAdapter: + """ + Registers bot state object into the TurnContext. The botstate will be available via the turn context. + + :param bot_adapter: The BotAdapter on which to register the state objects. + :param bot_states: One or more BotState objects to register. + :return: The updated adapter. + """ + if not bot_states: + raise TypeError("At least one BotAdapter is required") + + for bot_state in bot_states: + bot_adapter.use( + RegisterClassMiddleware( + bot_state, AdapterExtensions.fullname(bot_state) + ) + ) + + if auto: + bot_adapter.use(AutoSaveStateMiddleware(bot_states)) + + return bot_adapter + + @staticmethod + def fullname(obj): + module = obj.__class__.__module__ + if module is None or module == str.__class__.__module__: + return obj.__class__.__name__ # Avoid reporting __builtin__ + return module + "." + obj.__class__.__name__ + + @staticmethod + def use_state( + adapter: BotAdapter, + user_state: UserState, + conversation_state: ConversationState, + auto: bool = True, + ) -> BotAdapter: + """ + [DEPRECATED] Registers user and conversation state objects with the adapter. These objects will be available via + the turn context's `turn_state` property. + + :param adapter: The BotAdapter on which to register the state objects. + :param user_state: The UserState object to register. + :param conversation_state: The ConversationState object to register. + :param auto: True to automatically persist state each turn. + :return: The BotAdapter + """ + warn( + "This method is deprecated in 4.9. You should use the method .use_bot_state() instead.", + DeprecationWarning, + ) + + if not adapter: + raise TypeError("BotAdapter is required") + + if not user_state: + raise TypeError("UserState is required") + + if not conversation_state: + raise TypeError("ConversationState is required") + + adapter.use(RegisterClassMiddleware(user_state)) + adapter.use(RegisterClassMiddleware(conversation_state)) + + if auto: + adapter.use(AutoSaveStateMiddleware([user_state, conversation_state])) + + return adapter diff --git a/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py b/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py index 95048695f..ebfeb303a 100644 --- a/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py +++ b/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py @@ -7,11 +7,18 @@ import asyncio import inspect -from datetime import datetime +import uuid +from datetime import datetime, timezone from uuid import uuid4 from typing import Awaitable, Coroutine, Dict, List, Callable, Union from copy import copy from threading import Lock +from botframework.connector.auth import AppCredentials, ClaimsIdentity +from botframework.connector.token_api.models import ( + SignInUrlResponse, + TokenExchangeResource, + TokenExchangeRequest, +) from botbuilder.schema import ( ActivityTypes, Activity, @@ -21,15 +28,10 @@ ResourceResponse, TokenResponse, ) -from botframework.connector.auth import AppCredentials, ClaimsIdentity -from botframework.connector.token_api.models import ( - SignInUrlResponse, - TokenExchangeResource, - TokenExchangeRequest, -) from ..bot_adapter import BotAdapter from ..turn_context import TurnContext -from ..extended_user_token_provider import ExtendedUserTokenProvider +from ..oauth.extended_user_token_provider import ExtendedUserTokenProvider +from botframework.connector import Channels class UserToken: @@ -91,6 +93,7 @@ def __init__(self, key: UserToken = None, magic_code: str = None): class TestAdapter(BotAdapter, ExtendedUserTokenProvider): __test__ = False + __EXCEPTION_EXPECTED = "ExceptionExpected" def __init__( self, @@ -119,7 +122,7 @@ def __init__( template_or_conversation if isinstance(template_or_conversation, Activity) else Activity( - channel_id="test", + channel_id=Channels.test, service_url="https://test.com", from_property=ChannelAccount(id="User1", name="user"), recipient=ChannelAccount(id="bot", name="Bot"), @@ -139,7 +142,9 @@ async def process_activity( if activity.type is None: activity.type = ActivityTypes.message - activity.channel_id = self.template.channel_id + if activity.channel_id is None: + activity.channel_id = self.template.channel_id + activity.from_property = self.template.from_property activity.recipient = self.template.recipient activity.conversation = self.template.conversation @@ -150,8 +155,8 @@ async def process_activity( finally: self._conversation_lock.release() - activity.timestamp = activity.timestamp or datetime.utcnow() - await self.run_pipeline(TurnContext(self, activity), logic) + activity.timestamp = activity.timestamp or datetime.now(timezone.utc) + await self.run_pipeline(self.create_turn_context(activity), logic) async def send_activities( self, context, activities: List[Activity] @@ -215,6 +220,20 @@ async def continue_conversation( reference, callback, bot_id, claims_identity, audience ) + async def create_conversation( # pylint: disable=arguments-differ + self, channel_id: str, callback: Callable # pylint: disable=unused-argument + ): + self.activity_buffer.clear() + update = Activity( + type=ActivityTypes.conversation_update, + members_added=[], + members_removed=[], + channel_id=channel_id, + conversation=ConversationAccount(id=str(uuid.uuid4())), + ) + context = self.create_turn_context(update) + return await callback(context) + async def receive_activity(self, activity): """ INTERNAL: called by a `TestFlow` instance to simulate a user sending a message to the bot. @@ -237,7 +256,7 @@ async def receive_activity(self, activity): request.id = str(self._next_id) # Create context object and run middleware. - context = TurnContext(self, request) + context = self.create_turn_context(request) return await self.run_pipeline(context, self.logic) def get_next_activity(self) -> Activity: @@ -287,6 +306,28 @@ async def tests(self, *args): timeout = arg[3] await self.test(arg[0], arg[1], description, timeout) + @staticmethod + def create_conversation_reference( + name: str, user: str = "User1", bot: str = "Bot" + ) -> ConversationReference: + return ConversationReference( + channel_id=Channels.test, + service_url="https://test.com", + conversation=ConversationAccount( + is_group=False, + conversation_type=name, + id=name, + ), + user=ChannelAccount( + id=user.lower(), + name=user.lower(), + ), + bot=ChannelAccount( + id=bot.lower(), + name=bot.lower(), + ), + ) + def add_user_token( self, connection_name: str, @@ -418,6 +459,23 @@ def add_exchangeable_token( ) self.exchangeable_tokens[key.to_key()] = key + def throw_on_exchange_request( + self, + connection_name: str, + channel_id: str, + user_id: str, + exchangeable_item: str, + ): + key = ExchangeableToken( + connection_name=connection_name, + channel_id=channel_id, + user_id=user_id, + exchangeable_item=exchangeable_item, + token=TestAdapter.__EXCEPTION_EXPECTED, + ) + + self.exchangeable_tokens[key.to_key()] = key + async def get_sign_in_resource_from_user( self, turn_context: TurnContext, @@ -476,6 +534,9 @@ async def exchange_token_from_credentials( token_exchange_response = self.exchangeable_tokens.get(key.to_key()) if token_exchange_response: + if token_exchange_response.token == TestAdapter.__EXCEPTION_EXPECTED: + raise Exception("Exception occurred during exchanging tokens") + return TokenResponse( channel_id=key.channel_id, connection_name=key.connection_name, @@ -485,6 +546,9 @@ async def exchange_token_from_credentials( return None + def create_turn_context(self, activity: Activity) -> TurnContext: + return TurnContext(self, activity) + class TestFlow: __test__ = False @@ -546,6 +610,7 @@ async def assert_reply( :param is_substring: :return: """ + # TODO: refactor method so expected can take a Callable[[Activity], None] def default_inspector(reply, description=None): if isinstance(expected, Activity): @@ -602,6 +667,47 @@ async def wait_for_activity(): return TestFlow(await test_flow_previous(), self.adapter) + async def assert_no_reply( + self, + description=None, + timeout=None, # pylint: disable=unused-argument + ) -> "TestFlow": + """ + Generates an assertion if the bot responds when no response is expected. + :param description: + :param timeout: + """ + if description is None: + description = "" + + async def test_flow_previous(): + nonlocal timeout + if not timeout: + timeout = 3000 + start = datetime.now() + adapter = self.adapter + + async def wait_for_activity(): + nonlocal timeout + current = datetime.now() + + if (current - start).total_seconds() * 1000 > timeout: + # operation timed out and recieved no reply + return + + if adapter.activity_buffer: + reply = adapter.activity_buffer.pop(0) + raise RuntimeError( + f"TestAdapter.assert_no_reply(): '{reply.text}' is responded when waiting for no reply." + ) + + await asyncio.sleep(0.05) + await wait_for_activity() + + await wait_for_activity() + + return TestFlow(await test_flow_previous(), self.adapter) + def validate_activity(activity, expected) -> None: """ diff --git a/libraries/botbuilder-core/botbuilder/core/bot_adapter.py b/libraries/botbuilder-core/botbuilder/core/bot_adapter.py index 421e34ff2..5ab04eafb 100644 --- a/libraries/botbuilder-core/botbuilder/core/bot_adapter.py +++ b/libraries/botbuilder-core/botbuilder/core/bot_adapter.py @@ -3,8 +3,13 @@ from abc import ABC, abstractmethod from typing import List, Callable, Awaitable -from botbuilder.schema import Activity, ConversationReference, ResourceResponse -from botframework.connector.auth import ClaimsIdentity +from botbuilder.schema import ( + Activity, + ConversationReference, + ConversationParameters, + ResourceResponse, +) +from botframework.connector.auth import AppCredentials, ClaimsIdentity from . import conversation_reference_extension from .bot_assert import BotAssert @@ -17,6 +22,7 @@ class BotAdapter(ABC): BOT_OAUTH_SCOPE_KEY = "botbuilder.core.BotAdapter.OAuthScope" BOT_CONNECTOR_CLIENT_KEY = "ConnectorClient" BOT_CALLBACK_HANDLER_KEY = "BotCallbackHandler" + _INVOKE_RESPONSE_KEY = "BotFrameworkAdapter.InvokeResponse" def __init__( self, on_turn_error: Callable[[TurnContext, Exception], Awaitable] = None @@ -30,8 +36,11 @@ async def send_activities( ) -> List[ResourceResponse]: """ Sends a set of activities to the user. An array of responses from the server will be returned. - :param context: - :param activities: + + :param context: The context object for the turn. + :type context: :class:`TurnContext` + :param activities: The activities to send. + :type activities: :class:`typing.List[Activity]` :return: """ raise NotImplementedError() @@ -40,8 +49,11 @@ async def send_activities( async def update_activity(self, context: TurnContext, activity: Activity): """ Replaces an existing activity. - :param context: - :param activity: + + :param context: The context object for the turn. + :type context: :class:`TurnContext` + :param activity: New replacement activity. + :type activity: :class:`botbuilder.schema.Activity` :return: """ raise NotImplementedError() @@ -52,8 +64,11 @@ async def delete_activity( ): """ Deletes an existing activity. - :param context: - :param reference: + + :param context: The context object for the turn. + :type context: :class:`TurnContext` + :param reference: Conversation reference for the activity to delete. + :type reference: :class:`botbuilder.schema.ConversationReference` :return: """ raise NotImplementedError() @@ -61,7 +76,8 @@ async def delete_activity( def use(self, middleware): """ Registers a middleware handler with the adapter. - :param middleware: + + :param middleware: The middleware to register. :return: """ self._middleware.use(middleware) @@ -77,29 +93,78 @@ async def continue_conversation( ): """ Sends a proactive message to a conversation. Call this method to proactively send a message to a conversation. - Most _channels require a user to initiate a conversation with a bot before the bot can send activities + Most channels require a user to initiate a conversation with a bot before the bot can send activities to the user. + :param bot_id: The application ID of the bot. This parameter is ignored in - single tenant the Adpters (Console, Test, etc) but is critical to the BotFrameworkAdapter - which is multi-tenant aware. - :param reference: A reference to the conversation to continue. - :param callback: The method to call for the resulting bot turn. - :param claims_identity: - :param audience: + single tenant the Adapters (Console, Test, etc) but is critical to the BotFrameworkAdapter + which is multi-tenant aware. + :param reference: A reference to the conversation to continue. + :type reference: :class:`botbuilder.schema.ConversationReference` + :param callback: The method to call for the resulting bot turn. + :type callback: :class:`typing.Callable` + :param claims_identity: A :class:`botframework.connector.auth.ClaimsIdentity` for the conversation. + :type claims_identity: :class:`botframework.connector.auth.ClaimsIdentity` + :param audience:A value signifying the recipient of the proactive message. + :type audience: str """ context = TurnContext( self, conversation_reference_extension.get_continuation_activity(reference) ) return await self.run_pipeline(context, callback) + async def create_conversation( + self, + reference: ConversationReference, + logic: Callable[[TurnContext], Awaitable] = None, + conversation_parameters: ConversationParameters = None, + channel_id: str = None, + service_url: str = None, + credentials: AppCredentials = None, + ): + """ + Starts a new conversation with a user. Used to direct message to a member of a group. + + :param reference: The conversation reference that contains the tenant + :type reference: :class:`botbuilder.schema.ConversationReference` + :param logic: The logic to use for the creation of the conversation + :type logic: :class:`typing.Callable` + :param conversation_parameters: The information to use to create the conversation + :type conversation_parameters: + :param channel_id: The ID for the channel. + :type channel_id: :class:`typing.str` + :param service_url: The channel's service URL endpoint. + :type service_url: :class:`typing.str` + :param credentials: The application credentials for the bot. + :type credentials: :class:`botframework.connector.auth.AppCredentials` + + :raises: It raises a generic exception error. + + :return: A task representing the work queued to execute. + + .. remarks:: + To start a conversation, your bot must know its account information and the user's + account information on that channel. + Most channels only support initiating a direct message (non-group) conversation. + The adapter attempts to create a new conversation on the channel, and + then sends a conversation update activity through its middleware pipeline + to the the callback method. + If the conversation is established with the specified users, the ID of the activity + will contain the ID of the new conversation. + """ + raise Exception("Not Implemented") + async def run_pipeline( self, context: TurnContext, callback: Callable[[TurnContext], Awaitable] = None ): """ Called by the parent class to run the adapters middleware set and calls the passed in `callback()` handler at the end of the chain. - :param context: - :param callback: + + :param context: The context object for the turn. + :type context: :class:`TurnContext` + :param callback: A callback method to run at the end of the pipeline. + :type callback: :class:`typing.Callable[[TurnContext], Awaitable]` :return: """ BotAssert.context_not_none(context) diff --git a/libraries/botbuilder-core/botbuilder/core/bot_framework_adapter.py b/libraries/botbuilder-core/botbuilder/core/bot_framework_adapter.py index 7edf9da2e..601693fd3 100644 --- a/libraries/botbuilder-core/botbuilder/core/bot_framework_adapter.py +++ b/libraries/botbuilder-core/botbuilder/core/bot_framework_adapter.py @@ -36,25 +36,31 @@ TokenStatus, TokenExchangeRequest, SignInUrlResponse, + TokenResponse as ConnectorTokenResponse, ) from botbuilder.schema import ( Activity, + ActivityEventNames, ActivityTypes, ChannelAccount, ConversationAccount, ConversationParameters, ConversationReference, ExpectedReplies, + InvokeResponse, TokenResponse, ResourceResponse, DeliveryModes, + CallerIdConstants, ) from . import __version__ from .bot_adapter import BotAdapter +from .oauth import ( + ConnectorClientBuilder, + ExtendedUserTokenProvider, +) from .turn_context import TurnContext -from .extended_user_token_provider import ExtendedUserTokenProvider -from .invoke_response import InvokeResponse from .conversation_reference_extension import get_continuation_activity USER_AGENT = f"Microsoft-BotFramework/3.1 (BotBuilder Python/{__version__})" @@ -163,7 +169,9 @@ def __init__( ) -class BotFrameworkAdapter(BotAdapter, ExtendedUserTokenProvider): +class BotFrameworkAdapter( + BotAdapter, ExtendedUserTokenProvider, ConnectorClientBuilder +): """ Defines an adapter to connect a bot to a service endpoint. @@ -178,8 +186,6 @@ class BotFrameworkAdapter(BotAdapter, ExtendedUserTokenProvider): upon the activity, both before and after the bot logic runs. """ - _INVOKE_RESPONSE_KEY = "BotFrameworkAdapter.InvokeResponse" - def __init__(self, settings: BotFrameworkAdapterSettings): """ Initializes a new instance of the :class:`BotFrameworkAdapter` class. @@ -273,11 +279,6 @@ async def continue_conversation( context.turn_state[BotAdapter.BOT_CALLBACK_HANDLER_KEY] = callback context.turn_state[BotAdapter.BOT_OAUTH_SCOPE_KEY] = audience - # Add the channel service URL to the trusted services list so we can send messages back. - # the service URL for skills is trusted because it is applied by the SkillHandler based - # on the original request received by the root bot - AppCredentials.trust_service_url(reference.service_url) - client = await self.create_connector_client( reference.service_url, claims_identity, audience ) @@ -324,6 +325,7 @@ async def create_conversation( If the conversation is established with the specified users, the ID of the activity will contain the ID of the new conversation. """ + try: if not service_url: service_url = reference.service_url @@ -348,10 +350,16 @@ async def create_conversation( ) # Mix in the tenant ID if specified. This is required for MS Teams. - if reference.conversation and reference.conversation.tenant_id: + if ( + reference + and reference.conversation + and reference.conversation.tenant_id + ): # Putting tenant_id in channel_data is a temporary while we wait for the Teams API to be updated - parameters.channel_data = { - "tenant": {"id": reference.conversation.tenant_id} + if parameters.channel_data is None: + parameters.channel_data = {} + parameters.channel_data["tenant"] = { + "tenantId": reference.conversation.tenant_id } # Permanent solution is to put tenant_id in parameters.tenant_id @@ -376,14 +384,17 @@ async def create_conversation( event_activity = Activity( type=ActivityTypes.event, - name="CreateConversation", + name=ActivityEventNames.create_conversation, channel_id=channel_id, service_url=service_url, - id=resource_response.activity_id - if resource_response.activity_id - else str(uuid.uuid4()), + id=( + resource_response.activity_id + if resource_response.activity_id + else str(uuid.uuid4()) + ), conversation=ConversationAccount( - id=resource_response.id, tenant_id=parameters.tenant_id, + id=resource_response.id, + tenant_id=parameters.tenant_id, ), channel_data=parameters.channel_data, recipient=parameters.bot, @@ -437,16 +448,17 @@ async def process_activity_with_identity( self, activity: Activity, identity: ClaimsIdentity, logic: Callable ): context = self._create_context(activity) + + activity.caller_id = await self.__generate_callerid(identity) context.turn_state[BotAdapter.BOT_IDENTITY_KEY] = identity context.turn_state[BotAdapter.BOT_CALLBACK_HANDLER_KEY] = logic - # To create the correct cache key, provide the OAuthScope when calling CreateConnectorClientAsync. - # The OAuthScope is also stored on the TurnState to get the correct AppCredentials if fetching a token - # is required. + # The OAuthScope is also stored on the TurnState to get the correct AppCredentials if fetching + # a token is required. scope = ( - self.__get_botframework_oauth_scope() - if not SkillValidation.is_skill_claim(identity.claims) - else JwtTokenValidation.get_app_id_from_claims(identity.claims) + JwtTokenValidation.get_app_id_from_claims(identity.claims) + if SkillValidation.is_skill_claim(identity.claims) + else self.__get_botframework_oauth_scope() ) context.turn_state[BotAdapter.BOT_OAUTH_SCOPE_KEY] = scope @@ -474,14 +486,8 @@ async def process_activity_with_identity( await self.run_pipeline(context, logic) - if activity.type == ActivityTypes.invoke: - invoke_response = context.turn_state.get( - BotFrameworkAdapter._INVOKE_RESPONSE_KEY # pylint: disable=protected-access - ) - if invoke_response is None: - return InvokeResponse(status=int(HTTPStatus.NOT_IMPLEMENTED)) - return invoke_response.value - + # Handle ExpectedReplies scenarios where the all the activities have been buffered and sent back at once + # in an invoke response. # Return the buffered activities in the response. In this case, the invoker # should deserialize accordingly: # activities = ExpectedReplies().deserialize(response.body).activities @@ -491,6 +497,42 @@ async def process_activity_with_identity( ).serialize() return InvokeResponse(status=int(HTTPStatus.OK), body=expected_replies) + # Handle Invoke scenarios, which deviate from the request/request model in that + # the Bot will return a specific body and return code. + if activity.type == ActivityTypes.invoke: + invoke_response = context.turn_state.get( + BotFrameworkAdapter._INVOKE_RESPONSE_KEY # pylint: disable=protected-access + ) + if invoke_response is None: + return InvokeResponse(status=int(HTTPStatus.NOT_IMPLEMENTED)) + return InvokeResponse( + status=invoke_response.value.status, + body=invoke_response.value.body, + ) + + return None + + async def __generate_callerid(self, claims_identity: ClaimsIdentity) -> str: + # Is the bot accepting all incoming messages? + is_auth_disabled = await self._credential_provider.is_authentication_disabled() + if is_auth_disabled: + # Return None so that the callerId is cleared. + return None + + # Is the activity from another bot? + if SkillValidation.is_skill_claim(claims_identity.claims): + app_id = JwtTokenValidation.get_app_id_from_claims(claims_identity.claims) + return f"{CallerIdConstants.bot_to_bot_prefix}{app_id}" + + # Is the activity from Public Azure? + if not self._channel_provider or self._channel_provider.is_public_azure(): + return CallerIdConstants.public_azure_channel + + # Is the activity from Azure Gov? + if self._channel_provider and self._channel_provider.is_government(): + return CallerIdConstants.us_gov_channel + + # Return None so that the callerId is cleared. return None async def _authenticate_request( @@ -861,7 +903,6 @@ async def get_user_token( magic_code: str = None, oauth_app_credentials: AppCredentials = None, # pylint: disable=unused-argument ) -> TokenResponse: - """ Attempts to retrieve the token for a user that's in a login flow. @@ -1058,7 +1099,8 @@ async def create_connector_client( self, service_url: str, identity: ClaimsIdentity = None, audience: str = None ) -> ConnectorClient: """ - Creates the connector client + Implementation of ConnectorClientProvider.create_connector_client. + :param service_url: The service URL :param identity: The claims identity :param audience: @@ -1139,39 +1181,43 @@ async def get_sign_in_resource_from_user_and_credentials( ) -> SignInUrlResponse: if not connection_name: raise TypeError( - "BotFrameworkAdapter.get_sign_in_resource_from_user(): missing connection_name" - ) - if ( - not turn_context.activity.from_property - or not turn_context.activity.from_property.id - ): - raise TypeError( - "BotFrameworkAdapter.get_sign_in_resource_from_user(): missing activity id" + "BotFrameworkAdapter.get_sign_in_resource_from_user_and_credentials(): missing connection_name" ) - if user_id and turn_context.activity.from_property.id != user_id: + if not user_id: raise TypeError( - "BotFrameworkAdapter.get_sign_in_resource_from_user(): cannot get signin resource" - " for a user that is different from the conversation" + "BotFrameworkAdapter.get_sign_in_resource_from_user_and_credentials(): missing user_id" ) - client = await self._create_token_api_client( - turn_context, oauth_app_credentials - ) - conversation = TurnContext.get_conversation_reference(turn_context.activity) + activity = turn_context.activity - state = TokenExchangeState( + app_id = self.__get_app_id(turn_context) + token_exchange_state = TokenExchangeState( connection_name=connection_name, - conversation=conversation, - relates_to=turn_context.activity.relates_to, - ms_app_id=client.config.credentials.microsoft_app_id, + conversation=ConversationReference( + activity_id=activity.id, + bot=activity.recipient, + channel_id=activity.channel_id, + conversation=activity.conversation, + locale=activity.locale, + service_url=activity.service_url, + user=activity.from_property, + ), + relates_to=activity.relates_to, + ms_app_id=app_id, ) - final_state = base64.b64encode( - json.dumps(state.serialize()).encode(encoding="UTF-8", errors="strict") + state = base64.b64encode( + json.dumps(token_exchange_state.serialize()).encode( + encoding="UTF-8", errors="strict" + ) ).decode() + client = await self._create_token_api_client( + turn_context, oauth_app_credentials + ) + return client.bot_sign_in.get_sign_in_resource( - final_state, final_redirect=final_redirect + state, final_redirect=final_redirect ) async def exchange_token( @@ -1211,7 +1257,7 @@ async def exchange_token_from_credentials( turn_context, oauth_app_credentials ) - return client.user_token.exchange_async( + result = client.user_token.exchange_async( user_id, connection_name, turn_context.activity.channel_id, @@ -1219,12 +1265,33 @@ async def exchange_token_from_credentials( exchange_request.token, ) + if isinstance(result, ConnectorTokenResponse): + return TokenResponse( + channel_id=result.channel_id, + connection_name=result.connection_name, + token=result.token, + expiration=result.expiration, + ) + raise TypeError(f"exchange token returned improper result: {type(result)}") + + def can_process_outgoing_activity( + self, activity: Activity # pylint: disable=unused-argument + ) -> bool: + return False + + async def process_outgoing_activity( + self, turn_context: TurnContext, activity: Activity + ) -> ResourceResponse: + raise Exception("NotImplemented") + @staticmethod def key_for_connector_client(service_url: str, app_id: str, scope: str): - return f"{service_url}:{app_id}:{scope}" + return f"{service_url if service_url else ''}:{app_id if app_id else ''}:{scope if scope else ''}" async def _create_token_api_client( - self, context: TurnContext, oauth_app_credentials: AppCredentials = None, + self, + context: TurnContext, + oauth_app_credentials: AppCredentials = None, ) -> TokenApiClient: if ( not self._is_emulating_oauth_cards @@ -1234,7 +1301,7 @@ async def _create_token_api_client( self._is_emulating_oauth_cards = True app_id = self.__get_app_id(context) - scope = context.turn_state.get(BotAdapter.BOT_OAUTH_SCOPE_KEY) + scope = self.__get_botframework_oauth_scope() app_credentials = oauth_app_credentials or await self.__get_app_credentials( app_id, scope ) diff --git a/libraries/botbuilder-core/botbuilder/core/bot_state.py b/libraries/botbuilder-core/botbuilder/core/bot_state.py index 3c6b79329..72a2c2cfb 100644 --- a/libraries/botbuilder-core/botbuilder/core/bot_state.py +++ b/libraries/botbuilder-core/botbuilder/core/bot_state.py @@ -6,6 +6,7 @@ from typing import Callable, Dict, Union from jsonpickle.pickler import Pickler from botbuilder.core.state_property_accessor import StatePropertyAccessor +from .bot_assert import BotAssert from .turn_context import TurnContext from .storage import Storage from .property_manager import PropertyManager @@ -13,11 +14,10 @@ class CachedBotState: """ - Internal cached bot state. + Internal cached bot state. """ def __init__(self, state: Dict[str, object] = None): - self.state = state if state is not None else {} self.hash = self.compute_hash(state) @@ -61,6 +61,18 @@ def __init__(self, storage: Storage, context_service_key: str): self._storage = storage self._context_service_key = context_service_key + def get_cached_state(self, turn_context: TurnContext): + """ + Gets the cached bot state instance that wraps the raw cached data for this "BotState" + from the turn context. + + :param turn_context: The context object for this turn. + :type turn_context: :class:`TurnContext` + :return: The cached bot state instance. + """ + BotAssert.context_not_none(turn_context) + return turn_context.turn_state.get(self._context_service_key) + def create_property(self, name: str) -> StatePropertyAccessor: """ Creates a property definition and registers it with this :class:`BotState`. @@ -75,7 +87,8 @@ def create_property(self, name: str) -> StatePropertyAccessor: return BotStatePropertyAccessor(self, name) def get(self, turn_context: TurnContext) -> Dict[str, object]: - cached = turn_context.turn_state.get(self._context_service_key) + BotAssert.context_not_none(turn_context) + cached = self.get_cached_state(turn_context) return getattr(cached, "state", None) @@ -88,10 +101,9 @@ async def load(self, turn_context: TurnContext, force: bool = False) -> None: :param force: Optional, true to bypass the cache :type force: bool """ - if turn_context is None: - raise TypeError("BotState.load(): turn_context cannot be None.") + BotAssert.context_not_none(turn_context) - cached_state = turn_context.turn_state.get(self._context_service_key) + cached_state = self.get_cached_state(turn_context) storage_key = self.get_storage_key(turn_context) if force or not cached_state or not cached_state.state: @@ -111,10 +123,9 @@ async def save_changes( :param force: Optional, true to save state to storage whether or not there are changes :type force: bool """ - if turn_context is None: - raise TypeError("BotState.save_changes(): turn_context cannot be None.") + BotAssert.context_not_none(turn_context) - cached_state = turn_context.turn_state.get(self._context_service_key) + cached_state = self.get_cached_state(turn_context) if force or (cached_state is not None and cached_state.is_changed): storage_key = self.get_storage_key(turn_context) @@ -134,8 +145,7 @@ async def clear_state(self, turn_context: TurnContext): .. remarks:: This function must be called in order for the cleared state to be persisted to the underlying store. """ - if turn_context is None: - raise TypeError("BotState.clear_state(): turn_context cannot be None.") + BotAssert.context_not_none(turn_context) # Explicitly setting the hash will mean IsChanged is always true. And that will force a Save. cache_value = CachedBotState() @@ -151,8 +161,7 @@ async def delete(self, turn_context: TurnContext) -> None: :return: None """ - if turn_context is None: - raise TypeError("BotState.delete(): turn_context cannot be None.") + BotAssert.context_not_none(turn_context) turn_context.turn_state.pop(self._context_service_key) @@ -174,15 +183,12 @@ async def get_property_value(self, turn_context: TurnContext, property_name: str :return: The value of the property """ - if turn_context is None: - raise TypeError( - "BotState.get_property_value(): turn_context cannot be None." - ) + BotAssert.context_not_none(turn_context) if not property_name: raise TypeError( "BotState.get_property_value(): property_name cannot be None." ) - cached_state = turn_context.turn_state.get(self._context_service_key) + cached_state = self.get_cached_state(turn_context) # if there is no value, this will throw, to signal to IPropertyAccesor that a default value should be computed # This allows this to work with value types @@ -201,11 +207,10 @@ async def delete_property_value( :return: None """ - if turn_context is None: - raise TypeError("BotState.delete_property(): turn_context cannot be None.") + BotAssert.context_not_none(turn_context) if not property_name: raise TypeError("BotState.delete_property(): property_name cannot be None.") - cached_state = turn_context.turn_state.get(self._context_service_key) + cached_state = self.get_cached_state(turn_context) del cached_state.state[property_name] async def set_property_value( @@ -223,11 +228,10 @@ async def set_property_value( :return: None """ - if turn_context is None: - raise TypeError("BotState.delete_property(): turn_context cannot be None.") + BotAssert.context_not_none(turn_context) if not property_name: raise TypeError("BotState.delete_property(): property_name cannot be None.") - cached_state = turn_context.turn_state.get(self._context_service_key) + cached_state = self.get_cached_state(turn_context) cached_state.state[property_name] = value diff --git a/libraries/botbuilder-core/botbuilder/core/bot_state_set.py b/libraries/botbuilder-core/botbuilder/core/bot_state_set.py index 99016af48..67d337088 100644 --- a/libraries/botbuilder-core/botbuilder/core/bot_state_set.py +++ b/libraries/botbuilder-core/botbuilder/core/bot_state_set.py @@ -1,7 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from asyncio import wait from typing import List from .bot_state import BotState from .turn_context import TurnContext @@ -19,14 +18,9 @@ def add(self, bot_state: BotState) -> "BotStateSet": return self async def load_all(self, turn_context: TurnContext, force: bool = False): - await wait( - [bot_state.load(turn_context, force) for bot_state in self.bot_states] - ) + for bot_state in self.bot_states: + await bot_state.load(turn_context, force) async def save_all_changes(self, turn_context: TurnContext, force: bool = False): - await wait( - [ - bot_state.save_changes(turn_context, force) - for bot_state in self.bot_states - ] - ) + for bot_state in self.bot_states: + await bot_state.save_changes(turn_context, force) diff --git a/libraries/botbuilder-core/botbuilder/core/bot_telemetry_client.py b/libraries/botbuilder-core/botbuilder/core/bot_telemetry_client.py index c797b000b..0b935e943 100644 --- a/libraries/botbuilder-core/botbuilder/core/bot_telemetry_client.py +++ b/libraries/botbuilder-core/botbuilder/core/bot_telemetry_client.py @@ -144,7 +144,7 @@ def track_request( :param url: The actual URL for this request (to show in individual request instances). :param success: True if the request ended in success, False otherwise. :param start_time: the start time of the request. \ - The value should look the same as the one returned by :func:`datetime.isoformat()` (defaults to: None) + The value should look the same as the one returned by :func:`datetime.isoformat`. (defaults to: None) :param duration: the number of milliseconds that this request lasted. (defaults to: None) :param response_code: the response code that this request returned. (defaults to: None) :param http_method: the HTTP method that triggered this request. (defaults to: None) diff --git a/libraries/botbuilder-core/botbuilder/core/channel_service_handler.py b/libraries/botbuilder-core/botbuilder/core/channel_service_handler.py index 0cf90327c..2b819d00c 100644 --- a/libraries/botbuilder-core/botbuilder/core/channel_service_handler.py +++ b/libraries/botbuilder-core/botbuilder/core/channel_service_handler.py @@ -21,6 +21,7 @@ ClaimsIdentity, CredentialProvider, JwtTokenValidation, + SkillValidation, ) @@ -149,7 +150,9 @@ async def handle_upload_attachment( ) async def on_get_conversations( - self, claims_identity: ClaimsIdentity, continuation_token: str = "", + self, + claims_identity: ClaimsIdentity, + continuation_token: str = "", ) -> ConversationsResult: """ get_conversations() API for Skill @@ -174,7 +177,9 @@ async def on_get_conversations( raise BotActionNotImplementedError() async def on_create_conversation( - self, claims_identity: ClaimsIdentity, parameters: ConversationParameters, + self, + claims_identity: ClaimsIdentity, + parameters: ConversationParameters, ) -> ConversationResourceResponse: """ create_conversation() API for Skill @@ -208,7 +213,10 @@ async def on_create_conversation( raise BotActionNotImplementedError() async def on_send_to_conversation( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity: Activity, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity: Activity, ) -> ResourceResponse: """ send_to_conversation() API for Skill @@ -317,7 +325,10 @@ async def on_reply_to_activity( raise BotActionNotImplementedError() async def on_delete_activity( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, ): """ delete_activity() API for Skill. @@ -335,7 +346,9 @@ async def on_delete_activity( raise BotActionNotImplementedError() async def on_get_conversation_members( - self, claims_identity: ClaimsIdentity, conversation_id: str, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, ) -> List[ChannelAccount]: """ get_conversation_members() API for Skill. @@ -352,7 +365,10 @@ async def on_get_conversation_members( raise BotActionNotImplementedError() async def on_get_conversation_member( - self, claims_identity: ClaimsIdentity, conversation_id: str, member_id: str, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + member_id: str, ) -> ChannelAccount: """ get_conversation_member() API for Skill. @@ -406,7 +422,10 @@ async def on_get_conversation_paged_members( raise BotActionNotImplementedError() async def on_delete_conversation_member( - self, claims_identity: ClaimsIdentity, conversation_id: str, member_id: str, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + member_id: str, ): """ delete_conversation_member() API for Skill. @@ -426,7 +445,10 @@ async def on_delete_conversation_member( raise BotActionNotImplementedError() async def on_get_activity_members( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, ) -> List[ChannelAccount]: """ get_activity_members() API for Skill. @@ -469,18 +491,30 @@ async def on_upload_attachment( raise BotActionNotImplementedError() async def _authenticate(self, auth_header: str) -> ClaimsIdentity: + """ + Helper to authenticate the header. + + This code is very similar to the code in JwtTokenValidation.authenticate_request, + we should move this code somewhere in that library when we refactor auth, + for now we keep it private to avoid adding more public static functions that we will need to deprecate later. + """ if not auth_header: is_auth_disabled = ( await self._credential_provider.is_authentication_disabled() ) - if is_auth_disabled: - # In the scenario where Auth is disabled, we still want to have the - # IsAuthenticated flag set in the ClaimsIdentity. To do this requires - # adding in an empty claim. - return ClaimsIdentity({}, True) - - raise PermissionError() - + if not is_auth_disabled: + # No auth header. Auth is required. Request is not authorized. + raise PermissionError( + "Authorization is required but has been disabled." + ) + + # In the scenario where Auth is disabled, we still want to have the + # IsAuthenticated flag set in the ClaimsIdentity. To do this requires + # adding in an empty claim. + # Since ChannelServiceHandler calls are always a skill callback call, we set the skill claim too. + return SkillValidation.create_anonymous_skill_claim() + + # Validate the header and extract claims. return await JwtTokenValidation.validate_auth_header( auth_header, self._credential_provider, diff --git a/libraries/botbuilder-core/botbuilder/core/cloud_adapter_base.py b/libraries/botbuilder-core/botbuilder/core/cloud_adapter_base.py new file mode 100644 index 000000000..0f695a2a7 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/cloud_adapter_base.py @@ -0,0 +1,455 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC +from asyncio import sleep +from copy import Error +from http import HTTPStatus +from typing import Awaitable, Callable, List, Union +from uuid import uuid4 + +from botbuilder.core.invoke_response import InvokeResponse + +from botbuilder.schema import ( + Activity, + ActivityEventNames, + ActivityTypes, + ConversationAccount, + ConversationReference, + ConversationResourceResponse, + ConversationParameters, + DeliveryModes, + ExpectedReplies, + ResourceResponse, +) +from botframework.connector import Channels, ConnectorClient +from botframework.connector.auth import ( + AuthenticationConstants, + BotFrameworkAuthentication, + ClaimsIdentity, +) +from botframework.connector.auth.authenticate_request_result import ( + AuthenticateRequestResult, +) +from botframework.connector.auth.connector_factory import ConnectorFactory +from botframework.connector.auth.user_token_client import UserTokenClient +from .bot_adapter import BotAdapter +from .conversation_reference_extension import get_continuation_activity +from .turn_context import TurnContext + + +class CloudAdapterBase(BotAdapter, ABC): + CONNECTOR_FACTORY_KEY = "ConnectorFactory" + USER_TOKEN_CLIENT_KEY = "UserTokenClient" + + def __init__( + self, bot_framework_authentication: BotFrameworkAuthentication + ) -> None: + super().__init__() + + if not bot_framework_authentication: + raise TypeError("Expected BotFrameworkAuthentication but got None instead") + + self.bot_framework_authentication = bot_framework_authentication + + async def send_activities( + self, context: TurnContext, activities: List[Activity] + ) -> List[ResourceResponse]: + if not context: + raise TypeError("Expected TurnContext but got None instead") + + if activities is None: + raise TypeError("Expected Activities list but got None instead") + + if len(activities) == 0: + raise TypeError("Expecting one or more activities, but the list was empty.") + + responses = [] + + for activity in activities: + activity.id = None + + response = ResourceResponse() + + if activity.type == "delay": + delay_time = int((activity.value or 1000) / 1000) + await sleep(delay_time) + elif activity.type == ActivityTypes.invoke_response: + context.turn_state[self._INVOKE_RESPONSE_KEY] = activity + elif ( + activity.type == ActivityTypes.trace + and activity.channel_id != Channels.emulator + ): + # no-op + pass + else: + connector_client: ConnectorClient = context.turn_state.get( + self.BOT_CONNECTOR_CLIENT_KEY + ) + if not connector_client: + raise Error("Unable to extract ConnectorClient from turn context.") + + if activity.reply_to_id: + response = await connector_client.conversations.reply_to_activity( + activity.conversation.id, activity.reply_to_id, activity + ) + else: + response = ( + await connector_client.conversations.send_to_conversation( + activity.conversation.id, activity + ) + ) + + response = response or ResourceResponse(id=activity.id or "") + + responses.append(response) + + return responses + + async def update_activity(self, context: TurnContext, activity: Activity): + if not context: + raise TypeError("Expected TurnContext but got None instead") + + if activity is None: + raise TypeError("Expected Activity but got None instead") + + connector_client: ConnectorClient = context.turn_state.get( + self.BOT_CONNECTOR_CLIENT_KEY + ) + if not connector_client: + raise Error("Unable to extract ConnectorClient from turn context.") + + response = await connector_client.conversations.update_activity( + activity.conversation.id, activity.id, activity + ) + + response_id = response.id if response and response.id else None + + return ResourceResponse(id=response_id) if response_id else None + + async def delete_activity( + self, context: TurnContext, reference: ConversationReference + ): + if not context: + raise TypeError("Expected TurnContext but got None instead") + + if not reference: + raise TypeError("Expected ConversationReference but got None instead") + + connector_client: ConnectorClient = context.turn_state.get( + self.BOT_CONNECTOR_CLIENT_KEY + ) + if not connector_client: + raise Error("Unable to extract ConnectorClient from turn context.") + + await connector_client.conversations.delete_activity( + reference.conversation.id, reference.activity_id + ) + + async def continue_conversation( # pylint: disable=arguments-differ + self, + reference: ConversationReference, + callback: Callable, + bot_app_id: str = None, # pylint: disable=unused-argument + claims_identity: ClaimsIdentity = None, # pylint: disable=unused-argument + audience: str = None, # pylint: disable=unused-argument + ): + """ + Sends a proactive message to a conversation. + Call this method to proactively send a message to a conversation. + Most channels require a user to initiate a conversation with a bot before the bot can send activities + to the user. + + :param reference: A reference to the conversation to continue. + :type reference: :class:`botbuilder.schema.ConversationReference` + :param callback: The method to call for the resulting bot turn. + :type callback: :class:`typing.Callable` + :param bot_app_id: The application Id of the bot. This is the appId returned by the Azure portal registration, + and is generally found in the `MicrosoftAppId` parameter in `config.py`. + :type bot_app_id: :class:`typing.str` + """ + if claims_identity: + return await self.continue_conversation_with_claims( + claims_identity=claims_identity, + reference=reference, + audience=audience, + logic=callback, + ) + + return await self.process_proactive( + self.create_claims_identity(bot_app_id), + get_continuation_activity(reference), + None, + callback, + ) + + async def continue_conversation_with_claims( + self, + claims_identity: ClaimsIdentity, + reference: ConversationReference, + audience: str, + logic: Callable[[TurnContext], Awaitable], + ): + return await self.process_proactive( + claims_identity, get_continuation_activity(reference), audience, logic + ) + + async def create_conversation( # pylint: disable=arguments-differ + self, + bot_app_id: str, + callback: Callable[[TurnContext], Awaitable] = None, + conversation_parameters: ConversationParameters = None, + channel_id: str = None, + service_url: str = None, + audience: str = None, + ): + if not service_url: + raise TypeError( + "CloudAdapter.create_conversation(): service_url is required." + ) + if not conversation_parameters: + raise TypeError( + "CloudAdapter.create_conversation(): conversation_parameters is required." + ) + if not callback: + raise TypeError("CloudAdapter.create_conversation(): callback is required.") + + # Create a ClaimsIdentity, to create the connector and for adding to the turn context. + claims_identity = self.create_claims_identity(bot_app_id) + claims_identity.claims[AuthenticationConstants.SERVICE_URL_CLAIM] = service_url + + # create the connectror factory + connector_factory = self.bot_framework_authentication.create_connector_factory( + claims_identity + ) + + # Create the connector client to use for outbound requests. + connector_client = await connector_factory.create(service_url, audience) + + # Make the actual create conversation call using the connector. + create_conversation_result = ( + await connector_client.conversations.create_conversation( + conversation_parameters + ) + ) + + # Create the create activity to communicate the results to the application. + create_activity = self._create_create_activity( + create_conversation_result, channel_id, service_url, conversation_parameters + ) + + # Create a UserTokenClient instance for the application to use. (For example, in the OAuthPrompt.) + user_token_client = ( + await self.bot_framework_authentication.create_user_token_client( + claims_identity + ) + ) + + # Create a turn context and run the pipeline. + context = self._create_turn_context( + create_activity, + claims_identity, + None, + connector_client, + user_token_client, + callback, + connector_factory, + ) + + # Run the pipeline + await self.run_pipeline(context, callback) + + async def process_proactive( + self, + claims_identity: ClaimsIdentity, + continuation_activity: Activity, + audience: str, + logic: Callable[[TurnContext], Awaitable], + ): + # Create the connector factory and the inbound request, extracting parameters and then create a + # connector for outbound requests. + connector_factory = self.bot_framework_authentication.create_connector_factory( + claims_identity + ) + + # Create the connector client to use for outbound requests. + connector_client = await connector_factory.create( + continuation_activity.service_url, audience + ) + + # Create a UserTokenClient instance for the application to use. (For example, in the OAuthPrompt.) + user_token_client = ( + await self.bot_framework_authentication.create_user_token_client( + claims_identity + ) + ) + + # Create a turn context and run the pipeline. + context = self._create_turn_context( + continuation_activity, + claims_identity, + audience, + connector_client, + user_token_client, + logic, + connector_factory, + ) + + # Run the pipeline + await self.run_pipeline(context, logic) + + async def process_activity( + self, + auth_header_or_authenticate_request_result: Union[ + str, AuthenticateRequestResult + ], + activity: Activity, + logic: Callable[[TurnContext], Awaitable], + ): + """ + Creates a turn context and runs the middleware pipeline for an incoming activity. + + :param auth_header: The HTTP authentication header of the request + :type auth_header: :class:`typing.Union[typing.str, AuthenticateRequestResult]` + :param activity: The incoming activity + :type activity: :class:`Activity` + :param logic: The logic to execute at the end of the adapter's middleware pipeline. + :type logic: :class:`typing.Callable` + + :return: A task that represents the work queued to execute. + + .. remarks:: + This class processes an activity received by the bots web server. This includes any messages + sent from a user and is the method that drives what's often referred to as the + bots *reactive messaging* flow. + Call this method to reactively send a message to a conversation. + If the task completes successfully, then an :class:`InvokeResponse` is returned; + otherwise. `null` is returned. + """ + # Authenticate the inbound request, extracting parameters and create a ConnectorFactory for creating a + # Connector for outbound requests. + authenticate_request_result = ( + await self.bot_framework_authentication.authenticate_request( + activity, auth_header_or_authenticate_request_result + ) + if isinstance(auth_header_or_authenticate_request_result, str) + else auth_header_or_authenticate_request_result + ) + + # Set the caller_id on the activity + activity.caller_id = authenticate_request_result.caller_id + + # Create the connector client to use for outbound requests. + connector_client = ( + await authenticate_request_result.connector_factory.create( + activity.service_url, authenticate_request_result.audience + ) + if authenticate_request_result.connector_factory + else None + ) + + if not connector_client: + raise Error("Unable to extract ConnectorClient from turn context.") + + # Create a UserTokenClient instance for the application to use. + # (For example, it would be used in a sign-in prompt.) + user_token_client = ( + await self.bot_framework_authentication.create_user_token_client( + authenticate_request_result.claims_identity + ) + ) + + # Create a turn context and run the pipeline. + context = self._create_turn_context( + activity, + authenticate_request_result.claims_identity, + authenticate_request_result.audience, + connector_client, + user_token_client, + logic, + authenticate_request_result.connector_factory, + ) + + # Run the pipeline + await self.run_pipeline(context, logic) + + # If there are any results they will have been left on the TurnContext. + return self._process_turn_results(context) + + def create_claims_identity(self, bot_app_id: str = "") -> ClaimsIdentity: + return ClaimsIdentity( + { + AuthenticationConstants.AUDIENCE_CLAIM: bot_app_id, + AuthenticationConstants.APP_ID_CLAIM: bot_app_id, + }, + True, + ) + + def _create_create_activity( + self, + create_conversation_result: ConversationResourceResponse, + channel_id: str, + service_url: str, + conversation_parameters: ConversationParameters, + ) -> Activity: + # Create a conversation update activity to represent the result. + activity = Activity.create_event_activity() + activity.name = ActivityEventNames.create_conversation + activity.channel_id = channel_id + activity.service_url = service_url + activity.id = create_conversation_result.activity_id or str(uuid4()) + activity.conversation = ConversationAccount( + id=create_conversation_result.id, + tenant_id=conversation_parameters.tenant_id, + ) + activity.channel_data = conversation_parameters.channel_data + activity.recipient = conversation_parameters.bot + + return activity + + def _create_turn_context( + self, + activity: Activity, + claims_identity: ClaimsIdentity, + oauth_scope: str, + connector_client: ConnectorClient, + user_token_client: UserTokenClient, + logic: Callable[[TurnContext], Awaitable], + connector_factory: ConnectorFactory, + ) -> TurnContext: + context = TurnContext(self, activity) + + context.turn_state[self.BOT_IDENTITY_KEY] = claims_identity + context.turn_state[self.BOT_CONNECTOR_CLIENT_KEY] = connector_client + context.turn_state[self.USER_TOKEN_CLIENT_KEY] = user_token_client + + context.turn_state[self.BOT_CALLBACK_HANDLER_KEY] = logic + + context.turn_state[self.CONNECTOR_FACTORY_KEY] = connector_factory + context.turn_state[self.BOT_OAUTH_SCOPE_KEY] = oauth_scope + + return context + + def _process_turn_results(self, context: TurnContext) -> InvokeResponse: + # Handle ExpectedReplies scenarios where all activities have been + # buffered and sent back at once in an invoke response. + if context.activity.delivery_mode == DeliveryModes.expect_replies: + return InvokeResponse( + status=HTTPStatus.OK, + body=ExpectedReplies( + activities=context.buffered_reply_activities + ).serialize(), + ) + + # Handle Invoke scenarios where the bot will return a specific body and return code. + if context.activity.type == ActivityTypes.invoke: + activity_invoke_response: Activity = context.turn_state.get( + self._INVOKE_RESPONSE_KEY + ) + if not activity_invoke_response: + return InvokeResponse(status=HTTPStatus.NOT_IMPLEMENTED) + + return activity_invoke_response.value + + # No body to return + return None diff --git a/libraries/botbuilder-core/botbuilder/core/cloud_channel_service_handler.py b/libraries/botbuilder-core/botbuilder/core/cloud_channel_service_handler.py new file mode 100644 index 000000000..f3769d753 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/cloud_channel_service_handler.py @@ -0,0 +1,18 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botframework.connector.auth import BotFrameworkAuthentication, ClaimsIdentity + +from .channel_service_handler import ChannelServiceHandler + + +class CloudChannelServiceHandler(ChannelServiceHandler): + def __init__( # pylint: disable=super-init-not-called + self, auth: BotFrameworkAuthentication + ): + if not auth: + raise TypeError("Auth can't be None") + self._auth = auth + + async def _authenticate(self, auth_header: str) -> ClaimsIdentity: + return await self._auth.authenticate_channel_request(auth_header) diff --git a/libraries/botbuilder-core/botbuilder/core/component_registration.py b/libraries/botbuilder-core/botbuilder/core/component_registration.py new file mode 100644 index 000000000..03023abbf --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/component_registration.py @@ -0,0 +1,17 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Dict, Iterable, Type + + +class ComponentRegistration: + @staticmethod + def get_components() -> Iterable["ComponentRegistration"]: + return _components.values() + + @staticmethod + def add(component_registration: "ComponentRegistration"): + _components[component_registration.__class__] = component_registration + + +_components: Dict[Type, ComponentRegistration] = {} diff --git a/libraries/botbuilder-core/botbuilder/core/conversation_reference_extension.py b/libraries/botbuilder-core/botbuilder/core/conversation_reference_extension.py index 6dd4172e9..a04ce237d 100644 --- a/libraries/botbuilder-core/botbuilder/core/conversation_reference_extension.py +++ b/libraries/botbuilder-core/botbuilder/core/conversation_reference_extension.py @@ -1,13 +1,18 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. import uuid -from botbuilder.schema import Activity, ActivityTypes, ConversationReference +from botbuilder.schema import ( + Activity, + ActivityEventNames, + ActivityTypes, + ConversationReference, +) def get_continuation_activity(reference: ConversationReference) -> Activity: return Activity( type=ActivityTypes.event, - name="ContinueConversation", + name=ActivityEventNames.continue_conversation, id=str(uuid.uuid1()), channel_id=reference.channel_id, service_url=reference.service_url, diff --git a/libraries/botbuilder-core/botbuilder/core/conversation_state.py b/libraries/botbuilder-core/botbuilder/core/conversation_state.py index 4605700f6..2a8e2fd34 100644 --- a/libraries/botbuilder-core/botbuilder/core/conversation_state.py +++ b/libraries/botbuilder-core/botbuilder/core/conversation_state.py @@ -25,7 +25,7 @@ def __init__(self, storage: Storage): :param storage: The storage containing the conversation state. :type storage: :class:`Storage` """ - super(ConversationState, self).__init__(storage, "ConversationState") + super(ConversationState, self).__init__(storage, "Internal.ConversationState") def get_storage_key(self, turn_context: TurnContext) -> object: """ @@ -58,7 +58,7 @@ def get_storage_key(self, turn_context: TurnContext) -> object: return storage_key def __raise_type_error(self, err: str = "NoneType found while expecting value"): - """ Raise type error exception + """Raise type error exception :raises: :class:`TypeError` """ raise TypeError(err) diff --git a/libraries/botbuilder-core/botbuilder/core/inspection/inspection_middleware.py b/libraries/botbuilder-core/botbuilder/core/inspection/inspection_middleware.py index 02335092a..bf817c1af 100644 --- a/libraries/botbuilder-core/botbuilder/core/inspection/inspection_middleware.py +++ b/libraries/botbuilder-core/botbuilder/core/inspection/inspection_middleware.py @@ -29,7 +29,6 @@ def __init__( # pylint: disable=super-init-not-called conversation_state: ConversationState = None, credentials: MicrosoftAppCredentials = None, ): - self.inspection_state = inspection_state self.inspection_state_accessor = inspection_state.create_property( "InspectionSessionByStatus" @@ -43,13 +42,11 @@ def __init__( # pylint: disable=super-init-not-called async def process_command(self, context: TurnContext) -> Any: if context.activity.type == ActivityTypes.message and context.activity.text: - original_text = context.activity.text TurnContext.remove_recipient_mention(context.activity) command = context.activity.text.strip().split(" ") if len(command) > 1 and command[0] == InspectionMiddleware._COMMAND: - if len(command) == 2 and command[1] == "open": await self._process_open_command(context) return True @@ -98,10 +95,10 @@ async def _trace_state(self, context: TurnContext) -> Any: ) if self.conversation_state: - bot_state[ - "conversation_state" - ] = InspectionMiddleware._get_serialized_context( - self.conversation_state, context + bot_state["conversation_state"] = ( + InspectionMiddleware._get_serialized_context( + self.conversation_state, context + ) ) await self._invoke_send(context, session, from_state(bot_state)) diff --git a/libraries/botbuilder-core/botbuilder/core/inspection/inspection_sessions_by_status.py b/libraries/botbuilder-core/botbuilder/core/inspection/inspection_sessions_by_status.py index f2ef2676f..37131b0e4 100644 --- a/libraries/botbuilder-core/botbuilder/core/inspection/inspection_sessions_by_status.py +++ b/libraries/botbuilder-core/botbuilder/core/inspection/inspection_sessions_by_status.py @@ -13,9 +13,9 @@ def __init__( attached_sessions: Dict[str, ConversationReference] = None, ): self.opened_sessions: Dict[str, ConversationReference] = opened_sessions or {} - self.attached_sessions: Dict[ - str, ConversationReference - ] = attached_sessions or {} + self.attached_sessions: Dict[str, ConversationReference] = ( + attached_sessions or {} + ) DEFAULT_INSPECTION_SESSIONS_BY_STATUS = InspectionSessionsByStatus() diff --git a/libraries/botbuilder-core/botbuilder/core/inspection/trace_activity.py b/libraries/botbuilder-core/botbuilder/core/inspection/trace_activity.py index 307ef64cd..37cb33151 100644 --- a/libraries/botbuilder-core/botbuilder/core/inspection/trace_activity.py +++ b/libraries/botbuilder-core/botbuilder/core/inspection/trace_activity.py @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from datetime import datetime +from datetime import datetime, timezone from typing import Dict, Union from botbuilder.core import BotState @@ -11,7 +11,7 @@ def make_command_activity(command: str) -> Activity: return Activity( type=ActivityTypes.trace, - timestamp=datetime.utcnow(), + timestamp=datetime.now(timezone.utc), name="Command", label="Command", value=command, @@ -22,7 +22,7 @@ def make_command_activity(command: str) -> Activity: def from_activity(activity: Activity, name: str, label: str) -> Activity: return Activity( type=ActivityTypes.trace, - timestamp=datetime.utcnow(), + timestamp=datetime.now(timezone.utc), name=name, label=label, value=activity, @@ -33,7 +33,7 @@ def from_activity(activity: Activity, name: str, label: str) -> Activity: def from_state(bot_state: Union[BotState, Dict]) -> Activity: return Activity( type=ActivityTypes.trace, - timestamp=datetime.utcnow(), + timestamp=datetime.now(timezone.utc), name="Bot State", label="BotState", value=bot_state, diff --git a/libraries/botbuilder-core/botbuilder/core/integration/aiohttp_channel_service.py b/libraries/botbuilder-core/botbuilder/core/integration/aiohttp_channel_service.py index c4e8b3b2f..71447a1c4 100644 --- a/libraries/botbuilder-core/botbuilder/core/integration/aiohttp_channel_service.py +++ b/libraries/botbuilder-core/botbuilder/core/integration/aiohttp_channel_service.py @@ -127,7 +127,8 @@ async def get_conversation(request: Request): @routes.get(base_url + "/v3/conversations/{conversation_id}/members") async def get_conversation_members(request: Request): result = await handler.handle_get_conversation_members( - request.headers.get("Authorization"), request.match_info["conversation_id"], + request.headers.get("Authorization"), + request.match_info["conversation_id"], ) return get_serialized_response(result) @@ -145,7 +146,8 @@ async def get_conversation_member(request: Request): async def get_conversation_paged_members(request: Request): # TODO: continuation token? page size? result = await handler.handle_get_conversation_paged_members( - request.headers.get("Authorization"), request.match_info["conversation_id"], + request.headers.get("Authorization"), + request.match_info["conversation_id"], ) return get_serialized_response(result) diff --git a/libraries/botbuilder-core/botbuilder/core/integration/aiohttp_channel_service_exception_middleware.py b/libraries/botbuilder-core/botbuilder/core/integration/aiohttp_channel_service_exception_middleware.py index 7c5091121..ef87d7489 100644 --- a/libraries/botbuilder-core/botbuilder/core/integration/aiohttp_channel_service_exception_middleware.py +++ b/libraries/botbuilder-core/botbuilder/core/integration/aiohttp_channel_service_exception_middleware.py @@ -1,8 +1,11 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +import traceback + from aiohttp.web import ( middleware, + HTTPException, HTTPNotImplemented, HTTPUnauthorized, HTTPNotFound, @@ -25,5 +28,8 @@ async def aiohttp_error_middleware(request, handler): raise HTTPUnauthorized() except KeyError: raise HTTPNotFound() + except HTTPException: + raise except Exception: + traceback.print_exc() raise HTTPInternalServerError() diff --git a/libraries/botbuilder-core/botbuilder/core/invoke_response.py b/libraries/botbuilder-core/botbuilder/core/invoke_response.py index 7d258559e..fa0b74577 100644 --- a/libraries/botbuilder-core/botbuilder/core/invoke_response.py +++ b/libraries/botbuilder-core/botbuilder/core/invoke_response.py @@ -24,3 +24,11 @@ def __init__(self, status: int = None, body: object = None): """ self.status = status self.body = body + + def is_successful_status_code(self) -> bool: + """ + Gets a value indicating whether the invoke response was successful. + :return: A value that indicates if the HTTP response was successful. true if status is in + the Successful range (200-299); otherwise false. + """ + return 200 <= self.status <= 299 diff --git a/libraries/botbuilder-core/botbuilder/core/memory_storage.py b/libraries/botbuilder-core/botbuilder/core/memory_storage.py index c61b053c7..cc4a04aed 100644 --- a/libraries/botbuilder-core/botbuilder/core/memory_storage.py +++ b/libraries/botbuilder-core/botbuilder/core/memory_storage.py @@ -40,7 +40,7 @@ async def write(self, changes: Dict[str, StoreItem]): return try: # iterate over the changes - for (key, change) in changes.items(): + for key, change in changes.items(): new_value = deepcopy(change) old_state_etag = None @@ -67,7 +67,7 @@ async def write(self, changes: Dict[str, StoreItem]): old_state_etag is not None and new_value_etag is not None and new_value_etag != "*" - and new_value_etag < old_state_etag + and new_value_etag != old_state_etag ): raise KeyError( "Etag conflict.\nOriginal: %s\r\nCurrent: %s" diff --git a/libraries/botbuilder-core/botbuilder/core/memory_transcript_store.py b/libraries/botbuilder-core/botbuilder/core/memory_transcript_store.py index e8953e0ae..325cf32f6 100644 --- a/libraries/botbuilder-core/botbuilder/core/memory_transcript_store.py +++ b/libraries/botbuilder-core/botbuilder/core/memory_transcript_store.py @@ -6,6 +6,7 @@ from botbuilder.schema import Activity from .transcript_logger import PagedResult, TranscriptInfo, TranscriptStore + # pylint: disable=line-too-long class MemoryTranscriptStore(TranscriptStore): """This provider is most useful for simulating production storage when running locally against the @@ -59,7 +60,9 @@ async def get_transcript_activities( [ x for x in sorted( - transcript, key=lambda x: x.timestamp, reverse=False + transcript, + key=lambda x: x.timestamp or str(datetime.datetime.min), + reverse=False, ) if x.timestamp >= start_date ] @@ -72,9 +75,11 @@ async def get_transcript_activities( paged_result.items = [ x for x in sorted( - transcript, key=lambda x: x.timestamp, reverse=False + transcript, + key=lambda x: x.timestamp or datetime.datetime.min, + reverse=False, ) - if x.timestamp >= start_date + if (x.timestamp or datetime.datetime.min) >= start_date ][:20] if paged_result.items.count == 20: paged_result.continuation_token = paged_result.items[-1].id diff --git a/libraries/botbuilder-core/botbuilder/core/middleware_set.py b/libraries/botbuilder-core/botbuilder/core/middleware_set.py index aaa7f03cc..c62873b23 100644 --- a/libraries/botbuilder-core/botbuilder/core/middleware_set.py +++ b/libraries/botbuilder-core/botbuilder/core/middleware_set.py @@ -45,7 +45,7 @@ def use(self, *middleware: Middleware): :param middleware : :return: """ - for (idx, mid) in enumerate(middleware): + for idx, mid in enumerate(middleware): if hasattr(mid, "on_turn") and callable(mid.on_turn): self._middleware.append(mid) return self diff --git a/libraries/botbuilder-core/botbuilder/core/null_telemetry_client.py b/libraries/botbuilder-core/botbuilder/core/null_telemetry_client.py index dc9954385..6cb3e5789 100644 --- a/libraries/botbuilder-core/botbuilder/core/null_telemetry_client.py +++ b/libraries/botbuilder-core/botbuilder/core/null_telemetry_client.py @@ -118,7 +118,7 @@ def track_request( :param url: The actual URL for this request (to show in individual request instances). :param success: True if the request ended in success, False otherwise. :param start_time: the start time of the request. The value should look the same as the one returned \ - by :func:`datetime.isoformat()` (defaults to: None) + by :func:`datetime.isoformat`. (defaults to: None) :param duration: the number of milliseconds that this request lasted. (defaults to: None) :param response_code: the response code that this request returned. (defaults to: None) :param http_method: the HTTP method that triggered this request. (defaults to: None) diff --git a/libraries/botbuilder-core/botbuilder/core/oauth/__init__.py b/libraries/botbuilder-core/botbuilder/core/oauth/__init__.py new file mode 100644 index 000000000..4fd090b48 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/oauth/__init__.py @@ -0,0 +1,12 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .extended_user_token_provider import ExtendedUserTokenProvider +from .user_token_provider import UserTokenProvider +from .connector_client_builder import ConnectorClientBuilder + +__all__ = [ + "ConnectorClientBuilder", + "ExtendedUserTokenProvider", + "UserTokenProvider", +] diff --git a/libraries/botbuilder-core/botbuilder/core/oauth/connector_client_builder.py b/libraries/botbuilder-core/botbuilder/core/oauth/connector_client_builder.py new file mode 100644 index 000000000..e5256040f --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/oauth/connector_client_builder.py @@ -0,0 +1,26 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +from abc import ABC, abstractmethod + +from botframework.connector import ConnectorClient +from botframework.connector.auth import ClaimsIdentity + + +class ConnectorClientBuilder(ABC): + """ + Abstraction to build connector clients. + """ + + @abstractmethod + async def create_connector_client( + self, service_url: str, identity: ClaimsIdentity = None, audience: str = None + ) -> ConnectorClient: + """ + Creates the connector client asynchronous. + + :param service_url: The service URL. + :param identity: The claims claimsIdentity. + :param audience: The target audience for the connector. + :return: ConnectorClient instance + """ + raise NotImplementedError() diff --git a/libraries/botbuilder-core/botbuilder/core/extended_user_token_provider.py b/libraries/botbuilder-core/botbuilder/core/oauth/extended_user_token_provider.py similarity index 99% rename from libraries/botbuilder-core/botbuilder/core/extended_user_token_provider.py rename to libraries/botbuilder-core/botbuilder/core/oauth/extended_user_token_provider.py index f1c8301e8..ad07c3989 100644 --- a/libraries/botbuilder-core/botbuilder/core/extended_user_token_provider.py +++ b/libraries/botbuilder-core/botbuilder/core/oauth/extended_user_token_provider.py @@ -11,7 +11,7 @@ ) from botframework.connector.auth import AppCredentials -from .turn_context import TurnContext +from botbuilder.core.turn_context import TurnContext from .user_token_provider import UserTokenProvider diff --git a/libraries/botbuilder-core/botbuilder/core/user_token_provider.py b/libraries/botbuilder-core/botbuilder/core/oauth/user_token_provider.py similarity index 96% rename from libraries/botbuilder-core/botbuilder/core/user_token_provider.py rename to libraries/botbuilder-core/botbuilder/core/oauth/user_token_provider.py index 735af1e7a..04e92efc2 100644 --- a/libraries/botbuilder-core/botbuilder/core/user_token_provider.py +++ b/libraries/botbuilder-core/botbuilder/core/oauth/user_token_provider.py @@ -1,113 +1,112 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from abc import ABC, abstractmethod -from typing import Dict, List - -from botbuilder.schema import TokenResponse -from botframework.connector.auth import AppCredentials - -from .turn_context import TurnContext - - -class UserTokenProvider(ABC): - @abstractmethod - async def get_user_token( - self, - context: TurnContext, - connection_name: str, - magic_code: str = None, - oauth_app_credentials: AppCredentials = None, - ) -> TokenResponse: - """ - Retrieves the OAuth token for a user that is in a sign-in flow. - :param context: Context for the current turn of conversation with the user. - :param connection_name: Name of the auth connection to use. - :param magic_code: (Optional) Optional user entered code to validate. - :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the - Bots credentials are used. - :return: - """ - raise NotImplementedError() - - @abstractmethod - async def sign_out_user( - self, - context: TurnContext, - connection_name: str = None, - user_id: str = None, - oauth_app_credentials: AppCredentials = None, - ): - """ - Signs the user out with the token server. - :param context: Context for the current turn of conversation with the user. - :param connection_name: Name of the auth connection to use. - :param user_id: User id of user to sign out. - :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the - Bots credentials are used. - :return: - """ - raise NotImplementedError() - - @abstractmethod - async def get_oauth_sign_in_link( - self, - context: TurnContext, - connection_name: str, - final_redirect: str = None, - oauth_app_credentials: AppCredentials = None, - ) -> str: - """ - Get the raw signin link to be sent to the user for signin for a connection name. - :param context: Context for the current turn of conversation with the user. - :param connection_name: Name of the auth connection to use. - :param final_redirect: The final URL that the OAuth flow will redirect to. - :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the - Bots credentials are used. - :return: - """ - raise NotImplementedError() - - @abstractmethod - async def get_token_status( - self, - context: TurnContext, - connection_name: str = None, - user_id: str = None, - include_filter: str = None, - oauth_app_credentials: AppCredentials = None, - ) -> Dict[str, TokenResponse]: - """ - Retrieves Azure Active Directory tokens for particular resources on a configured connection. - :param context: Context for the current turn of conversation with the user. - :param connection_name: Name of the auth connection to use. - :param user_id: The user Id for which token status is retrieved. - :param include_filter: Optional comma separated list of connection's to include. Blank will return token status - for all configured connections. - :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the - Bots credentials are used. - :return: - """ - raise NotImplementedError() - - @abstractmethod - async def get_aad_tokens( - self, - context: TurnContext, - connection_name: str, - resource_urls: List[str], - user_id: str = None, - oauth_app_credentials: AppCredentials = None, - ) -> Dict[str, TokenResponse]: - """ - Retrieves Azure Active Directory tokens for particular resources on a configured connection. - :param context: Context for the current turn of conversation with the user. - :param connection_name: Name of the auth connection to use. - :param resource_urls: The list of resource URLs to retrieve tokens for. - :param user_id: The user Id for which tokens are retrieved. If passing in None the userId is taken - from the Activity in the TurnContext. - :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the - Bots credentials are used. - :return: - """ - raise NotImplementedError() +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod +from typing import Dict, List + +from botbuilder.core.turn_context import TurnContext +from botbuilder.schema import TokenResponse +from botframework.connector.auth import AppCredentials + + +class UserTokenProvider(ABC): + @abstractmethod + async def get_user_token( + self, + context: TurnContext, + connection_name: str, + magic_code: str = None, + oauth_app_credentials: AppCredentials = None, + ) -> TokenResponse: + """ + Retrieves the OAuth token for a user that is in a sign-in flow. + :param context: Context for the current turn of conversation with the user. + :param connection_name: Name of the auth connection to use. + :param magic_code: (Optional) Optional user entered code to validate. + :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the + Bots credentials are used. + :return: + """ + raise NotImplementedError() + + @abstractmethod + async def sign_out_user( + self, + context: TurnContext, + connection_name: str = None, + user_id: str = None, + oauth_app_credentials: AppCredentials = None, + ): + """ + Signs the user out with the token server. + :param context: Context for the current turn of conversation with the user. + :param connection_name: Name of the auth connection to use. + :param user_id: User id of user to sign out. + :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the + Bots credentials are used. + :return: + """ + raise NotImplementedError() + + @abstractmethod + async def get_oauth_sign_in_link( + self, + context: TurnContext, + connection_name: str, + final_redirect: str = None, + oauth_app_credentials: AppCredentials = None, + ) -> str: + """ + Get the raw signin link to be sent to the user for signin for a connection name. + :param context: Context for the current turn of conversation with the user. + :param connection_name: Name of the auth connection to use. + :param final_redirect: The final URL that the OAuth flow will redirect to. + :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the + Bots credentials are used. + :return: + """ + raise NotImplementedError() + + @abstractmethod + async def get_token_status( + self, + context: TurnContext, + connection_name: str = None, + user_id: str = None, + include_filter: str = None, + oauth_app_credentials: AppCredentials = None, + ) -> Dict[str, TokenResponse]: + """ + Retrieves Azure Active Directory tokens for particular resources on a configured connection. + :param context: Context for the current turn of conversation with the user. + :param connection_name: Name of the auth connection to use. + :param user_id: The user Id for which token status is retrieved. + :param include_filter: Optional comma separated list of connection's to include. Blank will return token status + for all configured connections. + :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the + Bots credentials are used. + :return: + """ + raise NotImplementedError() + + @abstractmethod + async def get_aad_tokens( + self, + context: TurnContext, + connection_name: str, + resource_urls: List[str], + user_id: str = None, + oauth_app_credentials: AppCredentials = None, + ) -> Dict[str, TokenResponse]: + """ + Retrieves Azure Active Directory tokens for particular resources on a configured connection. + :param context: Context for the current turn of conversation with the user. + :param connection_name: Name of the auth connection to use. + :param resource_urls: The list of resource URLs to retrieve tokens for. + :param user_id: The user Id for which tokens are retrieved. If passing in None the userId is taken + from the Activity in the TurnContext. + :param oauth_app_credentials: (Optional) AppCredentials for OAuth. If None is supplied, the + Bots credentials are used. + :return: + """ + raise NotImplementedError() diff --git a/libraries/botbuilder-core/botbuilder/core/queue_storage.py b/libraries/botbuilder-core/botbuilder/core/queue_storage.py new file mode 100644 index 000000000..dafc37edd --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/queue_storage.py @@ -0,0 +1,34 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod +from botbuilder.schema import Activity + + +class QueueStorage(ABC): + """ + A base class for enqueueing an Activity for later processing. + """ + + @abstractmethod + async def queue_activity( + self, + activity: Activity, + visibility_timeout: int = None, + time_to_live: int = None, + ) -> str: + """ + Enqueues an Activity for later processing. The visibility timeout specifies how long the message should be + visible to Dequeue and Peek operations. + + :param activity: The activity to be queued for later processing. + :type activity: :class:`botbuilder.schema.Activity` + :param visibility_timeout: Visibility timeout in seconds. Optional with a default value of 0. + Cannot be larger than 7 days. + :type visibility_timeout: int + :param time_to_live: Specifies the time-to-live interval for the message in seconds. + :type time_to_live: int + + :returns: String representing the read receipt. + """ + raise NotImplementedError() diff --git a/libraries/botbuilder-core/botbuilder/core/register_class_middleware.py b/libraries/botbuilder-core/botbuilder/core/register_class_middleware.py new file mode 100644 index 000000000..38be1f46b --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/register_class_middleware.py @@ -0,0 +1,32 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +from typing import Callable, Awaitable + +from botbuilder.core import Middleware, TurnContext + + +class RegisterClassMiddleware(Middleware): + """ + Middleware for adding an object to or registering a service with the current turn context. + """ + + def __init__(self, service, key: str = None): + self.service = service + self._key = key + + async def on_turn( + self, context: TurnContext, logic: Callable[[TurnContext], Awaitable] + ): + # C# has TurnStateCollection with has overrides for adding items + # to TurnState. Python does not. In C#'s case, there is an 'Add' + # to handle adding object, and that uses the fully qualified class name. + key = self._key or self.fullname(self.service) + context.turn_state[key] = self.service + await logic() + + @staticmethod + def fullname(obj): + module = obj.__class__.__module__ + if module is None or module == str.__class__.__module__: + return obj.__class__.__name__ # Avoid reporting __builtin__ + return module + "." + obj.__class__.__name__ diff --git a/libraries/botbuilder-core/botbuilder/core/serializer_helper.py b/libraries/botbuilder-core/botbuilder/core/serializer_helper.py index 766cd6291..3716913f5 100644 --- a/libraries/botbuilder-core/botbuilder/core/serializer_helper.py +++ b/libraries/botbuilder-core/botbuilder/core/serializer_helper.py @@ -1,6 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. - +from copy import copy from inspect import getmembers from typing import Type from enum import Enum @@ -25,6 +25,9 @@ def deserializer_helper(msrest_cls: Type[Model], dict_to_deserialize: dict) -> Model: deserializer = Deserializer(DEPENDICIES_DICT) + _clean_data_for_serialization( + deserializer.dependencies[msrest_cls.__name__], dict_to_deserialize + ) return deserializer(msrest_cls.__name__, dict_to_deserialize) @@ -35,3 +38,21 @@ def serializer_helper(object_to_serialize: Model) -> dict: serializer = Serializer(DEPENDICIES_DICT) # pylint: disable=protected-access return serializer._serialize(object_to_serialize) + + +def _clean_data_for_serialization(msrest_cls: Type[Model], dict_to_deserialize: dict): + # pylint: disable=protected-access + # Clean channel response of empty strings for expected objects. + if not isinstance(dict_to_deserialize, dict): + return + serialization_model = copy(msrest_cls._attribute_map) + for key, value in msrest_cls._attribute_map.items(): + if key != value["key"]: + serialization_model[value["key"]] = value + for prop, prop_value in dict_to_deserialize.items(): + if ( + prop in serialization_model + and serialization_model[prop]["type"] in DEPENDICIES_DICT + and not prop_value + ): + dict_to_deserialize[prop] = None diff --git a/libraries/botbuilder-core/botbuilder/core/show_typing_middleware.py b/libraries/botbuilder-core/botbuilder/core/show_typing_middleware.py index a659cd8bf..557fd56a2 100644 --- a/libraries/botbuilder-core/botbuilder/core/show_typing_middleware.py +++ b/libraries/botbuilder-core/botbuilder/core/show_typing_middleware.py @@ -4,7 +4,9 @@ from typing import Awaitable, Callable from botbuilder.schema import Activity, ActivityTypes +from botframework.connector.auth import ClaimsIdentity, SkillValidation +from .bot_adapter import BotAdapter from .middleware_set import Middleware from .turn_context import TurnContext @@ -58,7 +60,8 @@ async def on_turn( def start_interval(context: TurnContext, delay, period): async def aux(): typing_activity = Activity( - type=ActivityTypes.typing, relates_to=context.activity.relates_to, + type=ActivityTypes.typing, + relates_to=context.activity.relates_to, ) conversation_reference = TurnContext.get_conversation_reference( @@ -82,9 +85,12 @@ async def aux(): def stop_interval(): timer.set_clear_timer() - # if it's a message, start sending typing activities until the - # bot logic is done. - if context.activity.type == ActivityTypes.message: + # Start a timer to periodically send the typing activity + # (bots running as skills should not send typing activity) + if ( + context.activity.type == ActivityTypes.message + and not ShowTypingMiddleware._is_skill_bot(context) + ): start_interval(context, self._delay, self._period) # call the bot logic @@ -93,3 +99,10 @@ def stop_interval(): stop_interval() return result + + @staticmethod + def _is_skill_bot(context: TurnContext) -> bool: + claims_identity = context.turn_state.get(BotAdapter.BOT_IDENTITY_KEY) + return isinstance( + claims_identity, ClaimsIdentity + ) and SkillValidation.is_skill_claim(claims_identity.claims) diff --git a/libraries/botbuilder-core/botbuilder/core/skills/__init__.py b/libraries/botbuilder-core/botbuilder/core/skills/__init__.py index ce949b12a..b922a692d 100644 --- a/libraries/botbuilder-core/botbuilder/core/skills/__init__.py +++ b/libraries/botbuilder-core/botbuilder/core/skills/__init__.py @@ -9,6 +9,7 @@ from .bot_framework_client import BotFrameworkClient from .conversation_id_factory import ConversationIdFactoryBase from .skill_handler import SkillHandler +from .skill_conversation_id_factory import SkillConversationIdFactory from .skill_conversation_id_factory_options import SkillConversationIdFactoryOptions from .skill_conversation_reference import SkillConversationReference @@ -16,6 +17,7 @@ "BotFrameworkSkill", "BotFrameworkClient", "ConversationIdFactoryBase", + "SkillConversationIdFactory", "SkillConversationIdFactoryOptions", "SkillConversationReference", "SkillHandler", diff --git a/libraries/botbuilder-core/botbuilder/core/skills/_skill_handler_impl.py b/libraries/botbuilder-core/botbuilder/core/skills/_skill_handler_impl.py new file mode 100644 index 000000000..f50cd54ff --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/skills/_skill_handler_impl.py @@ -0,0 +1,316 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import uuid4 +from logging import Logger +from typing import Callable + +from botbuilder.core import Bot, BotAdapter, TurnContext +from botbuilder.schema import ( + Activity, + ActivityTypes, + ResourceResponse, + CallerIdConstants, +) +from botframework.connector.auth import ( + ClaimsIdentity, + JwtTokenValidation, +) +from .skill_conversation_reference import SkillConversationReference +from .conversation_id_factory import ConversationIdFactoryBase + +from .skill_handler import SkillHandler + + +class _SkillHandlerImpl(SkillHandler): + def __init__( # pylint: disable=super-init-not-called + self, + skill_conversation_reference_key: str, + adapter: BotAdapter, + bot: Bot, + conversation_id_factory: ConversationIdFactoryBase, + get_oauth_scope: Callable[[], str], + logger: Logger = None, + ): + if not skill_conversation_reference_key: + raise TypeError("skill_conversation_reference_key can't be None") + if not adapter: + raise TypeError("adapter can't be None") + if not bot: + raise TypeError("bot can't be None") + if not conversation_id_factory: + raise TypeError("conversation_id_factory can't be None") + + self._skill_conversation_reference_key = skill_conversation_reference_key + self._adapter = adapter + self._bot = bot + self._conversation_id_factory = conversation_id_factory + self._get_oauth_scope = get_oauth_scope or (lambda: "") + self._logger = logger + + async def on_send_to_conversation( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity: Activity, + ) -> ResourceResponse: + """ + send_to_conversation() API for Skill + + This method allows you to send an activity to the end of a conversation. + + This is slightly different from ReplyToActivity(). + * SendToConversation(conversation_id) - will append the activity to the end + of the conversation according to the timestamp or semantics of the channel. + * ReplyToActivity(conversation_id,ActivityId) - adds the activity as a reply + to another activity, if the channel supports it. If the channel does not + support nested replies, ReplyToActivity falls back to SendToConversation. + + Use ReplyToActivity when replying to a specific activity in the + conversation. + + Use SendToConversation in all other cases. + :param claims_identity: Claims identity for the bot. + :type claims_identity: :class:`botframework.connector.auth.ClaimsIdentity` + :param conversation_id:The conversation ID. + :type conversation_id: str + :param activity: Activity to send. + :type activity: Activity + :return: + """ + return await self._process_activity( + claims_identity, + conversation_id, + None, + activity, + ) + + async def on_reply_to_activity( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, + activity: Activity, + ) -> ResourceResponse: + """ + reply_to_activity() API for Skill. + + This method allows you to reply to an activity. + + This is slightly different from SendToConversation(). + * SendToConversation(conversation_id) - will append the activity to the end + of the conversation according to the timestamp or semantics of the channel. + * ReplyToActivity(conversation_id,ActivityId) - adds the activity as a reply + to another activity, if the channel supports it. If the channel does not + support nested replies, ReplyToActivity falls back to SendToConversation. + + Use ReplyToActivity when replying to a specific activity in the + conversation. + + Use SendToConversation in all other cases. + :param claims_identity: Claims identity for the bot. + :type claims_identity: :class:`botframework.connector.auth.ClaimsIdentity` + :param conversation_id:The conversation ID. + :type conversation_id: str + :param activity_id: Activity ID to send. + :type activity_id: str + :param activity: Activity to send. + :type activity: Activity + :return: + """ + return await self._process_activity( + claims_identity, + conversation_id, + activity_id, + activity, + ) + + async def on_delete_activity( + self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str + ): + skill_conversation_reference = await self._get_skill_conversation_reference( + conversation_id + ) + + async def callback(turn_context: TurnContext): + turn_context.turn_state[self.SKILL_CONVERSATION_REFERENCE_KEY] = ( + skill_conversation_reference + ) + await turn_context.delete_activity(activity_id) + + await self._adapter.continue_conversation( + skill_conversation_reference.conversation_reference, + callback, + claims_identity=claims_identity, + audience=skill_conversation_reference.oauth_scope, + ) + + async def on_update_activity( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, + activity: Activity, + ) -> ResourceResponse: + skill_conversation_reference = await self._get_skill_conversation_reference( + conversation_id + ) + + resource_response: ResourceResponse = None + + async def callback(turn_context: TurnContext): + nonlocal resource_response + turn_context.turn_state[self.SKILL_CONVERSATION_REFERENCE_KEY] = ( + skill_conversation_reference + ) + activity.apply_conversation_reference( + skill_conversation_reference.conversation_reference + ) + turn_context.activity.id = activity_id + turn_context.activity.caller_id = ( + f"{CallerIdConstants.bot_to_bot_prefix}" + f"{JwtTokenValidation.get_app_id_from_claims(claims_identity.claims)}" + ) + resource_response = await turn_context.update_activity(activity) + + await self._adapter.continue_conversation( + skill_conversation_reference.conversation_reference, + callback, + claims_identity=claims_identity, + audience=skill_conversation_reference.oauth_scope, + ) + + return resource_response or ResourceResponse(id=str(uuid4()).replace("-", "")) + + @staticmethod + def _apply_skill_activity_to_turn_context_activity( + context: TurnContext, activity: Activity + ): + context.activity.type = activity.type + context.activity.text = activity.text + context.activity.code = activity.code + context.activity.name = activity.name + context.activity.relates_to = activity.relates_to + + context.activity.reply_to_id = activity.reply_to_id + context.activity.value = activity.value + context.activity.entities = activity.entities + context.activity.locale = activity.locale + context.activity.local_timestamp = activity.local_timestamp + context.activity.timestamp = activity.timestamp + context.activity.channel_data = activity.channel_data + context.activity.additional_properties = activity.additional_properties + + async def _process_activity( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + reply_to_activity_id: str, + activity: Activity, + ) -> ResourceResponse: + skill_conversation_reference = await self._get_skill_conversation_reference( + conversation_id + ) + + # If an activity is sent, return the ResourceResponse + resource_response: ResourceResponse = None + + async def callback(context: TurnContext): + nonlocal resource_response + context.turn_state[SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY] = ( + skill_conversation_reference + ) + + TurnContext.apply_conversation_reference( + activity, skill_conversation_reference.conversation_reference + ) + + context.activity.id = reply_to_activity_id + + app_id = JwtTokenValidation.get_app_id_from_claims(claims_identity.claims) + context.activity.caller_id = ( + f"{CallerIdConstants.bot_to_bot_prefix}{app_id}" + ) + + if activity.type == ActivityTypes.end_of_conversation: + await self._conversation_id_factory.delete_conversation_reference( + conversation_id + ) + await self._send_to_bot(activity, context) + elif activity.type == ActivityTypes.event: + await self._send_to_bot(activity, context) + elif activity.type in (ActivityTypes.command, ActivityTypes.command_result): + if activity.name.startswith("application/"): + # Send to channel and capture the resource response for the SendActivityCall so we can return it. + resource_response = await context.send_activity(activity) + else: + await self._send_to_bot(activity, context) + else: + # Capture the resource response for the SendActivityCall so we can return it. + resource_response = await context.send_activity(activity) + + await self._adapter.continue_conversation( + skill_conversation_reference.conversation_reference, + callback, + claims_identity=claims_identity, + audience=skill_conversation_reference.oauth_scope, + ) + + if not resource_response: + resource_response = ResourceResponse(id=str(uuid4())) + + return resource_response + + async def _get_skill_conversation_reference( + self, conversation_id: str + ) -> SkillConversationReference: + # Get the SkillsConversationReference + try: + skill_conversation_reference = ( + await self._conversation_id_factory.get_skill_conversation_reference( + conversation_id + ) + ) + except (NotImplementedError, AttributeError): + if self._logger: + self._logger.log( + 30, + "Got NotImplementedError when trying to call get_skill_conversation_reference() " + "on the SkillConversationIdFactory, attempting to use deprecated " + "get_conversation_reference() method instead.", + ) + + # ConversationIdFactory can return either a SkillConversationReference (the newer way), + # or a ConversationReference (the old way, but still here for compatibility). If a + # ConversationReference is returned, build a new SkillConversationReference to simplify + # the remainder of this method. + conversation_reference_result = ( + await self._conversation_id_factory.get_conversation_reference( + conversation_id + ) + ) + if isinstance(conversation_reference_result, SkillConversationReference): + skill_conversation_reference: SkillConversationReference = ( + conversation_reference_result + ) + else: + skill_conversation_reference: SkillConversationReference = ( + SkillConversationReference( + conversation_reference=conversation_reference_result, + oauth_scope=self._get_oauth_scope(), + ) + ) + + if not skill_conversation_reference: + raise KeyError("SkillConversationReference not found") + + if not skill_conversation_reference.conversation_reference: + raise KeyError("conversationReference not found") + + return skill_conversation_reference + + async def _send_to_bot(self, activity: Activity, context: TurnContext): + _SkillHandlerImpl._apply_skill_activity_to_turn_context_activity( + context, activity + ) + await self._bot.on_turn(context) diff --git a/libraries/botbuilder-core/botbuilder/core/skills/cloud_skill_handler.py b/libraries/botbuilder-core/botbuilder/core/skills/cloud_skill_handler.py new file mode 100644 index 000000000..71572fada --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/skills/cloud_skill_handler.py @@ -0,0 +1,135 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger +from botbuilder.core import BotAdapter, Bot, CloudChannelServiceHandler +from botbuilder.schema import Activity, ResourceResponse +from botframework.connector.auth import BotFrameworkAuthentication, ClaimsIdentity + +from .conversation_id_factory import ConversationIdFactoryBase +from .skill_handler import SkillHandler +from ._skill_handler_impl import _SkillHandlerImpl + + +class CloudSkillHandler(CloudChannelServiceHandler): + SKILL_CONVERSATION_REFERENCE_KEY = SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY + + def __init__( + self, + adapter: BotAdapter, + bot: Bot, + conversation_id_factory: ConversationIdFactoryBase, + auth: BotFrameworkAuthentication, + logger: Logger = None, + ): + super().__init__(auth) + + if not adapter: + raise TypeError("adapter can't be None") + if not bot: + raise TypeError("bot can't be None") + if not conversation_id_factory: + raise TypeError("conversation_id_factory can't be None") + + self._inner = _SkillHandlerImpl( + self.SKILL_CONVERSATION_REFERENCE_KEY, + adapter, + bot, + conversation_id_factory, + auth.get_originating_audience, + logger, + ) + + async def on_send_to_conversation( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity: Activity, + ) -> ResourceResponse: + """ + send_to_conversation() API for Skill + + This method allows you to send an activity to the end of a conversation. + + This is slightly different from ReplyToActivity(). + * SendToConversation(conversation_id) - will append the activity to the end + of the conversation according to the timestamp or semantics of the channel. + * ReplyToActivity(conversation_id,ActivityId) - adds the activity as a reply + to another activity, if the channel supports it. If the channel does not + support nested replies, ReplyToActivity falls back to SendToConversation. + + Use ReplyToActivity when replying to a specific activity in the + conversation. + + Use SendToConversation in all other cases. + :param claims_identity: Claims identity for the bot. + :type claims_identity: :class:`botframework.connector.auth.ClaimsIdentity` + :param conversation_id:The conversation ID. + :type conversation_id: str + :param activity: Activity to send. + :type activity: Activity + :return: + """ + return await self._inner.on_send_to_conversation( + claims_identity, + conversation_id, + activity, + ) + + async def on_reply_to_activity( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, + activity: Activity, + ) -> ResourceResponse: + """ + reply_to_activity() API for Skill. + + This method allows you to reply to an activity. + + This is slightly different from SendToConversation(). + * SendToConversation(conversation_id) - will append the activity to the end + of the conversation according to the timestamp or semantics of the channel. + * ReplyToActivity(conversation_id,ActivityId) - adds the activity as a reply + to another activity, if the channel supports it. If the channel does not + support nested replies, ReplyToActivity falls back to SendToConversation. + + Use ReplyToActivity when replying to a specific activity in the + conversation. + + Use SendToConversation in all other cases. + :param claims_identity: Claims identity for the bot. + :type claims_identity: :class:`botframework.connector.auth.ClaimsIdentity` + :param conversation_id:The conversation ID. + :type conversation_id: str + :param activity_id: Activity ID to send. + :type activity_id: str + :param activity: Activity to send. + :type activity: Activity + :return: + """ + return await self._inner.on_reply_to_activity( + claims_identity, + conversation_id, + activity_id, + activity, + ) + + async def on_delete_activity( + self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str + ): + await self._inner.on_delete_activity( + claims_identity, conversation_id, activity_id + ) + + async def on_update_activity( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, + activity: Activity, + ) -> ResourceResponse: + return await self._inner.on_update_activity( + claims_identity, conversation_id, activity_id, activity + ) diff --git a/libraries/botbuilder-core/botbuilder/core/skills/conversation_id_factory.py b/libraries/botbuilder-core/botbuilder/core/skills/conversation_id_factory.py index 35b1d8b6a..5cb986bfb 100644 --- a/libraries/botbuilder-core/botbuilder/core/skills/conversation_id_factory.py +++ b/libraries/botbuilder-core/botbuilder/core/skills/conversation_id_factory.py @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from abc import ABC, abstractmethod +from abc import ABC from typing import Union from botbuilder.schema import ConversationReference from .skill_conversation_id_factory_options import SkillConversationIdFactoryOptions @@ -17,7 +17,6 @@ class ConversationIdFactoryBase(ABC): SkillConversationReferences and deletion. """ - @abstractmethod async def create_skill_conversation_id( self, options_or_conversation_reference: Union[ @@ -25,40 +24,48 @@ async def create_skill_conversation_id( ], ) -> str: """ - Using the options passed in, creates a conversation id and - SkillConversationReference, storing them for future use. + Using the options passed in, creates a conversation id and :class:`SkillConversationReference`, + storing them for future use. - :param options_or_conversation_reference: The options contain properties useful - for generating a SkillConversationReference and conversation id. - :type options_or_conversation_reference: :class: - `Union[SkillConversationIdFactoryOptions, ConversationReference]` + :param options_or_conversation_reference: The options contain properties useful for generating a + :class:`SkillConversationReference` and conversation id. + :type options_or_conversation_reference: + :class:`Union[SkillConversationIdFactoryOptions, ConversationReference]` :returns: A skill conversation id. .. note:: - SkillConversationIdFactoryOptions is the preferred parameter type, while ConversationReference - type is provided for backwards compatability. + :class:`SkillConversationIdFactoryOptions` is the preferred parameter type, while the + :class:`SkillConversationReference` type is provided for backwards compatability. """ raise NotImplementedError() - @abstractmethod async def get_conversation_reference( self, skill_conversation_id: str - ) -> Union[SkillConversationReference, ConversationReference]: + ) -> ConversationReference: """ - Retrieves a SkillConversationReference using a conversation id passed in. + [DEPRECATED] Method is deprecated, please use get_skill_conversation_reference() instead. - :param skill_conversation_id: The conversation id for which to retrieve - the SkillConversationReference. + Retrieves a :class:`ConversationReference` using a conversation id passed in. + + :param skill_conversation_id: The conversation id for which to retrieve the :class:`ConversationReference`. :type skill_conversation_id: str + :returns: `ConversationReference` for the specified ID. + """ + raise NotImplementedError() - .. note:: - SkillConversationReference is the preferred return type, while ConversationReference - type is provided for backwards compatability. + async def get_skill_conversation_reference( + self, skill_conversation_id: str + ) -> SkillConversationReference: + """ + Retrieves a :class:`SkillConversationReference` using a conversation id passed in. + + :param skill_conversation_id: The conversation id for which to retrieve the :class:`SkillConversationReference`. + :type skill_conversation_id: str + :returns: `SkillConversationReference` for the specified ID. """ raise NotImplementedError() - @abstractmethod async def delete_conversation_reference(self, skill_conversation_id: str): """ Removes any reference to objects keyed on the conversation id passed in. diff --git a/libraries/botbuilder-core/botbuilder/core/skills/skill_conversation_id_factory.py b/libraries/botbuilder-core/botbuilder/core/skills/skill_conversation_id_factory.py new file mode 100644 index 000000000..6cee0d0bc --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/skills/skill_conversation_id_factory.py @@ -0,0 +1,86 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import uuid4 as uuid +from botbuilder.core import TurnContext, Storage +from .conversation_id_factory import ConversationIdFactoryBase +from .skill_conversation_id_factory_options import SkillConversationIdFactoryOptions +from .skill_conversation_reference import SkillConversationReference +from .skill_conversation_reference import ConversationReference + + +class SkillConversationIdFactory(ConversationIdFactoryBase): + def __init__(self, storage: Storage): + if not storage: + raise TypeError("storage can't be None") + + self._storage = storage + + async def create_skill_conversation_id( # pylint: disable=arguments-differ + self, options: SkillConversationIdFactoryOptions + ) -> str: + """ + Creates a new `SkillConversationReference`. + + :param options: Creation options to use when creating the `SkillConversationReference`. + :type options: :class:`botbuilder.core.skills.SkillConversationIdFactoryOptions` + :return: ID of the created `SkillConversationReference`. + """ + + if not options: + raise TypeError("options can't be None") + + conversation_reference = TurnContext.get_conversation_reference( + options.activity + ) + + skill_conversation_id = str(uuid()) + + # Create the SkillConversationReference instance. + skill_conversation_reference = SkillConversationReference( + conversation_reference=conversation_reference, + oauth_scope=options.from_bot_oauth_scope, + ) + + # Store the SkillConversationReference using the skill_conversation_id as a key. + skill_conversation_info = {skill_conversation_id: skill_conversation_reference} + + await self._storage.write(skill_conversation_info) + + # Return the generated skill_conversation_id (that will be also used as the conversation ID to call the skill). + return skill_conversation_id + + async def get_conversation_reference( + self, skill_conversation_id: str + ) -> ConversationReference: + return await super().get_conversation_reference(skill_conversation_id) + + async def get_skill_conversation_reference( + self, skill_conversation_id: str + ) -> SkillConversationReference: + """ + Retrieve a `SkillConversationReference` with the specified ID. + + :param skill_conversation_id: The ID of the `SkillConversationReference` to retrieve. + :type skill_conversation_id: str + :return: `SkillConversationReference` for the specified ID; None if not found. + """ + + if not skill_conversation_id: + raise TypeError("skill_conversation_id can't be None") + + # Get the SkillConversationReference from storage for the given skill_conversation_id. + skill_conversation_reference = await self._storage.read([skill_conversation_id]) + + return skill_conversation_reference.get(skill_conversation_id) + + async def delete_conversation_reference(self, skill_conversation_id: str): + """ + Deletes the `SkillConversationReference` with the specified ID. + + :param skill_conversation_id: The ID of the `SkillConversationReference` to be deleted. + :type skill_conversation_id: str + """ + + # Delete the SkillConversationReference from storage. + await self._storage.delete([skill_conversation_id]) diff --git a/libraries/botbuilder-core/botbuilder/core/skills/skill_handler.py b/libraries/botbuilder-core/botbuilder/core/skills/skill_handler.py index 0aabdc9f3..8ea67e186 100644 --- a/libraries/botbuilder-core/botbuilder/core/skills/skill_handler.py +++ b/libraries/botbuilder-core/botbuilder/core/skills/skill_handler.py @@ -1,13 +1,11 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from uuid import uuid4 +from logging import Logger -from botbuilder.core import Bot, BotAdapter, ChannelServiceHandler, TurnContext +from botbuilder.core import Bot, BotAdapter, ChannelServiceHandler from botbuilder.schema import ( Activity, - ActivityTypes, - ConversationReference, ResourceResponse, ) from botframework.connector.auth import ( @@ -18,12 +16,10 @@ CredentialProvider, GovernmentConstants, ) -from .skill_conversation_reference import SkillConversationReference from .conversation_id_factory import ConversationIdFactoryBase class SkillHandler(ChannelServiceHandler): - SKILL_CONVERSATION_REFERENCE_KEY = ( "botbuilder.core.skills.SkillConversationReference" ) @@ -36,8 +32,9 @@ def __init__( credential_provider: CredentialProvider, auth_configuration: AuthenticationConfiguration, channel_provider: ChannelProvider = None, - logger: object = None, + logger: Logger = None, ): + # pylint: disable=import-outside-toplevel super().__init__(credential_provider, auth_configuration, channel_provider) if not adapter: @@ -47,13 +44,31 @@ def __init__( if not conversation_id_factory: raise TypeError("conversation_id_factory can't be None") - self._adapter = adapter - self._bot = bot - self._conversation_id_factory = conversation_id_factory self._logger = logger + def aux_func(): + nonlocal self + return ( + GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + if self._channel_provider and self._channel_provider.is_government() + else AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + ) + + from ._skill_handler_impl import _SkillHandlerImpl + + self._inner = _SkillHandlerImpl( + self.SKILL_CONVERSATION_REFERENCE_KEY, + adapter, + bot, + conversation_id_factory, + aux_func, + ) + async def on_send_to_conversation( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity: Activity, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity: Activity, ) -> ResourceResponse: """ send_to_conversation() API for Skill @@ -61,9 +76,9 @@ async def on_send_to_conversation( This method allows you to send an activity to the end of a conversation. This is slightly different from ReplyToActivity(). - * SendToConversation(conversationId) - will append the activity to the end + * SendToConversation(conversation_id) - will append the activity to the end of the conversation according to the timestamp or semantics of the channel. - * ReplyToActivity(conversationId,ActivityId) - adds the activity as a reply + * ReplyToActivity(conversation_id,ActivityId) - adds the activity as a reply to another activity, if the channel supports it. If the channel does not support nested replies, ReplyToActivity falls back to SendToConversation. @@ -71,13 +86,18 @@ async def on_send_to_conversation( conversation. Use SendToConversation in all other cases. - :param claims_identity: - :param conversation_id: - :param activity: + :param claims_identity: Claims identity for the bot. + :type claims_identity: :class:`botframework.connector.auth.ClaimsIdentity` + :param conversation_id:The conversation ID. + :type conversation_id: str + :param activity: Activity to send. + :type activity: Activity :return: """ - return await self._process_activity( - claims_identity, conversation_id, None, activity, + return await self._inner.on_send_to_conversation( + claims_identity, + conversation_id, + activity, ) async def on_reply_to_activity( @@ -93,9 +113,9 @@ async def on_reply_to_activity( This method allows you to reply to an activity. This is slightly different from SendToConversation(). - * SendToConversation(conversationId) - will append the activity to the end + * SendToConversation(conversation_id) - will append the activity to the end of the conversation according to the timestamp or semantics of the channel. - * ReplyToActivity(conversationId,ActivityId) - adds the activity as a reply + * ReplyToActivity(conversation_id,ActivityId) - adds the activity as a reply to another activity, if the channel supports it. If the channel does not support nested replies, ReplyToActivity falls back to SendToConversation. @@ -103,112 +123,37 @@ async def on_reply_to_activity( conversation. Use SendToConversation in all other cases. - :param claims_identity: - :param conversation_id: - :param activity_id: - :param activity: + :param claims_identity: Claims identity for the bot. + :type claims_identity: :class:`botframework.connector.auth.ClaimsIdentity` + :param conversation_id:The conversation ID. + :type conversation_id: str + :param activity_id: Activity ID to send. + :type activity_id: str + :param activity: Activity to send. + :type activity: Activity :return: """ - return await self._process_activity( - claims_identity, conversation_id, activity_id, activity, + return await self._inner.on_reply_to_activity( + claims_identity, + conversation_id, + activity_id, + activity, ) - async def _process_activity( + async def on_delete_activity( + self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str + ): + await self._inner.on_delete_activity( + claims_identity, conversation_id, activity_id + ) + + async def on_update_activity( self, claims_identity: ClaimsIdentity, conversation_id: str, - reply_to_activity_id: str, + activity_id: str, activity: Activity, ) -> ResourceResponse: - conversation_reference_result = await self._conversation_id_factory.get_conversation_reference( - conversation_id + return await self._inner.on_update_activity( + claims_identity, conversation_id, activity_id, activity ) - - oauth_scope = None - conversation_reference = None - if isinstance(conversation_reference_result, SkillConversationReference): - oauth_scope = conversation_reference_result.oauth_scope - conversation_reference = ( - conversation_reference_result.conversation_reference - ) - else: - conversation_reference = conversation_reference_result - oauth_scope = ( - GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE - if self._channel_provider and self._channel_provider.is_government() - else AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE - ) - - if not conversation_reference: - raise KeyError("ConversationReference not found") - - activity_conversation_reference = ConversationReference( - activity_id=activity.id, - user=activity.from_property, - bot=activity.recipient, - conversation=activity.conversation, - channel_id=activity.channel_id, - service_url=activity.service_url, - ) - - async def callback(context: TurnContext): - context.turn_state[ - SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY - ] = activity_conversation_reference - TurnContext.apply_conversation_reference(activity, conversation_reference) - context.activity.id = reply_to_activity_id - - if activity.type == ActivityTypes.end_of_conversation: - await self._conversation_id_factory.delete_conversation_reference( - conversation_id - ) - self._apply_eoc_to_turn_context_activity(context, activity) - await self._bot.on_turn(context) - elif activity.type == ActivityTypes.event: - self._apply_event_to_turn_context_activity(context, activity) - await self._bot.on_turn(context) - else: - await context.send_activity(activity) - - await self._adapter.continue_conversation( - conversation_reference, - callback, - claims_identity=claims_identity, - audience=oauth_scope, - ) - return ResourceResponse(id=str(uuid4())) - - @staticmethod - def _apply_eoc_to_turn_context_activity( - context: TurnContext, end_of_conversation_activity: Activity - ): - context.activity.type = end_of_conversation_activity.type - context.activity.text = end_of_conversation_activity.text - context.activity.code = end_of_conversation_activity.code - - context.activity.reply_to_id = end_of_conversation_activity.reply_to_id - context.activity.value = end_of_conversation_activity.value - context.activity.entities = end_of_conversation_activity.entities - context.activity.local_timestamp = end_of_conversation_activity.local_timestamp - context.activity.timestamp = end_of_conversation_activity.timestamp - context.activity.channel_data = end_of_conversation_activity.channel_data - context.activity.additional_properties = ( - end_of_conversation_activity.additional_properties - ) - - @staticmethod - def _apply_event_to_turn_context_activity( - context: TurnContext, event_activity: Activity - ): - context.activity.type = event_activity.type - context.activity.name = event_activity.name - context.activity.value = event_activity.value - context.activity.relates_to = event_activity.relates_to - - context.activity.reply_to_id = event_activity.reply_to_id - context.activity.value = event_activity.value - context.activity.entities = event_activity.entities - context.activity.local_timestamp = event_activity.local_timestamp - context.activity.timestamp = event_activity.timestamp - context.activity.channel_data = event_activity.channel_data - context.activity.additional_properties = event_activity.additional_properties diff --git a/libraries/botbuilder-core/botbuilder/core/streaming/__init__.py b/libraries/botbuilder-core/botbuilder/core/streaming/__init__.py new file mode 100644 index 000000000..4d029ae52 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/streaming/__init__.py @@ -0,0 +1,16 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .bot_framework_http_adapter_base import BotFrameworkHttpAdapterBase +from .streaming_activity_processor import StreamingActivityProcessor +from .streaming_http_client import StreamingHttpDriver +from .streaming_request_handler import StreamingRequestHandler +from .version_info import VersionInfo + +__all__ = [ + "BotFrameworkHttpAdapterBase", + "StreamingActivityProcessor", + "StreamingHttpDriver", + "StreamingRequestHandler", + "VersionInfo", +] diff --git a/libraries/botbuilder-core/botbuilder/core/streaming/bot_framework_http_adapter_base.py b/libraries/botbuilder-core/botbuilder/core/streaming/bot_framework_http_adapter_base.py new file mode 100644 index 000000000..73b8331b7 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/streaming/bot_framework_http_adapter_base.py @@ -0,0 +1,125 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from http import HTTPStatus +from typing import Awaitable, Callable, List + +from botbuilder.core import ( + Bot, + BotFrameworkAdapter, + BotFrameworkAdapterSettings, + InvokeResponse, + TurnContext, +) +from botbuilder.schema import Activity, ActivityTypes, ResourceResponse +from botframework.connector import AsyncBfPipeline, BotFrameworkConnectorConfiguration +from botframework.connector.aio import ConnectorClient +from botframework.connector.auth import ( + ClaimsIdentity, + MicrosoftAppCredentials, + MicrosoftGovernmentAppCredentials, +) + +from .streaming_activity_processor import StreamingActivityProcessor +from .streaming_request_handler import StreamingRequestHandler +from .streaming_http_client import StreamingHttpDriver + + +class BotFrameworkHttpAdapterBase(BotFrameworkAdapter, StreamingActivityProcessor): + # pylint: disable=pointless-string-statement + def __init__(self, settings: BotFrameworkAdapterSettings): + super().__init__(settings) + + self.connected_bot: Bot = None + self.claims_identity: ClaimsIdentity = None + self.request_handlers: List[StreamingRequestHandler] = None + + async def process_streaming_activity( + self, + activity: Activity, + bot_callback_handler: Callable[[TurnContext], Awaitable], + ) -> InvokeResponse: + if not activity: + raise TypeError( + f"'activity: {activity.__class__.__name__}' argument can't be None" + ) + + """ + If a conversation has moved from one connection to another for the same Channel or Skill and + hasn't been forgotten by the previous StreamingRequestHandler. The last requestHandler + the conversation has been associated with should always be the active connection. + """ + request_handler = [ + handler + for handler in self.request_handlers + if handler.service_url == activity.service_url + and handler.has_conversation(activity.conversation.id) + ] + request_handler = request_handler[-1] if request_handler else None + context = TurnContext(self, activity) + + if self.claims_identity: + context.turn_state[self.BOT_IDENTITY_KEY] = self.claims_identity + + connector_client = self._create_streaming_connector_client( + activity, request_handler + ) + context.turn_state[self.BOT_CONNECTOR_CLIENT_KEY] = connector_client + + await self.run_pipeline(context, bot_callback_handler) + + if activity.type == ActivityTypes.invoke: + activity_invoke_response = context.turn_state.get(self._INVOKE_RESPONSE_KEY) + + if not activity_invoke_response: + return InvokeResponse(status=HTTPStatus.NOT_IMPLEMENTED) + return activity_invoke_response.value + + return None + + async def send_streaming_activity(self, activity: Activity) -> ResourceResponse: + raise NotImplementedError() + + def can_process_outgoing_activity(self, activity: Activity) -> bool: + if not activity: + raise TypeError( + f"'activity: {activity.__class__.__name__}' argument can't be None" + ) + + return not activity.service_url.startswith("https") + + async def process_outgoing_activity( + self, _turn_context: TurnContext, activity: Activity + ) -> ResourceResponse: + if not activity: + raise TypeError( + f"'activity: {activity.__class__.__name__}' argument can't be None" + ) + + # TODO: Check if we have token responses from OAuth cards. + + # The ServiceUrl for streaming channels begins with the string "urn" and contains + # information unique to streaming connections. Now that we know that this is a streaming + # activity, process it in the streaming pipeline. + # Process streaming activity. + return await self.send_streaming_activity(activity) + + def _create_streaming_connector_client( + self, activity: Activity, request_handler: StreamingRequestHandler + ) -> ConnectorClient: + empty_credentials = ( + MicrosoftAppCredentials.empty() + if self._channel_provider and self._channel_provider.is_government() + else MicrosoftGovernmentAppCredentials.empty() + ) + streaming_driver = StreamingHttpDriver(request_handler) + config = BotFrameworkConnectorConfiguration( + empty_credentials, + activity.service_url, + pipeline_type=AsyncBfPipeline, + driver=streaming_driver, + ) + streaming_driver.config = config + connector_client = ConnectorClient(None, custom_configuration=config) + + return connector_client diff --git a/libraries/botbuilder-core/botbuilder/core/streaming/streaming_activity_processor.py b/libraries/botbuilder-core/botbuilder/core/streaming/streaming_activity_processor.py new file mode 100644 index 000000000..6b6f16893 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/streaming/streaming_activity_processor.py @@ -0,0 +1,21 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC +from typing import Awaitable, Callable + +from botbuilder.core import TurnContext, InvokeResponse +from botbuilder.schema import Activity + + +class StreamingActivityProcessor(ABC): + """ + Process streaming activities. + """ + + async def process_streaming_activity( + self, + activity: Activity, + bot_callback_handler: Callable[[TurnContext], Awaitable], + ) -> InvokeResponse: + raise NotImplementedError() diff --git a/libraries/botbuilder-core/botbuilder/core/streaming/streaming_http_client.py b/libraries/botbuilder-core/botbuilder/core/streaming/streaming_http_client.py new file mode 100644 index 000000000..251eb2a90 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/streaming/streaming_http_client.py @@ -0,0 +1,93 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from http import HTTPStatus +from logging import Logger +from typing import Any + +from msrest.universal_http import ClientRequest +from msrest.universal_http.async_abc import AsyncClientResponse +from msrest.universal_http.async_requests import ( + AsyncRequestsHTTPSender as AsyncRequestsHTTPDriver, +) +from botframework.streaming import StreamingRequest, ReceiveResponse + +from .streaming_request_handler import StreamingRequestHandler + + +class StreamingProtocolClientResponse(AsyncClientResponse): + def __init__( + self, request: StreamingRequest, streaming_response: ReceiveResponse + ) -> None: + super(StreamingProtocolClientResponse, self).__init__( + request, streaming_response + ) + # https://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientResponse + self.status_code = streaming_response.status_code + # self.headers = streaming_response.headers + # self.reason = streaming_response.reason + self._body = None + + def body(self) -> bytes: + """Return the whole body as bytes in memory.""" + if not self._body: + return bytes([]) + return self._body + + async def load_body(self) -> None: + """Load in memory the body, so it could be accessible from sync methods.""" + self._body: ReceiveResponse + self._body = self.internal_response.read_body() + + def raise_for_status(self): + if 400 <= self.internal_response.status_code <= 599: + raise Exception(f"Http error: {self.internal_response.status_code}") + + +class StreamingHttpDriver(AsyncRequestsHTTPDriver): + def __init__( + self, + request_handler: StreamingRequestHandler, + *, + config=None, + logger: Logger = None, + ): + super().__init__(config) + if not request_handler: + raise TypeError( + f"'request_handler: {request_handler.__class__.__name__}' argument can't be None" + ) + self._request_handler = request_handler + self._logger = logger + + async def send( + self, request: ClientRequest, **config: Any # pylint: disable=unused-argument + ) -> AsyncClientResponse: + # TODO: validate form of request to perform operations + streaming_request = StreamingRequest( + path=request.url[request.url.index("v3/") :], verb=request.method + ) + streaming_request.set_body(request.data) + + return await self._send_request(streaming_request) + + async def _send_request( + self, request: StreamingRequest + ) -> StreamingProtocolClientResponse: + try: + server_response = await self._request_handler.send_streaming_request( + request + ) + + if not server_response: + raise Exception("Server response from streaming request is None") + + if server_response.status_code == HTTPStatus.OK: + # TODO: this should be an object read from json + + return StreamingProtocolClientResponse(request, server_response) + except Exception as error: + # TODO: log error + raise error + + return None diff --git a/libraries/botbuilder-core/botbuilder/core/streaming/streaming_request_handler.py b/libraries/botbuilder-core/botbuilder/core/streaming/streaming_request_handler.py new file mode 100644 index 000000000..12b748ab2 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/streaming/streaming_request_handler.py @@ -0,0 +1,280 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import platform +import traceback +from http import HTTPStatus +from datetime import datetime +from logging import Logger +from json import loads +from typing import Dict, List + +from botbuilder.core import Bot +from botbuilder.schema import Activity, Attachment, ResourceResponse +from botframework.streaming import ( + RequestHandler, + ReceiveRequest, + ReceiveResponse, + StreamingRequest, + StreamingResponse, + __title__, + __version__, +) +from botframework.streaming.transport import DisconnectedEventArgs +from botframework.streaming.transport.web_socket import WebSocket, WebSocketServer + +from .streaming_activity_processor import StreamingActivityProcessor +from .version_info import VersionInfo + + +class StreamContent: + def __init__(self, stream: List[int], *, headers: Dict[str, str] = None): + self.stream = stream + self.headers: Dict[str, str] = headers if headers is not None else {} + + +class StreamingRequestHandler(RequestHandler): + def __init__( + self, + bot: Bot, + activity_processor: StreamingActivityProcessor, + web_socket: WebSocket, + logger: Logger = None, + ): + if not bot: + raise TypeError(f"'bot: {bot.__class__.__name__}' argument can't be None") + if not activity_processor: + raise TypeError( + f"'activity_processor: {activity_processor.__class__.__name__}' argument can't be None" + ) + + self._bot = bot + self._activity_processor = activity_processor + self._logger = logger + self._conversations: Dict[str, datetime] = {} + self._user_agent = StreamingRequestHandler._get_user_agent() + self._server = WebSocketServer(web_socket, self) + self._server_is_connected = True + self._server.disconnected_event_handler = self._server_disconnected + self._service_url: str = None + + @property + def service_url(self) -> str: + return self._service_url + + async def listen(self): + await self._server.start() + + # TODO: log it + + def has_conversation(self, conversation_id: str) -> bool: + return conversation_id in self._conversations + + def conversation_added_time(self, conversation_id: str) -> datetime: + added_time = self._conversations.get(conversation_id) + + if not added_time: + added_time = datetime.min + + return added_time + + def forget_conversation(self, conversation_id: str): + del self._conversations[conversation_id] + + async def process_request( + self, + request: ReceiveRequest, + logger: Logger, # pylint: disable=unused-argument + context: object, # pylint: disable=unused-argument + ) -> StreamingResponse: + # pylint: disable=pointless-string-statement + response = StreamingResponse() + + # We accept all POSTs regardless of path, but anything else requires special treatment. + if not request.verb == StreamingRequest.POST: + return self._handle_custom_paths(request, response) + + # Convert the StreamingRequest into an activity the adapter can understand. + try: + body_str = await request.read_body_as_str() + except Exception as error: + traceback.print_exc() + response.status_code = int(HTTPStatus.BAD_REQUEST) + # TODO: log error + + return response + + try: + # TODO: validate if should use deserialize or from_dict + body_dict = loads(body_str) + activity: Activity = Activity.deserialize(body_dict) + + # All activities received by this StreamingRequestHandler will originate from the same channel, but we won't + # know what that channel is until we've received the first request. + if not self.service_url: + self._service_url = activity.service_url + + # If this is the first time the handler has seen this conversation it needs to be added to the dictionary so + # the adapter is able to route requests to the correct handler. + if not self.has_conversation(activity.conversation.id): + self._conversations[activity.conversation.id] = datetime.now() + + """ + Any content sent as part of a StreamingRequest, including the request body + and inline attachments, appear as streams added to the same collection. The first + stream of any request will be the body, which is parsed and passed into this method + as the first argument, 'body'. Any additional streams are inline attachments that need + to be iterated over and added to the Activity as attachments to be sent to the Bot. + """ + + if len(request.streams) > 1: + stream_attachments = [ + Attachment(content_type=stream.content_type, content=stream.stream) + for stream in request.streams + ] + + if activity.attachments: + activity.attachments += stream_attachments + else: + activity.attachments = stream_attachments + + # Now that the request has been converted into an activity we can send it to the adapter. + adapter_response = ( + await self._activity_processor.process_streaming_activity( + activity, self._bot.on_turn + ) + ) + + # Now we convert the invokeResponse returned by the adapter into a StreamingResponse we can send back + # to the channel. + if not adapter_response: + response.status_code = int(HTTPStatus.OK) + else: + response.status_code = adapter_response.status + if adapter_response.body: + response.set_body(adapter_response.body) + + except Exception as error: + traceback.print_exc() + response.status_code = int(HTTPStatus.INTERNAL_SERVER_ERROR) + response.set_body(str(error)) + # TODO: log error + + return response + + async def send_activity(self, activity: Activity) -> ResourceResponse: + if activity.reply_to_id: + request_path = ( + f"/v3/conversations/{activity.conversation.id if activity.conversation else ''}/" + f"activities/{activity. reply_to_id}" + ) + else: + request_path = f"/v3/conversations/{activity.conversation.id if activity.conversation else ''}/activities" + + stream_attachments = self._update_attachment_streams(activity) + request = StreamingRequest.create_post(request_path) + request.set_body(activity) + if stream_attachments: + for attachment in stream_attachments: + # TODO: might be necessary to serialize this before adding + request.add_stream(attachment) + + try: + if not self._server_is_connected: + raise Exception( + "Error while attempting to send: Streaming transport is disconnected." + ) + + server_response = await self._server.send(request) + + if server_response.status_code == HTTPStatus.OK: + return server_response.read_body_as_json(ResourceResponse) + except Exception: + # TODO: log error + traceback.print_exc() + + return None + + async def send_streaming_request( + self, request: StreamingRequest + ) -> ReceiveResponse: + try: + if not self._server_is_connected: + raise Exception( + "Error while attempting to send: Streaming transport is disconnected." + ) + + return await self._server.send(request) + except Exception: + # TODO: remove printing and log it + traceback.print_exc() + + return None + + @staticmethod + def _get_user_agent() -> str: + package_user_agent = f"{__title__}/{__version__}" + uname = platform.uname() + os_version = f"{uname.machine}-{uname.system}-{uname.version}" + py_version = f"Python,Version={platform.python_version()}" + platform_user_agent = f"({os_version}; {py_version})" + user_agent = f"{package_user_agent} {platform_user_agent}" + return user_agent + + def _update_attachment_streams(self, activity: Activity) -> List[object]: + if not activity or not activity.attachments: + return None + + def validate_int_list(obj: object) -> bool: + if not isinstance(obj, list): + return False + + return all(isinstance(element, int) for element in obj) + + stream_attachments = [ + attachment + for attachment in activity.attachments + if validate_int_list(attachment.content) + ] + + if stream_attachments: + activity.attachments = [ + attachment + for attachment in activity.attachments + if not validate_int_list(attachment.content) + ] + + # TODO: validate StreamContent parallel + return [ + StreamContent( + attachment.content, + headers={"Content-Type": attachment.content_type}, + ) + for attachment in stream_attachments + ] + + return None + + def _server_disconnected( + self, + sender: object, # pylint: disable=unused-argument + event: DisconnectedEventArgs, # pylint: disable=unused-argument + ): + self._server_is_connected = False + + def _handle_custom_paths( + self, request: ReceiveRequest, response: StreamingResponse + ) -> StreamingResponse: + if not request or not request.verb or not request.path: + response.status_code = int(HTTPStatus.BAD_REQUEST) + # TODO: log error + + return response + + if request.verb == StreamingRequest.GET and request.path == "/api/version": + response.status_code = int(HTTPStatus.OK) + response.set_body(VersionInfo(user_agent=self._user_agent)) + + return response + + return None diff --git a/libraries/botbuilder-core/botbuilder/core/streaming/version_info.py b/libraries/botbuilder-core/botbuilder/core/streaming/version_info.py new file mode 100644 index 000000000..b11250375 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/streaming/version_info.py @@ -0,0 +1,22 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json + +from botframework.streaming.payloads.models import Serializable + + +class VersionInfo(Serializable): + def __init__(self, *, user_agent: str = None): + self.user_agent = user_agent + + def to_json(self) -> str: + obj = {"userAgent": self.user_agent} + + return json.dumps(obj) + + def from_json(self, json_str: str) -> "ResponsePayload": + obj = json.loads(json_str) + + self.user_agent = obj.get("userAgent") + return self diff --git a/libraries/botbuilder-core/botbuilder/core/teams/__init__.py b/libraries/botbuilder-core/botbuilder/core/teams/__init__.py index d9d4847e8..7e1f1eede 100644 --- a/libraries/botbuilder-core/botbuilder/core/teams/__init__.py +++ b/libraries/botbuilder-core/botbuilder/core/teams/__init__.py @@ -9,14 +9,18 @@ from .teams_info import TeamsInfo from .teams_activity_extensions import ( teams_get_channel_id, + teams_get_selected_channel_id, teams_get_team_info, teams_notify_user, ) +from .teams_sso_token_exchange_middleware import TeamsSSOTokenExchangeMiddleware __all__ = [ "TeamsActivityHandler", "TeamsInfo", + "TeamsSSOTokenExchangeMiddleware", "teams_get_channel_id", + "teams_get_selected_channel_id", "teams_get_team_info", "teams_notify_user", ] diff --git a/libraries/botbuilder-core/botbuilder/core/teams/teams_activity_extensions.py b/libraries/botbuilder-core/botbuilder/core/teams/teams_activity_extensions.py index 23d907e09..253b31f5c 100644 --- a/libraries/botbuilder-core/botbuilder/core/teams/teams_activity_extensions.py +++ b/libraries/botbuilder-core/botbuilder/core/teams/teams_activity_extensions.py @@ -1,8 +1,25 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from typing import List from botbuilder.schema import Activity -from botbuilder.schema.teams import NotificationInfo, TeamsChannelData, TeamInfo +from botbuilder.schema.teams import ( + NotificationInfo, + TeamsChannelData, + TeamInfo, + TeamsMeetingInfo, + OnBehalfOf, +) + + +def teams_get_channel_data(activity: Activity) -> TeamsChannelData: + if not activity: + return None + + if activity.channel_data: + return TeamsChannelData().deserialize(activity.channel_data) + + return None def teams_get_channel_id(activity: Activity) -> str: @@ -16,6 +33,23 @@ def teams_get_channel_id(activity: Activity) -> str: return None +def teams_get_selected_channel_id(activity: Activity) -> str: + if not activity: + return None + + if activity.channel_data: + channel_data = TeamsChannelData().deserialize(activity.channel_data) + return ( + channel_data.settings.selected_channel.id + if channel_data + and channel_data.settings + and channel_data.settings.selected_channel + else None + ) + + return None + + def teams_get_team_info(activity: Activity) -> TeamInfo: if not activity: return None @@ -27,7 +61,9 @@ def teams_get_team_info(activity: Activity) -> TeamInfo: return None -def teams_notify_user(activity: Activity): +def teams_notify_user( + activity: Activity, alert_in_meeting: bool = None, external_resource_url: str = None +): if not activity: return @@ -35,5 +71,29 @@ def teams_notify_user(activity: Activity): activity.channel_data = {} channel_data = TeamsChannelData().deserialize(activity.channel_data) - channel_data.notification = NotificationInfo(alert=True) + channel_data.notification = NotificationInfo(alert=not alert_in_meeting) + channel_data.notification.alert_in_meeting = alert_in_meeting + channel_data.notification.external_resource_url = external_resource_url activity.channel_data = channel_data + + +def teams_get_meeting_info(activity: Activity) -> TeamsMeetingInfo: + if not activity: + return None + + if activity.channel_data: + channel_data = TeamsChannelData().deserialize(activity.channel_data) + return channel_data.meeting + + return None + + +def teams_get_team_on_behalf_of(activity: Activity) -> List[OnBehalfOf]: + if not activity: + return None + + if activity.channel_data: + channel_data = TeamsChannelData().deserialize(activity.channel_data) + return channel_data.on_behalf_of + + return None diff --git a/libraries/botbuilder-core/botbuilder/core/teams/teams_activity_handler.py b/libraries/botbuilder-core/botbuilder/core/teams/teams_activity_handler.py index 4c767d40a..af45ba5b6 100644 --- a/libraries/botbuilder-core/botbuilder/core/teams/teams_activity_handler.py +++ b/libraries/botbuilder-core/botbuilder/core/teams/teams_activity_handler.py @@ -1,8 +1,10 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +# pylint: disable=too-many-lines + from http import HTTPStatus -from botbuilder.schema import ChannelAccount, SignInConstants +from botbuilder.schema import ChannelAccount, ErrorResponseException, SignInConstants from botbuilder.core import ActivityHandler, InvokeResponse from botbuilder.core.activity_handler import _InvokeResponseException from botbuilder.core.turn_context import TurnContext @@ -12,6 +14,8 @@ TeamInfo, ChannelInfo, FileConsentCardResponse, + MeetingStartEventDetails, + MeetingEndEventDetails, TeamsChannelData, TeamsChannelAccount, MessagingExtensionAction, @@ -21,6 +25,10 @@ O365ConnectorCardActionQuery, TaskModuleRequest, TaskModuleResponse, + TabRequest, + TabSubmit, + MeetingParticipantsEventDetails, + ReadReceiptInfo, ) from botframework.connector import Channels from ..serializer_helper import deserializer_helper @@ -28,6 +36,19 @@ class TeamsActivityHandler(ActivityHandler): async def on_invoke_activity(self, turn_context: TurnContext) -> InvokeResponse: + """ + Invoked when an invoke activity is received from the connector. + Invoke activities can be used to communicate many different things. + + :param turn_context: A context object for this turn. + + :returns: An InvokeResponse that represents the work queued to execute. + + .. remarks:: + Invoke activities communicate programmatic commands from a client or channel to a bot. + The meaning of an invoke activity is defined by the "invoke_activity.name" property, + which is meaningful within the scope of a channel. + """ try: if ( not turn_context.activity.name @@ -35,13 +56,6 @@ async def on_invoke_activity(self, turn_context: TurnContext) -> InvokeResponse: ): return await self.on_teams_card_action_invoke(turn_context) - if ( - turn_context.activity.name - == SignInConstants.token_exchange_operation_name - ): - await self.on_teams_signin_token_exchange(turn_context) - return self._create_invoke_response() - if turn_context.activity.name == "fileConsent/invoke": return await self.on_teams_file_consent( turn_context, @@ -69,6 +83,16 @@ async def on_invoke_activity(self, turn_context: TurnContext) -> InvokeResponse: ) ) + if turn_context.activity.name == "composeExtension/anonymousQueryLink": + return self._create_invoke_response( + await self.on_teams_anonymous_app_based_link_query( + turn_context, + deserializer_helper( + AppBasedLinkQuery, turn_context.activity.value + ), + ) + ) + if turn_context.activity.name == "composeExtension/query": return self._create_invoke_response( await self.on_teams_messaging_extension_query( @@ -101,7 +125,8 @@ async def on_invoke_activity(self, turn_context: TurnContext) -> InvokeResponse: await self.on_teams_messaging_extension_fetch_task( turn_context, deserializer_helper( - MessagingExtensionAction, turn_context.activity.value, + MessagingExtensionAction, + turn_context.activity.value, ), ) ) @@ -148,30 +173,94 @@ async def on_invoke_activity(self, turn_context: TurnContext) -> InvokeResponse: ) ) + if turn_context.activity.name == "tab/fetch": + return self._create_invoke_response( + await self.on_teams_tab_fetch( + turn_context, + deserializer_helper(TabRequest, turn_context.activity.value), + ) + ) + + if turn_context.activity.name == "tab/submit": + return self._create_invoke_response( + await self.on_teams_tab_submit( + turn_context, + deserializer_helper(TabSubmit, turn_context.activity.value), + ) + ) + + if turn_context.activity.name == "config/fetch": + return self._create_invoke_response( + await self.on_teams_config_fetch( + turn_context, + turn_context.activity.value, + ) + ) + + if turn_context.activity.name == "config/submit": + return self._create_invoke_response( + await self.on_teams_config_submit( + turn_context, + turn_context.activity.value, + ) + ) + return await super().on_invoke_activity(turn_context) except _InvokeResponseException as invoke_exception: return invoke_exception.create_invoke_response() async def on_sign_in_invoke(self, turn_context: TurnContext): + """ + Invoked when a signIn invoke activity is received from the connector. + + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ return await self.on_teams_signin_verify_state(turn_context) async def on_teams_card_action_invoke( self, turn_context: TurnContext ) -> InvokeResponse: + """ + Invoked when an card action invoke activity is received from the connector. + + :param turn_context: A context object for this turn. + + :returns: An InvokeResponse that represents the work queued to execute. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_signin_verify_state(self, turn_context: TurnContext): + """ + Invoked when a signIn verify state activity is received from the connector. + + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_signin_token_exchange(self, turn_context: TurnContext): - raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) + # This is for back-compat with previous versions of Python SDK. This method does not + # exist in the C# SDK, and is not used in the Python SDK. + return await self.on_teams_signin_verify_state(turn_context) async def on_teams_file_consent( self, turn_context: TurnContext, file_consent_card_response: FileConsentCardResponse, ) -> InvokeResponse: + """ + Invoked when a file consent card activity is received from the connector. + + :param turn_context: A context object for this turn. + :param file_consent_card_response: The response representing the value of the invoke + activity sent when the user acts on a file consent card. + + :returns: An InvokeResponse depending on the action of the file consent card. + """ if file_consent_card_response.action == "accept": await self.on_teams_file_consent_accept( turn_context, file_consent_card_response @@ -194,6 +283,15 @@ async def on_teams_file_consent_accept( # pylint: disable=unused-argument turn_context: TurnContext, file_consent_card_response: FileConsentCardResponse, ): + """ + Invoked when a file consent card is accepted by the user. + + :param turn_context: A context object for this turn. + :param file_consent_card_response: The response representing the value of the invoke + activity sent when the user accepts a file consent card. + + :returns: A task that represents the work queued to execute. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_file_consent_decline( # pylint: disable=unused-argument @@ -201,31 +299,93 @@ async def on_teams_file_consent_decline( # pylint: disable=unused-argument turn_context: TurnContext, file_consent_card_response: FileConsentCardResponse, ): + """ + Invoked when a file consent card is declined by the user. + + :param turn_context: A context object for this turn. + :param file_consent_card_response: The response representing the value of the invoke + activity sent when the user declines a file consent card. + + :returns: A task that represents the work queued to execute. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_o365_connector_card_action( # pylint: disable=unused-argument self, turn_context: TurnContext, query: O365ConnectorCardActionQuery ): + """ + Invoked when an O365 Connector Card Action activity is received from the connector. + + :param turn_context: A context object for this turn. + :param query: The O365 connector card HttpPOST invoke query. + + :returns: A task that represents the work queued to execute. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_app_based_link_query( # pylint: disable=unused-argument self, turn_context: TurnContext, query: AppBasedLinkQuery ) -> MessagingExtensionResponse: + """ + Invoked when an app based link query activity is received from the connector. + + :param turn_context: A context object for this turn. + :param query: The invoke request body type for app-based link query. + + :returns: The Messaging Extension Response for the query. + """ + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) + + async def on_teams_anonymous_app_based_link_query( # pylint: disable=unused-argument + self, turn_context: TurnContext, query: AppBasedLinkQuery + ) -> MessagingExtensionResponse: + """ + Invoked when an anonymous app based link query activity is received from the connector. + + :param turn_context: A context object for this turn. + :param query: The invoke request body type for app-based link query. + + :returns: The Messaging Extension Response for the query. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_messaging_extension_query( # pylint: disable=unused-argument self, turn_context: TurnContext, query: MessagingExtensionQuery ) -> MessagingExtensionResponse: + """ + Invoked when a Messaging Extension Query activity is received from the connector. + + :param turn_context: A context object for this turn. + :param query: The query for the search command. + + :returns: The Messaging Extension Response for the query. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_messaging_extension_select_item( # pylint: disable=unused-argument self, turn_context: TurnContext, query ) -> MessagingExtensionResponse: + """ + Invoked when a messaging extension select item activity is received from the connector. + + :param turn_context: A context object for this turn. + :param query: The object representing the query. + + :returns: The Messaging Extension Response for the query. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_messaging_extension_submit_action_dispatch( self, turn_context: TurnContext, action: MessagingExtensionAction ) -> MessagingExtensionActionResponse: + """ + Invoked when a messaging extension submit action dispatch activity is received from the connector. + + :param turn_context: A context object for this turn. + :param action: The messaging extension action. + + :returns: The Messaging Extension Action Response for the action. + """ if not action.bot_message_preview_action: return await self.on_teams_messaging_extension_submit_action( turn_context, action @@ -249,50 +409,187 @@ async def on_teams_messaging_extension_submit_action_dispatch( async def on_teams_messaging_extension_bot_message_preview_edit( # pylint: disable=unused-argument self, turn_context: TurnContext, action: MessagingExtensionAction ) -> MessagingExtensionActionResponse: + """ + Invoked when a messaging extension bot message preview edit activity is received from the connector. + + :param turn_context: A context object for this turn. + :param action: The messaging extension action. + + :returns: The Messaging Extension Action Response for the action. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_messaging_extension_bot_message_preview_send( # pylint: disable=unused-argument self, turn_context: TurnContext, action: MessagingExtensionAction ) -> MessagingExtensionActionResponse: + """ + Invoked when a messaging extension bot message preview send activity is received from the connector. + + :param turn_context: A context object for this turn. + :param action: The messaging extension action. + + :returns: The Messaging Extension Action Response for the action. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_messaging_extension_submit_action( # pylint: disable=unused-argument self, turn_context: TurnContext, action: MessagingExtensionAction ) -> MessagingExtensionActionResponse: + """ + Invoked when a messaging extension submit action activity is received from the connector. + + :param turn_context: A context object for this turn. + :param action: The messaging extension action. + + :returns: The Messaging Extension Action Response for the action. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_messaging_extension_fetch_task( # pylint: disable=unused-argument self, turn_context: TurnContext, action: MessagingExtensionAction ) -> MessagingExtensionActionResponse: + """ + Invoked when a Messaging Extension Fetch activity is received from the connector. + + :param turn_context: A context object for this turn. + :param action: The messaging extension action. + + :returns: The Messaging Extension Action Response for the action. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_messaging_extension_configuration_query_settings_url( # pylint: disable=unused-argument self, turn_context: TurnContext, query: MessagingExtensionQuery ) -> MessagingExtensionResponse: + """ + Invoked when a messaging extension configuration query setting url activity is received from the connector. + + :param turn_context: A context object for this turn. + :param query: The Messaging extension query. + + :returns: The Messaging Extension Response for the query. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_messaging_extension_configuration_setting( # pylint: disable=unused-argument self, turn_context: TurnContext, settings ): + """ + Override this in a derived class to provide logic for when a configuration is set for a messaging extension. + + :param turn_context: A context object for this turn. + :param settings: Object representing the configuration settings. + + :returns: A task that represents the work queued to execute. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_messaging_extension_card_button_clicked( # pylint: disable=unused-argument self, turn_context: TurnContext, card_data ): + """ + Override this in a derived class to provide logic for when a card button is clicked in a messaging extension. + + :param turn_context: A context object for this turn. + :param card_data: Object representing the card data. + + :returns: A task that represents the work queued to execute. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_task_module_fetch( # pylint: disable=unused-argument self, turn_context: TurnContext, task_module_request: TaskModuleRequest ) -> TaskModuleResponse: + """ + Override this in a derived class to provide logic for when a task module is fetched. + + :param turn_context: A context object for this turn. + :param task_module_request: The task module invoke request value payload. + + :returns: A Task Module Response for the request. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_teams_task_module_submit( # pylint: disable=unused-argument self, turn_context: TurnContext, task_module_request: TaskModuleRequest ) -> TaskModuleResponse: + """ + Override this in a derived class to provide logic for when a task module is submitted. + + :param turn_context: A context object for this turn. + :param task_module_request: The task module invoke request value payload. + + :returns: A Task Module Response for the request. + """ + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) + + async def on_teams_tab_fetch( # pylint: disable=unused-argument + self, turn_context: TurnContext, tab_request: TabRequest + ): + """ + Override this in a derived class to provide logic for when a tab is fetched. + + :param turn_context: A context object for this turn. + :param tab_request: The tab invoke request value payload. + + :returns: A Tab Response for the request. + """ + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) + + async def on_teams_tab_submit( # pylint: disable=unused-argument + self, turn_context: TurnContext, tab_submit: TabSubmit + ): + """ + Override this in a derived class to provide logic for when a tab is submitted. + + :param turn_context: A context object for this turn. + :param tab_submit: The tab submit invoke request value payload. + + :returns: A Tab Response for the request. + """ + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) + + async def on_teams_config_fetch( # pylint: disable=unused-argument + self, turn_context: TurnContext, config_data: any + ): + """ + Override this in a derived class to provide logic for when a config is fetched. + + :param turn_context: A context object for this turn. + :param config_data: The config fetch invoke request value payload. + + :returns: A Config Response for the request. + """ + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) + + async def on_teams_config_submit( # pylint: disable=unused-argument + self, turn_context: TurnContext, config_data: any + ): + """ + Override this in a derived class to provide logic for when a config is submitted. + + :param turn_context: A context object for this turn. + :param config_data: The config fetch invoke request value payload. + + :returns: A Config Response for the request. + """ raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) async def on_conversation_update_activity(self, turn_context: TurnContext): + """ + Invoked when a conversation update activity is received from the channel. + Conversation update activities are useful when it comes to responding to users + being added to or removed from the channel. + For example, a bot could respond to a user being added by greeting the user. + + :param turn_context: A context object for this turn. + :returns: A task that represents the work queued to execute. + + .. remarks:: + In a derived class, override this method to add logic that applies + to all conversation update activities. + """ if turn_context.activity.channel_id == Channels.ms_teams: channel_data = TeamsChannelData().deserialize( turn_context.activity.channel_data @@ -324,8 +621,32 @@ async def on_conversation_update_activity(self, turn_context: TurnContext): return await self.on_teams_channel_renamed( channel_data.channel, channel_data.team, turn_context ) + if channel_data.event_type == "teamArchived": + return await self.on_teams_team_archived( + channel_data.team, turn_context + ) + if channel_data.event_type == "teamDeleted": + return await self.on_teams_team_deleted( + channel_data.team, turn_context + ) + if channel_data.event_type == "teamHardDeleted": + return await self.on_teams_team_hard_deleted( + channel_data.team, turn_context + ) + if channel_data.event_type == "channelRestored": + return await self.on_teams_channel_restored( + channel_data.channel, channel_data.team, turn_context + ) if channel_data.event_type == "teamRenamed": - return await self.on_teams_team_renamed_activity( + return await self.on_teams_team_renamed( + channel_data.team, turn_context + ) + if channel_data.event_type == "teamRestored": + return await self.on_teams_team_restored( + channel_data.team, turn_context + ) + if channel_data.event_type == "teamUnarchived": + return await self.on_teams_team_unarchived( channel_data.team, turn_context ) @@ -334,11 +655,117 @@ async def on_conversation_update_activity(self, turn_context: TurnContext): async def on_teams_channel_created( # pylint: disable=unused-argument self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext ): + """ + Invoked when a Channel Created event activity is received from the connector. + Channel Created correspond to the user creating a new channel. + + :param channel_info: The channel info object which describes the channel. + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_team_archived( # pylint: disable=unused-argument + self, team_info: TeamInfo, turn_context: TurnContext + ): + """ + Invoked when a Team Archived event activity is received from the connector. + Team Archived correspond to the user archiving a team. + + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_team_deleted( # pylint: disable=unused-argument + self, team_info: TeamInfo, turn_context: TurnContext + ): + """ + Invoked when a Team Deleted event activity is received from the connector. + Team Deleted corresponds to the user deleting a team. + + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_team_hard_deleted( # pylint: disable=unused-argument + self, team_info: TeamInfo, turn_context: TurnContext + ): + """ + Invoked when a Team Hard Deleted event activity is received from the connector. + Team Hard Deleted corresponds to the user hard deleting a team. + + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ return + async def on_teams_team_renamed( # pylint: disable=unused-argument + self, team_info: TeamInfo, turn_context: TurnContext + ): + """ + Invoked when a Team Renamed event activity is received from the connector. + Team Renamed correspond to the user renaming an existing team. + + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return await self.on_teams_team_renamed_activity(team_info, turn_context) + async def on_teams_team_renamed_activity( # pylint: disable=unused-argument self, team_info: TeamInfo, turn_context: TurnContext ): + """ + DEPRECATED. Please use on_teams_team_renamed(). This method will remain in place throughout + v4 so as not to break existing bots. + + Invoked when a Team Renamed event activity is received from the connector. + Team Renamed correspond to the user renaming an existing team. + + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_team_restored( # pylint: disable=unused-argument + self, team_info: TeamInfo, turn_context: TurnContext + ): + """ + Invoked when a Team Restored event activity is received from the connector. + Team Restored corresponds to the user restoring a team. + + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_team_unarchived( # pylint: disable=unused-argument + self, team_info: TeamInfo, turn_context: TurnContext + ): + """ + Invoked when a Team Unarchived event activity is received from the connector. + Team Unarchived correspond to the user unarchiving a team. + + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ return async def on_teams_members_added_dispatch( # pylint: disable=unused-argument @@ -347,29 +774,48 @@ async def on_teams_members_added_dispatch( # pylint: disable=unused-argument team_info: TeamInfo, turn_context: TurnContext, ): + """ + Override this in a derived class to provide logic for when members other than the bot + join the channel, such as your bot's welcome logic. + It will get the associated members with the provided accounts. - team_members = {} + :param members_added: A list of all the accounts added to the channel, as + described by the conversation update activity. + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ team_members_added = [] for member in members_added: - if member.additional_properties != {}: + is_bot = ( + turn_context.activity.recipient is not None + and member.id == turn_context.activity.recipient.id + ) + if member.additional_properties != {} or is_bot: team_members_added.append( deserializer_helper(TeamsChannelAccount, member) ) else: - if team_members == {}: - result = await TeamsInfo.get_members(turn_context) - team_members = {i.id: i for i in result} - - if member.id in team_members: - team_members_added.append(member) - else: - new_teams_channel_account = TeamsChannelAccount( - id=member.id, - name=member.name, - aad_object_id=member.aad_object_id, - role=member.role, - ) - team_members_added.append(new_teams_channel_account) + team_member = None + try: + team_member = await TeamsInfo.get_member(turn_context, member.id) + team_members_added.append(team_member) + except ErrorResponseException as ex: + if ( + ex.error + and ex.error.error + and ex.error.error.code == "ConversationNotFound" + ): + new_teams_channel_account = TeamsChannelAccount( + id=member.id, + name=member.name, + aad_object_id=member.aad_object_id, + role=member.role, + ) + team_members_added.append(new_teams_channel_account) + else: + raise ex return await self.on_teams_members_added( team_members_added, team_info, turn_context @@ -381,13 +827,22 @@ async def on_teams_members_added( # pylint: disable=unused-argument team_info: TeamInfo, turn_context: TurnContext, ): + """ + Override this in a derived class to provide logic for when members other than the bot + join the channel, such as your bot's welcome logic. + + :param teams_members_added: A list of all the members added to the channel, as + described by the conversation update activity. + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ teams_members_added = [ ChannelAccount().deserialize(member.serialize()) for member in teams_members_added ] - return await super().on_members_added_activity( - teams_members_added, turn_context - ) + return await self.on_members_added_activity(teams_members_added, turn_context) async def on_teams_members_removed_dispatch( # pylint: disable=unused-argument self, @@ -395,6 +850,18 @@ async def on_teams_members_removed_dispatch( # pylint: disable=unused-argument team_info: TeamInfo, turn_context: TurnContext, ): + """ + Override this in a derived class to provide logic for when members other than the bot + leave the channel, such as your bot's good-bye logic. + It will get the associated members with the provided accounts. + + :param members_removed: A list of all the accounts removed from the channel, as + described by the conversation update activity. + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ teams_members_removed = [] for member in members_removed: new_account_json = member.serialize() @@ -404,23 +871,262 @@ async def on_teams_members_removed_dispatch( # pylint: disable=unused-argument TeamsChannelAccount().deserialize(new_account_json) ) - return await self.on_teams_members_removed(teams_members_removed, turn_context) + return await self.on_teams_members_removed( + teams_members_removed, team_info, turn_context + ) - async def on_teams_members_removed( - self, teams_members_removed: [TeamsChannelAccount], turn_context: TurnContext + async def on_teams_members_removed( # pylint: disable=unused-argument + self, + teams_members_removed: [TeamsChannelAccount], + team_info: TeamInfo, + turn_context: TurnContext, ): + """ + Override this in a derived class to provide logic for when members other than the bot + leave the channel, such as your bot's good-bye logic. + + :param teams_members_removed: A list of all the members removed from the channel, as + described by the conversation update activity. + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ members_removed = [ ChannelAccount().deserialize(member.serialize()) for member in teams_members_removed ] - return await super().on_members_removed_activity(members_removed, turn_context) + return await self.on_members_removed_activity(members_removed, turn_context) async def on_teams_channel_deleted( # pylint: disable=unused-argument self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext ): + """ + Invoked when a Channel Deleted event activity is received from the connector. + Channel Deleted correspond to the user deleting an existing channel. + + :param channel_info: The channel info object which describes the channel. + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ return async def on_teams_channel_renamed( # pylint: disable=unused-argument self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext ): + """ + Invoked when a Channel Renamed event activity is received from the connector. + Channel Renamed correspond to the user renaming an existing channel. + + :param channel_info: The channel info object which describes the channel. + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_channel_restored( # pylint: disable=unused-argument + self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext + ): + """ + Invoked when a Channel Restored event activity is received from the connector. + Channel Restored correspond to the user restoring a previously deleted channel. + + :param channel_info: The channel info object which describes the channel. + :param team_info: The team info object representing the team. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_event_activity(self, turn_context: TurnContext): + """ + Invoked when an event activity is received from the connector when the base behavior of + :meth:`on_turn()` is used. + + :param turn_context: The context object for this turn + :type turn_context: :class:`botbuilder.core.TurnContext` + + :returns: A task that represents the work queued to execute + + .. remarks:: + When the :meth:`on_turn()` method receives an event activity, it calls this method. + If the activity name is `tokens/response`, it calls :meth:`on_token_response_event()`; + otherwise, it calls :meth:`on_event()`. + + In a derived class, override this method to add logic that applies to all event activities. + Add logic to apply before the specific event-handling logic before the call to this base class method. + Add logic to apply after the specific event-handling logic after the call to this base class method. + + Event activities communicate programmatic information from a client or channel to a bot. + The meaning of an event activity is defined by the event activity name property, which is meaningful within + the scope of a channel. + """ + if turn_context.activity.channel_id == Channels.ms_teams: + if turn_context.activity.name == "application/vnd.microsoft.readReceipt": + return await self.on_teams_read_receipt_event( + turn_context.activity.value, turn_context + ) + if turn_context.activity.name == "application/vnd.microsoft.meetingStart": + return await self.on_teams_meeting_start_event( + turn_context.activity.value, turn_context + ) + if turn_context.activity.name == "application/vnd.microsoft.meetingEnd": + return await self.on_teams_meeting_end_event( + turn_context.activity.value, turn_context + ) + if ( + turn_context.activity.name + == "application/vnd.microsoft.meetingParticipantJoin" + ): + return await self.on_teams_meeting_participants_join_event( + turn_context.activity.value, turn_context + ) + if ( + turn_context.activity.name + == "application/vnd.microsoft.meetingParticipantLeave" + ): + return await self.on_teams_meeting_participants_leave_event( + turn_context.activity.value, turn_context + ) + + return await super().on_event_activity(turn_context) + + async def on_teams_read_receipt_event( + self, read_receipt_info: ReadReceiptInfo, turn_context: TurnContext + ): # pylint: disable=unused-argument + """ + Override this in a derived class to provide logic for when the bot receives a read receipt event. + + :param read_receipt_info: Information regarding the read receipt. i.e. Id of the message last read by the user. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_meeting_start_event( + self, meeting: MeetingStartEventDetails, turn_context: TurnContext + ): # pylint: disable=unused-argument + """ + Override this in a derived class to provide logic for when a Teams meeting start event is received. + + :param meeting: The details of the meeting. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_meeting_end_event( + self, meeting: MeetingEndEventDetails, turn_context: TurnContext + ): # pylint: disable=unused-argument + """ + Override this in a derived class to provide logic for when a Teams meeting end event is received. + + :param meeting: The details of the meeting. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_meeting_participants_join_event( + self, meeting: MeetingParticipantsEventDetails, turn_context: TurnContext + ): # pylint: disable=unused-argument + """ + Override this in a derived class to provide logic for when meeting participants are added. + + :param meeting: The details of the meeting. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_meeting_participants_leave_event( + self, meeting: MeetingParticipantsEventDetails, turn_context: TurnContext + ): # pylint: disable=unused-argument + """ + Override this in a derived class to provide logic for when meeting participants are removed. + + :param meeting: The details of the meeting. + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_message_update_activity(self, turn_context: TurnContext): + """ + Invoked when a message update activity is received, such as a message edit or undelete. + + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + if turn_context.activity.channel_id == Channels.ms_teams: + channel_data = TeamsChannelData().deserialize( + turn_context.activity.channel_data + ) + + if channel_data: + if channel_data.event_type == "editMessage": + return await self.on_teams_message_edit(turn_context) + if channel_data.event_type == "undeleteMessage": + return await self.on_teams_message_undelete(turn_context) + + return await super().on_message_update_activity(turn_context) + + async def on_message_delete_activity(self, turn_context: TurnContext): + """ + Invoked when a message delete activity is received, such as a soft delete message. + + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + if turn_context.activity.channel_id == Channels.ms_teams: + channel_data = TeamsChannelData().deserialize( + turn_context.activity.channel_data + ) + + if channel_data: + if channel_data.event_type == "softDeleteMessage": + return await self.on_teams_message_soft_delete(turn_context) + + return await super().on_message_delete_activity(turn_context) + + async def on_teams_message_edit(self, turn_context: TurnContext): + """ + Invoked when a Teams edit message event activity is received. + + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_message_undelete(self, turn_context: TurnContext): + """ + Invoked when a Teams undo soft delete message event activity is received. + + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ + return + + async def on_teams_message_soft_delete(self, turn_context: TurnContext): + """ + Invoked when a Teams soft delete message event activity is received. + + :param turn_context: A context object for this turn. + + :returns: A task that represents the work queued to execute. + """ return diff --git a/libraries/botbuilder-core/botbuilder/core/teams/teams_info.py b/libraries/botbuilder-core/botbuilder/core/teams/teams_info.py index 3593650c4..4afa50c05 100644 --- a/libraries/botbuilder-core/botbuilder/core/teams/teams_info.py +++ b/libraries/botbuilder-core/botbuilder/core/teams/teams_info.py @@ -2,31 +2,92 @@ # Licensed under the MIT License. from typing import List, Tuple -from botbuilder.schema import ConversationParameters, ConversationReference -from botbuilder.core.turn_context import Activity, TurnContext + +from botframework.connector import Channels +from botframework.connector.aio import ConnectorClient +from botframework.connector.teams import TeamsConnectorClient +from botbuilder.core.teams.teams_activity_extensions import ( + teams_get_meeting_info, + teams_get_channel_data, +) +from botbuilder.core import ( + CloudAdapterBase, + BotFrameworkAdapter, + TurnContext, + BotAdapter, +) +from botbuilder.schema import Activity, ConversationParameters, ConversationReference from botbuilder.schema.teams import ( ChannelInfo, + MeetingInfo, TeamDetails, TeamsChannelData, TeamsChannelAccount, TeamsPagedMembersResult, + TeamsMeetingParticipant, + MeetingNotificationBase, + MeetingNotificationResponse, ) -from botframework.connector.aio import ConnectorClient -from botframework.connector.teams.teams_connector_client import TeamsConnectorClient class TeamsInfo: @staticmethod async def send_message_to_teams_channel( - turn_context: TurnContext, activity: Activity, teams_channel_id: str + turn_context: TurnContext, + activity: Activity, + teams_channel_id: str, + *, + bot_app_id: str = None, ) -> Tuple[ConversationReference, str]: if not turn_context: raise ValueError("The turn_context cannot be None") if not turn_context.activity: - raise ValueError("The turn_context.activity cannot be None") + raise ValueError("The activity inside turn context cannot be None") + if not activity: + raise ValueError("The activity cannot be None") if not teams_channel_id: raise ValueError("The teams_channel_id cannot be None or empty") + if not bot_app_id: + return await TeamsInfo._legacy_send_message_to_teams_channel( + turn_context, activity, teams_channel_id + ) + + conversation_reference: ConversationReference = None + new_activity_id = "" + service_url = turn_context.activity.service_url + conversation_parameters = ConversationParameters( + is_group=True, + channel_data=TeamsChannelData(channel=ChannelInfo(id=teams_channel_id)), + activity=activity, + ) + + async def aux_callback( + new_turn_context, + ): + nonlocal new_activity_id + nonlocal conversation_reference + new_activity_id = new_turn_context.activity.id + conversation_reference = TurnContext.get_conversation_reference( + new_turn_context.activity + ) + + adapter: CloudAdapterBase = turn_context.adapter + await adapter.create_conversation( + bot_app_id, + aux_callback, + conversation_parameters, + Channels.ms_teams, + service_url, + None, + ) + + return (conversation_reference, new_activity_id) + + @staticmethod + async def _legacy_send_message_to_teams_channel( + turn_context: TurnContext, activity: Activity, teams_channel_id: str + ) -> Tuple[ConversationReference, str]: old_ref = TurnContext.get_conversation_reference(turn_context.activity) conversation_parameters = ConversationParameters( is_group=True, @@ -34,11 +95,38 @@ async def send_message_to_teams_channel( activity=activity, ) - result = await turn_context.adapter.create_conversation( + # if this version of the method is called the adapter probably wont be CloudAdapter + adapter: BotFrameworkAdapter = turn_context.adapter + result = await adapter.create_conversation( old_ref, TeamsInfo._create_conversation_callback, conversation_parameters ) return (result[0], result[1]) + @staticmethod + async def send_meeting_notification( + turn_context: TurnContext, + notification: MeetingNotificationBase, + meeting_id: str = None, + ) -> MeetingNotificationResponse: + meeting_id = ( + meeting_id + if meeting_id + else teams_get_meeting_info(turn_context.activity).id + ) + if meeting_id is None: + raise TypeError( + "TeamsInfo._send_meeting_notification: method requires a meeting_id or " + "TurnContext that contains a meeting id" + ) + + if notification is None: + raise TypeError("notification is required.") + + connector_client = await TeamsInfo.get_teams_connector_client(turn_context) + return await connector_client.teams.send_meeting_notification( + meeting_id, notification + ) + @staticmethod async def _create_conversation_callback( new_turn_context, @@ -93,7 +181,8 @@ async def get_team_members( connector_client = await TeamsInfo._get_connector_client(turn_context) return await TeamsInfo._get_members( - connector_client, turn_context.activity.conversation.id, + connector_client, + turn_context.activity.conversation.id, ) @staticmethod @@ -124,7 +213,7 @@ async def get_paged_team_members( connector_client = await TeamsInfo._get_connector_client(turn_context) return await TeamsInfo._get_paged_members( connector_client, - turn_context.activity.conversation.id, + team_id, continuation_token, page_size, ) @@ -133,7 +222,6 @@ async def get_paged_team_members( async def get_paged_members( turn_context: TurnContext, continuation_token: str = None, page_size: int = None ) -> List[TeamsPagedMembersResult]: - team_id = TeamsInfo.get_team_id(turn_context) if not team_id: conversation_id = turn_context.activity.conversation.id @@ -142,7 +230,9 @@ async def get_paged_members( connector_client, conversation_id, continuation_token, page_size ) - return await TeamsInfo.get_paged_team_members(turn_context, team_id, page_size) + return await TeamsInfo.get_paged_team_members( + turn_context, team_id, continuation_token, page_size + ) @staticmethod async def get_team_member( @@ -178,6 +268,66 @@ async def get_member( return await TeamsInfo.get_team_member(turn_context, team_id, member_id) + @staticmethod + async def get_meeting_participant( + turn_context: TurnContext, + meeting_id: str = None, + participant_id: str = None, + tenant_id: str = None, + ) -> TeamsMeetingParticipant: + meeting_id = ( + meeting_id + if meeting_id + else teams_get_meeting_info(turn_context.activity).id + ) + if meeting_id is None: + raise TypeError( + "TeamsInfo._get_meeting_participant: method requires a meeting_id" + ) + + participant_id = ( + participant_id + if participant_id + else turn_context.activity.from_property.aad_object_id + ) + if participant_id is None: + raise TypeError( + "TeamsInfo._get_meeting_participant: method requires a participant_id" + ) + + tenant_id = ( + tenant_id + if tenant_id + else teams_get_channel_data(turn_context.activity).tenant.id + ) + if tenant_id is None: + raise TypeError( + "TeamsInfo._get_meeting_participant: method requires a tenant_id" + ) + + connector_client = await TeamsInfo.get_teams_connector_client(turn_context) + return connector_client.teams.fetch_participant( + meeting_id, participant_id, tenant_id + ) + + @staticmethod + async def get_meeting_info( + turn_context: TurnContext, meeting_id: str = None + ) -> MeetingInfo: + meeting_id = ( + meeting_id + if meeting_id + else teams_get_meeting_info(turn_context.activity).id + ) + if meeting_id is None: + raise TypeError( + "TeamsInfo._get_meeting_participant: method requires a meeting_id or " + "TurnContext that contains a meeting id" + ) + + connector_client = await TeamsInfo.get_teams_connector_client(turn_context) + return connector_client.teams.fetch_meeting(meeting_id) + @staticmethod async def get_teams_connector_client( turn_context: TurnContext, @@ -187,7 +337,8 @@ async def get_teams_connector_client( connector_client = await TeamsInfo._get_connector_client(turn_context) return TeamsConnectorClient( - connector_client.config.credentials, turn_context.activity.service_url, + connector_client.config.credentials, + turn_context.activity.service_url, ) @staticmethod @@ -199,10 +350,15 @@ def get_team_id(turn_context: TurnContext): @staticmethod async def _get_connector_client(turn_context: TurnContext) -> ConnectorClient: - return await turn_context.adapter.create_connector_client( - turn_context.activity.service_url + connector_client = turn_context.turn_state.get( + BotAdapter.BOT_CONNECTOR_CLIENT_KEY ) + if connector_client is None: + raise ValueError("This method requires a connector client.") + + return connector_client + @staticmethod async def _get_members( connector_client: ConnectorClient, conversation_id: str @@ -244,8 +400,10 @@ async def _get_paged_members( "TeamsInfo._get_paged_members.conversation_id: cannot be empty." ) - return await connector_client.conversations.get_teams_conversation_paged_members( - conversation_id, continuation_token, page_size + return ( + await connector_client.conversations.get_teams_conversation_paged_members( + conversation_id, page_size, continuation_token + ) ) @staticmethod @@ -261,8 +419,10 @@ async def _get_member( if not member_id: raise TypeError("TeamsInfo._get_member.member_id: cannot be empty.") - member: TeamsChannelAccount = await connector_client.conversations.get_conversation_member( - conversation_id, member_id + member: TeamsChannelAccount = ( + await connector_client.conversations.get_conversation_member( + conversation_id, member_id + ) ) return TeamsChannelAccount().deserialize( diff --git a/libraries/botbuilder-core/botbuilder/core/teams/teams_sso_token_exchange_middleware.py b/libraries/botbuilder-core/botbuilder/core/teams/teams_sso_token_exchange_middleware.py new file mode 100644 index 000000000..5a6fa5de6 --- /dev/null +++ b/libraries/botbuilder-core/botbuilder/core/teams/teams_sso_token_exchange_middleware.py @@ -0,0 +1,202 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import traceback + +from http import HTTPStatus +from typing import Awaitable, Callable +from botframework.connector.channels import Channels + +from botframework.connector.token_api.models import ( + TokenResponse, + TokenExchangeRequest, +) +from botbuilder.schema import ( + Activity, + ActivityTypes, + SignInConstants, + TokenExchangeInvokeRequest, + TokenExchangeInvokeResponse, +) +from botbuilder.core import ( + ExtendedUserTokenProvider, + Middleware, + InvokeResponse, + Storage, + StoreItem, + TurnContext, +) +from botframework.connector.auth.user_token_client import UserTokenClient + + +class _TokenStoreItem(StoreItem): + def __init__(self, **kwargs): + self.e_tag: str = None + super().__init__(**kwargs) + + @staticmethod + def get_storage_key(turn_context: TurnContext): + activity = turn_context.activity + if not activity.channel_id: + raise TypeError("invalid activity-missing channel_id") + + if not activity.conversation or not activity.conversation.id: + raise TypeError("invalid activity-missing conversation.id") + + channel_id = activity.channel_id + conversation_id = activity.conversation.id + + value = activity.value + if not value or "id" not in value: + raise Exception("Invalid signin/tokenExchange. Missing activity.value[id]") + + return f"{channel_id}/{conversation_id}/{value['id']}" + + +class TeamsSSOTokenExchangeMiddleware(Middleware): + """ + If the activity name is signin/tokenExchange, self middleware will attempt to + exchange the token, and deduplicate the incoming call, ensuring only one + exchange request is processed. + + .. remarks:: + If a user is signed into multiple Teams clients, the Bot could receive a + "signin/tokenExchange" from each client. Each token exchange request for a + specific user login will have an identical Activity.Value.Id. + + Only one of these token exchange requests should be processed by the bot. + The others return . + For a distributed bot in production, self requires a distributed storage + ensuring only one token exchange is processed. self middleware supports + CosmosDb storage found in Microsoft.Bot.Builder.Azure, or MemoryStorage for + local development. IStorage's ETag implementation for token exchange activity + deduplication. + """ + + def __init__(self, storage: Storage, connection_name: str): + """ + Initializes a instance of the class. + + :param storage: The Storage to use for deduplication. + :param connection_name: The connection name to use for the single + sign on token exchange. + """ + if storage is None: + raise TypeError("storage cannot be None") + + if connection_name is None: + raise TypeError("connection name cannot be None") + + self._oauth_connection_name = connection_name + self._storage = storage + + async def on_turn( + self, context: TurnContext, logic: Callable[[TurnContext], Awaitable] + ): + if ( + context.activity.channel_id == Channels.ms_teams + and context.activity.name == SignInConstants.token_exchange_operation_name + ): + # If the TokenExchange is NOT successful, the response will have already been sent by _exchanged_token + if not await self._exchanged_token(context): + return + + # Only one token exchange should proceed from here. Deduplication is performed second because in the case + # of failure due to consent required, every caller needs to receive the + if not await self._deduplicated_token_exchange_id(context): + # If the token is not exchangeable, do not process this activity further. + return + + await logic() + + async def _deduplicated_token_exchange_id(self, turn_context: TurnContext) -> bool: + # Create a StoreItem with Etag of the unique 'signin/tokenExchange' request + store_item = _TokenStoreItem(e_tag=turn_context.activity.value.get("id", None)) + + store_items = {_TokenStoreItem.get_storage_key(turn_context): store_item} + try: + # Writing the IStoreItem with ETag of unique id will succeed only once + await self._storage.write(store_items) + except Exception as error: + # Memory storage throws a generic exception with a Message of 'Etag conflict. [other error info]' + # CosmosDbPartitionedStorage throws: ex.Message.Contains("precondition is not met") + if "Etag conflict" in str(error) or "precondition is not met" in str(error): + # Do NOT proceed processing self message, some other thread or machine already has processed it. + + # Send 200 invoke response. + await self._send_invoke_response(turn_context) + return False + + raise error + + return True + + async def _send_invoke_response( + self, + turn_context: TurnContext, + body: object = None, + http_status_code=HTTPStatus.OK, + ): + await turn_context.send_activity( + Activity( + type=ActivityTypes.invoke_response, + value=InvokeResponse(status=http_status_code, body=body), + ) + ) + + async def _exchanged_token(self, turn_context: TurnContext) -> bool: + token_exchange_response: TokenResponse = None + aux_dict = {} + if turn_context.activity.value: + for prop in ["id", "connectionName", "token", "properties"]: + aux_dict[prop] = turn_context.activity.value.get(prop) + token_exchange_request = TokenExchangeInvokeRequest( + id=aux_dict["id"], + connection_name=aux_dict["connectionName"], + token=aux_dict["token"], + properties=aux_dict["properties"], + ) + try: + adapter = turn_context.adapter + + user_token_client: UserTokenClient = turn_context.turn_state.get( + UserTokenClient.__name__, None + ) + if user_token_client: + # If the adapter has UserTokenClient, use it to exchange the token. + token_exchange_response = await user_token_client.exchange_token( + turn_context.activity.from_property.id, + token_exchange_request.connection_name, + turn_context.activity.channel_id, + TokenExchangeRequest(token=token_exchange_request.token), + ) + elif isinstance(turn_context.adapter, ExtendedUserTokenProvider): + token_exchange_response = await adapter.exchange_token( + turn_context, + self._oauth_connection_name, + turn_context.activity.from_property.id, + TokenExchangeRequest(token=token_exchange_request.token), + ) + else: + raise Exception( + "Not supported: Token Exchange is not supported by the current adapter." + ) + except: + traceback.print_exc() + if not token_exchange_response or not token_exchange_response.token: + # The token could not be exchanged (which could be due to a consent requirement) + # Notify the sender that PreconditionFailed so they can respond accordingly. + + invoke_response = TokenExchangeInvokeResponse( + id=token_exchange_request.id, + connection_name=self._oauth_connection_name, + failure_detail="The bot is unable to exchange token. Proceed with regular login.", + ) + + await self._send_invoke_response( + turn_context, invoke_response, HTTPStatus.PRECONDITION_FAILED + ) + + return False + + return True diff --git a/libraries/botbuilder-core/botbuilder/core/telemetry_constants.py b/libraries/botbuilder-core/botbuilder/core/telemetry_constants.py index 1ae0f1816..a67a56fbd 100644 --- a/libraries/botbuilder-core/botbuilder/core/telemetry_constants.py +++ b/libraries/botbuilder-core/botbuilder/core/telemetry_constants.py @@ -5,6 +5,7 @@ class TelemetryConstants: """Telemetry logger property names.""" + ATTACHMENTS_PROPERTY: str = "attachments" CHANNEL_ID_PROPERTY: str = "channelId" CONVERSATION_ID_PROPERTY: str = "conversationId" CONVERSATION_NAME_PROPERTY: str = "conversationName" diff --git a/libraries/botbuilder-core/botbuilder/core/telemetry_logger_middleware.py b/libraries/botbuilder-core/botbuilder/core/telemetry_logger_middleware.py index 251bf7fb7..d14c3f7f2 100644 --- a/libraries/botbuilder-core/botbuilder/core/telemetry_logger_middleware.py +++ b/libraries/botbuilder-core/botbuilder/core/telemetry_logger_middleware.py @@ -1,9 +1,13 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. """Middleware Component for logging Activity messages.""" - from typing import Awaitable, Callable, List, Dict +from jsonpickle import encode + from botbuilder.schema import Activity, ConversationReference, ActivityTypes +from botbuilder.schema.teams import TeamsChannelData, TeamInfo +from botframework.connector import Channels + from .bot_telemetry_client import BotTelemetryClient from .bot_assert import BotAssert from .middleware_set import Middleware @@ -31,7 +35,7 @@ def telemetry_client(self) -> BotTelemetryClient: @property def log_personal_information(self) -> bool: - """ Gets a value indicating whether determines whether to log personal + """Gets a value indicating whether determines whether to log personal information that came from the user.""" return self._log_personal_information @@ -160,23 +164,33 @@ async def fill_receive_event_properties( BotTelemetryClient.track_event method for the BotMessageReceived event. """ properties = { - TelemetryConstants.FROM_ID_PROPERTY: activity.from_property.id, + TelemetryConstants.FROM_ID_PROPERTY: ( + activity.from_property.id if activity.from_property else None + ), TelemetryConstants.CONVERSATION_NAME_PROPERTY: activity.conversation.name, TelemetryConstants.LOCALE_PROPERTY: activity.locale, TelemetryConstants.RECIPIENT_ID_PROPERTY: activity.recipient.id, - TelemetryConstants.RECIPIENT_NAME_PROPERTY: activity.from_property.name, + TelemetryConstants.RECIPIENT_NAME_PROPERTY: activity.recipient.name, } if self.log_personal_information: - if activity.from_property.name and activity.from_property.name.strip(): - properties[ - TelemetryConstants.FROM_NAME_PROPERTY - ] = activity.from_property.name + if ( + activity.from_property + and activity.from_property.name + and activity.from_property.name.strip() + ): + properties[TelemetryConstants.FROM_NAME_PROPERTY] = ( + activity.from_property.name + ) if activity.text and activity.text.strip(): properties[TelemetryConstants.TEXT_PROPERTY] = activity.text if activity.speak and activity.speak.strip(): properties[TelemetryConstants.SPEAK_PROPERTY] = activity.speak + TelemetryLoggerMiddleware.__populate_additional_channel_properties( + activity, properties + ) + # Additional properties can override "stock" properties if additional_properties: for prop in additional_properties: @@ -205,10 +219,14 @@ async def fill_send_event_properties( # Use the LogPersonalInformation flag to toggle logging PII data, text and user name are common examples if self.log_personal_information: + if activity.attachments and len(activity.attachments) > 0: + properties[TelemetryConstants.ATTACHMENTS_PROPERTY] = encode( + activity.attachments + ) if activity.from_property.name and activity.from_property.name.strip(): - properties[ - TelemetryConstants.FROM_NAME_PROPERTY - ] = activity.from_property.name + properties[TelemetryConstants.FROM_NAME_PROPERTY] = ( + activity.from_property.name + ) if activity.text and activity.text.strip(): properties[TelemetryConstants.TEXT_PROPERTY] = activity.text if activity.speak and activity.speak.strip(): @@ -278,3 +296,28 @@ async def fill_delete_event_properties( properties[prop.key] = prop.value return properties + + @staticmethod + def __populate_additional_channel_properties( + activity: Activity, + properties: dict, + ): + if activity.channel_id == Channels.ms_teams: + teams_channel_data: TeamsChannelData = TeamsChannelData().deserialize( + activity.channel_data + ) + + properties["TeamsTenantId"] = ( + teams_channel_data.tenant.id + if teams_channel_data and teams_channel_data.tenant + else "" + ) + + properties["TeamsUserAadObjectId"] = ( + activity.from_property.aad_object_id if activity.from_property else "" + ) + + if teams_channel_data and teams_channel_data.team: + properties["TeamsTeamInfo"] = TeamInfo.serialize( + teams_channel_data.team + ) diff --git a/libraries/botbuilder-core/botbuilder/core/transcript_logger.py b/libraries/botbuilder-core/botbuilder/core/transcript_logger.py index a35d18d75..5aa1ea726 100644 --- a/libraries/botbuilder-core/botbuilder/core/transcript_logger.py +++ b/libraries/botbuilder-core/botbuilder/core/transcript_logger.py @@ -2,14 +2,20 @@ # Licensed under the MIT License. """Logs incoming and outgoing activities to a TranscriptStore..""" -import datetime +from datetime import datetime, timezone import copy import random import string from queue import Queue from abc import ABC, abstractmethod from typing import Awaitable, Callable, List -from botbuilder.schema import Activity, ActivityTypes, ConversationReference +from botbuilder.schema import ( + Activity, + ActivityEventNames, + ActivityTypes, + ChannelAccount, + ConversationReference, +) from .middleware_set import Middleware from .turn_context import TurnContext @@ -46,9 +52,17 @@ async def on_turn( activity = context.activity # Log incoming activity at beginning of turn if activity: + if not activity.from_property: + activity.from_property = ChannelAccount() if not activity.from_property.role: activity.from_property.role = "user" - self.log_activity(transcript, copy.copy(activity)) + + # We should not log ContinueConversation events used by skills to initialize the middleware. + if not ( + context.activity.type == ActivityTypes.event + and context.activity.name == ActivityEventNames.continue_conversation + ): + await self.log_activity(transcript, copy.copy(activity)) # hook up onSend pipeline # pylint: disable=unused-argument @@ -61,7 +75,7 @@ async def send_activities_handler( responses = await next_send() for index, activity in enumerate(activities): cloned_activity = copy.copy(activity) - if index < len(responses): + if responses and index < len(responses): cloned_activity.id = responses[index].id # For certain channels, a ResourceResponse with an id is not always sent to the bot. @@ -72,14 +86,14 @@ async def send_activities_handler( prefix = "g_" + "".join( random.choice(alphanumeric) for i in range(5) ) - epoch = datetime.datetime.utcfromtimestamp(0) + epoch = datetime.fromtimestamp(0, timezone.utc) if cloned_activity.timestamp: reference = cloned_activity.timestamp else: - reference = datetime.datetime.today() + reference = datetime.now(timezone.utc) delta = (reference - epoch).total_seconds() * 1000 cloned_activity.id = f"{prefix}{delta}" - self.log_activity(transcript, cloned_activity) + await self.log_activity(transcript, cloned_activity) return responses context.on_send_activities(send_activities_handler) @@ -92,7 +106,7 @@ async def update_activity_handler( response = await next_update() update_activity = copy.copy(activity) update_activity.type = ActivityTypes.message_update - self.log_activity(transcript, update_activity) + await self.log_activity(transcript, update_activity) return response context.on_update_activity(update_activity_handler) @@ -112,7 +126,7 @@ async def delete_activity_handler( deleted_activity: Activity = TurnContext.apply_conversation_reference( delete_msg, reference, False ) - self.log_activity(transcript, deleted_activity) + await self.log_activity(transcript, deleted_activity) context.on_delete_activity(delete_activity_handler) @@ -127,7 +141,7 @@ async def delete_activity_handler( await self.logger.log_activity(activity) transcript.task_done() - def log_activity(self, transcript: Queue, activity: Activity) -> None: + async def log_activity(self, transcript: Queue, activity: Activity) -> None: """Logs the activity. :param transcript: transcript. :param activity: Activity to log. @@ -136,7 +150,7 @@ def log_activity(self, transcript: Queue, activity: Activity) -> None: class TranscriptStore(TranscriptLogger): - """ Transcript storage for conversations.""" + """Transcript storage for conversations.""" @abstractmethod async def get_transcript_activities( diff --git a/libraries/botbuilder-core/botbuilder/core/turn_context.py b/libraries/botbuilder-core/botbuilder/core/turn_context.py index e679907ba..72e25726c 100644 --- a/libraries/botbuilder-core/botbuilder/core/turn_context.py +++ b/libraries/botbuilder-core/botbuilder/core/turn_context.py @@ -3,8 +3,9 @@ import re from copy import copy, deepcopy -from datetime import datetime +from datetime import datetime, timezone from typing import List, Callable, Union, Dict +from botframework.connector import Channels from botbuilder.schema import ( Activity, ActivityTypes, @@ -18,6 +19,9 @@ class TurnContext: + # Same constant as in the BF Adapter, duplicating here to avoid circular dependency + _INVOKE_RESPONSE_KEY = "BotFrameworkAdapter.InvokeResponse" + def __init__(self, adapter_or_context, request: Activity = None): """ Creates a new TurnContext instance. @@ -154,7 +158,7 @@ async def send_activity( activity_or_text: Union[Activity, str], speak: str = None, input_hint: str = None, - ) -> ResourceResponse: + ) -> Union[ResourceResponse, None]: """ Sends a single activity or message to the user. :param activity_or_text: @@ -202,6 +206,11 @@ async def logic(): responses = [] for activity in output: self.buffered_reply_activities.append(activity) + # Ensure the TurnState has the InvokeResponseKey, since this activity + # is not being sent through the adapter, where it would be added to TurnState. + if activity.type == ActivityTypes.invoke_response: + self.turn_state[TurnContext._INVOKE_RESPONSE_KEY] = activity + responses.append(ResourceResponse()) if sent_non_trace_activity: @@ -299,7 +308,7 @@ async def send_trace_activity( ) -> ResourceResponse: trace_activity = Activity( type=ActivityTypes.trace, - timestamp=datetime.utcnow(), + timestamp=datetime.now(timezone.utc), name=name, value=value, value_type=value_type, @@ -320,11 +329,18 @@ def get_conversation_reference(activity: Activity) -> ConversationReference: :return: """ return ConversationReference( - activity_id=activity.id, + activity_id=( + activity.id + if activity.type != ActivityTypes.conversation_update + and activity.channel_id != Channels.direct_line + and activity.channel_id != Channels.webchat + else None + ), user=copy(activity.from_property), bot=copy(activity.recipient), conversation=copy(activity.conversation), channel_id=activity.channel_id, + locale=activity.locale, service_url=activity.service_url, ) @@ -342,6 +358,7 @@ def apply_conversation_reference( :return: """ activity.channel_id = reference.channel_id + activity.locale = reference.locale activity.service_url = reference.service_url activity.conversation = reference.conversation if is_incoming: @@ -379,9 +396,13 @@ def remove_mention_text(activity: Activity, identifier: str) -> str: mentions = TurnContext.get_mentions(activity) for mention in mentions: if mention.additional_properties["mentioned"]["id"] == identifier: + replace_text = ( + mention.additional_properties.get("text") + or mention.additional_properties.get("mentioned")["name"] + ) mention_name_match = re.match( r"(.*?)<\/at>", - escape(mention.additional_properties["text"]), + escape(replace_text), re.IGNORECASE, ) if mention_name_match: diff --git a/libraries/botbuilder-core/botbuilder/core/user_state.py b/libraries/botbuilder-core/botbuilder/core/user_state.py index ab4b3f676..7cd23f8b1 100644 --- a/libraries/botbuilder-core/botbuilder/core/user_state.py +++ b/libraries/botbuilder-core/botbuilder/core/user_state.py @@ -23,7 +23,7 @@ def __init__(self, storage: Storage, namespace=""): """ self.namespace = namespace - super(UserState, self).__init__(storage, "UserState") + super(UserState, self).__init__(storage, "Internal.UserState") def get_storage_key(self, turn_context: TurnContext) -> str: """ diff --git a/libraries/botbuilder-core/requirements.txt b/libraries/botbuilder-core/requirements.txt index 330c0f2c4..4b9aabc5a 100644 --- a/libraries/botbuilder-core/requirements.txt +++ b/libraries/botbuilder-core/requirements.txt @@ -1,7 +1,8 @@ -msrest==0.6.10 -botframework-connector>=4.7.1 -botbuilder-schema>=4.7.1 -requests==2.22.0 -PyJWT==1.5.3 -cryptography==2.8.0 +msrest== 0.7.* +botframework-connector==4.17.0 +botbuilder-schema==4.17.0 +botframework-streaming==4.17.0 +requests==2.32.0 +PyJWT==2.4.0 +cryptography==43.0.1 aiounittest==1.3.0 \ No newline at end of file diff --git a/libraries/botbuilder-core/setup.py b/libraries/botbuilder-core/setup.py index 21a356b49..24267bfb6 100644 --- a/libraries/botbuilder-core/setup.py +++ b/libraries/botbuilder-core/setup.py @@ -4,11 +4,12 @@ import os from setuptools import setup -VERSION = os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" +VERSION = os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" REQUIRES = [ - "botbuilder-schema>=4.7.1", - "botframework-connector>=4.7.1", - "jsonpickle==1.2", + "botbuilder-schema==4.17.0", + "botframework-connector==4.17.0", + "botframework-streaming==4.17.0", + "jsonpickle>=1.2,<4", ] root = os.path.abspath(os.path.dirname(__file__)) @@ -37,7 +38,9 @@ "botbuilder.core.inspection", "botbuilder.core.integration", "botbuilder.core.skills", + "botbuilder.core.streaming", "botbuilder.core.teams", + "botbuilder.core.oauth", ], install_requires=REQUIRES, classifiers=[ diff --git a/libraries/botbuilder-core/tests/simple_adapter.py b/libraries/botbuilder-core/tests/simple_adapter.py index 0ded46f45..2ba3f31b8 100644 --- a/libraries/botbuilder-core/tests/simple_adapter.py +++ b/libraries/botbuilder-core/tests/simple_adapter.py @@ -10,6 +10,7 @@ ResourceResponse, ConversationParameters, ) +from botbuilder.schema.teams import TeamsChannelAccount class SimpleAdapter(BotAdapter): @@ -58,7 +59,7 @@ async def send_activities( return responses - async def create_conversation( + async def create_conversation( # pylint: disable=arguments-differ self, reference: ConversationReference, logic: Callable[[TurnContext], Awaitable] = None, @@ -74,8 +75,23 @@ async def update_activity(self, context: TurnContext, activity: Activity): if self._call_on_update is not None: self._call_on_update(activity) - return ResourceResponse(activity.id) + return ResourceResponse(id=activity.id) async def process_request(self, activity, handler): context = TurnContext(self, activity) return await self.run_pipeline(context, handler) + + async def create_connector_client(self, service_url: str): + return TestConnectorClient() + + +class TestConnectorClient: + def __init__(self) -> None: + self.conversations = TestConversations() + + +class TestConversations: + async def get_conversation_member( # pylint: disable=unused-argument + self, conversation_id, member_id + ): + return TeamsChannelAccount(id=member_id) diff --git a/libraries/botbuilder-core/tests/skills/test_skill_conversation_id_factory.py b/libraries/botbuilder-core/tests/skills/test_skill_conversation_id_factory.py new file mode 100644 index 000000000..7b5dea414 --- /dev/null +++ b/libraries/botbuilder-core/tests/skills/test_skill_conversation_id_factory.py @@ -0,0 +1,126 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import uuid4 as uuid +from aiounittest import AsyncTestCase +from botbuilder.core import MemoryStorage +from botbuilder.schema import ( + Activity, + ConversationAccount, + ConversationReference, +) +from botbuilder.core.skills import ( + BotFrameworkSkill, + SkillConversationIdFactory, + SkillConversationIdFactoryOptions, +) + + +class SkillConversationIdFactoryForTest(AsyncTestCase): + SERVICE_URL = "http://testbot.com/api/messages" + SKILL_ID = "skill" + + @classmethod + def setUpClass(cls): + cls._skill_conversation_id_factory = SkillConversationIdFactory(MemoryStorage()) + cls._application_id = str(uuid()) + cls._bot_id = str(uuid()) + + async def test_skill_conversation_id_factory_happy_path(self): + conversation_reference = self._build_conversation_reference() + + # Create skill conversation + skill_conversation_id = ( + await self._skill_conversation_id_factory.create_skill_conversation_id( + options=SkillConversationIdFactoryOptions( + activity=self._build_message_activity(conversation_reference), + bot_framework_skill=self._build_bot_framework_skill(), + from_bot_id=self._bot_id, + from_bot_oauth_scope=self._bot_id, + ) + ) + ) + + assert ( + skill_conversation_id and skill_conversation_id.strip() + ), "Expected a valid skill conversation ID to be created" + + # Retrieve skill conversation + retrieved_conversation_reference = ( + await self._skill_conversation_id_factory.get_skill_conversation_reference( + skill_conversation_id + ) + ) + + # Delete + await self._skill_conversation_id_factory.delete_conversation_reference( + skill_conversation_id + ) + + # Retrieve again + deleted_conversation_reference = ( + await self._skill_conversation_id_factory.get_skill_conversation_reference( + skill_conversation_id + ) + ) + + self.assertIsNotNone(retrieved_conversation_reference) + self.assertIsNotNone(retrieved_conversation_reference.conversation_reference) + self.assertEqual( + conversation_reference, + retrieved_conversation_reference.conversation_reference, + ) + self.assertIsNone(deleted_conversation_reference) + + async def test_id_is_unique_each_time(self): + conversation_reference = self._build_conversation_reference() + + # Create skill conversation + first_id = ( + await self._skill_conversation_id_factory.create_skill_conversation_id( + options=SkillConversationIdFactoryOptions( + activity=self._build_message_activity(conversation_reference), + bot_framework_skill=self._build_bot_framework_skill(), + from_bot_id=self._bot_id, + from_bot_oauth_scope=self._bot_id, + ) + ) + ) + + second_id = ( + await self._skill_conversation_id_factory.create_skill_conversation_id( + options=SkillConversationIdFactoryOptions( + activity=self._build_message_activity(conversation_reference), + bot_framework_skill=self._build_bot_framework_skill(), + from_bot_id=self._bot_id, + from_bot_oauth_scope=self._bot_id, + ) + ) + ) + + # Ensure that we get a different conversation_id each time we call create_skill_conversation_id + self.assertNotEqual(first_id, second_id) + + def _build_conversation_reference(self) -> ConversationReference: + return ConversationReference( + conversation=ConversationAccount(id=str(uuid())), + service_url=self.SERVICE_URL, + ) + + def _build_message_activity( + self, conversation_reference: ConversationReference + ) -> Activity: + if not conversation_reference: + raise TypeError(str(conversation_reference)) + + activity = Activity.create_message_activity() + activity.apply_conversation_reference(conversation_reference) + + return activity + + def _build_bot_framework_skill(self) -> BotFrameworkSkill: + return BotFrameworkSkill( + app_id=self._application_id, + id=self.SKILL_ID, + skill_endpoint=self.SERVICE_URL, + ) diff --git a/libraries/botbuilder-core/tests/skills/test_skill_handler.py b/libraries/botbuilder-core/tests/skills/test_skill_handler.py index cbe61d0d0..66d79c2ce 100644 --- a/libraries/botbuilder-core/tests/skills/test_skill_handler.py +++ b/libraries/botbuilder-core/tests/skills/test_skill_handler.py @@ -1,14 +1,33 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import hashlib import json +from datetime import datetime from uuid import uuid4 from asyncio import Future -from typing import Dict, List +from typing import Dict, List, Callable from unittest.mock import Mock, MagicMock import aiounittest -from botbuilder.core import TurnContext, BotActionNotImplementedError -from botbuilder.core.skills import ConversationIdFactoryBase, SkillHandler +from botframework.connector.auth import ( + AuthenticationConfiguration, + AuthenticationConstants, + ClaimsIdentity, +) +from botbuilder.core import ( + TurnContext, + BotActionNotImplementedError, + conversation_reference_extension, +) +from botbuilder.core.skills import ( + ConversationIdFactoryBase, + SkillHandler, + SkillConversationReference, + SkillConversationIdFactoryOptions, + BotFrameworkSkill, +) from botbuilder.schema import ( Activity, ActivityTypes, @@ -22,15 +41,48 @@ PagedMembersResult, ResourceResponse, Transcript, -) -from botframework.connector.auth import ( - AuthenticationConfiguration, - AuthenticationConstants, - ClaimsIdentity, + CallerIdConstants, ) -class ConversationIdFactoryForTest(ConversationIdFactoryBase): +class ConversationIdFactoryForTest( + ConversationIdFactoryBase +): # pylint: disable=abstract-method + def __init__(self): + self._conversation_refs: Dict[str, str] = {} + + async def create_skill_conversation_id( # pylint: disable=W0221 + self, options: SkillConversationIdFactoryOptions + ) -> str: + conversation_reference = TurnContext.get_conversation_reference( + options.activity + ) + + key = hashlib.md5( + f"{conversation_reference.conversation.id}{conversation_reference.service_url}".encode() + ).hexdigest() + + skill_conversation_reference = SkillConversationReference( + conversation_reference=conversation_reference, + oauth_scope=options.from_bot_oauth_scope, + ) + + self._conversation_refs[key] = skill_conversation_reference + + return key + + async def get_skill_conversation_reference( + self, skill_conversation_id: str + ) -> SkillConversationReference: + return self._conversation_refs[skill_conversation_id] + + async def delete_conversation_reference(self, skill_conversation_id: str): + pass + + +class LegacyConversationIdFactoryForTest( + ConversationIdFactoryBase +): # pylint: disable=abstract-method def __init__(self): self._conversation_refs: Dict[str, str] = {} @@ -57,22 +109,29 @@ async def get_conversation_reference( return conversation_reference async def delete_conversation_reference(self, skill_conversation_id: str): - raise NotImplementedError() + pass class SkillHandlerInstanceForTests(SkillHandler): async def test_on_get_conversations( - self, claims_identity: ClaimsIdentity, continuation_token: str = "", + self, + claims_identity: ClaimsIdentity, + continuation_token: str = "", ) -> ConversationsResult: return await self.on_get_conversations(claims_identity, continuation_token) async def test_on_create_conversation( - self, claims_identity: ClaimsIdentity, parameters: ConversationParameters, + self, + claims_identity: ClaimsIdentity, + parameters: ConversationParameters, ) -> ConversationResourceResponse: return await self.on_create_conversation(claims_identity, parameters) async def test_on_send_to_conversation( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity: Activity, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity: Activity, ) -> ResourceResponse: return await self.on_send_to_conversation( claims_identity, conversation_id, activity @@ -111,14 +170,19 @@ async def test_on_reply_to_activity( ) async def test_on_delete_activity( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, ): return await self.on_delete_activity( claims_identity, conversation_id, activity_id ) async def test_on_get_conversation_members( - self, claims_identity: ClaimsIdentity, conversation_id: str, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, ) -> List[ChannelAccount]: return await self.on_get_conversation_members(claims_identity, conversation_id) @@ -134,14 +198,20 @@ async def test_on_get_conversation_paged_members( ) async def test_on_delete_conversation_member( - self, claims_identity: ClaimsIdentity, conversation_id: str, member_id: str, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + member_id: str, ): return await self.on_delete_conversation_member( claims_identity, conversation_id, member_id ) async def test_on_get_activity_members( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str, + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, ) -> List[ChannelAccount]: return await self.on_get_activity_members( claims_identity, conversation_id, activity_id @@ -165,111 +235,386 @@ async def test_on_upload_attachment( class TestSkillHandler(aiounittest.AsyncTestCase): @classmethod def setUpClass(cls): - bot_id = str(uuid4()) - skill_id = str(uuid4()) + cls.bot_id = str(uuid4()) + cls.skill_id = str(uuid4()) cls._test_id_factory = ConversationIdFactoryForTest() cls._claims_identity = ClaimsIdentity({}, False) - cls._claims_identity.claims[AuthenticationConstants.AUDIENCE_CLAIM] = bot_id - cls._claims_identity.claims[AuthenticationConstants.APP_ID_CLAIM] = skill_id - cls._claims_identity.claims[ - AuthenticationConstants.SERVICE_URL_CLAIM - ] = "http://testbot.com/api/messages" + cls._claims_identity.claims[AuthenticationConstants.AUDIENCE_CLAIM] = cls.bot_id + cls._claims_identity.claims[AuthenticationConstants.APP_ID_CLAIM] = cls.skill_id + cls._claims_identity.claims[AuthenticationConstants.SERVICE_URL_CLAIM] = ( + "http://testbot.com/api/messages" + ) cls._conversation_reference = ConversationReference( conversation=ConversationAccount(id=str(uuid4())), service_url="http://testbot.com/api/messages", ) + activity = Activity.create_message_activity() + activity.apply_conversation_reference(cls._conversation_reference) + skill = BotFrameworkSkill( + app_id=cls.skill_id, + id="skill", + skill_endpoint="http://testbot.com/api/messages", + ) + cls._options = SkillConversationIdFactoryOptions( + from_bot_oauth_scope=cls.bot_id, + from_bot_id=cls.bot_id, + activity=activity, + bot_framework_skill=skill, + ) + + def create_skill_handler_for_testing( + self, adapter, factory: ConversationIdFactoryBase = None + ) -> SkillHandlerInstanceForTests: + mock_bot = Mock() + mock_bot.on_turn = MagicMock(return_value=Future()) + mock_bot.on_turn.return_value.set_result(Mock()) - def create_skill_handler_for_testing(self, adapter) -> SkillHandlerInstanceForTests: return SkillHandlerInstanceForTests( adapter, - Mock(), - self._test_id_factory, + mock_bot, + (factory or self._test_id_factory), Mock(), AuthenticationConfiguration(), ) - async def test_on_send_to_conversation(self): - self._conversation_id = await self._test_id_factory.create_skill_conversation_id( - self._conversation_reference + async def test_legacy_conversation_id_factory(self): + mock_adapter = Mock() + + legacy_factory = LegacyConversationIdFactoryForTest() + conversation_reference = ConversationReference( + conversation=ConversationAccount(id=str(uuid4())), + service_url="http://testbot.com/api/messages", ) - mock_adapter = Mock() - mock_adapter.continue_conversation = MagicMock(return_value=Future()) - mock_adapter.continue_conversation.return_value.set_result(Mock()) + conversation_id = await legacy_factory.create_skill_conversation_id( + conversation_reference + ) - sut = self.create_skill_handler_for_testing(mock_adapter) + async def continue_conversation( + reference: ConversationReference, + callback: Callable, + bot_id: str = None, + claims_identity: ClaimsIdentity = None, + audience: str = None, + ): # pylint: disable=unused-argument + # Invoke the callback created by the handler so we can assert the rest of the execution. + turn_context = TurnContext( + mock_adapter, + conversation_reference_extension.get_continuation_activity( + conversation_reference + ), + ) + await callback(turn_context) - activity = Activity(type=ActivityTypes.message, attachments=[], entities=[]) - TurnContext.apply_conversation_reference(activity, self._conversation_reference) + async def send_activities( + context: TurnContext, activities: List[Activity] + ): # pylint: disable=unused-argument + return [ResourceResponse(id="resourceId")] + + mock_adapter.continue_conversation = continue_conversation + mock_adapter.send_activities = send_activities + + activity = Activity.create_message_activity() + activity.apply_conversation_reference(conversation_reference) + sut = self.create_skill_handler_for_testing(mock_adapter, legacy_factory) await sut.test_on_send_to_conversation( - self._claims_identity, self._conversation_id, activity + self._claims_identity, conversation_id, activity ) - args, kwargs = mock_adapter.continue_conversation.call_args_list[0] + async def test_on_send_to_conversation(self): + conversation_id = await self._test_id_factory.create_skill_conversation_id( + self._options + ) - assert isinstance(args[0], ConversationReference) - assert callable(args[1]) - assert isinstance(kwargs["claims_identity"], ClaimsIdentity) + # python 3.7 doesn't support AsyncMock, change this when min ver is 3.8 + send_activities_called = False - async def test_on_reply_to_activity(self): - self._conversation_id = await self._test_id_factory.create_skill_conversation_id( - self._conversation_reference + mock_adapter = Mock() + + async def continue_conversation( + reference: ConversationReference, + callback: Callable, + bot_id: str = None, + claims_identity: ClaimsIdentity = None, + audience: str = None, + ): # pylint: disable=unused-argument + # Invoke the callback created by the handler so we can assert the rest of the execution. + turn_context = TurnContext( + mock_adapter, + conversation_reference_extension.get_continuation_activity( + self._conversation_reference + ), + ) + await callback(turn_context) + + # Assert the callback set the right properties. + assert ( + f"{CallerIdConstants.bot_to_bot_prefix}{self.skill_id}" + ), turn_context.activity.caller_id + + async def send_activities( + context: TurnContext, activities: List[Activity] + ): # pylint: disable=unused-argument + # Messages should not have a caller id set when sent back to the caller. + nonlocal send_activities_called + assert activities[0].caller_id is None + assert activities[0].reply_to_id is None + send_activities_called = True + return [ResourceResponse(id="resourceId")] + + mock_adapter.continue_conversation = continue_conversation + mock_adapter.send_activities = send_activities + + types_to_test = [ + ActivityTypes.end_of_conversation, + ActivityTypes.event, + ActivityTypes.message, + ] + + for activity_type in types_to_test: + with self.subTest(act_type=activity_type): + send_activities_called = False + activity = Activity(type=activity_type, attachments=[], entities=[]) + TurnContext.apply_conversation_reference( + activity, self._conversation_reference + ) + sut = self.create_skill_handler_for_testing(mock_adapter) + + resource_response = await sut.test_on_send_to_conversation( + self._claims_identity, conversation_id, activity + ) + + if activity_type == ActivityTypes.message: + assert send_activities_called + assert resource_response.id == "resourceId" + + async def test_forwarding_on_send_to_conversation(self): + conversation_id = await self._test_id_factory.create_skill_conversation_id( + self._options ) + resource_response_id = "rId" + + async def side_effect( + *arg_list, **args_dict + ): # pylint: disable=unused-argument + fake_context = Mock() + fake_context.turn_state = {} + fake_context.send_activity = MagicMock(return_value=Future()) + fake_context.send_activity.return_value.set_result( + ResourceResponse(id=resource_response_id) + ) + await arg_list[1](fake_context) + mock_adapter = Mock() - mock_adapter.continue_conversation = MagicMock(return_value=Future()) - mock_adapter.continue_conversation.return_value.set_result(Mock()) + mock_adapter.continue_conversation = side_effect + mock_adapter.send_activities = MagicMock(return_value=Future()) + mock_adapter.send_activities.return_value.set_result([]) sut = self.create_skill_handler_for_testing(mock_adapter) activity = Activity(type=ActivityTypes.message, attachments=[], entities=[]) - activity_id = str(uuid4()) TurnContext.apply_conversation_reference(activity, self._conversation_reference) - await sut.test_on_reply_to_activity( - self._claims_identity, self._conversation_id, activity_id, activity + assert not activity.caller_id + + response = await sut.test_on_send_to_conversation( + self._claims_identity, conversation_id, activity ) - args, kwargs = mock_adapter.continue_conversation.call_args_list[0] + assert response.id is resource_response_id - assert isinstance(args[0], ConversationReference) - assert callable(args[1]) - assert isinstance(kwargs["claims_identity"], ClaimsIdentity) + async def test_on_reply_to_activity(self): + resource_response_id = "resourceId" + conversation_id = await self._test_id_factory.create_skill_conversation_id( + self._options + ) + + types_to_test = [ + ActivityTypes.end_of_conversation, + ActivityTypes.event, + ActivityTypes.message, + ] + + for activity_type in types_to_test: + with self.subTest(act_type=activity_type): + mock_adapter = Mock() + mock_adapter.continue_conversation = MagicMock(return_value=Future()) + mock_adapter.continue_conversation.return_value.set_result(Mock()) + mock_adapter.send_activities = MagicMock(return_value=Future()) + mock_adapter.send_activities.return_value.set_result( + [ResourceResponse(id=resource_response_id)] + ) + + sut = self.create_skill_handler_for_testing(mock_adapter) + + activity = Activity(type=activity_type, attachments=[], entities=[]) + activity_id = str(uuid4()) + TurnContext.apply_conversation_reference( + activity, self._conversation_reference + ) + + resource_response = await sut.test_on_reply_to_activity( + self._claims_identity, conversation_id, activity_id, activity + ) + + # continue_conversation validation + ( + args_continue, + kwargs_continue, + ) = mock_adapter.continue_conversation.call_args_list[0] + mock_adapter.continue_conversation.assert_called_once() + + assert isinstance(args_continue[0], ConversationReference) + assert callable(args_continue[1]) + assert isinstance(kwargs_continue["claims_identity"], ClaimsIdentity) + + turn_context = TurnContext( + mock_adapter, + conversation_reference_extension.get_continuation_activity( + self._conversation_reference + ), + ) + await args_continue[1](turn_context) + # assert the callback set the right properties. + assert ( + f"{CallerIdConstants.bot_to_bot_prefix}{self.skill_id}" + ), turn_context.activity.caller_id + + if activity_type == ActivityTypes.message: + # send_activities validation + ( + args_send, + _, + ) = mock_adapter.send_activities.call_args_list[0] + activity_from_send = args_send[1][0] + assert activity_from_send.caller_id is None + assert activity_from_send.reply_to_id, activity_id + assert resource_response.id, resource_response_id + else: + # Assert mock SendActivitiesAsync wasn't called + mock_adapter.send_activities.assert_not_called() async def test_on_update_activity(self): - self._conversation_id = "" + conversation_id = await self._test_id_factory.create_skill_conversation_id( + self._options + ) + resource_response_id = "resourceId" + called_continue = False + called_update = False mock_adapter = Mock() + activity = Activity(type=ActivityTypes.message, attachments=[], entities=[]) + activity_id = str(uuid4()) + message = activity.text = f"TestUpdate {datetime.now()}." + + async def continue_conversation( + reference: ConversationReference, + callback: Callable, + bot_id: str = None, + claims_identity: ClaimsIdentity = None, + audience: str = None, + ): # pylint: disable=unused-argument + # Invoke the callback created by the handler so we can assert the rest of the execution. + nonlocal called_continue + turn_context = TurnContext( + mock_adapter, + conversation_reference_extension.get_continuation_activity( + self._conversation_reference + ), + ) + await callback(turn_context) + + # Assert the callback set the right properties. + assert ( + f"{CallerIdConstants.bot_to_bot_prefix}{self.skill_id}" + ), turn_context.activity.caller_id + called_continue = True + + async def update_activity( + context: TurnContext, # pylint: disable=unused-argument + new_activity: Activity, + ) -> ResourceResponse: + # Assert the activity being sent. + nonlocal called_update + assert activity_id, new_activity.reply_to_id + assert message, new_activity.text + called_update = True + + return ResourceResponse(id=resource_response_id) + + mock_adapter.continue_conversation = continue_conversation + mock_adapter.update_activity = update_activity sut = self.create_skill_handler_for_testing(mock_adapter) + resource_response = await sut.test_on_update_activity( + self._claims_identity, conversation_id, activity_id, activity + ) - activity = Activity(type=ActivityTypes.message, attachments=[], entities=[]) + assert called_continue + assert called_update + assert resource_response, resource_response_id + + async def test_on_delete_activity(self): + conversation_id = await self._test_id_factory.create_skill_conversation_id( + self._options + ) + + resource_response_id = "resourceId" + called_continue = False + called_delete = False + + mock_adapter = Mock() activity_id = str(uuid4()) - with self.assertRaises(BotActionNotImplementedError): - await sut.test_on_update_activity( - self._claims_identity, self._conversation_id, activity_id, activity + async def continue_conversation( + reference: ConversationReference, + callback: Callable, + bot_id: str = None, + claims_identity: ClaimsIdentity = None, + audience: str = None, + ): # pylint: disable=unused-argument + # Invoke the callback created by the handler so we can assert the rest of the execution. + nonlocal called_continue + turn_context = TurnContext( + mock_adapter, + conversation_reference_extension.get_continuation_activity( + self._conversation_reference + ), ) + await callback(turn_context) + called_continue = True - async def test_on_delete_activity(self): - self._conversation_id = "" + async def delete_activity( + context: TurnContext, # pylint: disable=unused-argument + conversation_reference: ConversationReference, + ) -> ResourceResponse: + # Assert the activity being sent. + nonlocal called_delete + # Assert the activity_id being deleted. + assert activity_id, conversation_reference.activity_id + called_delete = True - mock_adapter = Mock() + return ResourceResponse(id=resource_response_id) + + mock_adapter.continue_conversation = continue_conversation + mock_adapter.delete_activity = delete_activity sut = self.create_skill_handler_for_testing(mock_adapter) - activity_id = str(uuid4()) - with self.assertRaises(BotActionNotImplementedError): - await sut.test_on_delete_activity( - self._claims_identity, self._conversation_id, activity_id - ) + await sut.test_on_delete_activity( + self._claims_identity, conversation_id, activity_id + ) + + assert called_continue + assert called_delete async def test_on_get_activity_members(self): - self._conversation_id = "" + conversation_id = "" mock_adapter = Mock() @@ -278,7 +623,7 @@ async def test_on_get_activity_members(self): with self.assertRaises(BotActionNotImplementedError): await sut.test_on_get_activity_members( - self._claims_identity, self._conversation_id, activity_id + self._claims_identity, conversation_id, activity_id ) async def test_on_create_conversation(self): @@ -293,19 +638,17 @@ async def test_on_create_conversation(self): ) async def test_on_get_conversations(self): - self._conversation_id = "" + conversation_id = "" mock_adapter = Mock() sut = self.create_skill_handler_for_testing(mock_adapter) with self.assertRaises(BotActionNotImplementedError): - await sut.test_on_get_conversations( - self._claims_identity, self._conversation_id - ) + await sut.test_on_get_conversations(self._claims_identity, conversation_id) async def test_on_get_conversation_members(self): - self._conversation_id = "" + conversation_id = "" mock_adapter = Mock() @@ -313,11 +656,11 @@ async def test_on_get_conversation_members(self): with self.assertRaises(BotActionNotImplementedError): await sut.test_on_get_conversation_members( - self._claims_identity, self._conversation_id + self._claims_identity, conversation_id ) async def test_on_get_conversation_paged_members(self): - self._conversation_id = "" + conversation_id = "" mock_adapter = Mock() @@ -325,11 +668,11 @@ async def test_on_get_conversation_paged_members(self): with self.assertRaises(BotActionNotImplementedError): await sut.test_on_get_conversation_paged_members( - self._claims_identity, self._conversation_id + self._claims_identity, conversation_id ) async def test_on_delete_conversation_member(self): - self._conversation_id = "" + conversation_id = "" mock_adapter = Mock() @@ -338,11 +681,11 @@ async def test_on_delete_conversation_member(self): with self.assertRaises(BotActionNotImplementedError): await sut.test_on_delete_conversation_member( - self._claims_identity, self._conversation_id, member_id + self._claims_identity, conversation_id, member_id ) async def test_on_send_conversation_history(self): - self._conversation_id = "" + conversation_id = "" mock_adapter = Mock() @@ -351,11 +694,11 @@ async def test_on_send_conversation_history(self): with self.assertRaises(BotActionNotImplementedError): await sut.test_on_send_conversation_history( - self._claims_identity, self._conversation_id, transcript + self._claims_identity, conversation_id, transcript ) async def test_on_upload_attachment(self): - self._conversation_id = "" + conversation_id = "" mock_adapter = Mock() @@ -364,5 +707,49 @@ async def test_on_upload_attachment(self): with self.assertRaises(BotActionNotImplementedError): await sut.test_on_upload_attachment( - self._claims_identity, self._conversation_id, attachment_data + self._claims_identity, conversation_id, attachment_data ) + + async def test_event_activity(self): + activity = Activity(type=ActivityTypes.event) + await self.__activity_callback_test(activity) + assert ( + activity.caller_id + == f"{CallerIdConstants.bot_to_bot_prefix}{self.skill_id}" + ) + + async def test_eoc_activity(self): + activity = Activity(type=ActivityTypes.end_of_conversation) + await self.__activity_callback_test(activity) + assert ( + activity.caller_id + == f"{CallerIdConstants.bot_to_bot_prefix}{self.skill_id}" + ) + + async def __activity_callback_test(self, activity: Activity): + conversation_id = await self._test_id_factory.create_skill_conversation_id( + self._options + ) + + mock_adapter = Mock() + mock_adapter.continue_conversation = MagicMock(return_value=Future()) + mock_adapter.continue_conversation.return_value.set_result(Mock()) + mock_adapter.send_activities = MagicMock(return_value=Future()) + mock_adapter.send_activities.return_value.set_result([]) + + sut = self.create_skill_handler_for_testing(mock_adapter) + + activity_id = str(uuid4()) + TurnContext.apply_conversation_reference(activity, self._conversation_reference) + + await sut.test_on_reply_to_activity( + self._claims_identity, conversation_id, activity_id, activity + ) + + args, kwargs = mock_adapter.continue_conversation.call_args_list[0] + + assert isinstance(args[0], ConversationReference) + assert callable(args[1]) + assert isinstance(kwargs["claims_identity"], ClaimsIdentity) + + await args[1](TurnContext(mock_adapter, activity)) diff --git a/libraries/botbuilder-core/tests/streaming/test_streaming_request_handler.py b/libraries/botbuilder-core/tests/streaming/test_streaming_request_handler.py new file mode 100644 index 000000000..262c995e7 --- /dev/null +++ b/libraries/botbuilder-core/tests/streaming/test_streaming_request_handler.py @@ -0,0 +1,56 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from unittest.mock import Mock +from typing import Any + +import aiounittest + +from botbuilder.core.streaming import StreamingRequestHandler +from botframework.streaming.transport.web_socket import ( + WebSocket, + WebSocketState, + WebSocketCloseStatus, + WebSocketMessage, + WebSocketMessageType, +) + + +class MockWebSocket(WebSocket): + # pylint: disable=unused-argument + def __init__(self): + super(MockWebSocket, self).__init__() + + self.receive_called = False + + def dispose(self): + return + + async def close(self, close_status: WebSocketCloseStatus, status_description: str): + return + + async def receive(self) -> WebSocketMessage: + self.receive_called = True + + async def send( + self, buffer: Any, message_type: WebSocketMessageType, end_of_message: bool + ): + raise Exception + + @property + def status(self) -> WebSocketState: + return WebSocketState.OPEN + + +class TestStramingRequestHandler(aiounittest.AsyncTestCase): + async def test_listen(self): + mock_bot = Mock() + mock_activity_processor = Mock() + mock_web_socket = MockWebSocket() + + sut = StreamingRequestHandler( + mock_bot, mock_activity_processor, mock_web_socket + ) + await sut.listen() + + assert mock_web_socket.receive_called diff --git a/libraries/botbuilder-core/tests/teams/simple_adapter_with_create_conversation.py b/libraries/botbuilder-core/tests/teams/simple_adapter_with_create_conversation.py index 477aa3b28..cacfbd5ed 100644 --- a/libraries/botbuilder-core/tests/teams/simple_adapter_with_create_conversation.py +++ b/libraries/botbuilder-core/tests/teams/simple_adapter_with_create_conversation.py @@ -58,7 +58,7 @@ async def send_activities( return responses - async def create_conversation( + async def create_conversation( # pylint: disable=arguments-differ self, reference: ConversationReference, logic: Callable[[TurnContext], Awaitable] = None, @@ -76,7 +76,7 @@ async def update_activity(self, context: TurnContext, activity: Activity): if self._call_on_update is not None: self._call_on_update(activity) - return ResourceResponse(activity.id) + return ResourceResponse(id=activity.id) async def process_request(self, activity, handler): context = TurnContext(self, activity) diff --git a/libraries/botbuilder-core/tests/teams/test_teams_activity_handler.py b/libraries/botbuilder-core/tests/teams/test_teams_activity_handler.py index f2dc9f0ce..257dc75f9 100644 --- a/libraries/botbuilder-core/tests/teams/test_teams_activity_handler.py +++ b/libraries/botbuilder-core/tests/teams/test_teams_activity_handler.py @@ -1,12 +1,21 @@ -from typing import List +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +# pylint: disable=too-many-lines +from typing import List import aiounittest +import sys +import os + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from botbuilder.core import BotAdapter, TurnContext from botbuilder.core.teams import TeamsActivityHandler from botbuilder.schema import ( Activity, ActivityTypes, ChannelAccount, + ConversationAccount, ConversationReference, ResourceResponse, ) @@ -14,6 +23,8 @@ AppBasedLinkQuery, ChannelInfo, FileConsentCardResponse, + MeetingStartEventDetails, + MeetingEndEventDetails, MessageActionsPayload, MessagingExtensionAction, MessagingExtensionQuery, @@ -22,6 +33,12 @@ TaskModuleRequestContext, TeamInfo, TeamsChannelAccount, + TabRequest, + TabSubmit, + TabContext, + MeetingParticipantsEventDetails, + ReadReceiptInfo, + TeamsChannelData, ) from botframework.connector import Channels from simple_adapter import SimpleAdapter @@ -37,12 +54,26 @@ async def on_conversation_update_activity(self, turn_context: TurnContext): self.record.append("on_conversation_update_activity") return await super().on_conversation_update_activity(turn_context) + async def on_teams_members_added( # pylint: disable=unused-argument + self, + teams_members_added: [TeamsChannelAccount], + team_info: TeamInfo, + turn_context: TurnContext, + ): + self.record.append("on_teams_members_added") + return await super().on_teams_members_added( + teams_members_added, team_info, turn_context + ) + async def on_teams_members_removed( - self, teams_members_removed: [TeamsChannelAccount], turn_context: TurnContext + self, + teams_members_removed: [TeamsChannelAccount], + team_info: TeamInfo, + turn_context: TurnContext, ): self.record.append("on_teams_members_removed") return await super().on_teams_members_removed( - teams_members_removed, turn_context + teams_members_removed, team_info, turn_context ) async def on_message_activity(self, turn_context: TurnContext): @@ -85,6 +116,14 @@ async def on_teams_channel_renamed( channel_info, team_info, turn_context ) + async def on_teams_channel_restored( + self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext + ): + self.record.append("on_teams_channel_restored") + return await super().on_teams_channel_restored( + channel_info, team_info, turn_context + ) + async def on_teams_channel_deleted( self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext ): @@ -93,12 +132,42 @@ async def on_teams_channel_deleted( channel_info, team_info, turn_context ) + async def on_teams_team_archived( + self, team_info: TeamInfo, turn_context: TurnContext + ): + self.record.append("on_teams_team_archived") + return await super().on_teams_team_archived(team_info, turn_context) + + async def on_teams_team_deleted( + self, team_info: TeamInfo, turn_context: TurnContext + ): + self.record.append("on_teams_team_deleted") + return await super().on_teams_team_deleted(team_info, turn_context) + + async def on_teams_team_hard_deleted( + self, team_info: TeamInfo, turn_context: TurnContext + ): + self.record.append("on_teams_team_hard_deleted") + return await super().on_teams_team_hard_deleted(team_info, turn_context) + async def on_teams_team_renamed_activity( self, team_info: TeamInfo, turn_context: TurnContext ): self.record.append("on_teams_team_renamed_activity") return await super().on_teams_team_renamed_activity(team_info, turn_context) + async def on_teams_team_restored( + self, team_info: TeamInfo, turn_context: TurnContext + ): + self.record.append("on_teams_team_restored") + return await super().on_teams_team_restored(team_info, turn_context) + + async def on_teams_team_unarchived( + self, team_info: TeamInfo, turn_context: TurnContext + ): + self.record.append("on_teams_team_unarchived") + return await super().on_teams_team_unarchived(team_info, turn_context) + async def on_invoke_activity(self, turn_context: TurnContext): self.record.append("on_invoke_activity") return await super().on_invoke_activity(turn_context) @@ -155,6 +224,14 @@ async def on_teams_messaging_extension_query( self.record.append("on_teams_messaging_extension_query") return await super().on_teams_messaging_extension_query(turn_context, query) + async def on_teams_anonymous_app_based_link_query( + self, turn_context: TurnContext, query: AppBasedLinkQuery + ): + self.record.append("on_teams_anonymous_app_based_link_query") + return await super().on_teams_anonymous_app_based_link_query( + turn_context, query + ) + async def on_teams_messaging_extension_submit_action_dispatch( self, turn_context: TurnContext, action: MessagingExtensionAction ): @@ -201,8 +278,10 @@ async def on_teams_messaging_extension_configuration_query_settings_url( self.record.append( "on_teams_messaging_extension_configuration_query_settings_url" ) - return await super().on_teams_messaging_extension_configuration_query_settings_url( - turn_context, query + return ( + await super().on_teams_messaging_extension_configuration_query_settings_url( + turn_context, query + ) ) async def on_teams_messaging_extension_configuration_setting( @@ -237,6 +316,90 @@ async def on_teams_task_module_submit( # pylint: disable=unused-argument turn_context, task_module_request ) + async def on_teams_tab_fetch( + self, turn_context: TurnContext, tab_request: TabRequest + ): + self.record.append("on_teams_tab_fetch") + return await super().on_teams_tab_fetch(turn_context, tab_request) + + async def on_teams_tab_submit( + self, turn_context: TurnContext, tab_submit: TabSubmit + ): + self.record.append("on_teams_tab_submit") + return await super().on_teams_tab_submit(turn_context, tab_submit) + + async def on_teams_config_fetch(self, turn_context: TurnContext, config_data: any): + self.record.append("on_teams_config_fetch") + return await super().on_teams_config_fetch(turn_context, config_data) + + async def on_teams_config_submit(self, turn_context: TurnContext, config_data: any): + self.record.append("on_teams_config_submit") + return await super().on_teams_config_submit(turn_context, config_data) + + async def on_event_activity(self, turn_context: TurnContext): + self.record.append("on_event_activity") + return await super().on_event_activity(turn_context) + + async def on_teams_read_receipt_event( + self, read_receipt_info: ReadReceiptInfo, turn_context: TurnContext + ): + self.record.append("on_teams_read_receipt_event") + return await super().on_teams_read_receipt_event( + turn_context.activity.value, turn_context + ) + + async def on_teams_meeting_start_event( + self, meeting: MeetingStartEventDetails, turn_context: TurnContext + ): + self.record.append("on_teams_meeting_start_event") + return await super().on_teams_meeting_start_event( + turn_context.activity.value, turn_context + ) + + async def on_teams_meeting_end_event( + self, meeting: MeetingEndEventDetails, turn_context: TurnContext + ): + self.record.append("on_teams_meeting_end_event") + return await super().on_teams_meeting_end_event( + turn_context.activity.value, turn_context + ) + + async def on_message_update_activity(self, turn_context: TurnContext): + self.record.append("on_message_update_activity") + return await super().on_message_update_activity(turn_context) + + async def on_teams_message_edit(self, turn_context: TurnContext): + self.record.append("on_teams_message_edit") + return await super().on_teams_message_edit(turn_context) + + async def on_teams_message_undelete(self, turn_context: TurnContext): + self.record.append("on_teams_message_undelete") + return await super().on_teams_message_undelete(turn_context) + + async def on_message_delete_activity(self, turn_context: TurnContext): + self.record.append("on_message_delete_activity") + return await super().on_message_delete_activity(turn_context) + + async def on_teams_message_soft_delete(self, turn_context: TurnContext): + self.record.append("on_teams_message_soft_delete") + return await super().on_teams_message_soft_delete(turn_context) + + async def on_teams_meeting_participants_join_event( + self, meeting: MeetingParticipantsEventDetails, turn_context: TurnContext + ): + self.record.append("on_teams_meeting_participants_join_event") + return await super().on_teams_meeting_participants_join_event( + turn_context.activity.value, turn_context + ) + + async def on_teams_meeting_participants_leave_event( + self, meeting: MeetingParticipantsEventDetails, turn_context: TurnContext + ): + self.record.append("on_teams_meeting_participants_leave_event") + return await super().on_teams_meeting_participants_leave_event( + turn_context.activity.value, turn_context + ) + class NotImplementedAdapter(BotAdapter): async def delete_activity( @@ -298,6 +461,28 @@ async def test_on_teams_channel_renamed_activity(self): assert bot.record[0] == "on_conversation_update_activity" assert bot.record[1] == "on_teams_channel_renamed" + async def test_on_teams_channel_restored_activity(self): + # arrange + activity = Activity( + type=ActivityTypes.conversation_update, + channel_data={ + "eventType": "channelRestored", + "channel": {"id": "asdfqwerty", "name": "channel_restored"}, + }, + channel_id=Channels.ms_teams, + ) + + turn_context = TurnContext(NotImplementedAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_conversation_update_activity" + assert bot.record[1] == "on_teams_channel_restored" + async def test_on_teams_channel_deleted_activity(self): # arrange activity = Activity( @@ -320,6 +505,72 @@ async def test_on_teams_channel_deleted_activity(self): assert bot.record[0] == "on_conversation_update_activity" assert bot.record[1] == "on_teams_channel_deleted" + async def test_on_teams_team_archived(self): + # arrange + activity = Activity( + type=ActivityTypes.conversation_update, + channel_data={ + "eventType": "teamArchived", + "team": {"id": "team_id_1", "name": "archived_team_name"}, + }, + channel_id=Channels.ms_teams, + ) + + turn_context = TurnContext(NotImplementedAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_conversation_update_activity" + assert bot.record[1] == "on_teams_team_archived" + + async def test_on_teams_team_deleted(self): + # arrange + activity = Activity( + type=ActivityTypes.conversation_update, + channel_data={ + "eventType": "teamDeleted", + "team": {"id": "team_id_1", "name": "deleted_team_name"}, + }, + channel_id=Channels.ms_teams, + ) + + turn_context = TurnContext(NotImplementedAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_conversation_update_activity" + assert bot.record[1] == "on_teams_team_deleted" + + async def test_on_teams_team_hard_deleted(self): + # arrange + activity = Activity( + type=ActivityTypes.conversation_update, + channel_data={ + "eventType": "teamHardDeleted", + "team": {"id": "team_id_1", "name": "hard_deleted_team_name"}, + }, + channel_id=Channels.ms_teams, + ) + + turn_context = TurnContext(NotImplementedAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_conversation_update_activity" + assert bot.record[1] == "on_teams_team_hard_deleted" + async def test_on_teams_team_renamed_activity(self): # arrange activity = Activity( @@ -342,11 +593,128 @@ async def test_on_teams_team_renamed_activity(self): assert bot.record[0] == "on_conversation_update_activity" assert bot.record[1] == "on_teams_team_renamed_activity" + async def test_on_teams_team_restored(self): + # arrange + activity = Activity( + type=ActivityTypes.conversation_update, + channel_data={ + "eventType": "teamRestored", + "team": {"id": "team_id_1", "name": "restored_team_name"}, + }, + channel_id=Channels.ms_teams, + ) + + turn_context = TurnContext(NotImplementedAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_conversation_update_activity" + assert bot.record[1] == "on_teams_team_restored" + + async def test_on_teams_team_unarchived(self): + # arrange + activity = Activity( + type=ActivityTypes.conversation_update, + channel_data={ + "eventType": "teamUnarchived", + "team": {"id": "team_id_1", "name": "unarchived_team_name"}, + }, + channel_id=Channels.ms_teams, + ) + + turn_context = TurnContext(NotImplementedAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_conversation_update_activity" + assert bot.record[1] == "on_teams_team_unarchived" + + async def test_on_teams_members_added_activity(self): + # arrange + activity = Activity( + type=ActivityTypes.conversation_update, + channel_data={ + "eventType": "teamMemberAdded", + "team": {"id": "team_id_1", "name": "new_team_name"}, + }, + members_added=[ + ChannelAccount( + id="123", + name="test_user", + aad_object_id="asdfqwerty", + role="tester", + ) + ], + channel_id=Channels.ms_teams, + conversation=ConversationAccount(id="456"), + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + mock_connector_client = await SimpleAdapter.create_connector_client( + self, turn_context.activity.service_url + ) + turn_context.turn_state[BotAdapter.BOT_CONNECTOR_CLIENT_KEY] = ( + mock_connector_client + ) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_conversation_update_activity" + assert bot.record[1] == "on_teams_members_added" + + async def test_bot_on_teams_members_added_activity(self): + # arrange + activity = Activity( + recipient=ChannelAccount(id="botid"), + type=ActivityTypes.conversation_update, + channel_data={ + "eventType": "teamMemberAdded", + "team": {"id": "team_id_1", "name": "new_team_name"}, + }, + members_added=[ + ChannelAccount( + id="botid", + name="test_user", + aad_object_id="asdfqwerty", + role="tester", + ) + ], + channel_id=Channels.ms_teams, + conversation=ConversationAccount(id="456"), + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_conversation_update_activity" + assert bot.record[1] == "on_teams_members_added" + async def test_on_teams_members_removed_activity(self): # arrange activity = Activity( type=ActivityTypes.conversation_update, - channel_data={"eventType": "teamMemberRemoved"}, + channel_data={ + "eventType": "teamMemberRemoved", + "team": {"id": "team_id_1", "name": "new_team_name"}, + }, members_removed=[ ChannelAccount( id="123", @@ -481,6 +849,25 @@ async def test_on_app_based_link_query(self): assert bot.record[0] == "on_invoke_activity" assert bot.record[1] == "on_teams_messaging_extension_query" + async def test_compose_extension_anonymous_query_link(self): + # arrange + activity = Activity( + type=ActivityTypes.invoke, + name="composeExtension/anonymousQueryLink", + value={"url": "http://www.test.com"}, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_invoke_activity" + assert bot.record[1] == "on_teams_anonymous_app_based_link_query" + async def test_on_teams_messaging_extension_bot_message_preview_edit_activity(self): # Arrange @@ -711,6 +1098,28 @@ async def test_on_teams_task_module_fetch(self): assert bot.record[0] == "on_invoke_activity" assert bot.record[1] == "on_teams_task_module_fetch" + async def test_on_teams_task_module_fetch_none_as_empty(self): + # Arrange + activity = Activity( + type=ActivityTypes.invoke, + name="task/fetch", + value={ + "data": {"key": "value"}, + "context": "", + }, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_invoke_activity" + assert bot.record[1] == "on_teams_task_module_fetch" + async def test_on_teams_task_module_submit(self): # Arrange activity = Activity( @@ -733,6 +1142,94 @@ async def test_on_teams_task_module_submit(self): assert bot.record[0] == "on_invoke_activity" assert bot.record[1] == "on_teams_task_module_submit" + async def test_on_teams_tab_fetch(self): + # Arrange + activity = Activity( + type=ActivityTypes.invoke, + name="tab/fetch", + value={ + "data": {"key": "value"}, + "context": TabContext().serialize(), + }, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_invoke_activity" + assert bot.record[1] == "on_teams_tab_fetch" + + async def test_on_teams_tab_submit(self): + # Arrange + activity = Activity( + type=ActivityTypes.invoke, + name="tab/submit", + value={ + "data": {"key": "value"}, + "context": TabContext().serialize(), + }, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_invoke_activity" + assert bot.record[1] == "on_teams_tab_submit" + + async def test_on_teams_config_fetch(self): + # Arrange + activity = Activity( + type=ActivityTypes.invoke, + name="config/fetch", + value={ + "data": {"key": "value", "type": "config/fetch"}, + "context": {"theme": "default"}, + }, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_invoke_activity" + assert bot.record[1] == "on_teams_config_fetch" + + async def test_on_teams_config_submit(self): + # Arrange + activity = Activity( + type=ActivityTypes.invoke, + name="config/submit", + value={ + "data": {"key": "value", "type": "config/submit"}, + "context": {"theme": "default"}, + }, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_invoke_activity" + assert bot.record[1] == "on_teams_config_submit" + async def test_on_end_of_conversation_activity(self): activity = Activity(type=ActivityTypes.end_of_conversation) @@ -756,3 +1253,227 @@ async def test_typing_activity(self): assert len(bot.record) == 1 assert bot.record[0] == "on_typing_activity" + + async def test_on_teams_read_receipt_event(self): + activity = Activity( + type=ActivityTypes.event, + name="application/vnd.microsoft.readReceipt", + channel_id=Channels.ms_teams, + value={"lastReadMessageId": "10101010"}, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 2 + assert bot.record[0] == "on_event_activity" + assert bot.record[1] == "on_teams_read_receipt_event" + + async def test_on_teams_meeting_start_event(self): + activity = Activity( + type=ActivityTypes.event, + name="application/vnd.microsoft.meetingStart", + channel_id=Channels.ms_teams, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 2 + assert bot.record[0] == "on_event_activity" + assert bot.record[1] == "on_teams_meeting_start_event" + + async def test_on_teams_meeting_end_event(self): + activity = Activity( + type=ActivityTypes.event, + name="application/vnd.microsoft.meetingEnd", + channel_id=Channels.ms_teams, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 2 + assert bot.record[0] == "on_event_activity" + assert bot.record[1] == "on_teams_meeting_end_event" + + async def test_message_update_activity_teams_message_edit(self): + # Arrange + activity = Activity( + type=ActivityTypes.message_update, + channel_data=TeamsChannelData(event_type="editMessage"), + channel_id=Channels.ms_teams, + ) + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + self.assertEqual(2, len(bot.record)) + self.assertEqual("on_message_update_activity", bot.record[0]) + self.assertEqual("on_teams_message_edit", bot.record[1]) + + async def test_message_update_activity_teams_message_undelete(self): + # Arrange + activity = Activity( + type=ActivityTypes.message_update, + channel_data=TeamsChannelData(event_type="undeleteMessage"), + channel_id=Channels.ms_teams, + ) + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + self.assertEqual(2, len(bot.record)) + self.assertEqual("on_message_update_activity", bot.record[0]) + self.assertEqual("on_teams_message_undelete", bot.record[1]) + + async def test_message_update_activity_teams_message_undelete_no_msteams(self): + # Arrange + activity = Activity( + type=ActivityTypes.message_update, + channel_data=TeamsChannelData(event_type="undeleteMessage"), + ) + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + self.assertEqual(1, len(bot.record)) + self.assertEqual("on_message_update_activity", bot.record[0]) + + async def test_message_update_activity_teams_no_channel_data(self): + # Arrange + activity = Activity( + type=ActivityTypes.message_update, + channel_id=Channels.ms_teams, + ) + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + self.assertEqual(1, len(bot.record)) + self.assertEqual("on_message_update_activity", bot.record[0]) + + async def test_message_delete_activity_teams_message_soft_delete(self): + # Arrange + activity = Activity( + type=ActivityTypes.message_delete, + channel_data=TeamsChannelData(event_type="softDeleteMessage"), + channel_id=Channels.ms_teams, + ) + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + self.assertEqual(2, len(bot.record)) + self.assertEqual("on_message_delete_activity", bot.record[0]) + self.assertEqual("on_teams_message_soft_delete", bot.record[1]) + + async def test_message_delete_activity_teams_message_soft_delete_no_msteams(self): + # Arrange + activity = Activity( + type=ActivityTypes.message_delete, + channel_data=TeamsChannelData(event_type="softDeleteMessage"), + ) + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + self.assertEqual(1, len(bot.record)) + self.assertEqual("on_message_delete_activity", bot.record[0]) + + async def test_message_delete_activity_teams_no_channel_data(self): + # Arrange + activity = Activity( + type=ActivityTypes.message_delete, + channel_id=Channels.ms_teams, + ) + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + self.assertEqual(1, len(bot.record)) + self.assertEqual("on_message_delete_activity", bot.record[0]) + + async def test_on_teams_meeting_participants_join_event(self): + # arrange + activity = Activity( + type=ActivityTypes.event, + channel_id=Channels.ms_teams, + name="application/vnd.microsoft.meetingParticipantJoin", + value={ + "members": [ + { + "user": {"id": "123", "name": "name"}, + "meeting": {"role": "role", "in_meeting": True}, + } + ], + }, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_event_activity" + assert bot.record[1] == "on_teams_meeting_participants_join_event" + + async def test_on_teams_meeting_participants_leave_event(self): + # arrange + activity = Activity( + type=ActivityTypes.event, + channel_id=Channels.ms_teams, + name="application/vnd.microsoft.meetingParticipantLeave", + value={ + "members": [ + { + "user": {"id": "id", "name": "name"}, + "meeting": {"role": "role", "in_meeting": True}, + } + ], + }, + ) + + turn_context = TurnContext(SimpleAdapter(), activity) + + # Act + bot = TestingTeamsActivityHandler() + await bot.on_turn(turn_context) + + # Assert + assert len(bot.record) == 2 + assert bot.record[0] == "on_event_activity" + assert bot.record[1] == "on_teams_meeting_participants_leave_event" diff --git a/libraries/botbuilder-core/tests/teams/test_teams_channel_data.py b/libraries/botbuilder-core/tests/teams/test_teams_channel_data.py new file mode 100644 index 000000000..324749ce5 --- /dev/null +++ b/libraries/botbuilder-core/tests/teams/test_teams_channel_data.py @@ -0,0 +1,86 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import uuid4 +import aiounittest + +from botbuilder.schema import Activity +from botbuilder.schema.teams import TeamsChannelData +from botbuilder.core.teams import teams_get_team_info +from botbuilder.schema.teams._models_py3 import ( + ChannelInfo, + NotificationInfo, + OnBehalfOf, + TeamInfo, + TeamsChannelDataSettings, + TeamsMeetingInfo, + TenantInfo, +) + + +class TestTeamsChannelData(aiounittest.AsyncTestCase): + def test_teams_aad_group_id_deserialize(self): + # Arrange + raw_channel_data = {"team": {"aadGroupId": "teamGroup123"}} + + # Act + channel_data = TeamsChannelData().deserialize(raw_channel_data) + + # Assert + assert channel_data.team.aad_group_id == "teamGroup123" + + def test_teams_get_team_info(self): + # Arrange + activity = Activity(channel_data={"team": {"aadGroupId": "teamGroup123"}}) + + # Act + team_info = teams_get_team_info(activity) + + # Assert + assert team_info.aad_group_id == "teamGroup123" + + def test_teams_channel_data_inits(self): + # Arrange + channel = ChannelInfo(id="general", name="General") + event_type = "eventType" + team = TeamInfo(id="supportEngineers", name="Support Engineers") + notification = NotificationInfo(alert=True) + tenant = TenantInfo(id="uniqueTenantId") + meeting = TeamsMeetingInfo(id="BFSE Stand Up") + settings = TeamsChannelDataSettings(selected_channel=channel) + on_behalf_of = [ + OnBehalfOf( + display_name="onBehalfOfTest", + item_id=0, + mention_type="person", + mri=str(uuid4()), + ) + ] + + # Act + channel_data = TeamsChannelData( + channel=channel, + event_type=event_type, + team=team, + notification=notification, + tenant=tenant, + meeting=meeting, + settings=settings, + on_behalf_of=on_behalf_of, + ) + + # Assert + self.assertIsNotNone(channel_data) + self.assertIsInstance(channel_data, TeamsChannelData) + self.assertEqual(channel, channel_data.channel) + self.assertEqual(event_type, channel_data.event_type) + self.assertEqual(team, channel_data.team) + self.assertEqual(notification, channel_data.notification) + self.assertEqual(tenant, channel_data.tenant) + self.assertEqual(meeting, channel_data.meeting) + self.assertEqual(settings, channel_data.settings) + self.assertEqual(on_behalf_of, channel_data.on_behalf_of) + self.assertEqual(on_behalf_of[0].display_name, "onBehalfOfTest") + self.assertEqual(on_behalf_of[0].mention_type, "person") + self.assertIsNotNone(on_behalf_of[0].mri) + self.assertEqual(on_behalf_of[0].item_id, 0) diff --git a/libraries/botbuilder-core/tests/teams/test_teams_extension.py b/libraries/botbuilder-core/tests/teams/test_teams_extension.py index e4ebb4449..406d3cb39 100644 --- a/libraries/botbuilder-core/tests/teams/test_teams_extension.py +++ b/libraries/botbuilder-core/tests/teams/test_teams_extension.py @@ -1,15 +1,22 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from uuid import uuid4 import aiounittest from botbuilder.schema import Activity from botbuilder.schema.teams import TeamInfo from botbuilder.core.teams import ( teams_get_channel_id, + teams_get_selected_channel_id, teams_get_team_info, teams_notify_user, ) +from botbuilder.core.teams.teams_activity_extensions import ( + teams_get_meeting_info, + teams_get_team_on_behalf_of, +) +from botbuilder.schema.teams._models_py3 import OnBehalfOf class TestTeamsActivityHandler(aiounittest.AsyncTestCase): @@ -25,6 +32,35 @@ def test_teams_get_channel_id(self): # Assert assert result == "id123" + def test_teams_get_selected_channel_id(self): + # Arrange + activity = Activity( + channel_data={ + "channel": {"id": "id123", "name": "channel_name"}, + "settings": { + "selectedChannel": {"id": "id12345", "name": "channel_name"} + }, + } + ) + + # Act + result = teams_get_selected_channel_id(activity) + + # Assert + assert result == "id12345" + + def test_teams_get_selected_channel_id_with_no_selected_channel(self): + # Arrange + activity = Activity( + channel_data={"channel": {"id": "id123", "name": "channel_name"}} + ) + + # Act + result = teams_get_selected_channel_id(activity) + + # Assert + assert result is None + def test_teams_get_channel_id_with_no_channel(self): # Arrange activity = Activity( @@ -119,6 +155,17 @@ def test_teams_notify_user(self): # Assert assert activity.channel_data.notification.alert + def test_teams_notify_user_alert_in_meeting(self): + # Arrange + activity = Activity() + + # Act + teams_notify_user(activity, alert_in_meeting=True) + + # Assert + assert activity.channel_data.notification.alert_in_meeting is True + assert activity.channel_data.notification.alert is False + def test_teams_notify_user_with_no_activity(self): # Arrange activity = None @@ -149,3 +196,33 @@ def test_teams_notify_user_with_no_channel_data(self): # Assert assert activity.channel_data.notification.alert assert activity.id == "id123" + + def test_teams_meeting_info(self): + # Arrange + activity = Activity(channel_data={"meeting": {"id": "meeting123"}}) + + # Act + meeting_id = teams_get_meeting_info(activity).id + + # Assert + assert meeting_id == "meeting123" + + def test_teams_channel_data_existing_on_behalf_of(self): + # Arrange + on_behalf_of_list = [ + OnBehalfOf( + display_name="onBehalfOfTest", + item_id=0, + mention_type="person", + mri=str(uuid4()), + ) + ] + + activity = Activity(channel_data={"onBehalfOf": on_behalf_of_list}) + + # Act + on_behalf_of_list = teams_get_team_on_behalf_of(activity) + + # Assert + self.assertEqual(1, len(on_behalf_of_list)) + self.assertEqual("onBehalfOfTest", on_behalf_of_list[0].display_name) diff --git a/libraries/botbuilder-core/tests/teams/test_teams_info.py b/libraries/botbuilder-core/tests/teams/test_teams_info.py index 41c3e5439..00f4ad8a4 100644 --- a/libraries/botbuilder-core/tests/teams/test_teams_info.py +++ b/libraries/botbuilder-core/tests/teams/test_teams_info.py @@ -1,18 +1,65 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +import json import aiounittest - +from botbuilder.schema.teams._models_py3 import ( + ContentType, + MeetingNotificationChannelData, + MeetingStageSurface, + MeetingTabIconSurface, + OnBehalfOf, + TargetedMeetingNotification, + TargetedMeetingNotificationValue, + TaskModuleContinueResponse, + TaskModuleTaskInfo, +) +from botframework.connector import Channels from botbuilder.core import TurnContext, MessageFactory from botbuilder.core.teams import TeamsInfo, TeamsActivityHandler -from botbuilder.schema import Activity -from botbuilder.schema.teams import TeamsChannelData, TeamInfo -from botframework.connector import Channels +from botbuilder.schema import ( + Activity, + ChannelAccount, + ConversationAccount, +) from simple_adapter_with_create_conversation import SimpleAdapterWithCreateConversation +ACTIVITY = Activity( + id="1234", + type="message", + text="test", + from_property=ChannelAccount(id="user", name="User Name"), + recipient=ChannelAccount(id="bot", name="Bot Name"), + conversation=ConversationAccount(id="convo", name="Convo Name"), + channel_data={"channelData": {}}, + channel_id="UnitTest", + locale="en-us", + service_url="https://example.org", +) + class TestTeamsInfo(aiounittest.AsyncTestCase): + async def test_send_message_to_teams_channels_without_activity(self): + def create_conversation(): + pass + + adapter = SimpleAdapterWithCreateConversation( + call_create_conversation=create_conversation + ) + + activity = Activity() + turn_context = TurnContext(adapter, activity) + + try: + await TeamsInfo.send_message_to_teams_channel( + turn_context, None, "channelId123" + ) + except ValueError: + pass + else: + assert False, "should have raise ValueError" + async def test_send_message_to_teams(self): def create_conversation(): pass @@ -21,17 +68,231 @@ def create_conversation(): call_create_conversation=create_conversation ) + turn_context = TurnContext(adapter, ACTIVITY) + handler = TestTeamsActivityHandler() + await handler.on_turn(turn_context) + + async def test_send_message_to_teams_channels_without_turn_context(self): + try: + await TeamsInfo.send_message_to_teams_channel( + None, ACTIVITY, "channelId123" + ) + except ValueError: + pass + else: + assert False, "should have raise ValueError" + + async def test_send_message_to_teams_channels_without_teams_channel_id(self): + def create_conversation(): + pass + + adapter = SimpleAdapterWithCreateConversation( + call_create_conversation=create_conversation + ) + + turn_context = TurnContext(adapter, ACTIVITY) + + try: + await TeamsInfo.send_message_to_teams_channel(turn_context, ACTIVITY, "") + except ValueError: + pass + else: + assert False, "should have raise ValueError" + + async def test_send_message_to_teams_channel_works(self): + adapter = SimpleAdapterWithCreateConversation() + + turn_context = TurnContext(adapter, ACTIVITY) + result = await TeamsInfo.send_message_to_teams_channel( + turn_context, ACTIVITY, "teamId123" + ) + assert result[0].activity_id == "new_conversation_id" + assert result[1] == "reference123" + + async def test_get_team_details_works_without_team_id(self): + adapter = SimpleAdapterWithCreateConversation() + ACTIVITY.channel_data = {} + turn_context = TurnContext(adapter, ACTIVITY) + result = TeamsInfo.get_team_id(turn_context) + + assert result == "" + + async def test_get_team_details_works_with_team_id(self): + adapter = SimpleAdapterWithCreateConversation() + team_id = "teamId123" + ACTIVITY.channel_data = {"team": {"id": team_id}} + turn_context = TurnContext(adapter, ACTIVITY) + result = TeamsInfo.get_team_id(turn_context) + + assert result == team_id + + async def test_get_team_details_without_team_id(self): + def create_conversation(): + pass + + adapter = SimpleAdapterWithCreateConversation( + call_create_conversation=create_conversation + ) + + turn_context = TurnContext(adapter, ACTIVITY) + + try: + await TeamsInfo.get_team_details(turn_context) + except TypeError: + pass + else: + assert False, "should have raise TypeError" + + async def test_get_team_channels_without_team_id(self): + def create_conversation(): + pass + + adapter = SimpleAdapterWithCreateConversation( + call_create_conversation=create_conversation + ) + + turn_context = TurnContext(adapter, ACTIVITY) + + try: + await TeamsInfo.get_team_channels(turn_context) + except TypeError: + pass + else: + assert False, "should have raise TypeError" + + async def test_get_paged_team_members_without_team_id(self): + def create_conversation(): + pass + + adapter = SimpleAdapterWithCreateConversation( + call_create_conversation=create_conversation + ) + + turn_context = TurnContext(adapter, ACTIVITY) + + try: + await TeamsInfo.get_paged_team_members(turn_context) + except TypeError: + pass + else: + assert False, "should have raise TypeError" + + async def test_get_team_members_without_team_id(self): + def create_conversation(): + pass + + adapter = SimpleAdapterWithCreateConversation( + call_create_conversation=create_conversation + ) + + turn_context = TurnContext(adapter, ACTIVITY) + + try: + await TeamsInfo.get_team_member(turn_context) + except TypeError: + pass + else: + assert False, "should have raise TypeError" + + async def test_get_team_members_without_member_id(self): + def create_conversation(): + pass + + adapter = SimpleAdapterWithCreateConversation( + call_create_conversation=create_conversation + ) + + turn_context = TurnContext(adapter, ACTIVITY) + + try: + await TeamsInfo.get_team_member(turn_context, "teamId123") + except TypeError: + pass + else: + assert False, "should have raise TypeError" + + async def test_get_participant(self): + adapter = SimpleAdapterWithCreateConversation() + activity = Activity( type="message", - text="test_send_message_to_teams_channel", + text="Test-get_participant", channel_id=Channels.ms_teams, - service_url="https://example.org", - channel_data=TeamsChannelData(team=TeamInfo(id="team-id")), + from_property=ChannelAccount(aad_object_id="participantId-1"), + channel_data={ + "meeting": {"id": "meetingId-1"}, + "tenant": {"id": "tenantId-1"}, + }, + service_url="https://test.coffee", ) + turn_context = TurnContext(adapter, activity) - handler = TestTeamsActivityHandler() + handler = TeamsActivityHandler() await handler.on_turn(turn_context) + async def test_get_meeting_info(self): + adapter = SimpleAdapterWithCreateConversation() + + activity = Activity( + type="message", + text="Test-get_meeting_info", + channel_id=Channels.ms_teams, + from_property=ChannelAccount(aad_object_id="participantId-1"), + channel_data={"meeting": {"id": "meetingId-1"}}, + service_url="https://test.coffee", + ) + + turn_context = TurnContext(adapter, activity) + handler = TeamsActivityHandler() + await handler.on_turn(turn_context) + + async def test_send_meeting_notificationt(self): + test_cases = [ + ("202", "accepted"), + ( + "207", + "if the notifications are sent only to parital number of recipients\ + because the validation on some recipients' ids failed or some\ + recipients were not found in the roster. In this case, \ + SMBA will return the user MRIs of those failed recipients\ + in a format that was given to a bot (ex: if a bot sent \ + encrypted user MRIs, return encrypted one).", + ), + ( + "400", + "when Meeting Notification request payload validation fails. For instance,\ + Recipients: # of recipients is greater than what the API allows ||\ + all of recipients' user ids were invalid, Surface: Surface list\ + is empty or null, Surface type is invalid, Duplicative \ + surface type exists in one payload", + ), + ( + "403", + "if the bot is not allowed to send the notification. In this case,\ + the payload should contain more detail error message. \ + There can be many reasons: bot disabled by tenant admin,\ + blocked during live site mitigation, the bot does not\ + have a correct RSC permission for a specific surface type, etc", + ), + ] + for status_code, expected_message in test_cases: + adapter = SimpleAdapterWithCreateConversation() + + activity = Activity( + type="targetedMeetingNotification", + text="Test-send_meeting_notificationt", + channel_id=Channels.ms_teams, + from_property=ChannelAccount( + aad_object_id="participantId-1", name=status_code + ), + service_url="https://test.coffee", + conversation=ConversationAccount(id="conversation-id"), + ) + + turn_context = TurnContext(adapter, activity) + handler = TeamsActivityHandler() + await handler.on_turn(turn_context) + class TestTeamsActivityHandler(TeamsActivityHandler): async def on_turn(self, turn_context: TurnContext): @@ -39,6 +300,8 @@ async def on_turn(self, turn_context: TurnContext): if turn_context.activity.text == "test_send_message_to_teams_channel": await self.call_send_message_to_teams(turn_context) + elif turn_context.activity.text == "test_send_meeting_notification": + await self.call_send_meeting_notification(turn_context) async def call_send_message_to_teams(self, turn_context: TurnContext): msg = MessageFactory.text("call_send_message_to_teams") @@ -49,3 +312,71 @@ async def call_send_message_to_teams(self, turn_context: TurnContext): assert reference[0].activity_id == "new_conversation_id" assert reference[1] == "reference123" + + async def call_send_meeting_notification(self, turn_context: TurnContext): + from_property = turn_context.activity.from_property + try: + # Send the meeting notification asynchronously + failed_participants = await TeamsInfo.send_meeting_notification( + turn_context, + self.get_targeted_meeting_notification(from_property), + "meeting-id", + ) + + # Handle based on the 'from_property.name' + if from_property.name == "207": + self.assertEqual( + "failingid", + failed_participants.recipients_failure_info[0].recipient_mri, + ) + elif from_property.name == "202": + assert failed_participants is None + else: + raise TypeError( + f"Expected HttpOperationException with response status code {from_property.name}." + ) + + except ValueError as ex: + # Assert that the response status code matches the from_property.name + assert from_property.name == str(int(ex.response.status_code)) + + # Deserialize the error response content to an ErrorResponse object + error_response = json.loads(ex.response.content) + + # Handle based on error codes + if from_property.name == "400": + assert error_response["error"]["code"] == "BadSyntax" + elif from_property.name == "403": + assert error_response["error"]["code"] == "BotNotInConversationRoster" + else: + raise TypeError( + f"Expected HttpOperationException with response status code {from_property.name}." + ) + + def get_targeted_meeting_notification(self, from_account: ChannelAccount): + recipients = [from_account.id] + + if from_account.name == "207": + recipients.append("failingid") + + meeting_stage_surface = MeetingStageSurface( + content=TaskModuleContinueResponse( + value=TaskModuleTaskInfo(title="title here", height=3, width=2) + ), + content_type=ContentType.Task, + ) + + meeting_tab_icon_surface = MeetingTabIconSurface( + tab_entity_id="test tab entity id" + ) + + value = TargetedMeetingNotificationValue( + recipients=recipients, + surfaces=[meeting_stage_surface, meeting_tab_icon_surface], + ) + + obo = OnBehalfOf(display_name=from_account.name, mri=from_account.id) + + channel_data = MeetingNotificationChannelData(on_behalf_of_list=[obo]) + + return TargetedMeetingNotification(value=value, channel_data=channel_data) diff --git a/libraries/botbuilder-core/tests/test_activity_handler.py b/libraries/botbuilder-core/tests/test_activity_handler.py index 20d9386e0..1ee0c5414 100644 --- a/libraries/botbuilder-core/tests/test_activity_handler.py +++ b/libraries/botbuilder-core/tests/test_activity_handler.py @@ -2,6 +2,9 @@ from typing import List import aiounittest +from botframework.connector import ConnectorClient +from botframework.connector.auth import AppCredentials + from botbuilder.core import ActivityHandler, BotAdapter, TurnContext from botbuilder.schema import ( Activity, @@ -23,6 +26,14 @@ async def on_message_activity(self, turn_context: TurnContext): self.record.append("on_message_activity") return await super().on_message_activity(turn_context) + async def on_message_update_activity(self, turn_context: TurnContext): + self.record.append("on_message_update_activity") + return await super().on_message_update_activity(turn_context) + + async def on_message_delete_activity(self, turn_context: TurnContext): + self.record.append("on_message_delete_activity") + return await super().on_message_delete_activity(turn_context) + async def on_members_added_activity( self, members_added: ChannelAccount, turn_context: TurnContext ): @@ -67,6 +78,18 @@ async def on_typing_activity(self, turn_context: TurnContext): self.record.append("on_typing_activity") return await super().on_typing_activity(turn_context) + async def on_installation_update(self, turn_context: TurnContext): + self.record.append("on_installation_update") + return await super().on_installation_update(turn_context) + + async def on_installation_update_add(self, turn_context: TurnContext): + self.record.append("on_installation_update_add") + return await super().on_installation_update_add(turn_context) + + async def on_installation_update_remove(self, turn_context: TurnContext): + self.record.append("on_installation_update_remove") + return await super().on_installation_update_remove(turn_context) + async def on_unrecognized_activity_type(self, turn_context: TurnContext): self.record.append("on_unrecognized_activity_type") return await super().on_unrecognized_activity_type(turn_context) @@ -129,6 +152,18 @@ async def update_activity(self, context: TurnContext, activity: Activity): raise NotImplementedError() +class MockConnectorClient(ConnectorClient): + def __init__(self): + super().__init__( + credentials=MockCredentials(), base_url="http://tempuri.org/whatever" + ) + + +class MockCredentials(AppCredentials): + def get_access_token(self, force_refresh: bool = False) -> str: + return "awesome" + + class TestActivityHandler(aiounittest.AsyncTestCase): async def test_message_reaction(self): # Note the code supports multiple adds and removes in the same activity though @@ -181,6 +216,32 @@ async def test_invoke_should_not_match(self): assert bot.record[0] == "on_invoke_activity" assert adapter.activity.value.status == int(HTTPStatus.NOT_IMPLEMENTED) + async def test_on_message_update_activity(self): + activity = Activity(type=ActivityTypes.message_update) + + adapter = TestInvokeAdapter() + turn_context = TurnContext(adapter, activity) + + # Act + bot = TestingActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 1 + assert bot.record[0] == "on_message_update_activity" + + async def test_on_message_delete_activity(self): + activity = Activity(type=ActivityTypes.message_delete) + + adapter = TestInvokeAdapter() + turn_context = TurnContext(adapter, activity) + + # Act + bot = TestingActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 1 + assert bot.record[0] == "on_message_delete_activity" + async def test_on_end_of_conversation_activity(self): activity = Activity(type=ActivityTypes.end_of_conversation) @@ -206,3 +267,76 @@ async def test_typing_activity(self): assert len(bot.record) == 1 assert bot.record[0] == "on_typing_activity" + + async def test_on_installation_update(self): + activity = Activity(type=ActivityTypes.installation_update) + + adapter = TestInvokeAdapter() + turn_context = TurnContext(adapter, activity) + + # Act + bot = TestingActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 1 + assert bot.record[0] == "on_installation_update" + + async def test_on_installation_update_add(self): + activity = Activity(type=ActivityTypes.installation_update, action="add") + + adapter = TestInvokeAdapter() + turn_context = TurnContext(adapter, activity) + + # Act + bot = TestingActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 2 + assert bot.record[0] == "on_installation_update" + assert bot.record[1] == "on_installation_update_add" + + async def test_on_installation_update_add_upgrade(self): + activity = Activity( + type=ActivityTypes.installation_update, action="add-upgrade" + ) + + adapter = TestInvokeAdapter() + turn_context = TurnContext(adapter, activity) + + # Act + bot = TestingActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 2 + assert bot.record[0] == "on_installation_update" + assert bot.record[1] == "on_installation_update_add" + + async def test_on_installation_update_remove(self): + activity = Activity(type=ActivityTypes.installation_update, action="remove") + + adapter = TestInvokeAdapter() + turn_context = TurnContext(adapter, activity) + + # Act + bot = TestingActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 2 + assert bot.record[0] == "on_installation_update" + assert bot.record[1] == "on_installation_update_remove" + + async def test_on_installation_update_remove_upgrade(self): + activity = Activity( + type=ActivityTypes.installation_update, action="remove-upgrade" + ) + + adapter = TestInvokeAdapter() + turn_context = TurnContext(adapter, activity) + + # Act + bot = TestingActivityHandler() + await bot.on_turn(turn_context) + + assert len(bot.record) == 2 + assert bot.record[0] == "on_installation_update" + assert bot.record[1] == "on_installation_update_remove" diff --git a/libraries/botbuilder-core/tests/test_bot_framework_adapter.py b/libraries/botbuilder-core/tests/test_bot_framework_adapter.py index 891ecdeb5..ee8faa773 100644 --- a/libraries/botbuilder-core/tests/test_bot_framework_adapter.py +++ b/libraries/botbuilder-core/tests/test_bot_framework_adapter.py @@ -12,6 +12,7 @@ BotFrameworkAdapterSettings, TurnContext, ) +from botbuilder.core.invoke_response import InvokeResponse from botbuilder.schema import ( Activity, ActivityTypes, @@ -21,6 +22,15 @@ ChannelAccount, DeliveryModes, ExpectedReplies, + CallerIdConstants, + SignInConstants, + TokenExchangeInvokeRequest, + TokenExchangeInvokeResponse, +) +from botframework.connector import Channels +from botframework.connector.token_api.models import ( + TokenExchangeRequest, + TokenResponse as ConnectorTokenResponse, ) from botframework.connector.aio import ConnectorClient from botframework.connector.auth import ( @@ -28,11 +38,15 @@ AuthenticationConstants, AppCredentials, CredentialProvider, + SimpleChannelProvider, + GovernmentConstants, + SimpleCredentialProvider, ) REFERENCE = ConversationReference( activity_id="1234", - channel_id="test", + channel_id=Channels.test, + locale="en-uS", # Intentionally oddly-cased to check that it isn't defaulted somewhere, but tests stay in English service_url="https://example.org/channel", user=ChannelAccount(id="user", name="User Name"), bot=ChannelAccount(id="bot", name="Bot Name"), @@ -184,6 +198,31 @@ async def mock_create_conversation(parameters): return self.connector_client_mock + async def _create_token_api_client( + self, context: TurnContext, oauth_app_credentials: AppCredentials = None + ): + client = await super()._create_token_api_client(context, oauth_app_credentials) + + def mock_exchange_async( + user_id, # pylint: disable=unused-argument + connection_name, + channel_id, + uri=None, # pylint: disable=unused-argument + token=None, + custom_headers=None, # pylint: disable=unused-argument + raw=False, # pylint: disable=unused-argument + **operation_config, # pylint: disable=unused-argument + ): + return ConnectorTokenResponse( + channel_id=channel_id, + connection_name=connection_name, + token=token, + expiration=None, + ) + + client.user_token.exchange_async = mock_exchange_async + return client + async def process_activity( channel_id: str, channel_data_tenant_id: str, conversation_tenant_id: str @@ -267,7 +306,7 @@ async def test_should_migrate_tenant_id_for_msteams(self): is_incoming=True, ) - incoming.channel_id = "msteams" + incoming.channel_id = Channels.ms_teams adapter = AdapterUnderTest() async def aux_func_assert_tenant_id_copied(context): @@ -281,7 +320,6 @@ async def aux_func_assert_tenant_id_copied(context): await adapter.process_activity(incoming, "", aux_func_assert_tenant_id_copied) async def test_should_create_valid_conversation_for_msteams(self): - tenant_id = "testTenant" reference = deepcopy(REFERENCE) @@ -303,7 +341,7 @@ async def aux_func_assert_valid_conversation(context): "request has invalid tenant_id on conversation", ) self.assertEqual( - context.activity.channel_data["tenant"]["id"], + context.activity.channel_data["tenant"]["tenantId"], tenant_id, "request has invalid tenant_id in channel_data", ) @@ -318,22 +356,23 @@ def get_creds_and_assert_values( turn_context: TurnContext, expected_app_id: str, expected_scope: str, - creds_count: int = None, + creds_count: int, ): - # pylint: disable=protected-access - credential_cache = turn_context.adapter._app_credential_map - cache_key = BotFrameworkAdapter.key_for_app_credentials( - expected_app_id, expected_scope - ) - credentials = credential_cache.get(cache_key) - assert credentials + if creds_count > 0: + # pylint: disable=protected-access + credential_cache = turn_context.adapter._app_credential_map + cache_key = BotFrameworkAdapter.key_for_app_credentials( + expected_app_id, expected_scope + ) + credentials = credential_cache.get(cache_key) + assert credentials - TestBotFrameworkAdapter.assert_credentials_values( - credentials, expected_app_id, expected_scope - ) + TestBotFrameworkAdapter.assert_credentials_values( + credentials, expected_app_id, expected_scope + ) - if creds_count: - assert creds_count == len(credential_cache) + if creds_count: + assert creds_count == len(credential_cache) @staticmethod def get_client_and_assert_values( @@ -341,22 +380,21 @@ def get_client_and_assert_values( expected_app_id: str, expected_scope: str, expected_url: str, - client_count: int = None, + client_count: int, ): # pylint: disable=protected-access client_cache = turn_context.adapter._connector_client_cache cache_key = BotFrameworkAdapter.key_for_connector_client( expected_url, expected_app_id, expected_scope ) - client = client_cache[cache_key] + client = client_cache.get(cache_key) assert client TestBotFrameworkAdapter.assert_connectorclient_vaules( client, expected_app_id, expected_url, expected_scope ) - if client_count: - assert client_count == len(client_cache) + assert client_count == len(client_cache) @staticmethod def assert_connectorclient_vaules( @@ -366,9 +404,17 @@ def assert_connectorclient_vaules( expected_scope=AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, ): creds = client.config.credentials - assert expected_app_id == creds.microsoft_app_id - assert expected_scope == creds.oauth_scope - assert expected_service_url == client.config.base_url + assert TestBotFrameworkAdapter.__str_equal( + expected_app_id, creds.microsoft_app_id + ) + assert TestBotFrameworkAdapter.__str_equal(expected_scope, creds.oauth_scope) + assert TestBotFrameworkAdapter.__str_equal( + expected_service_url, client.config.base_url + ) + + @staticmethod + def __str_equal(str1: str, str2: str) -> bool: + return (str1 if str1 is not None else "") == (str2 if str2 is not None else "") @staticmethod def assert_credentials_values( @@ -379,41 +425,87 @@ def assert_credentials_values( assert expected_app_id == credentials.microsoft_app_id assert expected_scope == credentials.oauth_scope - async def test_process_activity_creates_correct_creds_and_client(self): - bot_app_id = "00000000-0000-0000-0000-000000000001" - identity = ClaimsIdentity( - claims={ + async def test_process_activity_creates_correct_creds_and_client_channel_to_bot( + self, + ): + await self.__process_activity_creates_correct_creds_and_client( + None, + None, + None, + AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + 0, + 1, + ) + + async def test_process_activity_creates_correct_creds_and_client_public_azure(self): + await self.__process_activity_creates_correct_creds_and_client( + "00000000-0000-0000-0000-000000000001", + CallerIdConstants.public_azure_channel, + None, + AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + 1, + 1, + ) + + async def test_process_activity_creates_correct_creds_and_client_us_gov(self): + await self.__process_activity_creates_correct_creds_and_client( + "00000000-0000-0000-0000-000000000001", + CallerIdConstants.us_gov_channel, + GovernmentConstants.CHANNEL_SERVICE, + GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + 1, + 1, + ) + + async def __process_activity_creates_correct_creds_and_client( + self, + bot_app_id: str, + expected_caller_id: str, + channel_service: str, + expected_scope: str, + expected_app_credentials_count: int, + expected_client_credentials_count: int, + ): + identity = ClaimsIdentity({}, True) + if bot_app_id: + identity.claims = { AuthenticationConstants.AUDIENCE_CLAIM: bot_app_id, AuthenticationConstants.APP_ID_CLAIM: bot_app_id, AuthenticationConstants.VERSION_CLAIM: "1.0", - }, - is_authenticated=True, - ) + } + credential_provider = SimpleCredentialProvider(bot_app_id, None) service_url = "https://smba.trafficmanager.net/amer/" async def callback(context: TurnContext): TestBotFrameworkAdapter.get_creds_and_assert_values( context, bot_app_id, - AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, - 1, + expected_scope, + expected_app_credentials_count, ) TestBotFrameworkAdapter.get_client_and_assert_values( context, bot_app_id, - AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + expected_scope, service_url, - 1, + expected_client_credentials_count, ) - scope = context.turn_state[BotFrameworkAdapter.BOT_OAUTH_SCOPE_KEY] - assert AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE == scope + assert context.activity.caller_id == expected_caller_id - settings = BotFrameworkAdapterSettings(bot_app_id) + settings = BotFrameworkAdapterSettings( + bot_app_id, + credential_provider=credential_provider, + channel_provider=SimpleChannelProvider(channel_service), + ) sut = BotFrameworkAdapter(settings) await sut.process_activity_with_identity( - Activity(channel_id="emulator", service_url=service_url, text="test",), + Activity( + channel_id=Channels.emulator, + service_url=service_url, + text="test", + ), identity, callback, ) @@ -434,19 +526,34 @@ async def test_process_activity_for_forwarded_activity(self): async def callback(context: TurnContext): TestBotFrameworkAdapter.get_creds_and_assert_values( - context, skill_1_app_id, bot_app_id, 1, + context, + skill_1_app_id, + bot_app_id, + 1, ) TestBotFrameworkAdapter.get_client_and_assert_values( - context, skill_1_app_id, bot_app_id, service_url, 1, + context, + skill_1_app_id, + bot_app_id, + service_url, + 1, ) scope = context.turn_state[BotFrameworkAdapter.BOT_OAUTH_SCOPE_KEY] assert bot_app_id == scope + assert ( + context.activity.caller_id + == f"{CallerIdConstants.bot_to_bot_prefix}{bot_app_id}" + ) settings = BotFrameworkAdapterSettings(bot_app_id) sut = BotFrameworkAdapter(settings) await sut.process_activity_with_identity( - Activity(channel_id="emulator", service_url=service_url, text="test",), + Activity( + channel_id=Channels.emulator, + service_url=service_url, + text="test", + ), identity, callback, ) @@ -545,17 +652,26 @@ async def test_continue_conversation_with_audience(self): async def callback(context: TurnContext): TestBotFrameworkAdapter.get_creds_and_assert_values( - context, skill_1_app_id, skill_2_app_id, 1, + context, + skill_1_app_id, + skill_2_app_id, + 1, ) TestBotFrameworkAdapter.get_client_and_assert_values( - context, skill_1_app_id, skill_2_app_id, skill_2_service_url, 1, + context, + skill_1_app_id, + skill_2_app_id, + skill_2_service_url, + 1, ) # pylint: disable=protected-access client_cache = context.adapter._connector_client_cache client = client_cache.get( BotFrameworkAdapter.key_for_connector_client( - skill_2_service_url, skill_1_app_id, skill_2_app_id, + skill_2_service_url, + skill_1_app_id, + skill_2_app_id, ) ) assert client @@ -594,7 +710,7 @@ async def callback(context: TurnContext): inbound_activity = Activity( type=ActivityTypes.message, - channel_id="emulator", + channel_id=Channels.emulator, service_url="http://tempuri.org/whatever", delivery_mode=DeliveryModes.expect_replies, text="hello world", @@ -639,7 +755,7 @@ async def callback(context: TurnContext): inbound_activity = Activity( type=ActivityTypes.message, - channel_id="emulator", + channel_id=Channels.emulator, service_url="http://tempuri.org/whatever", delivery_mode=DeliveryModes.normal, text="hello world", @@ -663,3 +779,130 @@ async def callback(context: TurnContext): adapter.connector_client_mock.conversations.send_to_conversation.call_count == 3 ) + + async def test_process_activity_with_identity_token_exchange_invoke_response(self): + mock_credential_provider = unittest.mock.create_autospec(CredentialProvider) + + settings = BotFrameworkAdapterSettings( + app_id="bot_id", + credential_provider=mock_credential_provider, + ) + adapter = AdapterUnderTest(settings) + + identity = ClaimsIdentity( + claims={ + AuthenticationConstants.AUDIENCE_CLAIM: "bot_id", + AuthenticationConstants.APP_ID_CLAIM: "bot_id", + AuthenticationConstants.VERSION_CLAIM: "1.0", + }, + is_authenticated=True, + ) + + inbound_activity = Activity( + type=ActivityTypes.invoke, + name=SignInConstants.token_exchange_operation_name, + service_url="http://tempuri.org/whatever", + delivery_mode=DeliveryModes.normal, + conversation=ConversationAccount(id="conversationId"), + value=TokenExchangeInvokeRequest( + id="token_exchange_id", + token="token", + connection_name="connection_name", + ), + ) + + async def callback(context: TurnContext): + activity = Activity( + type=ActivityTypes.invoke_response, + value=InvokeResponse( + status=200, + body=TokenExchangeInvokeResponse( + id=context.activity.value.id, + connection_name=context.activity.value.connection_name, + ), + ), + ) + + await context.send_activity(activity) + + invoke_response = await adapter.process_activity_with_identity( + inbound_activity, + identity, + callback, + ) + + assert invoke_response + assert invoke_response.status == 200 + assert invoke_response.body.id == inbound_activity.value.id + assert ( + invoke_response.body.connection_name + == inbound_activity.value.connection_name + ) + + async def test_exchange_token_from_credentials(self): + mock_credential_provider = unittest.mock.create_autospec(CredentialProvider) + + settings = BotFrameworkAdapterSettings( + app_id="bot_id", + credential_provider=mock_credential_provider, + ) + adapter = AdapterUnderTest(settings) + + identity = ClaimsIdentity( + claims={ + AuthenticationConstants.AUDIENCE_CLAIM: "bot_id", + AuthenticationConstants.APP_ID_CLAIM: "bot_id", + AuthenticationConstants.VERSION_CLAIM: "1.0", + }, + is_authenticated=True, + ) + + inbound_activity = Activity( + type=ActivityTypes.invoke, + name=SignInConstants.token_exchange_operation_name, + service_url="http://tempuri.org/whatever", + conversation=ConversationAccount(id="conversationId"), + value=TokenExchangeInvokeRequest( + id="token_exchange_id", + token="token", + connection_name="connection_name", + ), + ) + + async def callback(context): + result = await adapter.exchange_token_from_credentials( + turn_context=context, + oauth_app_credentials=None, + connection_name=context.activity.value.connection_name, + exchange_request=TokenExchangeRequest( + token=context.activity.value.token, uri=context.activity.service_url + ), + user_id="user_id", + ) + + activity = Activity( + type=ActivityTypes.invoke_response, + value=InvokeResponse( + status=200, + body=TokenExchangeInvokeResponse( + id=context.activity.value.id, + connection_name=result.connection_name, + ), + ), + ) + + await context.send_activity(activity) + + invoke_response = await adapter.process_activity_with_identity( + inbound_activity, + identity, + callback, + ) + + assert invoke_response + assert invoke_response.status == 200 + assert invoke_response.body.id == inbound_activity.value.id + assert ( + invoke_response.body.connection_name + == inbound_activity.value.connection_name + ) diff --git a/libraries/botbuilder-core/tests/test_bot_state.py b/libraries/botbuilder-core/tests/test_bot_state.py index 2c0eb815e..fdf8ed6fa 100644 --- a/libraries/botbuilder-core/tests/test_bot_state.py +++ b/libraries/botbuilder-core/tests/test_bot_state.py @@ -473,13 +473,32 @@ async def test_bot_state_get(self): storage = MemoryStorage({}) - conversation_state = ConversationState(storage) + test_bot_state = BotStateForTest(storage) ( - await conversation_state.create_property("test-name").get( + await test_bot_state.create_property("test-name").get( turn_context, lambda: TestPocoState() ) ).value = "test-value" - result = conversation_state.get(turn_context) + result = test_bot_state.get(turn_context) assert result["test-name"].value == "test-value" + + async def test_bot_state_get_cached_state(self): + # pylint: disable=unnecessary-lambda + turn_context = TestUtilities.create_empty_context() + turn_context.activity.conversation = ConversationAccount(id="1234") + + storage = MemoryStorage({}) + + test_bot_state = BotStateForTest(storage) + ( + await test_bot_state.create_property("test-name").get( + turn_context, lambda: TestPocoState() + ) + ).value = "test-value" + + result = test_bot_state.get_cached_state(turn_context) + + assert result is not None + assert result == test_bot_state.get_cached_state(turn_context) diff --git a/libraries/botbuilder-core/tests/test_channel_service_handler.py b/libraries/botbuilder-core/tests/test_channel_service_handler.py new file mode 100644 index 000000000..8f0d9df12 --- /dev/null +++ b/libraries/botbuilder-core/tests/test_channel_service_handler.py @@ -0,0 +1,46 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botbuilder.core import ChannelServiceHandler +from botframework.connector.auth import ( + AuthenticationConfiguration, + ClaimsIdentity, + SimpleCredentialProvider, + JwtTokenValidation, + AuthenticationConstants, +) +import botbuilder.schema + + +class TestChannelServiceHandler(ChannelServiceHandler): + def __init__(self): + self.claims_identity = None + ChannelServiceHandler.__init__( + self, SimpleCredentialProvider("", ""), AuthenticationConfiguration() + ) + + async def on_reply_to_activity( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, + activity: botbuilder.schema.Activity, + ) -> botbuilder.schema.ResourceResponse: + self.claims_identity = claims_identity + return botbuilder.schema.ResourceResponse() + + +class ChannelServiceHandlerTests(aiounittest.AsyncTestCase): + async def test_should_authenticate_anonymous_skill_claim(self): + sut = TestChannelServiceHandler() + await sut.handle_reply_to_activity(None, "123", "456", {}) + + assert ( + sut.claims_identity.authentication_type + == AuthenticationConstants.ANONYMOUS_AUTH_TYPE + ) + assert ( + JwtTokenValidation.get_app_id_from_claims(sut.claims_identity.claims) + == AuthenticationConstants.ANONYMOUS_SKILL_APP_ID + ) diff --git a/libraries/botbuilder-core/tests/test_conversation_state.py b/libraries/botbuilder-core/tests/test_conversation_state.py index 4c4e74c19..79d90ca54 100644 --- a/libraries/botbuilder-core/tests/test_conversation_state.py +++ b/libraries/botbuilder-core/tests/test_conversation_state.py @@ -6,20 +6,25 @@ from botbuilder.core import TurnContext, MemoryStorage, ConversationState from botbuilder.core.adapters import TestAdapter from botbuilder.schema import Activity, ConversationAccount +from botframework.connector import Channels RECEIVED_MESSAGE = Activity( type="message", text="received", - channel_id="test", + channel_id=Channels.test, conversation=ConversationAccount(id="convo"), ) MISSING_CHANNEL_ID = Activity( type="message", text="received", conversation=ConversationAccount(id="convo") ) -MISSING_CONVERSATION = Activity(type="message", text="received", channel_id="test") +MISSING_CONVERSATION = Activity( + type="message", + text="received", + channel_id=Channels.test, +) END_OF_CONVERSATION = Activity( type="endOfConversation", - channel_id="test", + channel_id=Channels.test, conversation=ConversationAccount(id="convo"), ) diff --git a/libraries/botbuilder-core/tests/test_inspection_middleware.py b/libraries/botbuilder-core/tests/test_inspection_middleware.py index 68259a1b4..dbd2c7409 100644 --- a/libraries/botbuilder-core/tests/test_inspection_middleware.py +++ b/libraries/botbuilder-core/tests/test_inspection_middleware.py @@ -113,7 +113,6 @@ async def exec_test(turn_context): y_prop = conversation_state.create_property("y") async def exec_test2(turn_context): - await turn_context.send_activity( MessageFactory.text(f"echo: { turn_context.activity.text }") ) @@ -227,7 +226,6 @@ async def exec_test(turn_context): y_prop = conversation_state.create_property("y") async def exec_test2(turn_context): - await turn_context.send_activity( MessageFactory.text(f"echo: {turn_context.activity.text}") ) diff --git a/libraries/botbuilder-core/tests/test_memory_transcript_store.py b/libraries/botbuilder-core/tests/test_memory_transcript_store.py index 12cb0e8a7..f7ace436e 100644 --- a/libraries/botbuilder-core/tests/test_memory_transcript_store.py +++ b/libraries/botbuilder-core/tests/test_memory_transcript_store.py @@ -25,6 +25,8 @@ ConversationAccount, ConversationReference, ) +from botframework.connector import Channels + # pylint: disable=line-too-long,missing-docstring class TestMemoryTranscriptStore(aiounittest.AsyncTestCase): @@ -97,7 +99,7 @@ def create_activities(self, conversation_id: str, date: datetime, count: int = 5 timestamp=time_stamp, id=str(uuid.uuid4()), text=str(i), - channel_id="test", + channel_id=Channels.test, from_property=ChannelAccount(id=f"User{i}"), conversation=ConversationAccount(id=conversation_id), recipient=ChannelAccount(id="bot1", name="2"), @@ -111,7 +113,7 @@ def create_activities(self, conversation_id: str, date: datetime, count: int = 5 timestamp=date, id=str(uuid.uuid4()), text=str(i), - channel_id="test", + channel_id=Channels.test, from_property=ChannelAccount(id="Bot1", name="2"), conversation=ConversationAccount(id=conversation_id), recipient=ChannelAccount(id=f"User{i}"), diff --git a/libraries/botbuilder-core/tests/test_message_factory.py b/libraries/botbuilder-core/tests/test_message_factory.py index 265ef379a..3012dc498 100644 --- a/libraries/botbuilder-core/tests/test_message_factory.py +++ b/libraries/botbuilder-core/tests/test_message_factory.py @@ -49,7 +49,6 @@ def assert_attachments(activity: Activity, count: int, types: List[str] = None): class TestMessageFactory(aiounittest.AsyncTestCase): - suggested_actions = [ CardAction(title="a", type=ActionTypes.im_back, value="a"), CardAction(title="b", type=ActionTypes.im_back, value="b"), diff --git a/libraries/botbuilder-core/tests/test_middleware_set.py b/libraries/botbuilder-core/tests/test_middleware_set.py index a6785c508..55f6c471f 100644 --- a/libraries/botbuilder-core/tests/test_middleware_set.py +++ b/libraries/botbuilder-core/tests/test_middleware_set.py @@ -56,7 +56,6 @@ async def request_handler(context_or_string): await middleware_set.receive_activity_internal("Bye", request_handler) async def test_middleware_run_in_order(self): - called_first = False called_second = False diff --git a/libraries/botbuilder-core/tests/test_private_conversation_state.py b/libraries/botbuilder-core/tests/test_private_conversation_state.py index 802a2678b..5fb4507e3 100644 --- a/libraries/botbuilder-core/tests/test_private_conversation_state.py +++ b/libraries/botbuilder-core/tests/test_private_conversation_state.py @@ -6,11 +6,12 @@ from botbuilder.core import MemoryStorage, TurnContext, PrivateConversationState from botbuilder.core.adapters import TestAdapter from botbuilder.schema import Activity, ChannelAccount, ConversationAccount +from botframework.connector import Channels RECEIVED_MESSAGE = Activity( text="received", type="message", - channel_id="test", + channel_id=Channels.test, conversation=ConversationAccount(id="convo"), from_property=ChannelAccount(id="user"), ) diff --git a/libraries/botbuilder-core/tests/test_show_typing_middleware.py b/libraries/botbuilder-core/tests/test_show_typing_middleware.py index b3b10a13d..9d0e0b7ce 100644 --- a/libraries/botbuilder-core/tests/test_show_typing_middleware.py +++ b/libraries/botbuilder-core/tests/test_show_typing_middleware.py @@ -1,11 +1,31 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. import asyncio +from uuid import uuid4 import aiounittest -from botbuilder.core import ShowTypingMiddleware +from botbuilder.core import ShowTypingMiddleware, TurnContext from botbuilder.core.adapters import TestAdapter -from botbuilder.schema import ActivityTypes +from botbuilder.schema import Activity, ActivityTypes +from botframework.connector.auth import AuthenticationConstants, ClaimsIdentity + + +class SkillTestAdapter(TestAdapter): + def create_turn_context(self, activity: Activity) -> TurnContext: + turn_context = super().create_turn_context(activity) + + claims_identity = ClaimsIdentity( + claims={ + AuthenticationConstants.VERSION_CLAIM: "2.0", + AuthenticationConstants.AUDIENCE_CLAIM: str(uuid4()), + AuthenticationConstants.AUTHORIZED_PARTY: str(uuid4()), + }, + is_authenticated=True, + ) + + turn_context.turn_state[self.BOT_IDENTITY_KEY] = claims_identity + + return turn_context class TestShowTypingMiddleware(aiounittest.AsyncTestCase): @@ -65,3 +85,14 @@ def assert_is_message(activity, description): # pylint: disable=unused-argument step1 = await adapter.send("foo") await step1.assert_reply(assert_is_message) + + async def test_not_send_not_send_typing_indicator_when_bot_running_as_skill(self): + async def aux(context): + await asyncio.sleep(1) + await context.send_activity(f"echo:{context.activity.text}") + + skill_adapter = SkillTestAdapter(aux) + skill_adapter.use(ShowTypingMiddleware(0.001, 1)) + + step1 = await skill_adapter.send("foo") + await step1.assert_reply("echo:foo") diff --git a/libraries/botbuilder-core/tests/test_telemetry_middleware.py b/libraries/botbuilder-core/tests/test_telemetry_middleware.py index 7128dab8d..7fdd83109 100644 --- a/libraries/botbuilder-core/tests/test_telemetry_middleware.py +++ b/libraries/botbuilder-core/tests/test_telemetry_middleware.py @@ -3,14 +3,18 @@ # pylint: disable=line-too-long,missing-docstring,unused-variable import copy +import uuid from typing import Dict from unittest.mock import Mock import aiounittest +from botframework.connector import Channels + from botbuilder.core import ( NullTelemetryClient, TelemetryLoggerMiddleware, TelemetryLoggerConstants, TurnContext, + MessageFactory, ) from botbuilder.core.adapters import TestAdapter, TestFlow from botbuilder.schema import ( @@ -18,7 +22,9 @@ ActivityTypes, ChannelAccount, ConversationAccount, + ConversationReference, ) +from botbuilder.schema.teams import TeamInfo, TeamsChannelData, TenantInfo class TestTelemetryMiddleware(aiounittest.AsyncTestCase): @@ -28,6 +34,48 @@ async def test_create_middleware(self): my_logger = TelemetryLoggerMiddleware(telemetry, True) assert my_logger + async def test_do_not_throw_on_null_from(self): + telemetry = Mock() + my_logger = TelemetryLoggerMiddleware(telemetry, False) + + adapter = TestAdapter( + template_or_conversation=Activity( + channel_id=Channels.test, + recipient=ChannelAccount(id="bot", name="Bot"), + conversation=ConversationAccount(id=str(uuid.uuid4())), + ) + ) + adapter.use(my_logger) + + async def send_proactive(context: TurnContext): + await context.send_activity("proactive") + + async def logic(context: TurnContext): + await adapter.create_conversation( + context.activity.channel_id, + send_proactive, + ) + + adapter.logic = logic + + test_flow = TestFlow(None, adapter) + await test_flow.send("foo") + await test_flow.assert_reply("proactive") + + telemetry_calls = [ + ( + TelemetryLoggerConstants.BOT_MSG_RECEIVE_EVENT, + { + "fromId": None, + "conversationName": None, + "locale": None, + "recipientId": "bot", + "recipientName": "Bot", + }, + ), + ] + self.assert_telemetry_calls(telemetry, telemetry_calls) + async def test_should_send_receive(self): telemetry = Mock() my_logger = TelemetryLoggerMiddleware(telemetry, True) @@ -55,7 +103,7 @@ async def logic(context: TurnContext): "conversationName": None, "locale": None, "recipientId": "bot", - "recipientName": "user", + "recipientName": "Bot", }, ), ( @@ -76,7 +124,7 @@ async def logic(context: TurnContext): "conversationName": None, "locale": None, "recipientId": "bot", - "recipientName": "user", + "recipientName": "Bot", "fromName": "user", }, ), @@ -147,7 +195,7 @@ async def process(context: TurnContext) -> None: "conversationName": None, "locale": None, "recipientId": "bot", - "recipientName": "user", + "recipientName": "Bot", }, ), ( @@ -169,7 +217,7 @@ async def process(context: TurnContext) -> None: "conversationName": None, "locale": None, "recipientId": "bot", - "recipientName": "user", + "recipientName": "Bot", "fromName": "user", }, ), @@ -186,6 +234,53 @@ async def process(context: TurnContext) -> None: ] self.assert_telemetry_calls(telemetry, telemetry_call_expected) + async def test_log_teams(self): + telemetry = Mock() + my_logger = TelemetryLoggerMiddleware(telemetry, True) + + adapter = TestAdapter( + template_or_conversation=ConversationReference(channel_id=Channels.ms_teams) + ) + adapter.use(my_logger) + + team_info = TeamInfo( + id="teamId", + name="teamName", + ) + + channel_data = TeamsChannelData( + team=team_info, + tenant=TenantInfo(id="tenantId"), + ).serialize() + + activity = MessageFactory.text("test") + activity.channel_data = channel_data + activity.from_property = ChannelAccount( + id="userId", + name="userName", + aad_object_id="aaId", + ) + + test_flow = TestFlow(None, adapter) + await test_flow.send(activity) + + telemetry_call_expected = [ + ( + TelemetryLoggerConstants.BOT_MSG_RECEIVE_EVENT, + { + "text": "test", + "fromId": "userId", + "recipientId": "bot", + "recipientName": "Bot", + "TeamsTenantId": "tenantId", + "TeamsUserAadObjectId": "aaId", + "TeamsTeamInfo": TeamInfo.serialize(team_info), + }, + ), + ] + + self.assert_telemetry_calls(telemetry, telemetry_call_expected) + def create_reply(self, activity, text, locale=None): return Activity( type=ActivityTypes.message, diff --git a/libraries/botbuilder-core/tests/test_test_adapter.py b/libraries/botbuilder-core/tests/test_test_adapter.py index 4312ca352..269a5197f 100644 --- a/libraries/botbuilder-core/tests/test_test_adapter.py +++ b/libraries/botbuilder-core/tests/test_test_adapter.py @@ -3,10 +3,11 @@ import aiounittest +from botframework.connector.auth import MicrosoftAppCredentials from botbuilder.core import TurnContext from botbuilder.core.adapters import TestAdapter from botbuilder.schema import Activity, ConversationReference, ChannelAccount -from botframework.connector.auth import MicrosoftAppCredentials +from botframework.connector import Channels RECEIVED_MESSAGE = Activity(type="message", text="received") UPDATED_ACTIVITY = Activity(type="message", text="update") @@ -141,7 +142,7 @@ async def logic(context: TurnContext): async def test_get_user_token_returns_null(self): adapter = TestAdapter() activity = Activity( - channel_id="directline", from_property=ChannelAccount(id="testuser") + channel_id=Channels.direct_line, from_property=ChannelAccount(id="testuser") ) turn_context = TurnContext(adapter, activity) @@ -158,7 +159,7 @@ async def test_get_user_token_returns_null(self): async def test_get_user_token_returns_null_with_code(self): adapter = TestAdapter() activity = Activity( - channel_id="directline", from_property=ChannelAccount(id="testuser") + channel_id=Channels.direct_line, from_property=ChannelAccount(id="testuser") ) turn_context = TurnContext(adapter, activity) @@ -180,7 +181,7 @@ async def test_get_user_token_returns_null_with_code(self): async def test_get_user_token_returns_token(self): adapter = TestAdapter() connection_name = "myConnection" - channel_id = "directline" + channel_id = Channels.direct_line user_id = "testUser" token = "abc123" activity = Activity( @@ -207,7 +208,7 @@ async def test_get_user_token_returns_token(self): async def test_get_user_token_returns_token_with_magice_code(self): adapter = TestAdapter() connection_name = "myConnection" - channel_id = "directline" + channel_id = Channels.direct_line user_id = "testUser" token = "abc123" magic_code = "888999" @@ -245,3 +246,34 @@ async def test_get_user_token_returns_token_with_magice_code(self): assert token_response assert token == token_response.token assert connection_name == token_response.connection_name + + async def test_should_validate_no_reply_when_no_reply_expected(self): + async def logic(context: TurnContext): + await context.send_activity(RECEIVED_MESSAGE) + + adapter = TestAdapter(logic) + test_flow = await adapter.test("test", "received") + await test_flow.assert_no_reply("should be no additional replies") + + async def test_should_timeout_waiting_for_assert_no_reply_when_no_reply_expected( + self, + ): + async def logic(context: TurnContext): + await context.send_activity(RECEIVED_MESSAGE) + + adapter = TestAdapter(logic) + test_flow = await adapter.test("test", "received") + await test_flow.assert_no_reply("no reply received", 500) + + async def test_should_throw_error_with_assert_no_reply_when_no_reply_expected_but_was_received( + self, + ): + async def logic(context: TurnContext): + activities = [RECEIVED_MESSAGE, RECEIVED_MESSAGE] + await context.send_activities(activities) + + adapter = TestAdapter(logic) + test_flow = await adapter.test("test", "received") + + with self.assertRaises(Exception): + await test_flow.assert_no_reply("should be no additional replies") diff --git a/libraries/botbuilder-core/tests/test_transcript_logger_middleware.py b/libraries/botbuilder-core/tests/test_transcript_logger_middleware.py new file mode 100644 index 000000000..2752043e5 --- /dev/null +++ b/libraries/botbuilder-core/tests/test_transcript_logger_middleware.py @@ -0,0 +1,44 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest + +from botbuilder.core import ( + MemoryTranscriptStore, + TranscriptLoggerMiddleware, + TurnContext, +) +from botbuilder.core.adapters import TestAdapter, TestFlow +from botbuilder.schema import Activity, ActivityEventNames, ActivityTypes + + +class TestTranscriptLoggerMiddleware(aiounittest.AsyncTestCase): + async def test_should_not_log_continue_conversation(self): + transcript_store = MemoryTranscriptStore() + conversation_id = "" + sut = TranscriptLoggerMiddleware(transcript_store) + + async def aux_logic(context: TurnContext): + nonlocal conversation_id + conversation_id = context.activity.conversation.id + + adapter = TestAdapter(aux_logic) + adapter.use(sut) + + continue_conversation_activity = Activity( + type=ActivityTypes.event, name=ActivityEventNames.continue_conversation + ) + + test_flow = TestFlow(None, adapter) + step1 = await test_flow.send("foo") + step2 = await step1.send("bar") + await step2.send(continue_conversation_activity) + + paged_result = await transcript_store.get_transcript_activities( + "test", conversation_id + ) + self.assertEqual( + len(paged_result.items), + 2, + "only the two message activities should be logged", + ) diff --git a/libraries/botbuilder-core/tests/test_turn_context.py b/libraries/botbuilder-core/tests/test_turn_context.py index 5f3668844..7247caab9 100644 --- a/libraries/botbuilder-core/tests/test_turn_context.py +++ b/libraries/botbuilder-core/tests/test_turn_context.py @@ -23,6 +23,7 @@ recipient=ChannelAccount(id="bot", name="Bot Name"), conversation=ConversationAccount(id="convo", name="Convo Name"), channel_id="UnitTest", + locale="en-uS", # Intentionally oddly-cased to check that it isn't defaulted somewhere, but tests stay in English service_url="https://example.org", ) @@ -34,7 +35,7 @@ async def send_activities(self, context, activities) -> List[ResourceResponse]: assert activities is not None assert isinstance(activities, list) assert activities - for (idx, activity) in enumerate(activities): # pylint: disable=unused-variable + for idx, activity in enumerate(activities): # pylint: disable=unused-variable assert isinstance(activity, Activity) assert activity.type == "message" or activity.type == ActivityTypes.trace responses.append(ResourceResponse(id="5678")) @@ -257,6 +258,7 @@ def test_get_conversation_reference_should_return_valid_reference(self): assert reference.bot == ACTIVITY.recipient assert reference.conversation == ACTIVITY.conversation assert reference.channel_id == ACTIVITY.channel_id + assert reference.locale == ACTIVITY.locale assert reference.service_url == ACTIVITY.service_url def test_apply_conversation_reference_should_return_prepare_reply_when_is_incoming_is_false( @@ -270,6 +272,7 @@ def test_apply_conversation_reference_should_return_prepare_reply_when_is_incomi assert reply.recipient == ACTIVITY.from_property assert reply.from_property == ACTIVITY.recipient assert reply.conversation == ACTIVITY.conversation + assert reply.locale == ACTIVITY.locale assert reply.service_url == ACTIVITY.service_url assert reply.channel_id == ACTIVITY.channel_id @@ -284,6 +287,7 @@ def test_apply_conversation_reference_when_is_incoming_is_true_should_not_prepar assert reply.recipient == ACTIVITY.recipient assert reply.from_property == ACTIVITY.from_property assert reply.conversation == ACTIVITY.conversation + assert reply.locale == ACTIVITY.locale assert reply.service_url == ACTIVITY.service_url assert reply.channel_id == ACTIVITY.channel_id @@ -346,6 +350,48 @@ def test_should_remove_at_mention_with_regex_characters(self): assert text == " test activity" assert activity.text == " test activity" + def test_should_remove_custom_mention_from_activity(self): + activity = Activity( + text="Hallo", + text_format="plain", + type="message", + timestamp="2025-03-11T14:16:47.0093935Z", + id="1741702606984", + channel_id="msteams", + service_url="https://smba.trafficmanager.net/emea/REDACTED/", + from_property=ChannelAccount( + id="29:1J-K4xVh-sLpdwQ-R5GkOZ_TB0W3ec_37p710aH8qe8bITA0zxdgIGc9l-MdDdkdE_jasSfNOeWXyyL1nsrHtBQ", + name="", + aad_object_id="REDACTED", + ), + conversation=ConversationAccount( + is_group=True, + conversation_type="groupChat", + tenant_id="REDACTED", + id="19:Ql86tXNM2lTBXNKJdqKdwIF9ltGZwpvluLvnJdA0tmg1@thread.v2", + ), + recipient=ChannelAccount( + id="28:c5d5fb56-a1a4-4467-a7a3-1b37905498a0", name="Azure AI Agent" + ), + entities=[ + Entity().deserialize( + Mention( + type="mention", + mentioned=ChannelAccount( + id="28:c5d5fb56-a1a4-4467-a7a3-1b37905498a0", + name="Custom Agent", + ), + ).serialize() + ) + ], + channel_data={"tenant": {"id": "REDACTED"}, "productContext": "COPILOT"}, + ) + + text = TurnContext.remove_mention_text(activity, activity.recipient.id) + + assert text == "Hallo" + assert activity.text == "Hallo" + async def test_should_send_a_trace_activity(self): context = TurnContext(SimpleAdapter(), ACTIVITY) called = False diff --git a/libraries/botbuilder-core/tests/test_user_state.py b/libraries/botbuilder-core/tests/test_user_state.py index a39ee107a..9f7e22679 100644 --- a/libraries/botbuilder-core/tests/test_user_state.py +++ b/libraries/botbuilder-core/tests/test_user_state.py @@ -6,17 +6,20 @@ from botbuilder.core import TurnContext, MemoryStorage, UserState from botbuilder.core.adapters import TestAdapter from botbuilder.schema import Activity, ChannelAccount +from botframework.connector import Channels RECEIVED_MESSAGE = Activity( type="message", text="received", - channel_id="test", + channel_id=Channels.test, from_property=ChannelAccount(id="user"), ) MISSING_CHANNEL_ID = Activity( type="message", text="received", from_property=ChannelAccount(id="user") ) -MISSING_FROM_PROPERTY = Activity(type="message", text="received", channel_id="test") +MISSING_FROM_PROPERTY = Activity( + type="message", text="received", channel_id=Channels.test +) class TestUserState(aiounittest.AsyncTestCase): diff --git a/libraries/botbuilder-dialogs/README.rst b/libraries/botbuilder-dialogs/README.rst index 6c8208769..f76dc1983 100644 --- a/libraries/botbuilder-dialogs/README.rst +++ b/libraries/botbuilder-dialogs/README.rst @@ -3,8 +3,8 @@ BotBuilder-Dialogs SDK for Python ================================= -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch .. image:: https://badge.fury.io/py/botbuilder-dialogs.svg diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/__init__.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/__init__.py index bf2c8ae32..37c305536 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/__init__.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/__init__.py @@ -7,7 +7,9 @@ from .about import __version__ from .component_dialog import ComponentDialog +from .dialog_container import DialogContainer from .dialog_context import DialogContext +from .dialog_event import DialogEvent from .dialog_events import DialogEvents from .dialog_instance import DialogInstance from .dialog_reason import DialogReason @@ -15,17 +17,25 @@ from .dialog_state import DialogState from .dialog_turn_result import DialogTurnResult from .dialog_turn_status import DialogTurnStatus +from .dialog_manager import DialogManager +from .dialog_manager_result import DialogManagerResult from .dialog import Dialog +from .dialogs_component_registration import DialogsComponentRegistration +from .persisted_state_keys import PersistedStateKeys +from .persisted_state import PersistedState from .waterfall_dialog import WaterfallDialog from .waterfall_step_context import WaterfallStepContext from .dialog_extensions import DialogExtensions from .prompts import * from .choices import * from .skills import * +from .object_path import ObjectPath __all__ = [ "ComponentDialog", + "DialogContainer", "DialogContext", + "DialogEvent", "DialogEvents", "DialogInstance", "DialogReason", @@ -33,7 +43,10 @@ "DialogState", "DialogTurnResult", "DialogTurnStatus", + "DialogManager", + "DialogManagerResult", "Dialog", + "DialogsComponentRegistration", "WaterfallDialog", "WaterfallStepContext", "ConfirmPrompt", @@ -42,11 +55,14 @@ "NumberPrompt", "OAuthPrompt", "OAuthPromptSettings", + "PersistedStateKeys", + "PersistedState", "PromptRecognizerResult", "PromptValidatorContext", "Prompt", "PromptOptions", "TextPrompt", "DialogExtensions", + "ObjectPath", "__version__", ] diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/_user_token_access.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/_user_token_access.py new file mode 100644 index 000000000..ba25a0baa --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/_user_token_access.py @@ -0,0 +1,140 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC + +from botbuilder.core import TurnContext +from botbuilder.core.bot_framework_adapter import TokenExchangeRequest +from botbuilder.core.oauth import ConnectorClientBuilder, ExtendedUserTokenProvider +from botbuilder.schema import TokenResponse +from botframework.connector import ConnectorClient +from botframework.connector.auth import ClaimsIdentity, ConnectorFactory +from botframework.connector.auth.user_token_client import UserTokenClient +from botframework.connector.token_api.models import SignInUrlResponse + +from .prompts.oauth_prompt_settings import OAuthPromptSettings + + +class _UserTokenAccess(ABC): + @staticmethod + async def get_user_token( + turn_context: TurnContext, settings: OAuthPromptSettings, magic_code: str + ) -> TokenResponse: + user_token_client: UserTokenClient = turn_context.turn_state.get( + UserTokenClient.__name__, None + ) + if user_token_client: + return await user_token_client.get_user_token( + turn_context.activity.from_property.id, + settings.connection_name, + turn_context.activity.channel_id, + magic_code, + ) + if isinstance(turn_context.adapter, ExtendedUserTokenProvider): + return await turn_context.adapter.get_user_token( + turn_context, + settings.connection_name, + magic_code, + settings.oath_app_credentials, + ) + + raise TypeError("OAuthPrompt is not supported by the current adapter") + + @staticmethod + async def get_sign_in_resource( + turn_context: TurnContext, settings: OAuthPromptSettings + ) -> SignInUrlResponse: + user_token_client: UserTokenClient = turn_context.turn_state.get( + UserTokenClient.__name__, None + ) + if user_token_client: + return await user_token_client.get_sign_in_resource( + settings.connection_name, turn_context.activity, None + ) + if isinstance(turn_context.adapter, ExtendedUserTokenProvider): + return await turn_context.adapter.get_sign_in_resource_from_user_and_credentials( + turn_context, + settings.oath_app_credentials, + settings.connection_name, + ( + turn_context.activity.from_property.id + if turn_context.activity and turn_context.activity.from_property + else None + ), + ) + + raise TypeError("OAuthPrompt is not supported by the current adapter") + + @staticmethod + async def sign_out_user(turn_context: TurnContext, settings: OAuthPromptSettings): + user_token_client: UserTokenClient = turn_context.turn_state.get( + UserTokenClient.__name__, None + ) + if user_token_client: + return await user_token_client.sign_out_user( + turn_context.activity.from_property.id, + settings.connection_name, + turn_context.activity.channel_id, + ) + if isinstance(turn_context.adapter, ExtendedUserTokenProvider): + return await turn_context.adapter.sign_out_user( + turn_context, + settings.connection_name, + ( + turn_context.activity.from_property.id + if turn_context.activity and turn_context.activity.from_property + else None + ), + settings.oath_app_credentials, + ) + + raise TypeError("OAuthPrompt is not supported by the current adapter") + + @staticmethod + async def exchange_token( + turn_context: TurnContext, + settings: OAuthPromptSettings, + token_exchange_request: TokenExchangeRequest, + ) -> TokenResponse: + user_token_client: UserTokenClient = turn_context.turn_state.get( + UserTokenClient.__name__, None + ) + user_id = turn_context.activity.from_property.id + if user_token_client: + channel_id = turn_context.activity.channel_id + return await user_token_client.exchange_token( + user_id, + settings.connection_name, + channel_id, + token_exchange_request, + ) + if isinstance(turn_context.adapter, ExtendedUserTokenProvider): + return await turn_context.adapter.exchange_token( + turn_context, + settings.connection_name, + user_id, + token_exchange_request, + ) + + raise TypeError("OAuthPrompt is not supported by the current adapter") + + @staticmethod + async def create_connector_client( + turn_context: TurnContext, + service_url: str, + claims_identity: ClaimsIdentity, + audience: str, + ) -> ConnectorClient: + connector_factory: ConnectorFactory = turn_context.turn_state.get( + ConnectorFactory.__name__, None + ) + if connector_factory: + return await connector_factory.create(service_url, audience) + if isinstance(turn_context.adapter, ConnectorClientBuilder): + return await turn_context.adapter.create_connector_client( + service_url, + claims_identity, + audience, + ) + + raise TypeError("OAuthPrompt is not supported by the current adapter") diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/about.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/about.py index 2f0ceb142..5885a1a1e 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/about.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/about.py @@ -5,7 +5,7 @@ __title__ = "botbuilder-dialogs" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/channel.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/channel.py index 4c1c59d0f..aa19a2740 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/channel.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/channel.py @@ -32,9 +32,9 @@ def supports_suggested_actions(channel_id: str, button_cnt: int = 100) -> bool: # https://dev.kik.com/#/docs/messaging#text-response-object Channels.kik: 20, Channels.telegram: 100, - Channels.slack: 100, Channels.emulator: 100, Channels.direct_line: 100, + Channels.direct_line_speech: 100, Channels.webchat: 100, } return ( @@ -62,10 +62,11 @@ def supports_card_actions(channel_id: str, button_cnt: int = 100) -> bool: Channels.ms_teams: 3, Channels.line: 99, Channels.slack: 100, + Channels.telegram: 100, Channels.emulator: 100, Channels.direct_line: 100, + Channels.direct_line_speech: 100, Channels.webchat: 100, - Channels.cortana: 100, } return ( button_cnt <= max_actions[channel_id] @@ -74,7 +75,7 @@ def supports_card_actions(channel_id: str, button_cnt: int = 100) -> bool: ) @staticmethod - def has_message_feed(channel_id: str) -> bool: + def has_message_feed(_: str) -> bool: """Determine if a Channel has a Message Feed. Args: @@ -84,7 +85,7 @@ def has_message_feed(channel_id: str) -> bool: bool: True if the Channel has a Message Feed, False if it does not. """ - return not channel_id == Channels.cortana + return True @staticmethod def max_action_title_length( # pylint: disable=unused-argument diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/choice_factory.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/choice_factory.py index 52bf778b3..ef1dfc117 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/choice_factory.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/choice_factory.py @@ -26,7 +26,7 @@ def for_channel( Creates a message activity that includes a list of choices formatted based on the capabilities of a given channel. - Parameters: + Parameters ---------- channel_id: A channel ID. choices: List of choices to render @@ -46,8 +46,7 @@ def for_channel( else: size = len(choice.value) - if size > max_title_length: - max_title_length = size + max_title_length = max(max_title_length, size) # Determine list style supports_suggested_actions = Channel.supports_suggested_actions( @@ -81,7 +80,7 @@ def inline( """ Creates a message activity that includes a list of choices formatted as an inline list. - Parameters: + Parameters ---------- choices: The list of choices to render. text: (Optional) The text of the message to send. @@ -140,7 +139,7 @@ def list_style( """ Creates a message activity that includes a list of choices formatted as a numbered or bulleted list. - Parameters: + Parameters ---------- choices: The list of choices to render. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/choice_recognizers.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/choice_recognizers.py index 02fb71e6e..4bdec08c3 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/choice_recognizers.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/choice_recognizers.py @@ -14,7 +14,7 @@ class ChoiceRecognizers: - """ Contains methods for matching user input against a list of choices. """ + """Contains methods for matching user input against a list of choices.""" @staticmethod def recognize_choices( @@ -34,7 +34,7 @@ def recognize_choices( - By 1's based ordinal position. - By 1's based index position. - Parameters: + Parameters ----------- utterance: The input. @@ -43,7 +43,7 @@ def recognize_choices( options: (Optional) Options to control the recognition strategy. - Returns: + Returns -------- A list of found choices, sorted by most relevant first. """ diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find.py index f7f5b3cab..b3b3c6b99 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find.py @@ -14,7 +14,7 @@ class Find: - """ Contains methods for matching user input against a list of choices """ + """Contains methods for matching user input against a list of choices""" @staticmethod def find_choices( @@ -22,7 +22,7 @@ def find_choices( choices: [Union[str, Choice]], options: FindChoicesOptions = None, ): - """ Matches user input against a list of choices """ + """Matches user input against a list of choices""" if not choices: raise TypeError( @@ -92,16 +92,15 @@ def find_values( # Search for each value within the utterance. matches: [ModelResult] = [] opt = options if options else FindValuesOptions() - tokenizer: Callable[ - [str, str], List[Token] - ] = opt.tokenizer if opt.tokenizer else Tokenizer.default_tokenizer + tokenizer: Callable[[str, str], List[Token]] = ( + opt.tokenizer if opt.tokenizer else Tokenizer.default_tokenizer + ) tokens = tokenizer(utterance, opt.locale) max_distance = ( opt.max_token_distance if opt.max_token_distance is not None else 2 ) for entry in sorted_values: - # Find all matches for a value # - To match "last one" in "the last time I chose the last one" we need # to re-search the string starting from the end of the previous match. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find_choices_options.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find_choices_options.py index 418781ddb..750ab79c6 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find_choices_options.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find_choices_options.py @@ -5,7 +5,7 @@ class FindChoicesOptions(FindValuesOptions): - """ Contains options to control how input is matched against a list of choices """ + """Contains options to control how input is matched against a list of choices""" def __init__( self, @@ -16,7 +16,7 @@ def __init__( **kwargs, ): """ - Parameters: + Parameters ----------- no_value: (Optional) If `True`, the choices `value` field will NOT be search over. Defaults to `False`. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find_values_options.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find_values_options.py index 63753dda2..5af0614db 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find_values_options.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/find_values_options.py @@ -7,7 +7,7 @@ class FindValuesOptions: - """ Contains search options, used to control how choices are recognized in a user's utterance. """ + """Contains search options, used to control how choices are recognized in a user's utterance.""" def __init__( self, @@ -17,7 +17,7 @@ def __init__( tokenizer: Callable[[str, str], List[Token]] = None, ): """ - Parameters: + Parameters ---------- allow_partial_matches: (Optional) If `True`, then only some of the tokens in a value need to exist to diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/found_choice.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/found_choice.py index f3e994566..c179eab4c 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/found_choice.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/found_choice.py @@ -3,11 +3,11 @@ class FoundChoice: - """ Represents a result from matching user input against a list of choices """ + """Represents a result from matching user input against a list of choices""" def __init__(self, value: str, index: int, score: float, synonym: str = None): """ - Parameters: + Parameters ---------- value: The value of the choice that was matched. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/found_value.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/found_value.py index e0a94a269..48e236dc8 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/found_value.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/found_value.py @@ -3,11 +3,11 @@ class FoundValue: - """ Represents a result from matching user input against a list of choices """ + """Represents a result from matching user input against a list of choices""" def __init__(self, value: str, index: int, score: float): """ - Parameters: + Parameters ---------- value: The value that was matched. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/model_result.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/model_result.py index d2ec65a1e..b3dbe5beb 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/model_result.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/model_result.py @@ -9,7 +9,7 @@ def __init__( self, text: str, start: int, end: int, type_name: str, resolution: object ): """ - Parameters: + Parameters ---------- text: Substring of the utterance that was recognized. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/sorted_value.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/sorted_value.py index ea9724b23..f03c38aef 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/sorted_value.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/sorted_value.py @@ -3,11 +3,11 @@ class SortedValue: - """ A value that can be sorted and still refer to its original position with a source array. """ + """A value that can be sorted and still refer to its original position with a source array.""" def __init__(self, value: str, index: int): """ - Parameters: + Parameters ----------- value: The value that will be sorted. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/token.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/token.py index 3321e2eb4..1b7e028f2 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/token.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/token.py @@ -3,11 +3,11 @@ class Token: - """ Represents an individual token, such as a word in an input string. """ + """Represents an individual token, such as a word in an input string.""" def __init__(self, start: int, end: int, text: str, normalized: str): """ - Parameters: + Parameters ---------- start: The index of the first character of the token within the outer input string. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/tokenizer.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/tokenizer.py index 1b4af2139..59e796c84 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/tokenizer.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/choices/tokenizer.py @@ -7,7 +7,7 @@ class Tokenizer: - """ Provides a default tokenizer implementation. """ + """Provides a default tokenizer implementation.""" @staticmethod def default_tokenizer( # pylint: disable=unused-argument @@ -75,7 +75,7 @@ def _is_breaking_char(code_point) -> bool: @staticmethod def _is_between(value: int, from_val: int, to_val: int) -> bool: """ - Parameters: + Parameters ----------- value: number value diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/component_dialog.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/component_dialog.py index 34c84fbff..f063b4827 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/component_dialog.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/component_dialog.py @@ -164,7 +164,6 @@ async def end_dialog( :param context: The context object for this turn. :type context: :class:`botbuilder.core.TurnContext` :param instance: State information associated with the instance of this component dialog. - on its parent's dialog stack. :type instance: :class:`botbuilder.dialogs.DialogInstance` :param reason: Reason why the dialog ended. :type reason: :class:`botbuilder.dialogs.DialogReason` diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog.py index 63d816b94..f07a8afa5 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog.py @@ -4,6 +4,7 @@ from botbuilder.core import TurnContext, NullTelemetryClient, BotTelemetryClient from .dialog_reason import DialogReason +from .dialog_event import DialogEvent from .dialog_turn_status import DialogTurnStatus from .dialog_turn_result import DialogTurnResult from .dialog_instance import DialogInstance @@ -105,3 +106,82 @@ async def end_dialog( # pylint: disable=unused-argument """ # No-op by default return + + def get_version(self) -> str: + return self.id + + async def on_dialog_event( + self, dialog_context: "DialogContext", dialog_event: DialogEvent + ) -> bool: + """ + Called when an event has been raised, using `DialogContext.emitEvent()`, by either the current dialog or a + dialog that the current dialog started. + :param dialog_context: The dialog context for the current turn of conversation. + :param dialog_event: The event being raised. + :return: True if the event is handled by the current dialog and bubbling should stop. + """ + # Before bubble + handled = await self._on_pre_bubble_event(dialog_context, dialog_event) + + # Bubble as needed + if (not handled) and dialog_event.bubble and dialog_context.parent: + handled = await dialog_context.parent.emit_event( + dialog_event.name, dialog_event.value, True, False + ) + + # Post bubble + if not handled: + handled = await self._on_post_bubble_event(dialog_context, dialog_event) + + return handled + + async def _on_pre_bubble_event( # pylint: disable=unused-argument + self, dialog_context: "DialogContext", dialog_event: DialogEvent + ) -> bool: + """ + Called before an event is bubbled to its parent. + This is a good place to perform interception of an event as returning `true` will prevent + any further bubbling of the event to the dialogs parents and will also prevent any child + dialogs from performing their default processing. + :param dialog_context: The dialog context for the current turn of conversation. + :param dialog_event: The event being raised. + :return: Whether the event is handled by the current dialog and further processing should stop. + """ + return False + + async def _on_post_bubble_event( # pylint: disable=unused-argument + self, dialog_context: "DialogContext", dialog_event: DialogEvent + ) -> bool: + """ + Called after an event was bubbled to all parents and wasn't handled. + This is a good place to perform default processing logic for an event. Returning `true` will + prevent any processing of the event by child dialogs. + :param dialog_context: The dialog context for the current turn of conversation. + :param dialog_event: The event being raised. + :return: Whether the event is handled by the current dialog and further processing should stop. + """ + return False + + def _on_compute_id(self) -> str: + """ + Computes an unique ID for a dialog. + :return: An unique ID for a dialog + """ + return self.__class__.__name__ + + def _register_source_location( + self, path: str, line_number: int + ): # pylint: disable=unused-argument + """ + Registers a SourceRange in the provided location. + :param path: The path to the source file. + :param line_number: The line number where the source will be located on the file. + """ + if path: + # This will be added when debbuging support is ported. + # DebugSupport.source_map.add(self, SourceRange( + # path = path, + # start_point = SourcePoint(line_index = line_number, char_index = 0 ), + # end_point = SourcePoint(line_index = line_number + 1, char_index = 0 ), + # ) + pass diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_container.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_container.py new file mode 100644 index 000000000..1e0a6267c --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_container.py @@ -0,0 +1,108 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod + + +from botbuilder.core import NullTelemetryClient, BotTelemetryClient +from .dialog import Dialog +from .dialog_context import DialogContext +from .dialog_event import DialogEvent +from .dialog_events import DialogEvents +from .dialog_set import DialogSet + + +class DialogContainer(Dialog, ABC): + def __init__(self, dialog_id: str = None): + super().__init__(dialog_id) + + self.dialogs = DialogSet() + + @property + def telemetry_client(self) -> BotTelemetryClient: + """ + Gets the telemetry client for logging events. + """ + return self._telemetry_client + + @telemetry_client.setter + def telemetry_client(self, value: BotTelemetryClient) -> None: + """ + Sets the telemetry client for all dialogs in this set. + """ + if value is None: + self._telemetry_client = NullTelemetryClient() + else: + self._telemetry_client = value + + # Care! Dialogs.TelemetryClient assignment internally assigns the + # TelemetryClient for each dialog which could lead to an eventual stack + # overflow in cyclical dialog structures. + # Don't set the telemetry client if the candidate instance is the same as + # the currently set one. + if self.dialogs.telemetry_client != value: + self.dialogs.telemetry_client = self._telemetry_client + + @abstractmethod + def create_child_context(self, dialog_context: DialogContext) -> DialogContext: + raise NotImplementedError() + + def find_dialog(self, dialog_id: str) -> Dialog: + # TODO: deprecate DialogSet.find + return self.dialogs.find_dialog(dialog_id) + + async def on_dialog_event( + self, dialog_context: DialogContext, dialog_event: DialogEvent + ) -> bool: + """ + Called when an event has been raised, using `DialogContext.emitEvent()`, by either the current dialog or a + dialog that the current dialog started. + :param dialog_context: The dialog context for the current turn of conversation. + :param dialog_event: The event being raised. + :return: True if the event is handled by the current dialog and bubbling should stop. + """ + handled = await super().on_dialog_event(dialog_context, dialog_event) + + # Trace unhandled "versionChanged" events. + if not handled and dialog_event.name == DialogEvents.version_changed: + trace_message = ( + f"Unhandled dialog event: {dialog_event.name}. Active Dialog: " + f"{dialog_context.active_dialog.id}" + ) + + await dialog_context.context.send_trace_activity(trace_message) + + return handled + + def get_internal_version(self) -> str: + """ + GetInternalVersion - Returns internal version identifier for this container. + DialogContainers detect changes of all sub-components in the container and map that to an DialogChanged event. + Because they do this, DialogContainers "hide" the internal changes and just have the .id. This isolates changes + to the container level unless a container doesn't handle it. To support this DialogContainers define a + protected virtual method GetInternalVersion() which computes if this dialog or child dialogs have changed + which is then examined via calls to check_for_version_change_async(). + :return: version which represents the change of the internals of this container. + """ + return self.dialogs.get_version() + + async def check_for_version_change_async(self, dialog_context: DialogContext): + """ + :param dialog_context: dialog context. + :return: task. + Checks to see if a containers child dialogs have changed since the current dialog instance + was started. + + This should be called at the start of `beginDialog()`, `continueDialog()`, and `resumeDialog()`. + """ + current = dialog_context.active_dialog.version + dialog_context.active_dialog.version = self.get_internal_version() + + # Check for change of previously stored hash + if current and current != dialog_context.active_dialog.version: + # Give bot an opportunity to handle the change. + # - If bot handles it the changeHash will have been updated as to avoid triggering the + # change again. + await dialog_context.emit_event( + DialogEvents.version_changed, self.id, True, False + ) diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_context.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_context.py index 2862acfb0..0181e67a2 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_context.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_context.py @@ -1,7 +1,14 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from typing import List, Optional + from botbuilder.core.turn_context import TurnContext +from botbuilder.dialogs.memory import DialogStateManager + +from .dialog_event import DialogEvent +from .dialog_events import DialogEvents +from .dialog_set import DialogSet from .dialog_state import DialogState from .dialog_turn_status import DialogTurnStatus from .dialog_turn_result import DialogTurnResult @@ -12,7 +19,7 @@ class DialogContext: def __init__( - self, dialog_set: object, turn_context: TurnContext, state: DialogState + self, dialog_set: DialogSet, turn_context: TurnContext, state: DialogState ): if dialog_set is None: raise TypeError("DialogContext(): dialog_set cannot be None.") @@ -21,16 +28,17 @@ def __init__( raise TypeError("DialogContext(): turn_context cannot be None.") self._turn_context = turn_context self._dialogs = dialog_set - # self._id = dialog_id; self._stack = state.dialog_stack - self.parent = None + self.services = {} + self.parent: DialogContext = None + self.state = DialogStateManager(self) @property - def dialogs(self): + def dialogs(self) -> DialogSet: """Gets the set of dialogs that can be called from this context. :param: - :return str: + :return DialogSet: """ return self._dialogs @@ -39,16 +47,16 @@ def context(self) -> TurnContext: """Gets the context for the current turn of conversation. :param: - :return str: + :return TurnContext: """ return self._turn_context @property - def stack(self): + def stack(self) -> List: """Gets the current dialog stack. :param: - :return str: + :return list: """ return self._stack @@ -57,38 +65,63 @@ def active_dialog(self): """Return the container link in the database. :param: - :return str: + :return: """ if self._stack: return self._stack[0] return None + @property + def child(self) -> Optional["DialogContext"]: + """Return the container link in the database. + + :param: + :return DialogContext: + """ + # pylint: disable=import-outside-toplevel + instance = self.active_dialog + + if instance: + dialog = self.find_dialog_sync(instance.id) + + # This import prevents circular dependency issues + from .dialog_container import DialogContainer + + if isinstance(dialog, DialogContainer): + return dialog.create_child_context(self) + + return None + async def begin_dialog(self, dialog_id: str, options: object = None): """ Pushes a new dialog onto the dialog stack. - :param dialog_id: ID of the dialog to start.. + :param dialog_id: ID of the dialog to start :param options: (Optional) additional argument(s) to pass to the dialog being started. """ - if not dialog_id: - raise TypeError("Dialog(): dialogId cannot be None.") - # Look up dialog - dialog = await self.find_dialog(dialog_id) - if dialog is None: - raise Exception( - "'DialogContext.begin_dialog(): A dialog with an id of '%s' wasn't found." - " The dialog must be included in the current or parent DialogSet." - " For example, if subclassing a ComponentDialog you can call add_dialog() within your constructor." - % dialog_id - ) - # Push new instance onto stack - instance = DialogInstance() - instance.id = dialog_id - instance.state = {} + try: + if not dialog_id: + raise TypeError("Dialog(): dialog_id cannot be None.") + # Look up dialog + dialog = await self.find_dialog(dialog_id) + if dialog is None: + raise Exception( + "'DialogContext.begin_dialog(): A dialog with an id of '%s' wasn't found." + " The dialog must be included in the current or parent DialogSet." + " For example, if subclassing a ComponentDialog you can call add_dialog() within your constructor." + % dialog_id + ) + # Push new instance onto stack + instance = DialogInstance() + instance.id = dialog_id + instance.state = {} - self._stack.insert(0, (instance)) + self._stack.insert(0, (instance)) - # Call dialog's begin_dialog() method - return await dialog.begin_dialog(self, options) + # Call dialog's begin_dialog() method + return await dialog.begin_dialog(self, options) + except Exception as err: + self.__set_exception_context_data(err) + raise # TODO: Fix options: PromptOptions instead of object async def prompt(self, dialog_id: str, options) -> DialogTurnResult: @@ -99,13 +132,17 @@ async def prompt(self, dialog_id: str, options) -> DialogTurnResult: :param options: Contains a Prompt, potentially a RetryPrompt and if using ChoicePrompt, Choices. :return: """ - if not dialog_id: - raise TypeError("DialogContext.prompt(): dialogId cannot be None.") + try: + if not dialog_id: + raise TypeError("DialogContext.prompt(): dialogId cannot be None.") - if not options: - raise TypeError("DialogContext.prompt(): options cannot be None.") + if not options: + raise TypeError("DialogContext.prompt(): options cannot be None.") - return await self.begin_dialog(dialog_id, options) + return await self.begin_dialog(dialog_id, options) + except Exception as err: + self.__set_exception_context_data(err) + raise async def continue_dialog(self): """ @@ -114,20 +151,25 @@ async def continue_dialog(self): to determine if a dialog was run and a reply was sent to the user. :return: """ - # Check for a dialog on the stack - if self.active_dialog is not None: - # Look up dialog - dialog = await self.find_dialog(self.active_dialog.id) - if not dialog: - raise Exception( - "DialogContext.continue_dialog(): Can't continue dialog. A dialog with an id of '%s' wasn't found." - % self.active_dialog.id - ) - - # Continue execution of dialog - return await dialog.continue_dialog(self) - - return DialogTurnResult(DialogTurnStatus.Empty) + try: + # Check for a dialog on the stack + if self.active_dialog is not None: + # Look up dialog + dialog = await self.find_dialog(self.active_dialog.id) + if not dialog: + raise Exception( + "DialogContext.continue_dialog(): Can't continue dialog. " + "A dialog with an id of '%s' wasn't found." + % self.active_dialog.id + ) + + # Continue execution of dialog + return await dialog.continue_dialog(self) + + return DialogTurnResult(DialogTurnStatus.Empty) + except Exception as err: + self.__set_exception_context_data(err) + raise # TODO: instance is DialogInstance async def end_dialog(self, result: object = None): @@ -142,35 +184,80 @@ async def end_dialog(self, result: object = None): :param result: (Optional) result to pass to the parent dialogs. :return: """ - await self.end_active_dialog(DialogReason.EndCalled) - - # Resume previous dialog - if self.active_dialog is not None: - # Look up dialog - dialog = await self.find_dialog(self.active_dialog.id) - if not dialog: - raise Exception( - "DialogContext.EndDialogAsync(): Can't resume previous dialog." - " A dialog with an id of '%s' wasn't found." % self.active_dialog.id - ) - - # Return result to previous dialog - return await dialog.resume_dialog(self, DialogReason.EndCalled, result) - - return DialogTurnResult(DialogTurnStatus.Complete, result) - - async def cancel_all_dialogs(self): + try: + await self.end_active_dialog(DialogReason.EndCalled) + + # Resume previous dialog + if self.active_dialog is not None: + # Look up dialog + dialog = await self.find_dialog(self.active_dialog.id) + if not dialog: + raise Exception( + "DialogContext.EndDialogAsync(): Can't resume previous dialog." + " A dialog with an id of '%s' wasn't found." + % self.active_dialog.id + ) + + # Return result to previous dialog + return await dialog.resume_dialog(self, DialogReason.EndCalled, result) + + return DialogTurnResult(DialogTurnStatus.Complete, result) + except Exception as err: + self.__set_exception_context_data(err) + raise + + async def cancel_all_dialogs( + self, + cancel_parents: bool = None, + event_name: str = None, + event_value: object = None, + ): """ Deletes any existing dialog stack thus cancelling all dialogs on the stack. - :param result: (Optional) result to pass to the parent dialogs. + :param cancel_parents: + :param event_name: + :param event_value: :return: """ - if self.stack: - while self.stack: - await self.end_active_dialog(DialogReason.CancelCalled) - return DialogTurnResult(DialogTurnStatus.Cancelled) - - return DialogTurnResult(DialogTurnStatus.Empty) + try: + event_name = event_name or DialogEvents.cancel_dialog + if self.stack or self.parent: + # Cancel all local and parent dialogs while checking for interception + notify = False + dialog_context = self + + while dialog_context: + if dialog_context.stack: + # Check to see if the dialog wants to handle the event + if notify: + event_handled = await dialog_context.emit_event( + event_name, + event_value, + bubble=False, + from_leaf=False, + ) + + if event_handled: + break + + # End the active dialog + await dialog_context.end_active_dialog( + DialogReason.CancelCalled + ) + else: + dialog_context = ( + dialog_context.parent if cancel_parents else None + ) + + notify = True + + return DialogTurnResult(DialogTurnStatus.Cancelled) + + # Stack was empty and no parent + return DialogTurnResult(DialogTurnStatus.Empty) + except Exception as err: + self.__set_exception_context_data(err) + raise async def find_dialog(self, dialog_id: str) -> Dialog: """ @@ -179,10 +266,27 @@ async def find_dialog(self, dialog_id: str) -> Dialog: :param dialog_id: ID of the dialog to search for. :return: """ - dialog = await self.dialogs.find(dialog_id) + try: + dialog = await self.dialogs.find(dialog_id) + + if dialog is None and self.parent is not None: + dialog = await self.parent.find_dialog(dialog_id) + return dialog + except Exception as err: + self.__set_exception_context_data(err) + raise + + def find_dialog_sync(self, dialog_id: str) -> Dialog: + """ + If the dialog cannot be found within the current `DialogSet`, the parent `DialogContext` + will be searched if there is one. + :param dialog_id: ID of the dialog to search for. + :return: + """ + dialog = self.dialogs.find_dialog(dialog_id) if dialog is None and self.parent is not None: - dialog = await self.parent.find_dialog(dialog_id) + dialog = self.parent.find_dialog_sync(dialog_id) return dialog async def replace_dialog( @@ -195,29 +299,37 @@ async def replace_dialog( :param options: (Optional) additional argument(s) to pass to the new dialog. :return: """ - # End the current dialog and giving the reason. - await self.end_active_dialog(DialogReason.ReplaceCalled) + try: + # End the current dialog and giving the reason. + await self.end_active_dialog(DialogReason.ReplaceCalled) - # Start replacement dialog - return await self.begin_dialog(dialog_id, options) + # Start replacement dialog + return await self.begin_dialog(dialog_id, options) + except Exception as err: + self.__set_exception_context_data(err) + raise async def reprompt_dialog(self): """ Calls reprompt on the currently active dialog, if there is one. Used with Prompts that have a reprompt behavior. :return: """ - # Check for a dialog on the stack - if self.active_dialog is not None: - # Look up dialog - dialog = await self.find_dialog(self.active_dialog.id) - if not dialog: - raise Exception( - "DialogSet.reprompt_dialog(): Can't find A dialog with an id of '%s'." - % self.active_dialog.id - ) - - # Ask dialog to re-prompt if supported - await dialog.reprompt_dialog(self.context, self.active_dialog) + try: + # Check for a dialog on the stack + if self.active_dialog is not None: + # Look up dialog + dialog = await self.find_dialog(self.active_dialog.id) + if not dialog: + raise Exception( + "DialogSet.reprompt_dialog(): Can't find A dialog with an id of '%s'." + % self.active_dialog.id + ) + + # Ask dialog to re-prompt if supported + await dialog.reprompt_dialog(self.context, self.active_dialog) + except Exception as err: + self.__set_exception_context_data(err) + raise async def end_active_dialog(self, reason: DialogReason): instance = self.active_dialog @@ -230,3 +342,75 @@ async def end_active_dialog(self, reason: DialogReason): # Pop dialog off stack self._stack.pop(0) + + async def emit_event( + self, + name: str, + value: object = None, + bubble: bool = True, + from_leaf: bool = False, + ) -> bool: + """ + Searches for a dialog with a given ID. + Emits a named event for the current dialog, or someone who started it, to handle. + :param name: Name of the event to raise. + :param value: Value to send along with the event. + :param bubble: Flag to control whether the event should be bubbled to its parent if not handled locally. + Defaults to a value of `True`. + :param from_leaf: Whether the event is emitted from a leaf node. + :param cancellationToken: The cancellation token. + :return: True if the event was handled. + """ + try: + # Initialize event + dialog_event = DialogEvent( + bubble=bubble, + name=name, + value=value, + ) + + dialog_context = self + + # Find starting dialog + if from_leaf: + while True: + child_dc = dialog_context.child + + if child_dc: + dialog_context = child_dc + else: + break + + # Dispatch to active dialog first + instance = dialog_context.active_dialog + + if instance: + dialog = await dialog_context.find_dialog(instance.id) + + if dialog: + return await dialog.on_dialog_event(dialog_context, dialog_event) + + return False + except Exception as err: + self.__set_exception_context_data(err) + raise + + def __set_exception_context_data(self, exception: Exception): + if not hasattr(exception, "data"): + exception.data = {} + + if not type(self).__name__ in exception.data: + stack = [] + current_dc = self + + while current_dc is not None: + stack = stack + [x.id for x in current_dc.stack] + current_dc = current_dc.parent + + exception.data[type(self).__name__] = { + "active_dialog": ( + None if self.active_dialog is None else self.active_dialog.id + ), + "parent": None if self.parent is None else self.parent.active_dialog.id, + "stack": self.stack, + } diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_event.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_event.py new file mode 100644 index 000000000..64753e824 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_event.py @@ -0,0 +1,9 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +class DialogEvent: + def __init__(self, bubble: bool = False, name: str = "", value: object = None): + self.bubble = bubble + self.name = name + self.value: object = value diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_events.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_events.py index 0c28a7e02..4de7ed990 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_events.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_events.py @@ -5,9 +5,10 @@ class DialogEvents(str, Enum): - begin_dialog = "beginDialog" reprompt_dialog = "repromptDialog" cancel_dialog = "cancelDialog" activity_received = "activityReceived" + version_changed = "versionChanged" error = "error" + custom = "custom" diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_extensions.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_extensions.py index 808bf2b1a..f9fb67c96 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_extensions.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_extensions.py @@ -1,89 +1,218 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from botframework.connector.auth import ( + ClaimsIdentity, + SkillValidation, + AuthenticationConstants, + GovernmentConstants, +) from botbuilder.core import BotAdapter, StatePropertyAccessor, TurnContext +from botbuilder.core.skills import SkillHandler, SkillConversationReference +import botbuilder.dialogs as dialogs # pylint: disable=unused-import +from botbuilder.dialogs.memory import DialogStateManager +from botbuilder.dialogs.dialog_context import DialogContext +from botbuilder.dialogs.dialog_turn_result import DialogTurnResult from botbuilder.dialogs import ( - Dialog, DialogEvents, DialogSet, DialogTurnStatus, ) -from botbuilder.schema import Activity, ActivityTypes -from botframework.connector.auth import ClaimsIdentity, SkillValidation +from botbuilder.schema import Activity, ActivityTypes, EndOfConversationCodes class DialogExtensions: @staticmethod async def run_dialog( - dialog: Dialog, turn_context: TurnContext, accessor: StatePropertyAccessor + dialog: "dialogs.Dialog", + turn_context: TurnContext, + accessor: StatePropertyAccessor, ): + """ + Creates a dialog stack and starts a dialog, pushing it onto the stack. + """ + dialog_set = DialogSet(accessor) dialog_set.add(dialog) - dialog_context = await dialog_set.create_context(turn_context) + dialog_context: DialogContext = await dialog_set.create_context(turn_context) - claims = turn_context.turn_state.get(BotAdapter.BOT_IDENTITY_KEY) - if isinstance(claims, ClaimsIdentity) and SkillValidation.is_skill_claim( - claims.claims - ): - # The bot is running as a skill. + await DialogExtensions._internal_run(turn_context, dialog.id, dialog_context) + + @staticmethod + async def _internal_run( + context: TurnContext, dialog_id: str, dialog_context: DialogContext + ) -> DialogTurnResult: + # map TurnState into root dialog context.services + for key, service in context.turn_state.items(): + dialog_context.services[key] = service + + # get the DialogStateManager configuration + dialog_state_manager = DialogStateManager(dialog_context) + await dialog_state_manager.load_all_scopes() + dialog_context.context.turn_state[dialog_state_manager.__class__.__name__] = ( + dialog_state_manager + ) + + # Loop as long as we are getting valid OnError handled we should continue executing the actions for the turn. + + # NOTE: We loop around this block because each pass through we either complete the turn and break out of the + # loop or we have had an exception AND there was an OnError action which captured the error. We need to + # continue the turn based on the actions the OnError handler introduced. + end_of_turn = False + while not end_of_turn: + try: + dialog_turn_result = await DialogExtensions.__inner_run( + context, dialog_id, dialog_context + ) + + # turn successfully completed, break the loop + end_of_turn = True + except Exception as err: + # fire error event, bubbling from the leaf. + handled = await dialog_context.emit_event( + DialogEvents.error, err, bubble=True, from_leaf=True + ) + + if not handled: + # error was NOT handled, throw the exception and end the turn. (This will trigger the + # Adapter.OnError handler and end the entire dialog stack) + raise + + # save all state scopes to their respective botState locations. + await dialog_state_manager.save_all_changes() + + # return the redundant result because the DialogManager contract expects it + return dialog_turn_result + + @staticmethod + async def __inner_run( + turn_context: TurnContext, dialog_id: str, dialog_context: DialogContext + ) -> DialogTurnResult: + # Handle EoC and Reprompt event from a parent bot (can be root bot to skill or skill to skill) + if DialogExtensions.__is_from_parent_to_skill(turn_context): + # Handle remote cancellation request from parent. + if turn_context.activity.type == ActivityTypes.end_of_conversation: + if not dialog_context.stack: + # No dialogs to cancel, just return. + return DialogTurnResult(DialogTurnStatus.Empty) + + # Send cancellation message to the dialog to ensure all the parents are canceled + # in the right order. + return await dialog_context.cancel_all_dialogs(True) + + # Handle a reprompt event sent from the parent. if ( - turn_context.activity.type == ActivityTypes.end_of_conversation - and dialog_context.stack - and DialogExtensions.__is_eoc_coming_from_parent(turn_context) + turn_context.activity.type == ActivityTypes.event + and turn_context.activity.name == DialogEvents.reprompt_dialog ): - remote_cancel_text = "Skill was canceled through an EndOfConversation activity from the parent." - await turn_context.send_trace_activity( - f"Extension {Dialog.__name__}.run_dialog", label=remote_cancel_text, + if not dialog_context.stack: + # No dialogs to reprompt, just return. + return DialogTurnResult(DialogTurnStatus.Empty) + + await dialog_context.reprompt_dialog() + return DialogTurnResult(DialogTurnStatus.Waiting) + + # Continue or start the dialog. + result = await dialog_context.continue_dialog() + if result.status == DialogTurnStatus.Empty: + result = await dialog_context.begin_dialog(dialog_id) + + await DialogExtensions._send_state_snapshot_trace(dialog_context) + + # Skills should send EoC when the dialog completes. + if ( + result.status == DialogTurnStatus.Complete + or result.status == DialogTurnStatus.Cancelled + ): + if DialogExtensions.__send_eoc_to_parent(turn_context): + activity = Activity( + type=ActivityTypes.end_of_conversation, + value=result.result, + locale=turn_context.activity.locale, + code=( + EndOfConversationCodes.completed_successfully + if result.status == DialogTurnStatus.Complete + else EndOfConversationCodes.user_cancelled + ), ) + await turn_context.send_activity(activity) + + return result + + @staticmethod + def __is_from_parent_to_skill(turn_context: TurnContext) -> bool: + if turn_context.turn_state.get(SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY): + return False - await dialog_context.cancel_all_dialogs() - else: - # Process a reprompt event sent from the parent. - if ( - turn_context.activity.type == ActivityTypes.event - and turn_context.activity.name == DialogEvents.reprompt_dialog - and dialog_context.stack - ): - await dialog_context.reprompt_dialog() - return - - # Run the Dialog with the new message Activity and capture the results - # so we can send end of conversation if needed. - result = await dialog_context.continue_dialog() - if result.status == DialogTurnStatus.Empty: - start_message_text = f"Starting {dialog.id}" - await turn_context.send_trace_activity( - f"Extension {Dialog.__name__}.run_dialog", - label=start_message_text, - ) - result = await dialog_context.begin_dialog(dialog.id) - - # Send end of conversation if it is completed or cancelled. - if ( - result.status == DialogTurnStatus.Complete - or result.status == DialogTurnStatus.Cancelled - ): - end_message_text = f"Dialog {dialog.id} has **completed**. Sending EndOfConversation." - await turn_context.send_trace_activity( - f"Extension {Dialog.__name__}.run_dialog", - label=end_message_text, - value=result.result, - ) - - activity = Activity( - type=ActivityTypes.end_of_conversation, value=result.result - ) - await turn_context.send_activity(activity) - - else: - # The bot is running as a standard bot. - results = await dialog_context.continue_dialog() - if results.status == DialogTurnStatus.Empty: - await dialog_context.begin_dialog(dialog.id) + claims_identity = turn_context.turn_state.get(BotAdapter.BOT_IDENTITY_KEY) + return isinstance( + claims_identity, ClaimsIdentity + ) and SkillValidation.is_skill_claim(claims_identity.claims) @staticmethod - def __is_eoc_coming_from_parent(turn_context: TurnContext) -> bool: - # To determine the direction we check callerId property which is set to the parent bot - # by the BotFrameworkHttpClient on outgoing requests. - return bool(turn_context.activity.caller_id) + async def _send_state_snapshot_trace(dialog_context: DialogContext): + """ + Helper to send a trace activity with a memory snapshot of the active dialog DC. + :param dialog_context: + :return: + """ + claims_identity = dialog_context.context.turn_state.get( + BotAdapter.BOT_IDENTITY_KEY, None + ) + trace_label = ( + "Skill State" + if isinstance(claims_identity, ClaimsIdentity) + and SkillValidation.is_skill_claim(claims_identity.claims) + else "Bot State" + ) + # send trace of memory + snapshot = DialogExtensions._get_active_dialog_context( + dialog_context + ).state.get_memory_snapshot() + trace_activity = Activity.create_trace_activity( + "BotState", + "https://www.botframework.com/schemas/botState", + snapshot, + trace_label, + ) + await dialog_context.context.send_activity(trace_activity) + + @staticmethod + def __send_eoc_to_parent(turn_context: TurnContext) -> bool: + claims_identity = turn_context.turn_state.get(BotAdapter.BOT_IDENTITY_KEY) + if isinstance( + claims_identity, ClaimsIdentity + ) and SkillValidation.is_skill_claim(claims_identity.claims): + # EoC Activities returned by skills are bounced back to the bot by SkillHandler. + # In those cases we will have a SkillConversationReference instance in state. + skill_conversation_reference: SkillConversationReference = ( + turn_context.turn_state.get( + SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY + ) + ) + if skill_conversation_reference: + # If the skillConversationReference.OAuthScope is for one of the supported channels, + # we are at the root and we should not send an EoC. + return ( + skill_conversation_reference.oauth_scope + != AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + and skill_conversation_reference.oauth_scope + != GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + ) + return True + + return False + + @staticmethod + def _get_active_dialog_context(dialog_context: DialogContext) -> DialogContext: + """ + Recursively walk up the DC stack to find the active DC. + :param dialog_context: + :return: + """ + child = dialog_context.child + if not child: + return dialog_context + + return DialogExtensions._get_active_dialog_context(child) diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_instance.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_instance.py index add9e2dc6..0d4e3400b 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_instance.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_instance.py @@ -9,7 +9,9 @@ class DialogInstance: Tracking information for a dialog on the stack. """ - def __init__(self): + def __init__( + self, id: str = None, state: Dict[str, object] = None + ): # pylint: disable=invalid-name """ Gets or sets the ID of the dialog and gets or sets the instance's persisted state. @@ -18,9 +20,9 @@ def __init__(self): :var self.state: The instance's persisted state. :vartype self.state: :class:`typing.Dict[str, object]` """ - self.id: str = None # pylint: disable=invalid-name + self.id = id # pylint: disable=invalid-name - self.state: Dict[str, object] = {} + self.state = state or {} def __str__(self): """ diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_manager.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_manager.py new file mode 100644 index 000000000..df7a5569e --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_manager.py @@ -0,0 +1,326 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from datetime import datetime, timedelta +from threading import Lock +from warnings import warn + +from botbuilder.core import ( + BotAdapter, + BotStateSet, + ConversationState, + UserState, + TurnContext, +) +from botbuilder.core.skills import SkillConversationReference, SkillHandler +from botbuilder.dialogs.memory import DialogStateManagerConfiguration +from botbuilder.schema import Activity, ActivityTypes, EndOfConversationCodes +from botframework.connector.auth import ( + AuthenticationConstants, + ClaimsIdentity, + GovernmentConstants, + SkillValidation, +) + +from .dialog import Dialog +from .dialog_context import DialogContext +from .dialog_events import DialogEvents +from .dialog_extensions import DialogExtensions +from .dialog_set import DialogSet +from .dialog_state import DialogState +from .dialog_manager_result import DialogManagerResult +from .dialog_turn_status import DialogTurnStatus +from .dialog_turn_result import DialogTurnResult + + +class DialogManager: + """ + Class which runs the dialog system. + """ + + def __init__(self, root_dialog: Dialog = None, dialog_state_property: str = None): + """ + Initializes a instance of the class. + :param root_dialog: Root dialog to use. + :param dialog_state_property: alternate name for the dialog_state property. (Default is "DialogState"). + """ + self.last_access = "_lastAccess" + self._root_dialog_id = "" + self._dialog_state_property = dialog_state_property or "DialogState" + self._lock = Lock() + + # Gets or sets root dialog to use to start conversation. + self.root_dialog = root_dialog + + # Gets or sets the ConversationState. + self.conversation_state: ConversationState = None + + # Gets or sets the UserState. + self.user_state: UserState = None + + # Gets InitialTurnState collection to copy into the TurnState on every turn. + self.initial_turn_state = {} + + # Gets or sets global dialogs that you want to have be callable. + self.dialogs = DialogSet() + + # Gets or sets the DialogStateManagerConfiguration. + self.state_configuration: DialogStateManagerConfiguration = None + + # Gets or sets (optional) number of milliseconds to expire the bot's state after. + self.expire_after: int = None + + async def on_turn(self, context: TurnContext) -> DialogManagerResult: + """ + Runs dialog system in the context of an ITurnContext. + :param context: turn context. + :return: + """ + # pylint: disable=too-many-statements + # Lazy initialize RootDialog so it can refer to assets like LG function templates + if not self._root_dialog_id: + with self._lock: + if not self._root_dialog_id: + self._root_dialog_id = self.root_dialog.id + # self.dialogs = self.root_dialog.telemetry_client + self.dialogs.add(self.root_dialog) + + bot_state_set = BotStateSet([]) + + # Preload TurnState with DM TurnState. + for key, val in self.initial_turn_state.items(): + context.turn_state[key] = val + + # register DialogManager with TurnState. + context.turn_state[DialogManager.__name__] = self + conversation_state_name = ConversationState.__name__ + if self.conversation_state is None: + if conversation_state_name not in context.turn_state: + raise Exception( + f"Unable to get an instance of {conversation_state_name} from turn_context." + ) + self.conversation_state: ConversationState = context.turn_state[ + conversation_state_name + ] + else: + context.turn_state[conversation_state_name] = self.conversation_state + + bot_state_set.add(self.conversation_state) + + user_state_name = UserState.__name__ + if self.user_state is None: + self.user_state = context.turn_state.get(user_state_name, None) + else: + context.turn_state[user_state_name] = self.user_state + + if self.user_state is not None: + self.user_state: UserState = self.user_state + bot_state_set.add(self.user_state) + + # create property accessors + # DateTime(last_access) + last_access_property = self.conversation_state.create_property(self.last_access) + last_access: datetime = await last_access_property.get(context, datetime.now) + + # Check for expired conversation + if self.expire_after is not None and ( + datetime.now() - last_access + ) >= timedelta(milliseconds=float(self.expire_after)): + # Clear conversation state + await self.conversation_state.clear_state(context) + + last_access = datetime.now() + await last_access_property.set(context, last_access) + + # get dialog stack + dialogs_property = self.conversation_state.create_property( + self._dialog_state_property + ) + dialog_state: DialogState = await dialogs_property.get(context, DialogState) + + # Create DialogContext + dialog_context = DialogContext(self.dialogs, context, dialog_state) + + # Call the common dialog "continue/begin" execution pattern shared with the classic RunAsync extension method + turn_result = ( + await DialogExtensions._internal_run( # pylint: disable=protected-access + context, self._root_dialog_id, dialog_context + ) + ) + + # save BotState changes + await bot_state_set.save_all_changes(dialog_context.context, False) + + return DialogManagerResult(turn_result=turn_result) + + @staticmethod + async def send_state_snapshot_trace( + dialog_context: DialogContext, + trace_label: str = None, # pylint: disable=unused-argument + ): + """ + Helper to send a trace activity with a memory snapshot of the active dialog DC. + :param dialog_context: + :param trace_label: + :return: + """ + warn( + "This method will be deprecated as no longer is necesary", + PendingDeprecationWarning, + ) + await DialogExtensions._send_state_snapshot_trace( # pylint: disable=protected-access + dialog_context + ) + + @staticmethod + def is_from_parent_to_skill(turn_context: TurnContext) -> bool: + if turn_context.turn_state.get( + SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY, None + ): + return False + + claims_identity: ClaimsIdentity = turn_context.turn_state.get( + BotAdapter.BOT_IDENTITY_KEY, None + ) + return isinstance( + claims_identity, ClaimsIdentity + ) and SkillValidation.is_skill_claim(claims_identity.claims) + + # Recursively walk up the DC stack to find the active DC. + @staticmethod + def get_active_dialog_context(dialog_context: DialogContext) -> DialogContext: + """ + Recursively walk up the DC stack to find the active DC. + :param dialog_context: + :return: + """ + warn( + "This method will be deprecated as no longer is necesary", + PendingDeprecationWarning, + ) + return DialogExtensions._get_active_dialog_context( # pylint: disable=protected-access + dialog_context + ) + + @staticmethod + def should_send_end_of_conversation_to_parent( + context: TurnContext, turn_result: DialogTurnResult + ) -> bool: + """ + Helper to determine if we should send an EndOfConversation to the parent or not. + :param context: + :param turn_result: + :return: + """ + if not ( + turn_result.status == DialogTurnStatus.Complete + or turn_result.status == DialogTurnStatus.Cancelled + ): + # The dialog is still going, don't return EoC. + return False + claims_identity: ClaimsIdentity = context.turn_state.get( + BotAdapter.BOT_IDENTITY_KEY, None + ) + if isinstance( + claims_identity, ClaimsIdentity + ) and SkillValidation.is_skill_claim(claims_identity.claims): + # EoC Activities returned by skills are bounced back to the bot by SkillHandler. + # In those cases we will have a SkillConversationReference instance in state. + skill_conversation_reference: SkillConversationReference = ( + context.turn_state.get(SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY) + ) + if skill_conversation_reference: + # If the skill_conversation_reference.OAuthScope is for one of the supported channels, we are at the + # root and we should not send an EoC. + return skill_conversation_reference.oauth_scope not in ( + AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + ) + + return True + + return False + + async def handle_skill_on_turn( + self, dialog_context: DialogContext + ) -> DialogTurnResult: + warn( + "This method will be deprecated as no longer is necesary", + PendingDeprecationWarning, + ) + # the bot is running as a skill. + turn_context = dialog_context.context + + # Process remote cancellation + if ( + turn_context.activity.type == ActivityTypes.end_of_conversation + and dialog_context.active_dialog is not None + and self.is_from_parent_to_skill(turn_context) + ): + # Handle remote cancellation request from parent. + active_dialog_context = self.get_active_dialog_context(dialog_context) + + # Send cancellation message to the top dialog in the stack to ensure all the parents are canceled in the + # right order. + return await active_dialog_context.cancel_all_dialogs(True) + + # Handle reprompt + # Process a reprompt event sent from the parent. + if ( + turn_context.activity.type == ActivityTypes.event + and turn_context.activity.name == DialogEvents.reprompt_dialog + ): + if not dialog_context.active_dialog: + return DialogTurnResult(DialogTurnStatus.Empty) + + await dialog_context.reprompt_dialog() + return DialogTurnResult(DialogTurnStatus.Waiting) + + # Continue execution + # - This will apply any queued up interruptions and execute the current/next step(s). + turn_result = await dialog_context.continue_dialog() + if turn_result.status == DialogTurnStatus.Empty: + # restart root dialog + turn_result = await dialog_context.begin_dialog(self._root_dialog_id) + + await DialogManager.send_state_snapshot_trace(dialog_context, "Skill State") + + if self.should_send_end_of_conversation_to_parent(turn_context, turn_result): + # Send End of conversation at the end. + activity = Activity( + type=ActivityTypes.end_of_conversation, + value=turn_result.result, + locale=turn_context.activity.locale, + code=( + EndOfConversationCodes.completed_successfully + if turn_result.status == DialogTurnStatus.Complete + else EndOfConversationCodes.user_cancelled + ), + ) + await turn_context.send_activity(activity) + + return turn_result + + async def handle_bot_on_turn( + self, dialog_context: DialogContext + ) -> DialogTurnResult: + warn( + "This method will be deprecated as no longer is necesary", + PendingDeprecationWarning, + ) + # the bot is running as a root bot. + if dialog_context.active_dialog is None: + # start root dialog + turn_result = await dialog_context.begin_dialog(self._root_dialog_id) + else: + # Continue execution + # - This will apply any queued up interruptions and execute the current/next step(s). + turn_result = await dialog_context.continue_dialog() + + if turn_result.status == DialogTurnStatus.Empty: + # restart root dialog + turn_result = await dialog_context.begin_dialog(self._root_dialog_id) + + await self.send_state_snapshot_trace(dialog_context, "Bot State") + + return turn_result diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_manager_result.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_manager_result.py new file mode 100644 index 000000000..c184f0df2 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_manager_result.py @@ -0,0 +1,21 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List + +from botbuilder.schema import Activity + +from .dialog_turn_result import DialogTurnResult +from .persisted_state import PersistedState + + +class DialogManagerResult: + def __init__( + self, + turn_result: DialogTurnResult = None, + activities: List[Activity] = None, + persisted_state: PersistedState = None, + ): + self.turn_result = turn_result + self.activities = activities + self.persisted_state = persisted_state diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_reason.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_reason.py index 471646e84..4383ab0d4 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_reason.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_reason.py @@ -13,8 +13,7 @@ class DialogReason(Enum): :vartype ContinueCalled: int :var EndCalled: A dialog ended normally through a call to :meth:`DialogContext.end_dialog() :vartype EndCalled: int - :var ReplaceCalled: A dialog is ending because it's being replaced through a call to - :meth:``DialogContext.replace_dialog()`. + :var ReplaceCalled: A dialog is ending and replaced through a call to :meth:``DialogContext.replace_dialog()`. :vartype ReplacedCalled: int :var CancelCalled: A dialog was cancelled as part of a call to :meth:`DialogContext.cancel_all_dialogs()`. :vartype CancelCalled: int diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_set.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_set.py index d6870128a..ce2070cae 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_set.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_set.py @@ -1,16 +1,23 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. import inspect +from hashlib import sha256 from typing import Dict -from botbuilder.core import TurnContext, BotAssert, StatePropertyAccessor +from botbuilder.core import ( + NullTelemetryClient, + BotTelemetryClient, + TurnContext, + BotAssert, + StatePropertyAccessor, +) from .dialog import Dialog from .dialog_state import DialogState -from .dialog_context import DialogContext class DialogSet: def __init__(self, dialog_state: StatePropertyAccessor = None): + # pylint: disable=import-outside-toplevel if dialog_state is None: frame = inspect.currentframe().f_back try: @@ -20,19 +27,59 @@ def __init__(self, dialog_state: StatePropertyAccessor = None): except KeyError: raise TypeError("DialogSet(): dialog_state cannot be None.") # Only ComponentDialog can initialize with None dialog_state - # pylint: disable=import-outside-toplevel from .component_dialog import ComponentDialog + from .dialog_manager import DialogManager + from .dialog_container import DialogContainer - if not isinstance(self_obj, ComponentDialog): + if not isinstance( + self_obj, (ComponentDialog, DialogContainer, DialogManager) + ): raise TypeError("DialogSet(): dialog_state cannot be None.") finally: # make sure to clean up the frame at the end to avoid ref cycles del frame self._dialog_state = dialog_state - # self.__telemetry_client = NullBotTelemetryClient.Instance; + self.__telemetry_client = NullTelemetryClient() - self._dialogs: Dict[str, object] = {} + self._dialogs: Dict[str, Dialog] = {} + self._version: str = None + + @property + def telemetry_client(self) -> BotTelemetryClient: + """ + Gets the telemetry client for logging events. + """ + return self.__telemetry_client + + @telemetry_client.setter + def telemetry_client(self, value: BotTelemetryClient) -> None: + """ + Sets the telemetry client for all dialogs in this set. + """ + if value is None: + self.__telemetry_client = NullTelemetryClient() + else: + self.__telemetry_client = value + + for dialog in self._dialogs.values(): + dialog.telemetry_client = self.__telemetry_client + + def get_version(self) -> str: + """ + Gets a unique string which represents the combined versions of all dialogs in this this dialogset. + Version will change when any of the child dialogs version changes. + """ + if not self._version: + version = "" + for _, dialog in self._dialogs.items(): + aux_version = dialog.get_version() + if aux_version: + version += aux_version + + self._version = sha256(version) + + return self._version def add(self, dialog: Dialog): """ @@ -55,7 +102,11 @@ def add(self, dialog: Dialog): return self - async def create_context(self, turn_context: TurnContext) -> DialogContext: + async def create_context(self, turn_context: TurnContext) -> "DialogContext": + # This import prevents circular dependency issues + # pylint: disable=import-outside-toplevel + from .dialog_context import DialogContext + # pylint: disable=unnecessary-lambda BotAssert.context_not_none(turn_context) @@ -64,7 +115,9 @@ async def create_context(self, turn_context: TurnContext) -> DialogContext: "DialogSet.CreateContextAsync(): DialogSet created with a null IStatePropertyAccessor." ) - state = await self._dialog_state.get(turn_context, lambda: DialogState()) + state: DialogState = await self._dialog_state.get( + turn_context, lambda: DialogState() + ) return DialogContext(self, turn_context, state) @@ -82,6 +135,20 @@ async def find(self, dialog_id: str) -> Dialog: return None + def find_dialog(self, dialog_id: str) -> Dialog: + """ + Finds a dialog that was previously added to the set using add(dialog) + :param dialog_id: ID of the dialog/prompt to look up. + :return: The dialog if found, otherwise null. + """ + if not dialog_id: + raise TypeError("DialogContext.find(): dialog_id cannot be None.") + + if dialog_id in self._dialogs: + return self._dialogs[dialog_id] + + return None + def __str__(self): if self._dialogs: return "dialog set empty!" diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_state.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_state.py index a00c78701..8201225e5 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_state.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_state.py @@ -15,7 +15,7 @@ def __init__(self, stack: List[DialogInstance] = None): Initializes a new instance of the :class:`DialogState` class. :param stack: The state information to initialize the stack with. - :type stack: :class:`typing.List[:class:`DialogInstance`]` + :type stack: :class:`typing.List` """ if stack is None: self._dialog_stack = [] @@ -28,7 +28,7 @@ def dialog_stack(self): Initializes a new instance of the :class:`DialogState` class. :return: The state information to initialize the stack with. - :rtype: :class:`typing.List[:class:`DialogInstance`]` + :rtype: :class:`typing.List` """ return self._dialog_stack diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_turn_result.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_turn_result.py index 466cfac0f..4ddf01ebf 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_turn_result.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_turn_result.py @@ -26,7 +26,7 @@ def status(self): :return self._status: The status of the stack. :rtype self._status: :class:`DialogTurnStatus` - """ + """ return self._status @property diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/dialogs_component_registration.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialogs_component_registration.py new file mode 100644 index 000000000..acbddd1e0 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/dialogs_component_registration.py @@ -0,0 +1,53 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +from typing import Iterable + +from botbuilder.core import ComponentRegistration + +from botbuilder.dialogs.memory import ( + ComponentMemoryScopesBase, + ComponentPathResolversBase, + PathResolverBase, +) +from botbuilder.dialogs.memory.scopes import ( + TurnMemoryScope, + SettingsMemoryScope, + DialogMemoryScope, + DialogContextMemoryScope, + DialogClassMemoryScope, + ClassMemoryScope, + MemoryScope, + ThisMemoryScope, + ConversationMemoryScope, + UserMemoryScope, +) + +from botbuilder.dialogs.memory.path_resolvers import ( + AtAtPathResolver, + AtPathResolver, + DollarPathResolver, + HashPathResolver, + PercentPathResolver, +) + + +class DialogsComponentRegistration( + ComponentRegistration, ComponentMemoryScopesBase, ComponentPathResolversBase +): + def get_memory_scopes(self) -> Iterable[MemoryScope]: + yield TurnMemoryScope() + yield SettingsMemoryScope() + yield DialogMemoryScope() + yield DialogContextMemoryScope() + yield DialogClassMemoryScope() + yield ClassMemoryScope() + yield ThisMemoryScope() + yield ConversationMemoryScope() + yield UserMemoryScope() + + def get_path_resolvers(self) -> Iterable[PathResolverBase]: + yield AtAtPathResolver() + yield AtPathResolver() + yield DollarPathResolver() + yield HashPathResolver() + yield PercentPathResolver() diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/__init__.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/__init__.py new file mode 100644 index 000000000..a43b4cfb8 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/__init__.py @@ -0,0 +1,24 @@ +# coding=utf-8 +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +from .dialog_path import DialogPath +from .dialog_state_manager import DialogStateManager +from .dialog_state_manager_configuration import DialogStateManagerConfiguration +from .component_memory_scopes_base import ComponentMemoryScopesBase +from .component_path_resolvers_base import ComponentPathResolversBase +from .path_resolver_base import PathResolverBase +from . import scope_path + +__all__ = [ + "DialogPath", + "DialogStateManager", + "DialogStateManagerConfiguration", + "ComponentMemoryScopesBase", + "ComponentPathResolversBase", + "PathResolverBase", + "scope_path", +] diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/component_memory_scopes_base.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/component_memory_scopes_base.py new file mode 100644 index 000000000..428e631ff --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/component_memory_scopes_base.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +from abc import ABC, abstractmethod +from typing import Iterable + +from botbuilder.dialogs.memory.scopes import MemoryScope + + +class ComponentMemoryScopesBase(ABC): + @abstractmethod + def get_memory_scopes(self) -> Iterable[MemoryScope]: + raise NotImplementedError() diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/component_path_resolvers_base.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/component_path_resolvers_base.py new file mode 100644 index 000000000..4c3c0ec73 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/component_path_resolvers_base.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +from abc import ABC, abstractmethod +from typing import Iterable + +from .path_resolver_base import PathResolverBase + + +class ComponentPathResolversBase(ABC): + @abstractmethod + def get_path_resolvers(self) -> Iterable[PathResolverBase]: + raise NotImplementedError() diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/dialog_path.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/dialog_path.py new file mode 100644 index 000000000..be11cb2fb --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/dialog_path.py @@ -0,0 +1,32 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +class DialogPath: + # Counter of emitted events. + EVENT_COUNTER = "dialog.eventCounter" + + # Currently expected properties. + EXPECTED_PROPERTIES = "dialog.expectedProperties" + + # Default operation to use for entities where there is no identified operation entity. + DEFAULT_OPERATION = "dialog.defaultOperation" + + # Last surfaced entity ambiguity event. + LAST_EVENT = "dialog.lastEvent" + + # Currently required properties. + REQUIRED_PROPERTIES = "dialog.requiredProperties" + + # Number of retries for the current Ask. + RETRIES = "dialog.retries" + + # Last intent. + LAST_INTENT = "dialog.lastIntent" + + # Last trigger event: defined in FormEvent, ask, clarifyEntity etc.. + LAST_TRIGGER_EVENT = "dialog.lastTriggerEvent" + + @staticmethod + def get_property_name(prop: str) -> str: + return prop.replace("dialog.", "") diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/dialog_state_manager.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/dialog_state_manager.py new file mode 100644 index 000000000..a11ab9c3a --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/dialog_state_manager.py @@ -0,0 +1,662 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +import builtins + +from inspect import isawaitable +from traceback import print_tb +from typing import ( + Callable, + Dict, + Iterable, + Iterator, + List, + Tuple, + Type, + TypeVar, +) + +from botbuilder.core import ComponentRegistration + +from botbuilder.dialogs.memory.scopes import MemoryScope + +from .component_memory_scopes_base import ComponentMemoryScopesBase +from .component_path_resolvers_base import ComponentPathResolversBase +from .dialog_path import DialogPath +from .dialog_state_manager_configuration import DialogStateManagerConfiguration + +# Declare type variable +T = TypeVar("T") # pylint: disable=invalid-name + +BUILTIN_TYPES = list(filter(lambda x: not x.startswith("_"), dir(builtins))) + + +# +# The DialogStateManager manages memory scopes and pathresolvers +# MemoryScopes are named root level objects, which can exist either in the dialogcontext or off of turn state +# PathResolvers allow for shortcut behavior for mapping things like $foo -> dialog.foo. +# +class DialogStateManager: + SEPARATORS = [",", "["] + + def __init__( + self, + dialog_context: "DialogContext", + configuration: DialogStateManagerConfiguration = None, + ): + """ + Initializes a new instance of the DialogStateManager class. + :param dialog_context: The dialog context for the current turn of the conversation. + :param configuration: Configuration for the dialog state manager. Default is None. + """ + # pylint: disable=import-outside-toplevel + # These modules are imported at static level to avoid circular dependency problems + from botbuilder.dialogs import ( + DialogsComponentRegistration, + ObjectPath, + ) + + self._object_path_cls = ObjectPath + self._dialog_component_registration_cls = DialogsComponentRegistration + + # Information for tracking when path was last modified. + self.path_tracker = "dialog._tracker.paths" + + self._dialog_context = dialog_context + self._version: int = 0 + + ComponentRegistration.add(self._dialog_component_registration_cls()) + + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + self._configuration = configuration or dialog_context.context.turn_state.get( + DialogStateManagerConfiguration.__name__, None + ) + if not self._configuration: + self._configuration = DialogStateManagerConfiguration() + + # get all of the component memory scopes + memory_component: ComponentMemoryScopesBase + for memory_component in filter( + lambda comp: isinstance(comp, ComponentMemoryScopesBase), + ComponentRegistration.get_components(), + ): + for memory_scope in memory_component.get_memory_scopes(): + self._configuration.memory_scopes.append(memory_scope) + + # get all of the component path resolvers + path_component: ComponentPathResolversBase + for path_component in filter( + lambda comp: isinstance(comp, ComponentPathResolversBase), + ComponentRegistration.get_components(), + ): + for path_resolver in path_component.get_path_resolvers(): + self._configuration.path_resolvers.append(path_resolver) + + # cache for any other new dialog_state_manager instances in this turn. + dialog_context.context.turn_state[self._configuration.__class__.__name__] = ( + self._configuration + ) + + def __len__(self) -> int: + """ + Gets the number of memory scopes in the dialog state manager. + :return: Number of memory scopes in the configuration. + """ + return len(self._configuration.memory_scopes) + + @property + def configuration(self) -> DialogStateManagerConfiguration: + """ + Gets or sets the configured path resolvers and memory scopes for the dialog state manager. + :return: The configuration object. + """ + return self._configuration + + @property + def keys(self) -> Iterable[str]: + """ + Gets a Iterable containing the keys of the memory scopes + :return: Keys of the memory scopes. + """ + return [memory_scope.name for memory_scope in self.configuration.memory_scopes] + + @property + def values(self) -> Iterable[object]: + """ + Gets a Iterable containing the values of the memory scopes. + :return: Values of the memory scopes. + """ + return [ + memory_scope.get_memory(self._dialog_context) + for memory_scope in self.configuration.memory_scopes + ] + + # + # Gets a value indicating whether the dialog state manager is read-only. + # + # true. + @property + def is_read_only(self) -> bool: + """ + Gets a value indicating whether the dialog state manager is read-only. + :return: True. + """ + return True + + # + # Gets or sets the elements with the specified key. + # + # Key to get or set the element. + # The element with the specified key. + def __getitem__(self, key): + """ + :param key: + :return The value stored at key's position: + """ + return self.get_value(object, key, default_value=lambda: None) + + def __setitem__(self, key, value): + if self._index_of_any(key, self.SEPARATORS) == -1: + # Root is handled by SetMemory rather than SetValue + scope = self.get_memory_scope(key) + if not scope: + raise IndexError(self._get_bad_scope_message(key)) + # TODO: C# transforms value to JToken + scope.set_memory(self._dialog_context, value) + else: + self.set_value(key, value) + + def _get_bad_scope_message(self, path: str) -> str: + return ( + f"'{path}' does not match memory scopes:[" + f"{', '.join((memory_scope.name for memory_scope in self.configuration.memory_scopes))}]" + ) + + @staticmethod + def _index_of_any(string: str, elements_to_search_for) -> int: + for element in elements_to_search_for: + index = string.find(element) + if index != -1: + return index + + return -1 + + def get_memory_scope(self, name: str) -> MemoryScope: + """ + Get MemoryScope by name. + :param name: + :return: A memory scope. + """ + if not name: + raise TypeError(f"Expecting: {str.__name__}, but received None") + + return next( + ( + memory_scope + for memory_scope in self.configuration.memory_scopes + if memory_scope.name.lower() == name.lower() + ), + None, + ) + + def version(self) -> str: + """ + Version help caller to identify the updates and decide cache or not. + :return: Current version. + """ + return str(self._version) + + def resolve_memory_scope(self, path: str) -> Tuple[MemoryScope, str]: + """ + Will find the MemoryScope for and return the remaining path. + :param path: + :return: The memory scope and remaining subpath in scope. + """ + scope = path + sep_index = -1 + dot = path.find(".") + open_square_bracket = path.find("[") + + if dot > 0 and open_square_bracket > 0: + sep_index = min(dot, open_square_bracket) + + elif dot > 0: + sep_index = dot + + elif open_square_bracket > 0: + sep_index = open_square_bracket + + if sep_index > 0: + scope = path[0:sep_index] + memory_scope = self.get_memory_scope(scope) + if memory_scope: + remaining_path = path[sep_index + 1 :] + return memory_scope, remaining_path + + memory_scope = self.get_memory_scope(scope) + if not scope: + raise IndexError(self._get_bad_scope_message(scope)) + return memory_scope, "" + + def transform_path(self, path: str) -> str: + """ + Transform the path using the registered PathTransformers. + :param path: Path to transform. + :return: The transformed path. + """ + for path_resolver in self.configuration.path_resolvers: + path = path_resolver.transform_path(path) + + return path + + @staticmethod + def _is_primitive(type_to_check: Type) -> bool: + return type_to_check.__name__ in BUILTIN_TYPES + + def try_get_value( + self, path: str, class_type: Type = object + ) -> Tuple[bool, object]: + """ + Get the value from memory using path expression (NOTE: This always returns clone of value). + :param class_type: The value type to return. + :param path: Path expression to use. + :return: True if found, false if not and the value. + """ + if not path: + raise TypeError(f"Expecting: {str.__name__}, but received None") + return_value = ( + class_type() if DialogStateManager._is_primitive(class_type) else None + ) + path = self.transform_path(path) + + try: + memory_scope, remaining_path = self.resolve_memory_scope(path) + except Exception as error: + print_tb(error.__traceback__) + return False, return_value + + if not memory_scope: + return False, return_value + + if not remaining_path: + memory = memory_scope.get_memory(self._dialog_context) + if not memory: + return False, return_value + + return True, memory + + # TODO: HACK to support .First() retrieval on turn.recognized.entities.foo, replace with Expressions once + # expressions ship + first = ".FIRST()" + try: + i_first = path.upper().rindex(first) + except ValueError: + i_first = -1 + if i_first >= 0: + remaining_path = path[i_first + len(first) :] + path = path[0:i_first] + success, first_value = self._try_get_first_nested_value(path, self) + if success: + if not remaining_path: + return True, first_value + + path_value = self._object_path_cls.try_get_path_value( + first_value, remaining_path + ) + return bool(path_value), path_value + + return False, return_value + + path_value = self._object_path_cls.try_get_path_value(self, path) + return bool(path_value), path_value + + def get_value( + self, + class_type: Type, + path_expression: str, + default_value: Callable[[], T] = None, + ) -> T: + """ + Get the value from memory using path expression (NOTE: This always returns clone of value). + :param class_type: The value type to return. + :param path_expression: Path expression to use. + :param default_value: Function to give default value if there is none (OPTIONAL). + :return: Result or null if the path is not valid. + """ + if not path_expression: + raise TypeError(f"Expecting: {str.__name__}, but received None") + + success, value = self.try_get_value(path_expression, class_type) + if success: + return value + + return default_value() if default_value else None + + def get_int_value(self, path_expression: str, default_value: int = 0) -> int: + """ + Get an int value from memory using a path expression. + :param path_expression: Path expression to use. + :param default_value: Default value if there is none (OPTIONAL). + :return: + """ + if not path_expression: + raise TypeError(f"Expecting: {str.__name__}, but received None") + success, value = self.try_get_value(path_expression, int) + if success: + return value + + return default_value + + def get_bool_value(self, path_expression: str, default_value: bool = False) -> bool: + """ + Get a bool value from memory using a path expression. + :param path_expression: Path expression to use. + :param default_value: Default value if there is none (OPTIONAL). + :return: + """ + if not path_expression: + raise TypeError(f"Expecting: {str.__name__}, but received None") + success, value = self.try_get_value(path_expression, bool) + if success: + return value + + return default_value + + def get_string_value(self, path_expression: str, default_value: str = "") -> str: + """ + Get a string value from memory using a path expression. + :param path_expression: Path expression to use. + :param default_value: Default value if there is none (OPTIONAL). + :return: + """ + if not path_expression: + raise TypeError(f"Expecting: {str.__name__}, but received None") + success, value = self.try_get_value(path_expression, str) + if success: + return value + + return default_value + + def set_value(self, path: str, value: object): + """ + Set memory to value. + :param path: Path to memory. + :param value: Object to set. + :return: + """ + if isawaitable(value): + raise Exception(f"{path} = You can't pass an awaitable to set_value") + + if not path: + raise TypeError(f"Expecting: {str.__name__}, but received None") + + path = self.transform_path(path) + if self._track_change(path, value): + self._object_path_cls.set_path_value(self, path, value) + + # Every set will increase version + self._version += 1 + + def remove_value(self, path: str): + """ + Set memory to value. + :param path: Path to memory. + :param value: Object to set. + :return: + """ + if not path: + raise TypeError(f"Expecting: {str.__name__}, but received None") + + path = self.transform_path(path) + if self._track_change(path, None): + self._object_path_cls.remove_path_value(self, path) + + def get_memory_snapshot(self) -> Dict[str, object]: + """ + Gets all memoryscopes suitable for logging. + :return: object which represents all memory scopes. + """ + result = {} + + for scope in [ + ms for ms in self.configuration.memory_scopes if ms.include_in_snapshot + ]: + memory = scope.get_memory(self._dialog_context) + if memory: + result[scope.name] = memory + + return result + + async def load_all_scopes(self): + """ + Load all of the scopes. + :return: + """ + for scope in self.configuration.memory_scopes: + await scope.load(self._dialog_context) + + async def save_all_changes(self): + """ + Save all changes for all scopes. + :return: + """ + for scope in self.configuration.memory_scopes: + await scope.save_changes(self._dialog_context) + + async def delete_scopes_memory_async(self, name: str): + """ + Delete the memory for a scope. + :param name: name of the scope. + :return: + """ + name = name.upper() + scope_list = [ + ms for ms in self.configuration.memory_scopes if ms.name.upper == name + ] + if len(scope_list) > 1: + raise RuntimeError(f"More than 1 scopes found with the name '{name}'") + scope = scope_list[0] if scope_list else None + if scope: + await scope.delete(self._dialog_context) + + def add(self, key: str, value: object): + """ + Adds an element to the dialog state manager. + :param key: Key of the element to add. + :param value: Value of the element to add. + :return: + """ + raise RuntimeError("Not supported") + + def contains_key(self, key: str) -> bool: + """ + Determines whether the dialog state manager contains an element with the specified key. + :param key: The key to locate in the dialog state manager. + :return: True if the dialog state manager contains an element with the key otherwise, False. + """ + scopes_with_key = [ + ms + for ms in self.configuration.memory_scopes + if ms.name.upper == key.upper() + ] + return bool(scopes_with_key) + + def remove(self, key: str): + """ + Removes the element with the specified key from the dialog state manager. + :param key: Key of the element to remove. + :return: + """ + raise RuntimeError("Not supported") + + # + # Removes all items from the dialog state manager. + # + # This method is not supported. + def clear(self, key: str): + """ + Removes all items from the dialog state manager. + :param key: Key of the element to remove. + :return: + """ + raise RuntimeError("Not supported") + + def contains(self, item: Tuple[str, object]) -> bool: + """ + Determines whether the dialog state manager contains a specific value (should use __contains__). + :param item: The tuple of the item to locate. + :return bool: True if item is found in the dialog state manager otherwise, False + """ + raise RuntimeError("Not supported") + + def __contains__(self, item: Tuple[str, object]) -> bool: + """ + Determines whether the dialog state manager contains a specific value. + :param item: The tuple of the item to locate. + :return bool: True if item is found in the dialog state manager otherwise, False + """ + raise RuntimeError("Not supported") + + def copy_to(self, array: List[Tuple[str, object]], array_index: int): + """ + Copies the elements of the dialog state manager to an array starting at a particular index. + :param array: The one-dimensional array that is the destination of the elements copied + from the dialog state manager. The array must have zero-based indexing. + :param array_index: + :return: + """ + for memory_scope in self.configuration.memory_scopes: + array[array_index] = ( + memory_scope.name, + memory_scope.get_memory(self._dialog_context), + ) + array_index += 1 + + def remove_item(self, item: Tuple[str, object]) -> bool: + """ + Determines whether the dialog state manager contains a specific value (should use __contains__). + :param item: The tuple of the item to locate. + :return bool: True if item is found in the dialog state manager otherwise, False + """ + raise RuntimeError("Not supported") + + # + # Returns an enumerator that iterates through the collection. + # + # An enumerator that can be used to iterate through the collection. + def get_enumerator(self) -> Iterator[Tuple[str, object]]: + """ + Returns an enumerator that iterates through the collection. + :return: An enumerator that can be used to iterate through the collection. + """ + for memory_scope in self.configuration.memory_scopes: + yield (memory_scope.name, memory_scope.get_memory(self._dialog_context)) + + def track_paths(self, paths: Iterable[str]) -> List[str]: + """ + Track when specific paths are changed. + :param paths: Paths to track. + :return: Normalized paths to pass to any_path_changed. + """ + all_paths = [] + for path in paths: + t_path = self.transform_path(path) + + # Track any path that resolves to a constant path + segments = self._object_path_cls.try_resolve_path(self, t_path) + if segments: + n_path = "_".join(segments) + self.set_value(self.path_tracker + "." + n_path, 0) + all_paths.append(n_path) + + return all_paths + + def any_path_changed(self, counter: int, paths: Iterable[str]) -> bool: + """ + Check to see if any path has changed since watermark. + :param counter: Time counter to compare to. + :param paths: Paths from track_paths to check. + :return: True if any path has changed since counter. + """ + found = False + if paths: + for path in paths: + if self.get_value(int, self.path_tracker + "." + path) > counter: + found = True + break + + return found + + def __iter__(self): + for memory_scope in self.configuration.memory_scopes: + yield (memory_scope.name, memory_scope.get_memory(self._dialog_context)) + + @staticmethod + def _try_get_first_nested_value( + remaining_path: str, memory: object + ) -> Tuple[bool, object]: + # These modules are imported at static level to avoid circular dependency problems + # pylint: disable=import-outside-toplevel + + from botbuilder.dialogs import ObjectPath + + array = ObjectPath.try_get_path_value(memory, remaining_path) + if array: + if isinstance(array[0], list): + first = array[0] + if first: + second = first[0] + return True, second + + return False, None + + return True, array[0] + + return False, None + + def _track_change(self, path: str, value: object) -> bool: + has_path = False + segments = self._object_path_cls.try_resolve_path(self, path) + if segments: + root = segments[1] if len(segments) > 1 else "" + + # Skip _* as first scope, i.e. _adaptive, _tracker, ... + if not root.startswith("_"): + # Convert to a simple path with _ between segments + path_name = "_".join(segments) + tracked_path = f"{self.path_tracker}.{path_name}" + counter = None + + def update(): + nonlocal counter + last_changed = self.try_get_value(tracked_path, int) + if last_changed: + if counter is not None: + counter = self.get_value(int, DialogPath.EVENT_COUNTER) + + self.set_value(tracked_path, counter) + + update() + if not self._is_primitive(type(value)): + # For an object we need to see if any children path are being tracked + def check_children(property: str, instance: object): + nonlocal tracked_path + # Add new child segment + tracked_path += "_" + property.lower() + update() + if not self._is_primitive(type(instance)): + self._object_path_cls.for_each_property( + property, check_children + ) + + # Remove added child segment + tracked_path = tracked_path.Substring( + 0, tracked_path.LastIndexOf("_") + ) + + self._object_path_cls.for_each_property(value, check_children) + + has_path = True + + return has_path diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/dialog_state_manager_configuration.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/dialog_state_manager_configuration.py new file mode 100644 index 000000000..b1565a53d --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/dialog_state_manager_configuration.py @@ -0,0 +1,10 @@ +from typing import List + +from botbuilder.dialogs.memory.scopes import MemoryScope +from .path_resolver_base import PathResolverBase + + +class DialogStateManagerConfiguration: + def __init__(self): + self.path_resolvers: List[PathResolverBase] = list() + self.memory_scopes: List[MemoryScope] = list() diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolver_base.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolver_base.py new file mode 100644 index 000000000..42b80c93f --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolver_base.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class PathResolverBase(ABC): + @abstractmethod + def transform_path(self, path: str): + raise NotImplementedError() diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/__init__.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/__init__.py new file mode 100644 index 000000000..b22ac063a --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/__init__.py @@ -0,0 +1,19 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +from .alias_path_resolver import AliasPathResolver +from .at_at_path_resolver import AtAtPathResolver +from .at_path_resolver import AtPathResolver +from .dollar_path_resolver import DollarPathResolver +from .hash_path_resolver import HashPathResolver +from .percent_path_resolver import PercentPathResolver + +__all__ = [ + "AliasPathResolver", + "AtAtPathResolver", + "AtPathResolver", + "DollarPathResolver", + "HashPathResolver", + "PercentPathResolver", +] diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/alias_path_resolver.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/alias_path_resolver.py new file mode 100644 index 000000000..b16930284 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/alias_path_resolver.py @@ -0,0 +1,53 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.dialogs.memory import PathResolverBase + + +class AliasPathResolver(PathResolverBase): + def __init__(self, alias: str, prefix: str, postfix: str = None): + """ + Initializes a new instance of the class. + Alias name. + Prefix name. + Postfix name. + """ + if alias is None: + raise TypeError(f"Expecting: alias, but received None") + if prefix is None: + raise TypeError(f"Expecting: prefix, but received None") + + # Gets the alias name. + self.alias = alias.strip() + self._prefix = prefix.strip() + self._postfix = postfix.strip() if postfix else "" + + def transform_path(self, path: str): + """ + Transforms the path. + Path to inspect. + Transformed path. + """ + if not path: + raise TypeError(f"Expecting: path, but received None") + + path = path.strip() + if ( + path.startswith(self.alias) + and len(path) > len(self.alias) + and AliasPathResolver._is_path_char(path[len(self.alias)]) + ): + # here we only deals with trailing alias, alias in middle be handled in further breakdown + # $xxx -> path.xxx + return f"{self._prefix}{path[len(self.alias):]}{self._postfix}".rstrip(".") + + return path + + @staticmethod + def _is_path_char(char: str) -> bool: + """ + Verifies if a character is valid for a path. + Character to verify. + true if the character is valid for a path otherwise, false. + """ + return len(char) == 1 and (char.isalpha() or char == "_") diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/at_at_path_resolver.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/at_at_path_resolver.py new file mode 100644 index 000000000..d440c040a --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/at_at_path_resolver.py @@ -0,0 +1,9 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .alias_path_resolver import AliasPathResolver + + +class AtAtPathResolver(AliasPathResolver): + def __init__(self): + super().__init__(alias="@@", prefix="turn.recognized.entities.") diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/at_path_resolver.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/at_path_resolver.py new file mode 100644 index 000000000..0cc1ccc73 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/at_path_resolver.py @@ -0,0 +1,42 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .alias_path_resolver import AliasPathResolver + + +class AtPathResolver(AliasPathResolver): + _DELIMITERS = [".", "["] + + def __init__(self): + super().__init__(alias="@", prefix="") + + self._PREFIX = "turn.recognized.entities." # pylint: disable=invalid-name + + def transform_path(self, path: str): + if not path: + raise TypeError(f"Expecting: path, but received None") + + path = path.strip() + if ( + path.startswith("@") + and len(path) > 1 + and AtPathResolver._is_path_char(path[1]) + ): + end = any(delimiter in path for delimiter in AtPathResolver._DELIMITERS) + if end == -1: + end = len(path) + + prop = path[1:end] + suffix = path[end:] + path = f"{self._PREFIX}{prop}.first(){suffix}" + + return path + + @staticmethod + def _index_of_any(string: str, elements_to_search_for) -> int: + for element in elements_to_search_for: + index = string.find(element) + if index != -1: + return index + + return -1 diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/dollar_path_resolver.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/dollar_path_resolver.py new file mode 100644 index 000000000..8152d23c5 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/dollar_path_resolver.py @@ -0,0 +1,9 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .alias_path_resolver import AliasPathResolver + + +class DollarPathResolver(AliasPathResolver): + def __init__(self): + super().__init__(alias="$", prefix="dialog.") diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/hash_path_resolver.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/hash_path_resolver.py new file mode 100644 index 000000000..b00376e59 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/hash_path_resolver.py @@ -0,0 +1,9 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .alias_path_resolver import AliasPathResolver + + +class HashPathResolver(AliasPathResolver): + def __init__(self): + super().__init__(alias="#", prefix="turn.recognized.intents.") diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/percent_path_resolver.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/percent_path_resolver.py new file mode 100644 index 000000000..dd0fa2e17 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/path_resolvers/percent_path_resolver.py @@ -0,0 +1,9 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .alias_path_resolver import AliasPathResolver + + +class PercentPathResolver(AliasPathResolver): + def __init__(self): + super().__init__(alias="%", prefix="class.") diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scope_path.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scope_path.py new file mode 100644 index 000000000..faf906699 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scope_path.py @@ -0,0 +1,35 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +# User memory scope root path. +# This property is deprecated, use ScopePath.User instead. +USER = "user" + +# Conversation memory scope root path. +# This property is deprecated, use ScopePath.Conversation instead.This property is deprecated, use ScopePath.Dialog instead.This property is deprecated, use ScopePath.DialogClass instead.This property is deprecated, use ScopePath.This instead.This property is deprecated, use ScopePath.Class instead. +CLASS = "class" + +# Settings memory scope root path. +# This property is deprecated, use ScopePath.Settings instead. + +SETTINGS = "settings" + +# Turn memory scope root path. +# This property is deprecated, use ScopePath.Turn instead. +TURN = "turn" diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/__init__.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/__init__.py new file mode 100644 index 000000000..ec2e2b61c --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/__init__.py @@ -0,0 +1,32 @@ +# coding=utf-8 +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +from .bot_state_memory_scope import BotStateMemoryScope +from .class_memory_scope import ClassMemoryScope +from .conversation_memory_scope import ConversationMemoryScope +from .dialog_class_memory_scope import DialogClassMemoryScope +from .dialog_context_memory_scope import DialogContextMemoryScope +from .dialog_memory_scope import DialogMemoryScope +from .memory_scope import MemoryScope +from .settings_memory_scope import SettingsMemoryScope +from .this_memory_scope import ThisMemoryScope +from .turn_memory_scope import TurnMemoryScope +from .user_memory_scope import UserMemoryScope + + +__all__ = [ + "BotStateMemoryScope", + "ClassMemoryScope", + "ConversationMemoryScope", + "DialogClassMemoryScope", + "DialogContextMemoryScope", + "DialogMemoryScope", + "MemoryScope", + "SettingsMemoryScope", + "ThisMemoryScope", + "TurnMemoryScope", + "UserMemoryScope", +] diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/bot_state_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/bot_state_memory_scope.py new file mode 100644 index 000000000..088c7a0fb --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/bot_state_memory_scope.py @@ -0,0 +1,43 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Type + +from botbuilder.core import BotState + +from .memory_scope import MemoryScope + + +class BotStateMemoryScope(MemoryScope): + def __init__(self, bot_state_type: Type[BotState], name: str): + super().__init__(name, include_in_snapshot=True) + self.bot_state_type = bot_state_type + + def get_memory(self, dialog_context: "DialogContext") -> object: + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + bot_state: BotState = self._get_bot_state(dialog_context) + cached_state = ( + bot_state.get_cached_state(dialog_context.context) if bot_state else None + ) + + return cached_state.state if cached_state else None + + def set_memory(self, dialog_context: "DialogContext", memory: object): + raise RuntimeError("You cannot replace the root BotState object") + + async def load(self, dialog_context: "DialogContext", force: bool = False): + bot_state: BotState = self._get_bot_state(dialog_context) + + if bot_state: + await bot_state.load(dialog_context.context, force) + + async def save_changes(self, dialog_context: "DialogContext", force: bool = False): + bot_state: BotState = self._get_bot_state(dialog_context) + + if bot_state: + await bot_state.save_changes(dialog_context.context, force) + + def _get_bot_state(self, dialog_context: "DialogContext") -> BotState: + return dialog_context.context.turn_state.get(self.bot_state_type.__name__, None) diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/class_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/class_memory_scope.py new file mode 100644 index 000000000..d5592e238 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/class_memory_scope.py @@ -0,0 +1,57 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from collections import namedtuple + +from botbuilder.dialogs.memory import scope_path + +from .memory_scope import MemoryScope + + +class ClassMemoryScope(MemoryScope): + def __init__(self): + super().__init__(scope_path.SETTINGS, include_in_snapshot=False) + + def get_memory(self, dialog_context: "DialogContext") -> object: + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + # if active dialog is a container dialog then "dialogclass" binds to it. + if dialog_context.active_dialog: + dialog = dialog_context.find_dialog_sync(dialog_context.active_dialog.id) + if dialog: + return ClassMemoryScope._bind_to_dialog_context(dialog, dialog_context) + + return None + + def set_memory(self, dialog_context: "DialogContext", memory: object): + raise Exception( + f"{self.__class__.__name__}.set_memory not supported (read only)" + ) + + @staticmethod + def _bind_to_dialog_context(obj, dialog_context: "DialogContext") -> object: + clone = {} + for prop in dir(obj): + # don't process double underscore attributes + if prop[:1] != "_": + prop_value = getattr(obj, prop) + if not callable(prop_value): + # the only objects + if hasattr(prop_value, "try_get_value"): + clone[prop] = prop_value.try_get_value(dialog_context.state) + elif hasattr(prop_value, "__dict__") and not isinstance( + prop_value, type(prop_value) + ): + clone[prop] = ClassMemoryScope._bind_to_dialog_context( + prop_value, dialog_context + ) + else: + clone[prop] = prop_value + if clone: + ReadOnlyObject = namedtuple( # pylint: disable=invalid-name + "ReadOnlyObject", clone + ) + return ReadOnlyObject(**clone) + + return None diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/conversation_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/conversation_memory_scope.py new file mode 100644 index 000000000..2f88dd57a --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/conversation_memory_scope.py @@ -0,0 +1,12 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import ConversationState +from botbuilder.dialogs.memory import scope_path + +from .bot_state_memory_scope import BotStateMemoryScope + + +class ConversationMemoryScope(BotStateMemoryScope): + def __init__(self): + super().__init__(ConversationState, scope_path.CONVERSATION) diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/dialog_class_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/dialog_class_memory_scope.py new file mode 100644 index 000000000..b363d1065 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/dialog_class_memory_scope.py @@ -0,0 +1,45 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from copy import deepcopy + +from botbuilder.dialogs.memory import scope_path + +from .memory_scope import MemoryScope + + +class DialogClassMemoryScope(MemoryScope): + def __init__(self): + # pylint: disable=import-outside-toplevel + super().__init__(scope_path.DIALOG_CLASS, include_in_snapshot=False) + + # This import is to avoid circular dependency issues + from botbuilder.dialogs import DialogContainer + + self._dialog_container_cls = DialogContainer + + def get_memory(self, dialog_context: "DialogContext") -> object: + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + # if active dialog is a container dialog then "dialogclass" binds to it. + if dialog_context.active_dialog: + dialog = dialog_context.find_dialog_sync(dialog_context.active_dialog.id) + if isinstance(dialog, self._dialog_container_cls): + return deepcopy(dialog) + + # Otherwise we always bind to parent, or if there is no parent the active dialog + parent_id = ( + dialog_context.parent.active_dialog.id + if dialog_context.parent and dialog_context.parent.active_dialog + else None + ) + active_id = ( + dialog_context.active_dialog.id if dialog_context.active_dialog else None + ) + return deepcopy(dialog_context.find_dialog_sync(parent_id or active_id)) + + def set_memory(self, dialog_context: "DialogContext", memory: object): + raise Exception( + f"{self.__class__.__name__}.set_memory not supported (read only)" + ) diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/dialog_context_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/dialog_context_memory_scope.py new file mode 100644 index 000000000..200f71b8c --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/dialog_context_memory_scope.py @@ -0,0 +1,65 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.dialogs.memory import scope_path + +from .memory_scope import MemoryScope + + +class DialogContextMemoryScope(MemoryScope): + def __init__(self): + # pylint: disable=invalid-name + + super().__init__(scope_path.SETTINGS, include_in_snapshot=False) + # Stack name. + self.STACK = "stack" + + # Active dialog name. + self.ACTIVE_DIALOG = "activeDialog" + + # Parent name. + self.PARENT = "parent" + + def get_memory(self, dialog_context: "DialogContext") -> object: + """ + Gets the backing memory for this scope. + The object for this turn. + Memory for the scope. + """ + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + # TODO: make sure that every object in the dict is serializable + memory = {} + stack = list([]) + current_dc = dialog_context + + # go to leaf node + while current_dc.child: + current_dc = current_dc.child + + while current_dc: + # (PORTERS NOTE: javascript stack is reversed with top of stack on end) + for item in current_dc.stack: + # filter out ActionScope items because they are internal bookkeeping. + if not item.id.startswith("ActionScope["): + stack.append(item.id) + + current_dc = current_dc.parent + + # top of stack is stack[0]. + memory[self.STACK] = stack + memory[self.ACTIVE_DIALOG] = ( + dialog_context.active_dialog.id if dialog_context.active_dialog else None + ) + memory[self.PARENT] = ( + dialog_context.parent.active_dialog.id + if dialog_context.parent and dialog_context.parent.active_dialog + else None + ) + return memory + + def set_memory(self, dialog_context: "DialogContext", memory: object): + raise Exception( + f"{self.__class__.__name__}.set_memory not supported (read only)" + ) diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/dialog_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/dialog_memory_scope.py new file mode 100644 index 000000000..490ad23a1 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/dialog_memory_scope.py @@ -0,0 +1,68 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.dialogs.memory import scope_path + +from .memory_scope import MemoryScope + + +class DialogMemoryScope(MemoryScope): + def __init__(self): + # pylint: disable=import-outside-toplevel + super().__init__(scope_path.DIALOG) + + # This import is to avoid circular dependency issues + from botbuilder.dialogs import DialogContainer + + self._dialog_container_cls = DialogContainer + + def get_memory(self, dialog_context: "DialogContext") -> object: + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + # if active dialog is a container dialog then "dialog" binds to it. + if dialog_context.active_dialog: + dialog = dialog_context.find_dialog_sync(dialog_context.active_dialog.id) + if isinstance(dialog, self._dialog_container_cls): + return dialog_context.active_dialog.state + + # Otherwise we always bind to parent, or if there is no parent the active dialog + parent_state = ( + dialog_context.parent.active_dialog.state + if dialog_context.parent and dialog_context.parent.active_dialog + else None + ) + dc_state = ( + dialog_context.active_dialog.state if dialog_context.active_dialog else None + ) + return parent_state or dc_state + + def set_memory(self, dialog_context: "DialogContext", memory: object): + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + if not memory: + raise TypeError(f"Expecting: memory object, but received None") + + # If active dialog is a container dialog then "dialog" binds to it. + # Otherwise the "dialog" will bind to the dialogs parent assuming it + # is a container. + parent = dialog_context + if not self.is_container(parent) and self.is_container(parent.parent): + parent = parent.parent + + # If there's no active dialog then throw an error. + if not parent.active_dialog: + raise Exception( + "Cannot set DialogMemoryScope. There is no active dialog dialog or parent dialog in the context" + ) + + parent.active_dialog.state = memory + + def is_container(self, dialog_context: "DialogContext"): + if dialog_context and dialog_context.active_dialog: + dialog = dialog_context.find_dialog_sync(dialog_context.active_dialog.id) + if isinstance(dialog, self._dialog_container_cls): + return True + + return False diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/memory_scope.py new file mode 100644 index 000000000..3b00401fc --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/memory_scope.py @@ -0,0 +1,84 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod + + +class MemoryScope(ABC): + def __init__(self, name: str, include_in_snapshot: bool = True): + # + # Gets or sets name of the scope. + # + # + # Name of the scope. + # + self.include_in_snapshot = include_in_snapshot + # + # Gets or sets a value indicating whether this memory should be included in snapshot. + # + # + # True or false. + # + self.name = name + + # + # Get the backing memory for this scope. + # + # dc. + # memory for the scope. + @abstractmethod + def get_memory( + self, dialog_context: "DialogContext" + ) -> object: # pylint: disable=unused-argument + raise NotImplementedError() + + # + # Changes the backing object for the memory scope. + # + # dc. + # memory. + @abstractmethod + def set_memory( + self, dialog_context: "DialogContext", memory: object + ): # pylint: disable=unused-argument + raise NotImplementedError() + + # + # Populates the state cache for this from the storage layer. + # + # The dialog context object for this turn. + # Optional, true to overwrite any existing state cache + # or false to load state from storage only if the cache doesn't already exist. + # A cancellation token that can be used by other objects + # or threads to receive notice of cancellation. + # A task that represents the work queued to execute. + async def load( + self, dialog_context: "DialogContext", force: bool = False + ): # pylint: disable=unused-argument + return + + # + # Writes the state cache for this to the storage layer. + # + # The dialog context object for this turn. + # Optional, true to save the state cache to storage + # or false to save state to storage only if a property in the cache has changed. + # A cancellation token that can be used by other objects + # or threads to receive notice of cancellation. + # A task that represents the work queued to execute. + async def save_changes( + self, dialog_context: "DialogContext", force: bool = False + ): # pylint: disable=unused-argument + return + + # + # Deletes any state in storage and the cache for this . + # + # The dialog context object for this turn. + # A cancellation token that can be used by other objects + # or threads to receive notice of cancellation. + # A task that represents the work queued to execute. + async def delete( + self, dialog_context: "DialogContext" + ): # pylint: disable=unused-argument + return diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/settings_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/settings_memory_scope.py new file mode 100644 index 000000000..790137aea --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/settings_memory_scope.py @@ -0,0 +1,31 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.dialogs.memory import scope_path + +from .memory_scope import MemoryScope + + +class SettingsMemoryScope(MemoryScope): + def __init__(self): + super().__init__(scope_path.SETTINGS) + self._empty_settings = {} + self.include_in_snapshot = False + + def get_memory(self, dialog_context: "DialogContext") -> object: + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + settings: dict = dialog_context.context.turn_state.get( + scope_path.SETTINGS, None + ) + + if not settings: + settings = self._empty_settings + + return settings + + def set_memory(self, dialog_context: "DialogContext", memory: object): + raise Exception( + f"{self.__class__.__name__}.set_memory not supported (read only)" + ) diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/this_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/this_memory_scope.py new file mode 100644 index 000000000..3de53bab3 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/this_memory_scope.py @@ -0,0 +1,28 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.dialogs.memory import scope_path + +from .memory_scope import MemoryScope + + +class ThisMemoryScope(MemoryScope): + def __init__(self): + super().__init__(scope_path.THIS) + + def get_memory(self, dialog_context: "DialogContext") -> object: + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + return ( + dialog_context.active_dialog.state if dialog_context.active_dialog else None + ) + + def set_memory(self, dialog_context: "DialogContext", memory: object): + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + if not memory: + raise TypeError(f"Expecting: object, but received None") + + dialog_context.active_dialog.state = memory diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/turn_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/turn_memory_scope.py new file mode 100644 index 000000000..661124bbf --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/turn_memory_scope.py @@ -0,0 +1,79 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.dialogs.memory import scope_path + +from .memory_scope import MemoryScope + + +class CaseInsensitiveDict(dict): + # pylint: disable=protected-access + + @classmethod + def _k(cls, key): + return key.lower() if isinstance(key, str) else key + + def __init__(self, *args, **kwargs): + super(CaseInsensitiveDict, self).__init__(*args, **kwargs) + self._convert_keys() + + def __getitem__(self, key): + return super(CaseInsensitiveDict, self).__getitem__(self.__class__._k(key)) + + def __setitem__(self, key, value): + super(CaseInsensitiveDict, self).__setitem__(self.__class__._k(key), value) + + def __delitem__(self, key): + return super(CaseInsensitiveDict, self).__delitem__(self.__class__._k(key)) + + def __contains__(self, key): + return super(CaseInsensitiveDict, self).__contains__(self.__class__._k(key)) + + def pop(self, key, *args, **kwargs): + return super(CaseInsensitiveDict, self).pop( + self.__class__._k(key), *args, **kwargs + ) + + def get(self, key, *args, **kwargs): + return super(CaseInsensitiveDict, self).get( + self.__class__._k(key), *args, **kwargs + ) + + def setdefault(self, key, *args, **kwargs): + return super(CaseInsensitiveDict, self).setdefault( + self.__class__._k(key), *args, **kwargs + ) + + def update(self, e=None, **f): + if e is None: + e = {} + super(CaseInsensitiveDict, self).update(self.__class__(e)) + super(CaseInsensitiveDict, self).update(self.__class__(**f)) + + def _convert_keys(self): + for k in list(self.keys()): + val = super(CaseInsensitiveDict, self).pop(k) + self.__setitem__(k, val) + + +class TurnMemoryScope(MemoryScope): + def __init__(self): + super().__init__(scope_path.TURN, False) + + def get_memory(self, dialog_context: "DialogContext") -> object: + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + turn_value = dialog_context.context.turn_state.get(scope_path.TURN, None) + + if not turn_value: + turn_value = CaseInsensitiveDict() + dialog_context.context.turn_state[scope_path.TURN] = turn_value + + return turn_value + + def set_memory(self, dialog_context: "DialogContext", memory: object): + if not dialog_context: + raise TypeError(f"Expecting: DialogContext, but received None") + + dialog_context.context.turn_state[scope_path.TURN] = memory diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/user_memory_scope.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/user_memory_scope.py new file mode 100644 index 000000000..b1bc6351d --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/memory/scopes/user_memory_scope.py @@ -0,0 +1,12 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import UserState +from botbuilder.dialogs.memory import scope_path + +from .bot_state_memory_scope import BotStateMemoryScope + + +class UserMemoryScope(BotStateMemoryScope): + def __init__(self): + super().__init__(UserState, scope_path.USER) diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/object_path.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/object_path.py new file mode 100644 index 000000000..80f722519 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/object_path.py @@ -0,0 +1,313 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import copy +from typing import Union, Callable + + +class ObjectPath: + """ + Helper methods for working with json objects. + """ + + @staticmethod + def assign(start_object, overlay_object, default: Union[Callable, object] = None): + """ + Creates a new object by overlaying values in start_object with non-null values from overlay_object. + + :param start_object: dict or typed object, the target object to set values on + :param overlay_object: dict or typed object, the item to overlay values form + :param default: Provides a default object if both source and overlay are None + :return: A copy of start_object, with values from overlay_object + """ + if start_object and overlay_object: + merged = copy.deepcopy(start_object) + + def merge(target: dict, source: dict): + key_set = set(target).union(set(source)) + + for key in key_set: + target_value = target.get(key) + source_value = source.get(key) + + # skip empty overlay items + if source_value: + if isinstance(source_value, dict): + # merge dictionaries + if not target_value: + target[key] = copy.deepcopy(source_value) + else: + merge(target_value, source_value) + elif not hasattr(source_value, "__dict__"): + # simple type. just copy it. + target[key] = copy.copy(source_value) + elif not target_value: + # the target doesn't have the value, but + # the overlay does. just copy it. + target[key] = copy.deepcopy(source_value) + else: + # recursive class copy + merge(target_value.__dict__, source_value.__dict__) + + target_dict = merged if isinstance(merged, dict) else merged.__dict__ + overlay_dict = ( + overlay_object + if isinstance(overlay_object, dict) + else overlay_object.__dict__ + ) + merge(target_dict, overlay_dict) + + return merged + + if overlay_object: + return copy.deepcopy(overlay_object) + + if start_object: + return start_object + if default: + return default() if callable(default) else copy.deepcopy(default) + return None + + @staticmethod + def set_path_value(obj, path: str, value: object): + """ + Given an object evaluate a path to set the value. + """ + + segments = ObjectPath.try_resolve_path(obj, path) + if not segments: + return + + current = obj + for i in range(len(segments) - 1): + segment = segments[i] + if ObjectPath.is_int(segment): + index = int(segment) + next_obj = current[index] + if not next_obj and len(current) <= index: + # Expand list to index + current += [None] * ((index + 1) - len(current)) + next_obj = current[index] + else: + next_obj = ObjectPath.__get_object_property(current, segment) + if not next_obj: + # Create object or list based on next segment + next_segment = segments[i + 1] + if not ObjectPath.is_int(next_segment): + ObjectPath.__set_object_segment(current, segment, {}) + else: + ObjectPath.__set_object_segment(current, segment, []) + + next_obj = ObjectPath.__get_object_property(current, segment) + + current = next_obj + + last_segment = segments[-1] + ObjectPath.__set_object_segment(current, last_segment, value) + + @staticmethod + def get_path_value( + obj, path: str, default: Union[Callable, object] = None + ) -> object: + """ + Get the value for a path relative to an object. + """ + + value = ObjectPath.try_get_path_value(obj, path) + if value: + return value + + if default is None: + raise KeyError(f"Key {path} not found") + return default() if callable(default) else copy.deepcopy(default) + + @staticmethod + def has_value(obj, path: str) -> bool: + """ + Does an object have a subpath. + """ + return ObjectPath.try_get_path_value(obj, path) is not None + + @staticmethod + def remove_path_value(obj, path: str): + """ + Remove path from object. + """ + + segments = ObjectPath.try_resolve_path(obj, path) + if not segments: + return + + current = obj + for i in range(len(segments) - 1): + segment = segments[i] + current = ObjectPath.__resolve_segment(current, segment) + if not current: + return + + if current: + last_segment = segments[-1] + if ObjectPath.is_int(last_segment): + current[int(last_segment)] = None + else: + current.pop(last_segment) + + @staticmethod + def try_get_path_value(obj, path: str) -> object: + """ + Get the value for a path relative to an object. + """ + + if not obj: + return None + + if path is None: + return None + + if not path: + return obj + + segments = ObjectPath.try_resolve_path(obj, path) + if not segments: + return None + + result = ObjectPath.__resolve_segments(obj, segments) + if not result: + return None + + return result + + @staticmethod + def __set_object_segment(obj, segment, value): + val = ObjectPath.__get_normalized_value(value) + + if ObjectPath.is_int(segment): + # the target is an list + index = int(segment) + + # size the list if needed + obj += [None] * ((index + 1) - len(obj)) + + obj[index] = val + return + + # the target is a dictionary + obj[segment] = val + + @staticmethod + def __get_normalized_value(value): + return value + + @staticmethod + def try_resolve_path(obj, property_path: str, evaluate: bool = False) -> []: + so_far = [] + first = property_path[0] if property_path else " " + if first in ("'", '"'): + if not property_path.endswith(first): + return None + + so_far.append(property_path[1 : len(property_path) - 2]) + elif ObjectPath.is_int(property_path): + so_far.append(int(property_path)) + else: + start = 0 + i = 0 + + def emit(): + nonlocal start, i + segment = property_path[start:i] + if segment: + so_far.append(segment) + start = i + 1 + + while i < len(property_path): + char = property_path[i] + if char in (".", "["): + emit() + + if char == "[": + nesting = 1 + i += 1 + while i < len(property_path): + char = property_path[i] + if char == "[": + nesting += 1 + elif char == "]": + nesting -= 1 + if nesting == 0: + break + i += 1 + + if nesting > 0: + return None + + expr = property_path[start:i] + start = i + 1 + indexer = ObjectPath.try_resolve_path(obj, expr, True) + if not indexer: + return None + + result = indexer[0] + if ObjectPath.is_int(result): + so_far.append(int(result)) + else: + so_far.append(result) + + i += 1 + + emit() + + if evaluate: + result = ObjectPath.__resolve_segments(obj, so_far) + if not result: + return None + + so_far.clear() + so_far.append(result) + + return so_far + + @staticmethod + def for_each_property(obj: object, action: Callable[[str, object], None]): + if isinstance(obj, dict): + for key, value in obj.items(): + action(key, value) + elif hasattr(obj, "__dict__"): + for key, value in vars(obj).items(): + action(key, value) + + @staticmethod + def __resolve_segments(current, segments: []) -> object: + result = current + + for segment in segments: + result = ObjectPath.__resolve_segment(result, segment) + if not result: + return None + + return result + + @staticmethod + def __resolve_segment(current, segment) -> object: + if current: + if ObjectPath.is_int(segment): + current = current[int(segment)] + else: + current = ObjectPath.__get_object_property(current, segment) + + return current + + @staticmethod + def __get_object_property(obj, property_name: str): + # doing a case insensitive search + property_name_lower = property_name.lower() + matching = [obj[key] for key in obj if key.lower() == property_name_lower] + return matching[0] if matching else None + + @staticmethod + def is_int(value: str) -> bool: + try: + int(value) + return True + except ValueError: + return False diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/persisted_state.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/persisted_state.py new file mode 100644 index 000000000..7825a3d9f --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/persisted_state.py @@ -0,0 +1,20 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Dict + +from .persisted_state_keys import PersistedStateKeys + + +class PersistedState: + def __init__(self, keys: PersistedStateKeys = None, data: Dict[str, object] = None): + if keys and data: + self.user_state: Dict[str, object] = ( + data[keys.user_state] if keys.user_state in data else {} + ) + self.conversation_state: Dict[str, object] = ( + data[keys.conversation_state] if keys.conversation_state in data else {} + ) + else: + self.user_state: Dict[str, object] = {} + self.conversation_state: Dict[str, object] = {} diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/persisted_state_keys.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/persisted_state_keys.py new file mode 100644 index 000000000..59f7c34cd --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/persisted_state_keys.py @@ -0,0 +1,8 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +class PersistedStateKeys: + def __init__(self): + self.user_state: str = None + self.conversation_state: str = None diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/__init__.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/__init__.py index 457121fad..9f9dc6242 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/__init__.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/__init__.py @@ -14,6 +14,7 @@ from .number_prompt import NumberPrompt from .oauth_prompt import OAuthPrompt from .oauth_prompt_settings import OAuthPromptSettings +from .prompt_culture_models import PromptCultureModel, PromptCultureModels from .prompt_options import PromptOptions from .prompt_recognizer_result import PromptRecognizerResult from .prompt_validator_context import PromptValidatorContext @@ -30,6 +31,8 @@ "NumberPrompt", "OAuthPrompt", "OAuthPromptSettings", + "PromptCultureModel", + "PromptCultureModels", "PromptOptions", "PromptRecognizerResult", "PromptValidatorContext", diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/activity_prompt.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/activity_prompt.py index 70e02f457..a8f2f944e 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/activity_prompt.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/activity_prompt.py @@ -1,7 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from abc import ABC from typing import Callable, Dict from botbuilder.core import TurnContext @@ -20,7 +19,7 @@ from .prompt_validator_context import PromptValidatorContext -class ActivityPrompt(Dialog, ABC): +class ActivityPrompt(Dialog): """ Waits for an activity to be received. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/attachment_prompt.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/attachment_prompt.py index ab2cf1736..3f157fb4a 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/attachment_prompt.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/attachment_prompt.py @@ -3,7 +3,7 @@ from typing import Callable, Dict -from botbuilder.schema import ActivityTypes, InputHints +from botbuilder.schema import ActivityTypes from botbuilder.core import TurnContext from .prompt import Prompt, PromptValidatorContext @@ -39,10 +39,8 @@ async def on_prompt( ) if is_retry and options.retry_prompt: - options.retry_prompt.input_hint = InputHints.expecting_input await turn_context.send_activity(options.retry_prompt) elif options.prompt: - options.prompt.input_hint = InputHints.expecting_input await turn_context.send_activity(options.prompt) async def on_recognize( diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/choice_prompt.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/choice_prompt.py index 3332f3994..9996c7964 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/choice_prompt.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/choice_prompt.py @@ -1,9 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from typing import Callable, Dict, List, Union +from typing import Callable, Dict, List -from recognizers_text import Culture from botbuilder.core import TurnContext from botbuilder.dialogs.choices import ( Choice, @@ -15,6 +14,7 @@ from botbuilder.schema import Activity, ActivityTypes from .prompt import Prompt +from .prompt_culture_models import PromptCultureModels from .prompt_options import PromptOptions from .prompt_validator_context import PromptValidatorContext from .prompt_recognizer_result import PromptRecognizerResult @@ -29,54 +29,13 @@ class ChoicePrompt(Prompt): """ _default_choice_options: Dict[str, ChoiceFactoryOptions] = { - Culture.Spanish: ChoiceFactoryOptions( - inline_separator=", ", - inline_or=" o ", - inline_or_more=", o ", + c.locale: ChoiceFactoryOptions( + inline_separator=c.separator, + inline_or=c.inline_or_more, + inline_or_more=c.inline_or_more, include_numbers=True, - ), - Culture.Dutch: ChoiceFactoryOptions( - inline_separator=", ", - inline_or=" of ", - inline_or_more=", of ", - include_numbers=True, - ), - Culture.English: ChoiceFactoryOptions( - inline_separator=", ", - inline_or=" or ", - inline_or_more=", or ", - include_numbers=True, - ), - Culture.French: ChoiceFactoryOptions( - inline_separator=", ", - inline_or=" ou ", - inline_or_more=", ou ", - include_numbers=True, - ), - "de-de": ChoiceFactoryOptions( - inline_separator=", ", - inline_or=" oder ", - inline_or_more=", oder ", - include_numbers=True, - ), - Culture.Japanese: ChoiceFactoryOptions( - inline_separator="、 ", - inline_or=" または ", - inline_or_more="、 または ", - include_numbers=True, - ), - Culture.Portuguese: ChoiceFactoryOptions( - inline_separator=", ", - inline_or=" ou ", - inline_or_more=", ou ", - include_numbers=True, - ), - Culture.Chinese: ChoiceFactoryOptions( - inline_separator=", ", - inline_or=" 要么 ", - inline_or_more=", 要么 ", - include_numbers=True, - ), + ) + for c in PromptCultureModels.get_supported_cultures() } def __init__( @@ -84,7 +43,19 @@ def __init__( dialog_id: str, validator: Callable[[PromptValidatorContext], bool] = None, default_locale: str = None, + choice_defaults: Dict[str, ChoiceFactoryOptions] = None, ): + """ + :param dialog_id: Unique ID of the dialog within its parent `DialogSet`. + :param validator: (Optional) validator that will be called each time the user responds to the prompt. + If the validator replies with a message no additional retry prompt will be sent. + :param default_locale: (Optional) locale to use if `dc.context.activity.locale` not specified. + Defaults to a value of `en-us`. + :param choice_defaults: (Optional) Overrides the dictionary of + Bot Framework SDK-supported _default_choice_options. + As type Dict[str, ChoiceFactoryOptions], the key is a string of the locale, such as "en-us". + * Must be passed in to each ConfirmPrompt that needs the custom choice defaults. + """ super().__init__(dialog_id, validator) self.style = ListStyle.auto @@ -92,6 +63,9 @@ def __init__( self.choice_options: ChoiceFactoryOptions = None self.recognizer_options: FindChoicesOptions = None + if choice_defaults is not None: + self._default_choice_options = choice_defaults + async def on_prompt( self, turn_context: TurnContext, @@ -106,12 +80,7 @@ async def on_prompt( raise TypeError("ChoicePrompt.on_prompt(): options cannot be None.") # Determine culture - culture: Union[ - str, None - ] = turn_context.activity.locale if turn_context.activity.locale else self.default_locale - - if not culture or culture not in ChoicePrompt._default_choice_options: - culture = Culture.English + culture = self._determine_culture(turn_context.activity) # Format prompt to send choices: List[Choice] = options.choices if options.choices else [] @@ -119,7 +88,7 @@ async def on_prompt( choice_options: ChoiceFactoryOptions = ( self.choice_options if self.choice_options - else ChoicePrompt._default_choice_options[culture] + else self._default_choice_options[culture] ) choice_style = ( 0 if options.style == 0 else options.style if options.style else self.style @@ -152,12 +121,14 @@ async def on_recognize( if turn_context.activity.type == ActivityTypes.message: activity: Activity = turn_context.activity utterance: str = activity.text - opt: FindChoicesOptions = self.recognizer_options if self.recognizer_options else FindChoicesOptions() - opt.locale = ( - activity.locale - if activity.locale - else (self.default_locale or Culture.English) + if not utterance: + return result + opt: FindChoicesOptions = ( + self.recognizer_options + if self.recognizer_options + else FindChoicesOptions() ) + opt.locale = self._determine_culture(turn_context.activity, opt) results = ChoiceRecognizers.recognize_choices(utterance, choices, opt) if results is not None and results: @@ -165,3 +136,17 @@ async def on_recognize( result.value = results[0].resolution return result + + def _determine_culture( + self, activity: Activity, opt: FindChoicesOptions = FindChoicesOptions() + ) -> str: + culture = ( + PromptCultureModels.map_to_nearest_language(activity.locale) + or opt.locale + or self.default_locale + or PromptCultureModels.English.locale + ) + if not culture or not self._default_choice_options.get(culture): + culture = PromptCultureModels.English.locale + + return culture diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/confirm_prompt.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/confirm_prompt.py index 706369cc6..30f2248e8 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/confirm_prompt.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/confirm_prompt.py @@ -12,60 +12,30 @@ ListStyle, ) from .prompt import Prompt +from .prompt_culture_models import PromptCultureModels from .prompt_options import PromptOptions from .prompt_recognizer_result import PromptRecognizerResult class ConfirmPrompt(Prompt): - # TODO: Fix to reference recognizer to use proper constants - choice_defaults: Dict[str, object] = { - "Spanish": ( - Choice("Si"), - Choice("No"), - ChoiceFactoryOptions(", ", " o ", ", o ", True), - ), - "Dutch": ( - Choice("Ja"), - Choice("Nee"), - ChoiceFactoryOptions(", ", " of ", ", of ", True), - ), - "English": ( - Choice("Yes"), - Choice("No"), - ChoiceFactoryOptions(", ", " or ", ", or ", True), - ), - "French": ( - Choice("Oui"), - Choice("Non"), - ChoiceFactoryOptions(", ", " ou ", ", ou ", True), - ), - "German": ( - Choice("Ja"), - Choice("Nein"), - ChoiceFactoryOptions(", ", " oder ", ", oder ", True), - ), - "Japanese": ( - Choice("はい"), - Choice("いいえ"), - ChoiceFactoryOptions("、 ", " または ", "、 または ", True), - ), - "Portuguese": ( - Choice("Sim"), - Choice("Não"), - ChoiceFactoryOptions(", ", " ou ", ", ou ", True), - ), - "Chinese": ( - Choice("是的"), - Choice("不"), - ChoiceFactoryOptions(", ", " 要么 ", ", 要么 ", True), - ), + _default_choice_options: Dict[str, object] = { + c.locale: ( + Choice(c.yes_in_language), + Choice(c.no_in_language), + ChoiceFactoryOptions(c.separator, c.inline_or, c.inline_or_more, True), + ) + for c in PromptCultureModels.get_supported_cultures() } # TODO: PromptValidator def __init__( - self, dialog_id: str, validator: object = None, default_locale: str = None + self, + dialog_id: str, + validator: object = None, + default_locale: str = None, + choice_defaults: Dict[str, object] = None, ): - super(ConfirmPrompt, self).__init__(dialog_id, validator) + super().__init__(dialog_id, validator) if dialog_id is None: raise TypeError("ConfirmPrompt(): dialog_id cannot be None.") # TODO: Port ListStyle @@ -75,6 +45,9 @@ def __init__( self.choice_options = None self.confirm_choices = None + if choice_defaults is not None: + self._default_choice_options = choice_defaults + async def on_prompt( self, turn_context: TurnContext, @@ -89,8 +62,8 @@ async def on_prompt( # Format prompt to send channel_id = turn_context.activity.channel_id - culture = self.determine_culture(turn_context.activity) - defaults = self.choice_defaults[culture] + culture = self._determine_culture(turn_context.activity) + defaults = self._default_choice_options[culture] choice_opts = ( self.choice_options if self.choice_options is not None else defaults[2] ) @@ -122,9 +95,11 @@ async def on_recognize( result = PromptRecognizerResult() if turn_context.activity.type == ActivityTypes.message: # Recognize utterance - message = turn_context.activity - culture = self.determine_culture(turn_context.activity) - results = recognize_boolean(message.text, culture) + utterance = turn_context.activity.text + if not utterance: + return result + culture = self._determine_culture(turn_context.activity) + results = recognize_boolean(utterance, culture) if results: first = results[0] if "value" in first.resolution: @@ -133,7 +108,7 @@ async def on_recognize( else: # First check whether the prompt was sent to the user with numbers # if it was we should recognize numbers - defaults = self.choice_defaults[culture] + defaults = self._default_choice_options[culture] opts = ( self.choice_options if self.choice_options is not None @@ -151,7 +126,7 @@ async def on_recognize( ) choices = {confirm_choices[0], confirm_choices[1]} second_attempt_results = ChoiceRecognizers.recognize_choices( - message.text, choices + utterance, choices ) if second_attempt_results: result.succeeded = True @@ -159,12 +134,13 @@ async def on_recognize( return result - def determine_culture(self, activity: Activity) -> str: + def _determine_culture(self, activity: Activity) -> str: culture = ( - activity.locale if activity.locale is not None else self.default_locale + PromptCultureModels.map_to_nearest_language(activity.locale) + or self.default_locale + or PromptCultureModels.English.locale ) - if not culture or culture not in self.choice_defaults: - culture = ( - "English" # TODO: Fix to reference recognizer to use proper constants - ) + if not culture or not self._default_choice_options.get(culture): + culture = PromptCultureModels.English.locale + return culture diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/datetime_prompt.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/datetime_prompt.py index 3eceeb184..907d81f7d 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/datetime_prompt.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/datetime_prompt.py @@ -50,11 +50,17 @@ async def on_recognize( result = PromptRecognizerResult() if turn_context.activity.type == ActivityTypes.message: # Recognize utterance - message = turn_context.activity + utterance = turn_context.activity.text + if not utterance: + return result # TODO: English constant needs to be ported. - culture = message.locale if message.locale is not None else "English" + culture = ( + turn_context.activity.locale + if turn_context.activity.locale is not None + else "English" + ) - results = recognize_datetime(message.text, culture) + results = recognize_datetime(utterance, culture) if results: result.succeeded = True result.value = [] diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/number_prompt.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/number_prompt.py index ed757c391..519ba39c9 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/number_prompt.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/number_prompt.py @@ -55,9 +55,11 @@ async def on_recognize( result = PromptRecognizerResult() if turn_context.activity.type == ActivityTypes.message: - message = turn_context.activity + utterance = turn_context.activity.text + if not utterance: + return result culture = self._get_culture(turn_context) - results: [ModelResult] = recognize_number(message.text, culture) + results: [ModelResult] = recognize_number(utterance, culture) if results: result.succeeded = True diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/oauth_prompt.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/oauth_prompt.py index 718aa2427..270d4f324 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/oauth_prompt.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/oauth_prompt.py @@ -7,15 +7,17 @@ from typing import Union, Awaitable, Callable from botframework.connector import Channels -from botframework.connector.auth import ClaimsIdentity, SkillValidation -from botframework.connector.token_api.models import SignInUrlResponse +from botframework.connector.auth import ( + ClaimsIdentity, + SkillValidation, + JwtTokenValidation, +) from botbuilder.core import ( CardFactory, - ExtendedUserTokenProvider, MessageFactory, InvokeResponse, TurnContext, - UserTokenProvider, + BotAdapter, ) from botbuilder.core.bot_framework_adapter import TokenExchangeRequest from botbuilder.dialogs import Dialog, DialogContext, DialogTurnResult @@ -38,10 +40,21 @@ from .prompt_validator_context import PromptValidatorContext from .prompt_recognizer_result import PromptRecognizerResult -# TODO: Consider moving TokenExchangeInvokeRequest and TokenExchangeInvokeResponse to here +from .._user_token_access import _UserTokenAccess + + +class CallerInfo: + def __init__(self, caller_service_url: str = None, scope: str = None): + self.caller_service_url = caller_service_url + self.scope = scope class OAuthPrompt(Dialog): + PERSISTED_OPTIONS = "options" + PERSISTED_STATE = "state" + PERSISTED_EXPIRES = "expires" + PERSISTED_CALLER = "caller" + """ Creates a new prompt that asks the user to sign in, using the Bot Framework Single Sign On (SSO) service. @@ -124,7 +137,7 @@ async def begin_dialog( """ if dialog_context is None: raise TypeError( - f"OAuthPrompt.begin_dialog: Expected DialogContext but got NoneType instead" + f"OAuthPrompt.begin_dialog(): Expected DialogContext but got NoneType instead" ) options = options or PromptOptions() @@ -143,23 +156,21 @@ async def begin_dialog( else 900000 ) state = dialog_context.active_dialog.state - state["state"] = {} - state["options"] = options - state["expires"] = datetime.now() + timedelta(seconds=timeout / 1000) - - if not isinstance(dialog_context.context.adapter, UserTokenProvider): - raise TypeError( - "OAuthPrompt.get_user_token(): not supported by the current adapter" - ) + state[OAuthPrompt.PERSISTED_STATE] = {} + state[OAuthPrompt.PERSISTED_OPTIONS] = options + state[OAuthPrompt.PERSISTED_EXPIRES] = datetime.now() + timedelta( + seconds=timeout / 1000 + ) + state[OAuthPrompt.PERSISTED_CALLER] = OAuthPrompt.__create_caller_info( + dialog_context.context + ) - output = await dialog_context.context.adapter.get_user_token( - dialog_context.context, - self._settings.connection_name, - None, - self._settings.oath_app_credentials, + output = await _UserTokenAccess.get_user_token( + dialog_context.context, self._settings, None ) if output is not None: + # Return token return await dialog_context.end_dialog(output) await self._send_oauth_card(dialog_context.context, options.prompt) @@ -181,13 +192,19 @@ async def continue_dialog(self, dialog_context: DialogContext) -> DialogTurnResu The prompt generally continues to receive the user's replies until it accepts the user's reply as valid input for the prompt. """ - # Recognize token - recognized = await self._recognize_token(dialog_context.context) - # Check for timeout state = dialog_context.active_dialog.state is_message = dialog_context.context.activity.type == ActivityTypes.message - has_timed_out = is_message and (datetime.now() > state["expires"]) + is_timeout_activity_type = ( + is_message + or OAuthPrompt._is_token_response_event(dialog_context.context) + or OAuthPrompt._is_teams_verification_invoke(dialog_context.context) + or OAuthPrompt._is_token_exchange_request_invoke(dialog_context.context) + ) + + has_timed_out = is_timeout_activity_type and ( + datetime.now() > state[OAuthPrompt.PERSISTED_EXPIRES] + ) if has_timed_out: return await dialog_context.end_dialog(None) @@ -197,6 +214,9 @@ async def continue_dialog(self, dialog_context: DialogContext) -> DialogTurnResu else: state["state"]["attemptCount"] += 1 + # Recognize token + recognized = await self._recognize_token(dialog_context) + # Validate the return value is_valid = False if self._validator is not None: @@ -204,8 +224,8 @@ async def continue_dialog(self, dialog_context: DialogContext) -> DialogTurnResu PromptValidatorContext( dialog_context.context, recognized, - state["state"], - state["options"], + state[OAuthPrompt.PERSISTED_STATE], + state[OAuthPrompt.PERSISTED_OPTIONS], ) ) elif recognized.succeeded: @@ -214,14 +234,19 @@ async def continue_dialog(self, dialog_context: DialogContext) -> DialogTurnResu # Return recognized value or re-prompt if is_valid: return await dialog_context.end_dialog(recognized.value) + if is_message and self._settings.end_on_invalid_message: + # If EndOnInvalidMessage is set, complete the prompt with no result. + return await dialog_context.end_dialog(None) # Send retry prompt if ( not dialog_context.context.responded and is_message - and state["options"].retry_prompt is not None + and state[OAuthPrompt.PERSISTED_OPTIONS].retry_prompt is not None ): - await dialog_context.context.send_activity(state["options"].retry_prompt) + await dialog_context.context.send_activity( + state[OAuthPrompt.PERSISTED_OPTIONS].retry_prompt + ) return Dialog.end_of_turn @@ -243,20 +268,7 @@ async def get_user_token( If the task is successful and the user already has a token or the user successfully signs in, the result contains the user's token. """ - adapter = context.adapter - - # Validate adapter type - if not hasattr(adapter, "get_user_token"): - raise Exception( - "OAuthPrompt.get_user_token(): not supported for the current adapter." - ) - - return await adapter.get_user_token( - context, - self._settings.connection_name, - code, - self._settings.oath_app_credentials, - ) + return await _UserTokenAccess.get_user_token(context, self._settings, code) async def sign_out_user(self, context: TurnContext): """ @@ -270,20 +282,18 @@ async def sign_out_user(self, context: TurnContext): If the task is successful and the user already has a token or the user successfully signs in, the result contains the user's token. """ - adapter = context.adapter + return await _UserTokenAccess.sign_out_user(context, self._settings) - # Validate adapter type - if not hasattr(adapter, "sign_out_user"): - raise Exception( - "OAuthPrompt.sign_out_user(): not supported for the current adapter." + @staticmethod + def __create_caller_info(context: TurnContext) -> CallerInfo: + bot_identity = context.turn_state.get(BotAdapter.BOT_IDENTITY_KEY) + if bot_identity and SkillValidation.is_skill_claim(bot_identity.claims): + return CallerInfo( + caller_service_url=context.activity.service_url, + scope=JwtTokenValidation.get_app_id_from_claims(bot_identity.claims), ) - return await adapter.sign_out_user( - context, - self._settings.connection_name, - None, - self._settings.oath_app_credentials, - ) + return None async def _send_oauth_card( self, context: TurnContext, prompt: Union[Activity, str] = None @@ -300,20 +310,16 @@ async def _send_oauth_card( att.content_type == CardFactory.content_types.oauth_card for att in prompt.attachments ): - adapter: ExtendedUserTokenProvider = context.adapter card_action_type = ActionTypes.signin - sign_in_resource: SignInUrlResponse = await adapter.get_sign_in_resource_from_user_and_credentials( - context, - self._settings.oath_app_credentials, - self._settings.connection_name, - context.activity.from_property.id, + sign_in_resource = await _UserTokenAccess.get_sign_in_resource( + context, self._settings ) link = sign_in_resource.sign_in_link - bot_identity: ClaimsIdentity = context.turn_state.get("BotIdentity") + bot_identity: ClaimsIdentity = context.turn_state.get( + BotAdapter.BOT_IDENTITY_KEY + ) - # use the SignInLink when - # in speech channel or - # bot is a skill or + # use the SignInLink when in speech channel or bot is a skill or # an extra OAuthAppCredentials is being passed in if ( ( @@ -325,7 +331,9 @@ async def _send_oauth_card( ): if context.activity.channel_id == Channels.emulator: card_action_type = ActionTypes.open_url - else: + elif not OAuthPrompt._channel_requires_sign_in_link( + context.activity.channel_id + ): link = None json_token_ex_resource = ( @@ -333,6 +341,13 @@ async def _send_oauth_card( if sign_in_resource.token_exchange_resource else None ) + + json_token_ex_post = ( + sign_in_resource.token_post_resource.as_dict() + if sign_in_resource.token_post_resource + else None + ) + prompt.attachments.append( CardFactory.oauth_card( OAuthCard( @@ -347,6 +362,7 @@ async def _send_oauth_card( ) ], token_exchange_resource=json_token_ex_resource, + token_post_resource=json_token_ex_post, ) ) ) @@ -357,7 +373,7 @@ async def _send_oauth_card( ): if not hasattr(context.adapter, "get_oauth_sign_in_link"): raise Exception( - "OAuthPrompt.send_oauth_card(): get_oauth_sign_in_link() not supported by the current adapter" + "OAuthPrompt._send_oauth_card(): get_oauth_sign_in_link() not supported by the current adapter" ) link = await context.adapter.get_oauth_sign_in_link( @@ -384,33 +400,62 @@ async def _send_oauth_card( # Send prompt await context.send_activity(prompt) - async def _recognize_token(self, context: TurnContext) -> PromptRecognizerResult: + async def _recognize_token( + self, dialog_context: DialogContext + ) -> PromptRecognizerResult: + context = dialog_context.context token = None if OAuthPrompt._is_token_response_event(context): token = context.activity.value + + # fixup the turnContext's state context if this was received from a skill host caller + state: CallerInfo = dialog_context.active_dialog.state[ + OAuthPrompt.PERSISTED_CALLER + ] + if state: + # set the ServiceUrl to the skill host's Url + dialog_context.context.activity.service_url = state.caller_service_url + claims_identity = context.turn_state.get(BotAdapter.BOT_IDENTITY_KEY) + connector_client = await _UserTokenAccess.create_connector_client( + context, + dialog_context.context.activity.service_url, + claims_identity, + state.scope, + ) + + context.turn_state[BotAdapter.BOT_CONNECTOR_CLIENT_KEY] = ( + connector_client + ) + elif OAuthPrompt._is_teams_verification_invoke(context): - code = context.activity.value["state"] + code = ( + context.activity.value.get("state", None) + if context.activity.value + else None + ) try: - token = await self.get_user_token(context, code) + token = await _UserTokenAccess.get_user_token( + context, self._settings, code + ) if token is not None: await context.send_activity( Activity( type="invokeResponse", - value=InvokeResponse(int(HTTPStatus.OK)), + value=InvokeResponse(status=HTTPStatus.OK), ) ) else: await context.send_activity( Activity( type="invokeResponse", - value=InvokeResponse(int(HTTPStatus.NOT_FOUND)), + value=InvokeResponse(status=HTTPStatus.NOT_FOUND), ) ) except Exception: await context.send_activity( Activity( type="invokeResponse", - value=InvokeResponse(int(HTTPStatus.INTERNAL_SERVER_ERROR)), + value=InvokeResponse(status=HTTPStatus.INTERNAL_SERVER_ERROR), ) ) elif self._is_token_exchange_request_invoke(context): @@ -444,34 +489,25 @@ async def _recognize_token(self, context: TurnContext) -> PromptRecognizerResult " ConnectionName in the TokenExchangeInvokeRequest", ) ) - elif not getattr(context.adapter, "exchange_token"): - # Token Exchange not supported in the adapter. - await context.send_activity( - self._get_token_exchange_invoke_response( - int(HTTPStatus.BAD_GATEWAY), - "The bot's BotAdapter does not support token exchange operations." - " Ensure the bot's Adapter supports the ITokenExchangeProvider interface.", - ) - ) - - raise AttributeError( - "OAuthPrompt.recognize(): not supported by the current adapter." - ) else: # No errors. Proceed with token exchange. - extended_user_token_provider: ExtendedUserTokenProvider = context.adapter - token_exchange_response = await extended_user_token_provider.exchange_token_from_credentials( - context, - self._settings.oath_app_credentials, - self._settings.connection_name, - context.activity.from_property.id, - TokenExchangeRequest(token=context.activity.value.token), - ) + token_exchange_response = None + try: + token_exchange_response = await _UserTokenAccess.exchange_token( + context, + self._settings, + TokenExchangeRequest(token=context.activity.value.token), + ) + except: + # Ignore Exceptions + # If token exchange failed for any reason, tokenExchangeResponse above stays null, and + # hence we send back a failure invoke response to the caller. + pass if not token_exchange_response or not token_exchange_response.token: await context.send_activity( self._get_token_exchange_invoke_response( - int(HTTPStatus.CONFLICT), + int(HTTPStatus.PRECONDITION_FAILED), "The bot is unable to exchange token. Proceed with regular login.", ) ) @@ -490,7 +526,9 @@ async def _recognize_token(self, context: TurnContext) -> PromptRecognizerResult elif context.activity.type == ActivityTypes.message and context.activity.text: match = re.match(r"(? bool: @staticmethod def _channel_suppports_oauth_card(channel_id: str) -> bool: if channel_id in [ - Channels.ms_teams, Channels.cortana, Channels.skype, Channels.skype_for_business, @@ -543,6 +580,13 @@ def _channel_suppports_oauth_card(channel_id: str) -> bool: return True + @staticmethod + def _channel_requires_sign_in_link(channel_id: str) -> bool: + if channel_id in [Channels.ms_teams]: + return True + + return False + @staticmethod def _is_token_exchange_request_invoke(context: TurnContext) -> bool: activity = context.activity diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/oauth_prompt_settings.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/oauth_prompt_settings.py index 1d8f04eca..c071c590e 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/oauth_prompt_settings.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/oauth_prompt_settings.py @@ -11,6 +11,7 @@ def __init__( text: str = None, timeout: int = None, oauth_app_credentials: AppCredentials = None, + end_on_invalid_message: bool = False, ): """ Settings used to configure an `OAuthPrompt` instance. @@ -22,9 +23,15 @@ def __init__( `OAuthPrompt` defaults value to `900,000` ms (15 minutes). oauth_app_credentials (AppCredentials): (Optional) AppCredentials to use for OAuth. If None, the Bots credentials are used. + end_on_invalid_message (bool): (Optional) value indicating whether the OAuthPrompt should end upon + receiving an invalid message. Generally the OAuthPrompt will ignore incoming messages from the + user during the auth flow, if they are not related to the auth flow. This flag enables ending the + OAuthPrompt rather than ignoring the user's message. Typically, this flag will be set to 'true', + but is 'false' by default for backwards compatibility. """ self.connection_name = connection_name self.title = title self.text = text self.timeout = timeout self.oath_app_credentials = oauth_app_credentials + self.end_on_invalid_message = end_on_invalid_message diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt.py index 268ede9ab..cf0a4123d 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt.py @@ -211,10 +211,9 @@ async def on_prompt( :type turn_context: :class:`botbuilder.core.TurnContext` :param state: Contains state for the current instance of the prompt on the dialog stack :type state: :class:`Dict` - :param options: A prompt options object constructed from the options initially provided - in the call :meth:`DialogContext.prompt()` + :param options: A prompt options object constructed from:meth:`DialogContext.prompt()` :type options: :class:`PromptOptions` - :param is_retry: true if is the first time the user for input; otherwise, false + :param is_retry: Determines whether `prompt` or `retry_prompt` should be used :type is_retry: bool :return: A task representing the asynchronous operation. @@ -235,8 +234,7 @@ async def on_recognize( :type turn_context: :class:`botbuilder.core.TurnContext` :param state: Contains state for the current instance of the prompt on the dialog stack :type state: :class:`Dict` - :param options: A prompt options object constructed from the options initially provided - in the call to :meth:`DialogContext.prompt()` + :param options: A prompt options object constructed from :meth:`DialogContext.prompt()` :type options: :class:`PromptOptions` :return: A task representing the asynchronous operation. diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt_culture_models.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt_culture_models.py new file mode 100644 index 000000000..abb527e21 --- /dev/null +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt_culture_models.py @@ -0,0 +1,191 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List + +from recognizers_text import Culture + + +class PromptCultureModel: + """ + Culture model used in Choice and Confirm Prompts. + """ + + def __init__( + self, + locale: str, + separator: str, + inline_or: str, + inline_or_more: str, + yes_in_language: str, + no_in_language: str, + ): + """ + + :param locale: Culture Model's Locale. Example: "en-US". + :param separator: Culture Model's Inline Separator. Example: ", ". + :param inline_or: Culture Model's Inline Or. Example: " or ". + :param inline_or_more Culture Model's Inline Or More. Example: ", or ". + :param yes_in_language: Equivalent of "Yes" in Culture Model's Language. Example: "Yes". + :param no_in_language: Equivalent of "No" in Culture Model's Language. Example: "No". + """ + self.locale = locale + self.separator = separator + self.inline_or = inline_or + self.inline_or_more = inline_or_more + self.yes_in_language = yes_in_language + self.no_in_language = no_in_language + + +class PromptCultureModels: + """ + Class container for currently-supported Culture Models in Confirm and Choice Prompt. + """ + + Chinese = PromptCultureModel( + locale=Culture.Chinese, + inline_or=" 要么 ", + inline_or_more=", 要么 ", + separator=", ", + no_in_language="不", + yes_in_language="是的", + ) + + Dutch = PromptCultureModel( + locale=Culture.Dutch, + inline_or=" of ", + inline_or_more=", of ", + separator=", ", + no_in_language="Nee", + yes_in_language="Ja", + ) + + English = PromptCultureModel( + locale=Culture.English, + inline_or=" or ", + inline_or_more=", or ", + separator=", ", + no_in_language="No", + yes_in_language="Yes", + ) + + French = PromptCultureModel( + locale=Culture.French, + inline_or=" ou ", + inline_or_more=", ou ", + separator=", ", + no_in_language="Non", + yes_in_language="Oui", + ) + + German = PromptCultureModel( + # TODO: Replace with Culture.German after Recognizers-Text package updates. + locale="de-de", + inline_or=" oder ", + inline_or_more=", oder ", + separator=", ", + no_in_language="Nein", + yes_in_language="Ja", + ) + + Italian = PromptCultureModel( + locale=Culture.Italian, + inline_or=" o ", + inline_or_more=" o ", + separator=", ", + no_in_language="No", + yes_in_language="Si", + ) + + Japanese = PromptCultureModel( + locale=Culture.Japanese, + inline_or=" または ", + inline_or_more="、 または ", + separator="、 ", + no_in_language="いいえ", + yes_in_language="はい", + ) + + Korean = PromptCultureModel( + locale=Culture.Korean, + inline_or=" 또는 ", + inline_or_more=" 또는 ", + separator=", ", + no_in_language="아니", + yes_in_language="예", + ) + + Portuguese = PromptCultureModel( + locale=Culture.Portuguese, + inline_or=" ou ", + inline_or_more=", ou ", + separator=", ", + no_in_language="Não", + yes_in_language="Sim", + ) + + Spanish = PromptCultureModel( + locale=Culture.Spanish, + inline_or=" o ", + inline_or_more=", o ", + separator=", ", + no_in_language="No", + yes_in_language="Sí", + ) + + Turkish = PromptCultureModel( + locale=Culture.Turkish, + inline_or=" veya ", + inline_or_more=" veya ", + separator=", ", + no_in_language="Hayır", + yes_in_language="Evet", + ) + + @classmethod + def map_to_nearest_language(cls, culture_code: str) -> str: + """ + Normalize various potential locale strings to a standard. + :param culture_code: Represents locale. Examples: "en-US, en-us, EN". + :return: Normalized locale. + :rtype: str + + .. remarks:: + In our other SDKs, this method is a copy/paste of the ones from the Recognizers-Text library. + However, that doesn't exist in Python. + """ + if culture_code: + culture_code = culture_code.lower() + supported_culture_codes = cls._get_supported_locales() + + if culture_code not in supported_culture_codes: + culture_prefix = culture_code.split("-")[0] + + for supported_culture_code in supported_culture_codes: + if supported_culture_code.startswith(culture_prefix): + culture_code = supported_culture_code + + return culture_code + + @classmethod + def get_supported_cultures(cls) -> List[PromptCultureModel]: + """ + Gets a list of the supported culture models. + """ + return [ + cls.Chinese, + cls.German, + cls.Dutch, + cls.English, + cls.French, + cls.Italian, + cls.Japanese, + cls.Korean, + cls.Portuguese, + cls.Spanish, + cls.Turkish, + ] + + @classmethod + def _get_supported_locales(cls) -> List[str]: + return [c.locale for c in cls.get_supported_cultures()] diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt_recognizer_result.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt_recognizer_result.py index aa90d7704..9d4f6b1ee 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt_recognizer_result.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt_recognizer_result.py @@ -7,7 +7,6 @@ class PromptRecognizerResult: def __init__(self, succeeded: bool = False, value: object = None): - """Creates result returned by a prompts recognizer function. - """ + """Creates result returned by a prompts recognizer function.""" self.succeeded = succeeded self.value = value diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/begin_skill_dialog_options.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/begin_skill_dialog_options.py index 62a02ab2e..a9d21ca3f 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/begin_skill_dialog_options.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/begin_skill_dialog_options.py @@ -5,7 +5,7 @@ class BeginSkillDialogOptions: - def __init__(self, activity: Activity): # pylint: disable=unused-argument + def __init__(self, activity: Activity): self.activity = activity @staticmethod @@ -14,5 +14,4 @@ def from_object(obj: object) -> "BeginSkillDialogOptions": return BeginSkillDialogOptions(obj["activity"]) if hasattr(obj, "activity"): return BeginSkillDialogOptions(obj.activity) - return None diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/skill_dialog.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/skill_dialog.py index 58c3857e0..d848c13c7 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/skill_dialog.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/skill_dialog.py @@ -4,13 +4,18 @@ from copy import deepcopy from typing import List -from botbuilder.schema import Activity, ActivityTypes, ExpectedReplies, DeliveryModes -from botbuilder.core import ( - BotAdapter, - TurnContext, +from botframework.connector.token_api.models import TokenExchangeRequest +from botbuilder.schema import ( + Activity, + ActivityTypes, + ExpectedReplies, + DeliveryModes, + SignInConstants, + TokenExchangeInvokeRequest, ) +from botbuilder.core import BotAdapter, TurnContext, ExtendedUserTokenProvider +from botbuilder.core.card_factory import ContentTypes from botbuilder.core.skills import SkillConversationIdFactoryOptions - from botbuilder.dialogs import ( Dialog, DialogContext, @@ -21,9 +26,15 @@ from .begin_skill_dialog_options import BeginSkillDialogOptions from .skill_dialog_options import SkillDialogOptions +from botbuilder.dialogs.prompts import OAuthPromptSettings +from .._user_token_access import _UserTokenAccess class SkillDialog(Dialog): + SKILLCONVERSATIONIDSTATEKEY = ( + "Microsoft.Bot.Builder.Dialogs.SkillDialog.SkillConversationId" + ) + def __init__(self, dialog_options: SkillDialogOptions, dialog_id: str): super().__init__(dialog_id) if not dialog_options: @@ -38,12 +49,7 @@ async def begin_dialog(self, dialog_context: DialogContext, options: object = No :param dialog_context: The dialog context for the current turn of conversation. :param options: (Optional) additional argument(s) to pass to the dialog being started. """ - dialog_args = SkillDialog._validate_begin_dialog_args(options) - - await dialog_context.context.send_trace_activity( - f"{SkillDialog.__name__}.BeginDialogAsync()", - label=f"Using activity of type: {dialog_args.activity.type}", - ) + dialog_args = self._validate_begin_dialog_args(options) # Create deep clone of the original activity to avoid altering it before forwarding it. skill_activity: Activity = deepcopy(dialog_args.activity) @@ -55,52 +61,55 @@ async def begin_dialog(self, dialog_context: DialogContext, options: object = No is_incoming=True, ) - dialog_context.active_dialog.state[ - self._deliver_mode_state_key - ] = dialog_args.activity.delivery_mode + # Store delivery mode in dialog state for later use. + dialog_context.active_dialog.state[self._deliver_mode_state_key] = ( + dialog_args.activity.delivery_mode + ) + + # Create the conversationId and store it in the dialog context state so we can use it later + skill_conversation_id = await self._create_skill_conversation_id( + dialog_context.context, dialog_context.context.activity + ) + dialog_context.active_dialog.state[SkillDialog.SKILLCONVERSATIONIDSTATEKEY] = ( + skill_conversation_id + ) # Send the activity to the skill. - eoc_activity = await self._send_to_skill(dialog_context.context, skill_activity) + eoc_activity = await self._send_to_skill( + dialog_context.context, skill_activity, skill_conversation_id + ) if eoc_activity: return await dialog_context.end_dialog(eoc_activity.value) return self.end_of_turn async def continue_dialog(self, dialog_context: DialogContext): - await dialog_context.context.send_trace_activity( - f"{SkillDialog.__name__}.continue_dialog()", - label=f"ActivityType: {dialog_context.context.activity.type}", - ) + if not self._on_validate_activity(dialog_context.context.activity): + return self.end_of_turn # Handle EndOfConversation from the skill (this will be sent to the this dialog by the SkillHandler if # received from the Skill) if dialog_context.context.activity.type == ActivityTypes.end_of_conversation: - await dialog_context.context.send_trace_activity( - f"{SkillDialog.__name__}.continue_dialog()", - label=f"Got {ActivityTypes.end_of_conversation}", - ) - return await dialog_context.end_dialog( dialog_context.context.activity.value ) - # Forward only Message and Event activities to the skill - if ( - dialog_context.context.activity.type == ActivityTypes.message - or dialog_context.context.activity.type == ActivityTypes.event - ): - # Create deep clone of the original activity to avoid altering it before forwarding it. - skill_activity = deepcopy(dialog_context.context.activity) - skill_activity.delivery_mode = dialog_context.active_dialog.state[ - self._deliver_mode_state_key - ] + # Create deep clone of the original activity to avoid altering it before forwarding it. + skill_activity = deepcopy(dialog_context.context.activity) - # Just forward to the remote skill - eoc_activity = await self._send_to_skill( - dialog_context.context, skill_activity - ) - if eoc_activity: - return await dialog_context.end_dialog(eoc_activity.value) + skill_activity.delivery_mode = dialog_context.active_dialog.state[ + self._deliver_mode_state_key + ] + + # Just forward to the remote skill + skill_conversation_id = dialog_context.active_dialog.state[ + SkillDialog.SKILLCONVERSATIONIDSTATEKEY + ] + eoc_activity = await self._send_to_skill( + dialog_context.context, skill_activity, skill_conversation_id + ) + if eoc_activity: + return await dialog_context.end_dialog(eoc_activity.value) return self.end_of_turn @@ -119,7 +128,9 @@ async def reprompt_dialog( # pylint: disable=unused-argument is_incoming=True, ) - await self._send_to_skill(context, reprompt_event) + # connection Name is not applicable for a RePrompt, as we don't expect as OAuthCard in response. + skill_conversation_id = instance.state[SkillDialog.SKILLCONVERSATIONIDSTATEKEY] + await self._send_to_skill(context, reprompt_event, skill_conversation_id) async def resume_dialog( # pylint: disable=unused-argument self, dialog_context: "DialogContext", reason: DialogReason, result: object @@ -132,10 +143,6 @@ async def end_dialog( ): # Send of of conversation to the skill if the dialog has been cancelled. if reason in (DialogReason.CancelCalled, DialogReason.ReplaceCalled): - await context.send_trace_activity( - f"{SkillDialog.__name__}.end_dialog()", - label=f"ActivityType: {context.activity.type}", - ) activity = Activity(type=ActivityTypes.end_of_conversation) # Apply conversation reference and common properties from incoming activity before sending. @@ -147,12 +154,15 @@ async def end_dialog( activity.channel_data = context.activity.channel_data activity.additional_properties = context.activity.additional_properties - await self._send_to_skill(context, activity) + # connection Name is not applicable for an EndDialog, as we don't expect as OAuthCard in response. + skill_conversation_id = instance.state[ + SkillDialog.SKILLCONVERSATIONIDSTATEKEY + ] + await self._send_to_skill(context, activity, skill_conversation_id) await super().end_dialog(context, instance, reason) - @staticmethod - def _validate_begin_dialog_args(options: object) -> BeginSkillDialogOptions: + def _validate_begin_dialog_args(self, options: object) -> BeginSkillDialogOptions: if not options: raise TypeError("options cannot be None.") @@ -168,38 +178,34 @@ def _validate_begin_dialog_args(options: object) -> BeginSkillDialogOptions: "SkillDialog: activity object in options as BeginSkillDialogOptions cannot be None." ) - # Only accept Message or Event activities - if ( - dialog_args.activity.type != ActivityTypes.message - and dialog_args.activity.type != ActivityTypes.event - ): - raise TypeError( - f"Only {ActivityTypes.message} and {ActivityTypes.event} activities are supported." - f" Received activity of type {dialog_args.activity.type}." - ) - return dialog_args + def _on_validate_activity( + self, activity: Activity # pylint: disable=unused-argument + ) -> bool: + """ + Validates the activity sent during continue_dialog. + + Override this method to implement a custom validator for the activity being sent during continue_dialog. + This method can be used to ignore activities of a certain type if needed. + If this method returns false, the dialog will end the turn without processing the activity. + """ + return True + async def _send_to_skill( - self, context: TurnContext, activity: Activity, + self, context: TurnContext, activity: Activity, skill_conversation_id: str ) -> Activity: - # Create a conversationId to interact with the skill and send the activity - conversation_id_factory_options = SkillConversationIdFactoryOptions( - from_bot_oauth_scope=context.turn_state.get(BotAdapter.BOT_OAUTH_SCOPE_KEY), - from_bot_id=self.dialog_options.bot_id, - activity=activity, - bot_framework_skill=self.dialog_options.skill, - ) - - skill_conversation_id = await self.dialog_options.conversation_id_factory.create_skill_conversation_id( - conversation_id_factory_options - ) + if activity.type == ActivityTypes.invoke: + # Force ExpectReplies for invoke activities so we can get the replies right away and send + # them back to the channel if needed. This makes sure that the dialog will receive the Invoke + # response from the skill and any other activities sent, including EoC. + activity.delivery_mode = DeliveryModes.expect_replies # Always save state before forwarding # (the dialog stack won't get updated with the skillDialog and things won't work if you don't) - skill_info = self.dialog_options.skill await self.dialog_options.conversation_state.save_changes(context, True) + skill_info = self.dialog_options.skill response = await self.dialog_options.skill_client.post_activity( self.dialog_options.bot_id, skill_info.app_id, @@ -221,13 +227,132 @@ async def _send_to_skill( # Process replies in the response.Body. response.body: List[Activity] response.body = ExpectedReplies().deserialize(response.body).activities + # Track sent invoke responses, so more than one is not sent. + sent_invoke_response = False for from_skill_activity in response.body: if from_skill_activity.type == ActivityTypes.end_of_conversation: # Capture the EndOfConversation activity if it was sent from skill eoc_activity = from_skill_activity + + # The conversation has ended, so cleanup the conversation id + await self.dialog_options.conversation_id_factory.delete_conversation_reference( + skill_conversation_id + ) + elif not sent_invoke_response and await self._intercept_oauth_cards( + context, from_skill_activity, self.dialog_options.connection_name + ): + # Token exchange succeeded, so no oauthcard needs to be shown to the user + sent_invoke_response = True else: + # If an invoke response has already been sent we should ignore future invoke responses as this + # represents a bug in the skill. + if from_skill_activity.type == ActivityTypes.invoke_response: + if sent_invoke_response: + continue + sent_invoke_response = True # Send the response back to the channel. await context.send_activity(from_skill_activity) return eoc_activity + + async def _create_skill_conversation_id( + self, context: TurnContext, activity: Activity + ) -> str: + # Create a conversationId to interact with the skill and send the activity + conversation_id_factory_options = SkillConversationIdFactoryOptions( + from_bot_oauth_scope=context.turn_state.get(BotAdapter.BOT_OAUTH_SCOPE_KEY), + from_bot_id=self.dialog_options.bot_id, + activity=activity, + bot_framework_skill=self.dialog_options.skill, + ) + skill_conversation_id = await self.dialog_options.conversation_id_factory.create_skill_conversation_id( + conversation_id_factory_options + ) + return skill_conversation_id + + async def _intercept_oauth_cards( + self, context: TurnContext, activity: Activity, connection_name: str + ): + """ + Tells is if we should intercept the OAuthCard message. + """ + if not connection_name or connection_name.isspace(): + # The adapter may choose not to support token exchange, in which case we fallback to + # showing an oauth card to the user. + return False + + oauth_card_attachment = next( + ( + attachment + for attachment in activity.attachments + if attachment.content_type == ContentTypes.oauth_card + ), + None, + ) + if oauth_card_attachment is None: + return False + + oauth_card = oauth_card_attachment.content + if ( + not oauth_card + or not oauth_card.token_exchange_resource + or not oauth_card.token_exchange_resource.uri + ): + return False + + try: + settings = OAuthPromptSettings( + connection_name=connection_name, title="Sign In" + ) + result = await _UserTokenAccess.exchange_token( + context, + settings, + TokenExchangeRequest(uri=oauth_card.token_exchange_resource.uri), + ) + + if not result or not result.token: + # If token above is null, then SSO has failed and hence we return false. + return False + + # If not, send an invoke to the skill with the token. + return await self._send_token_exchange_invoke_to_skill( + activity, + oauth_card.token_exchange_resource.id, + oauth_card.connection_name, + result.token, + ) + except: + # Failures in token exchange are not fatal. They simply mean that the user needs + # to be shown the OAuth card. + return False + + async def _send_token_exchange_invoke_to_skill( + self, + incoming_activity: Activity, + request_id: str, + connection_name: str, + token: str, + ): + activity = incoming_activity.create_reply() + activity.type = ActivityTypes.invoke + activity.name = SignInConstants.token_exchange_operation_name + activity.value = TokenExchangeInvokeRequest( + id=request_id, + token=token, + connection_name=connection_name, + ) + + # route the activity to the skill + skill_info = self.dialog_options.skill + response = await self.dialog_options.skill_client.post_activity( + self.dialog_options.bot_id, + skill_info.app_id, + skill_info.skill_endpoint, + self.dialog_options.skill_host_endpoint, + incoming_activity.conversation.id, + activity, + ) + + # Check response status: true if success, false if failure + return response.is_successful_status_code() diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/skill_dialog_options.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/skill_dialog_options.py index 53d56f72e..028490a40 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/skill_dialog_options.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/skills/skill_dialog_options.py @@ -18,6 +18,7 @@ def __init__( skill: BotFrameworkSkill = None, conversation_id_factory: ConversationIdFactoryBase = None, conversation_state: ConversationState = None, + connection_name: str = None, ): self.bot_id = bot_id self.skill_client = skill_client @@ -25,3 +26,4 @@ def __init__( self.skill = skill self.conversation_id_factory = conversation_id_factory self.conversation_state = conversation_state + self.connection_name = connection_name diff --git a/libraries/botbuilder-dialogs/botbuilder/dialogs/waterfall_dialog.py b/libraries/botbuilder-dialogs/botbuilder/dialogs/waterfall_dialog.py index bced214fb..02dfbbbe3 100644 --- a/libraries/botbuilder-dialogs/botbuilder/dialogs/waterfall_dialog.py +++ b/libraries/botbuilder-dialogs/botbuilder/dialogs/waterfall_dialog.py @@ -44,7 +44,6 @@ def add_step(self, step): async def begin_dialog( self, dialog_context: DialogContext, options: object = None ) -> DialogTurnResult: - if not dialog_context: raise TypeError("WaterfallDialog.begin_dialog(): dc cannot be None.") @@ -113,7 +112,6 @@ async def end_dialog( # pylint: disable=unused-argument self.telemetry_client.track_event("WaterfallCancel", properties) else: if reason is DialogReason.EndCalled: - instance_id = str(instance.state[self.PersistedInstanceId]) properties = {"DialogId": self.id, "InstanceId": instance_id} self.telemetry_client.track_event("WaterfallComplete", properties) @@ -164,7 +162,7 @@ def get_step_name(self, index: int) -> str: """ step_name = self._steps[index].__qualname__ - if not step_name or ">" in step_name: + if not step_name or step_name.endswith(""): step_name = f"Step{index + 1}of{len(self._steps)}" return step_name diff --git a/libraries/botbuilder-dialogs/requirements.txt b/libraries/botbuilder-dialogs/requirements.txt index fec9928c2..d8f2cb4f2 100644 --- a/libraries/botbuilder-dialogs/requirements.txt +++ b/libraries/botbuilder-dialogs/requirements.txt @@ -1,8 +1,8 @@ -msrest==0.6.10 -botframework-connector>=4.7.1 -botbuilder-schema>=4.7.1 -botbuilder-core>=4.7.1 -requests==2.22.0 -PyJWT==1.5.3 -cryptography==2.8 -aiounittest==1.3.0 \ No newline at end of file +msrest== 0.7.* +botframework-connector==4.17.0 +botbuilder-schema==4.17.0 +botbuilder-core==4.17.0 +requests==2.32.0 +PyJWT==2.4.0 +cryptography==43.0.1 +aiounittest==1.3.0 diff --git a/libraries/botbuilder-dialogs/setup.py b/libraries/botbuilder-dialogs/setup.py index f242baec4..8cedaa53c 100644 --- a/libraries/botbuilder-dialogs/setup.py +++ b/libraries/botbuilder-dialogs/setup.py @@ -5,16 +5,17 @@ from setuptools import setup REQUIRES = [ - "regex<=2019.08.19", + "regex>=2022.1.18", + "emoji==1.7.0", "recognizers-text-date-time>=1.0.2a1", "recognizers-text-number-with-unit>=1.0.2a1", "recognizers-text-number>=1.0.2a1", "recognizers-text>=1.0.2a1", "recognizers-text-choice>=1.0.2a1", - "babel==2.7.0", - "botbuilder-schema>=4.7.1", - "botframework-connector>=4.7.1", - "botbuilder-core>=4.7.1", + "babel==2.9.1", + "botbuilder-schema==4.17.0", + "botframework-connector==4.17.0", + "botbuilder-core==4.17.0", ] TEST_REQUIRES = ["aiounittest==1.3.0"] @@ -44,6 +45,9 @@ "botbuilder.dialogs.prompts", "botbuilder.dialogs.choices", "botbuilder.dialogs.skills", + "botbuilder.dialogs.memory", + "botbuilder.dialogs.memory.path_resolvers", + "botbuilder.dialogs.memory.scopes", ], install_requires=REQUIRES + TEST_REQUIRES, tests_require=TEST_REQUIRES, diff --git a/libraries/botbuilder-dialogs/tests/choices/test_channel.py b/libraries/botbuilder-dialogs/tests/choices/test_channel.py index 2285e1fe0..23d26ac4a 100644 --- a/libraries/botbuilder-dialogs/tests/choices/test_channel.py +++ b/libraries/botbuilder-dialogs/tests/choices/test_channel.py @@ -25,6 +25,7 @@ def test_supports_suggested_actions_many(self): (Channels.kik, 21, False), (Channels.emulator, 100, True), (Channels.emulator, 101, False), + (Channels.direct_line_speech, 100, True), ] for channel, button_cnt, expected in supports_suggested_actions_data: @@ -38,10 +39,10 @@ def test_supports_card_actions_many(self): supports_card_action_data: List[Tuple[Channels, int, bool]] = [ (Channels.line, 99, True), (Channels.line, 100, False), - (Channels.cortana, 100, True), (Channels.slack, 100, True), (Channels.skype, 3, True), (Channels.skype, 5, False), + (Channels.direct_line_speech, 99, True), ] for channel, button_cnt, expected in supports_card_action_data: @@ -51,10 +52,6 @@ def test_supports_card_actions_many(self): actual = Channel.supports_card_actions(channel, button_cnt) self.assertEqual(expected, actual) - def test_should_return_false_for_has_message_feed_with_cortana(self): - supports = Channel.has_message_feed(Channels.cortana) - self.assertFalse(supports) - def test_should_return_channel_id_from_context_activity(self): test_activity = Activity(channel_id=Channels.facebook) test_context = TurnContext(BotFrameworkAdapter(settings=None), test_activity) diff --git a/libraries/botbuilder-dialogs/tests/choices/test_choice_factory.py b/libraries/botbuilder-dialogs/tests/choices/test_choice_factory.py index 1195fde21..624930ff7 100644 --- a/libraries/botbuilder-dialogs/tests/choices/test_choice_factory.py +++ b/libraries/botbuilder-dialogs/tests/choices/test_choice_factory.py @@ -139,37 +139,6 @@ def test_should_automatically_choose_render_style_based_on_channel_type(self): self.assertEqual(expected, activity) - def test_should_choose_correct_styles_for_cortana(self): - expected = Activity( - type=ActivityTypes.message, - input_hint=InputHints.expecting_input, - attachment_layout=AttachmentLayoutTypes.list, - attachments=[ - Attachment( - content=HeroCard( - text="select from:", - buttons=[ - CardAction( - type=ActionTypes.im_back, value="red", title="red" - ), - CardAction( - type=ActionTypes.im_back, value="green", title="green" - ), - CardAction( - type=ActionTypes.im_back, value="blue", title="blue" - ), - ], - ), - content_type="application/vnd.microsoft.card.hero", - ) - ], - ) - - activity = ChoiceFactory.for_channel( - Channels.cortana, ChoiceFactoryTest.color_choices, "select from:" - ) - self.assertEqual(expected, activity) - def test_should_choose_correct_styles_for_teams(self): expected = Activity( type=ActivityTypes.message, diff --git a/libraries/botbuilder-dialogs/tests/choices/test_choice_recognizers.py b/libraries/botbuilder-dialogs/tests/choices/test_choice_recognizers.py index c37243fd1..ac202d044 100644 --- a/libraries/botbuilder-dialogs/tests/choices/test_choice_recognizers.py +++ b/libraries/botbuilder-dialogs/tests/choices/test_choice_recognizers.py @@ -57,10 +57,9 @@ def assert_choice(result, value, index, score, synonym=None): resolution.score == score ), f"Invalid resolution.score of '{resolution.score}' for '{value}' choice." if synonym: - assert ( # pylint: disable=assert-on-tuple - resolution.synonym == synonym, - f"Invalid resolution.synonym of '{resolution.synonym}' for '{value}' choice.", - ) + assert ( + resolution.synonym == synonym + ), f"Invalid resolution.synonym of '{resolution.synonym}' for '{value}' choice." _color_choices: List[str] = ["red", "green", "blue"] diff --git a/libraries/botbuilder-dialogs/tests/memory/scopes/test_memory_scopes.py b/libraries/botbuilder-dialogs/tests/memory/scopes/test_memory_scopes.py new file mode 100644 index 000000000..d7b305358 --- /dev/null +++ b/libraries/botbuilder-dialogs/tests/memory/scopes/test_memory_scopes.py @@ -0,0 +1,567 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +# pylint: disable=pointless-string-statement + +from collections import namedtuple + +import aiounittest + +from botbuilder.core import ConversationState, MemoryStorage, TurnContext, UserState +from botbuilder.core.adapters import TestAdapter +from botbuilder.dialogs import ( + Dialog, + DialogContext, + DialogContainer, + DialogInstance, + DialogSet, + DialogState, + ObjectPath, +) +from botbuilder.dialogs.memory.scopes import ( + ClassMemoryScope, + ConversationMemoryScope, + DialogMemoryScope, + UserMemoryScope, + SettingsMemoryScope, + ThisMemoryScope, + TurnMemoryScope, +) +from botbuilder.schema import ( + Activity, + ActivityTypes, + ChannelAccount, + ConversationAccount, +) +from botframework.connector import Channels + + +class TestDialog(Dialog): + def __init__(self, id: str, message: str): + super().__init__(id) + + def aux_try_get_value(state): # pylint: disable=unused-argument + return "resolved value" + + ExpressionObject = namedtuple("ExpressionObject", "try_get_value") + self.message = message + self.expression = ExpressionObject(aux_try_get_value) + + async def begin_dialog(self, dialog_context: DialogContext, options: object = None): + dialog_context.active_dialog.state["is_dialog"] = True + await dialog_context.context.send_activity(self.message) + return Dialog.end_of_turn + + +class TestContainer(DialogContainer): + def __init__(self, id: str, child: Dialog = None): + super().__init__(id) + self.child_id = None + if child: + self.dialogs.add(child) + self.child_id = child.id + + async def begin_dialog(self, dialog_context: DialogContext, options: object = None): + state = dialog_context.active_dialog.state + state["is_container"] = True + if self.child_id: + state["dialog"] = DialogState() + child_dc = self.create_child_context(dialog_context) + return await child_dc.begin_dialog(self.child_id, options) + + return Dialog.end_of_turn + + async def continue_dialog(self, dialog_context: DialogContext): + child_dc = self.create_child_context(dialog_context) + if child_dc: + return await child_dc.continue_dialog() + + return Dialog.end_of_turn + + def create_child_context(self, dialog_context: DialogContext): + state = dialog_context.active_dialog.state + if state["dialog"] is not None: + child_dc = DialogContext( + self.dialogs, dialog_context.context, state["dialog"] + ) + child_dc.parent = dialog_context + return child_dc + + return None + + +class MemoryScopesTests(aiounittest.AsyncTestCase): + begin_message = Activity( + text="begin", + type=ActivityTypes.message, + channel_id=Channels.test, + from_property=ChannelAccount(id="user"), + recipient=ChannelAccount(id="bot"), + conversation=ConversationAccount(id="convo1"), + ) + + async def test_class_memory_scope_should_find_registered_dialog(self): + # Create ConversationState with MemoryStorage and register the state as middleware. + conversation_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet and register the dialogs. + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + await dialog_state.set( + context, DialogState(stack=[DialogInstance(id="test", state={})]) + ) + + dialog_context = await dialogs.create_context(context) + + # Run test + scope = ClassMemoryScope() + memory = scope.get_memory(dialog_context) + self.assertTrue(memory, "memory not returned") + self.assertEqual("test message", memory.message) + self.assertEqual("resolved value", memory.expression) + + async def test_class_memory_scope_should_not_allow_set_memory_call(self): + # Create ConversationState with MemoryStorage and register the state as middleware. + conversation_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet and register the dialogs. + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + await dialog_state.set( + context, DialogState(stack=[DialogInstance(id="test", state={})]) + ) + + dialog_context = await dialogs.create_context(context) + + # Run test + scope = ClassMemoryScope() + with self.assertRaises(Exception) as context: + scope.set_memory(dialog_context, {}) + + self.assertTrue("not supported" in str(context.exception)) + + async def test_class_memory_scope_should_not_allow_load_and_save_changes_calls( + self, + ): + # Create ConversationState with MemoryStorage and register the state as middleware. + conversation_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet and register the dialogs. + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + await dialog_state.set( + context, DialogState(stack=[DialogInstance(id="test", state={})]) + ) + + dialog_context = await dialogs.create_context(context) + + # Run test + scope = ClassMemoryScope() + await scope.load(dialog_context) + memory = scope.get_memory(dialog_context) + with self.assertRaises(AttributeError) as context: + memory.message = "foo" + + self.assertTrue("can't set attribute" in str(context.exception)) + await scope.save_changes(dialog_context) + self.assertEqual("test message", dialog.message) + + async def test_conversation_memory_scope_should_return_conversation_state(self): + # Create ConversationState with MemoryStorage and register the state as middleware. + conversation_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet and register the dialogs. + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + context.turn_state["ConversationState"] = conversation_state + + dialog_context = await dialogs.create_context(context) + + # Initialize conversation state + foo_cls = namedtuple("TestObject", "foo") + conversation_prop = conversation_state.create_property("conversation") + await conversation_prop.set(context, foo_cls(foo="bar")) + await conversation_state.save_changes(context) + + # Run test + scope = ConversationMemoryScope() + memory = scope.get_memory(dialog_context) + self.assertTrue(memory, "memory not returned") + + # TODO: Make get_path_value take conversation.foo + test_obj = ObjectPath.get_path_value(memory, "conversation") + self.assertEqual("bar", test_obj.foo) + + async def test_user_memory_scope_should_not_return_state_if_not_loaded(self): + # Initialize user state + storage = MemoryStorage() + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + user_state = UserState(storage) + context.turn_state["UserState"] = user_state + foo_cls = namedtuple("TestObject", "foo") + user_prop = user_state.create_property("conversation") + await user_prop.set(context, foo_cls(foo="bar")) + await user_state.save_changes(context) + + # Replace context and user_state with new instances + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + user_state = UserState(storage) + context.turn_state["UserState"] = user_state + + # Create a DialogState property, DialogSet and register the dialogs. + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + dialog_context = await dialogs.create_context(context) + + # Run test + scope = UserMemoryScope() + memory = scope.get_memory(dialog_context) + self.assertIsNone(memory, "state returned") + + async def test_user_memory_scope_should_return_state_once_loaded(self): + # Initialize user state + storage = MemoryStorage() + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + user_state = UserState(storage) + context.turn_state["UserState"] = user_state + foo_cls = namedtuple("TestObject", "foo") + user_prop = user_state.create_property("conversation") + await user_prop.set(context, foo_cls(foo="bar")) + await user_state.save_changes(context) + + # Replace context and conversation_state with instances + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + user_state = UserState(storage) + context.turn_state["UserState"] = user_state + + # Create a DialogState property, DialogSet and register the dialogs. + conversation_state = ConversationState(storage) + context.turn_state["ConversationState"] = conversation_state + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + dialog_context = await dialogs.create_context(context) + + # Run test + scope = UserMemoryScope() + memory = scope.get_memory(dialog_context) + self.assertIsNone(memory, "state returned") + + await scope.load(dialog_context) + memory = scope.get_memory(dialog_context) + self.assertIsNotNone(memory, "state not returned") + + # TODO: Make get_path_value take conversation.foo + test_obj = ObjectPath.get_path_value(memory, "conversation") + self.assertEqual("bar", test_obj.foo) + + async def test_dialog_memory_scope_should_return_containers_state(self): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + container = TestContainer("container") + dialogs.add(container) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + scope = DialogMemoryScope() + await dialog_context.begin_dialog("container") + memory = scope.get_memory(dialog_context) + self.assertIsNotNone(memory, "state not returned") + self.assertTrue(memory["is_container"]) + + async def test_dialog_memory_scope_should_return_parent_containers_state_for_children( + self, + ): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + container = TestContainer("container", TestDialog("child", "test message")) + dialogs.add(container) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + scope = DialogMemoryScope() + await dialog_context.begin_dialog("container") + child_dc = dialog_context.child + self.assertIsNotNone(child_dc, "No child DC") + memory = scope.get_memory(child_dc) + self.assertIsNotNone(memory, "state not returned") + self.assertTrue(memory["is_container"]) + + async def test_dialog_memory_scope_should_return_childs_state_when_no_parent(self): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + scope = DialogMemoryScope() + await dialog_context.begin_dialog("test") + memory = scope.get_memory(dialog_context) + self.assertIsNotNone(memory, "state not returned") + self.assertTrue(memory["is_dialog"]) + + async def test_dialog_memory_scope_should_overwrite_parents_memory(self): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + container = TestContainer("container", TestDialog("child", "test message")) + dialogs.add(container) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + scope = DialogMemoryScope() + await dialog_context.begin_dialog("container") + child_dc = dialog_context.child + self.assertIsNotNone(child_dc, "No child DC") + + foo_cls = namedtuple("TestObject", "foo") + scope.set_memory(child_dc, foo_cls("bar")) + memory = scope.get_memory(child_dc) + self.assertIsNotNone(memory, "state not returned") + self.assertEqual(memory.foo, "bar") + + async def test_dialog_memory_scope_should_overwrite_active_dialogs_memory(self): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + container = TestContainer("container") + dialogs.add(container) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + scope = DialogMemoryScope() + await dialog_context.begin_dialog("container") + foo_cls = namedtuple("TestObject", "foo") + scope.set_memory(dialog_context, foo_cls("bar")) + memory = scope.get_memory(dialog_context) + self.assertIsNotNone(memory, "state not returned") + self.assertEqual(memory.foo, "bar") + + async def test_dialog_memory_scope_should_raise_error_if_set_memory_called_without_memory( + self, + ): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + container = TestContainer("container") + dialogs.add(container) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + with self.assertRaises(Exception): + scope = DialogMemoryScope() + await dialog_context.begin_dialog("container") + scope.set_memory(dialog_context, None) + + async def test_settings_memory_scope_should_return_content_of_settings(self): + # pylint: disable=import-outside-toplevel + from test_settings import DefaultConfig + + # Create a DialogState property, DialogSet and register the dialogs. + conversation_state = ConversationState(MemoryStorage()) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state).add(TestDialog("test", "test message")) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + settings = DefaultConfig() + dialog_context.context.turn_state["settings"] = settings + + # Run test + scope = SettingsMemoryScope() + memory = scope.get_memory(dialog_context) + self.assertIsNotNone(memory) + self.assertEqual(memory.STRING, "test") + self.assertEqual(memory.INT, 3) + self.assertEqual(memory.LIST[0], "zero") + self.assertEqual(memory.LIST[1], "one") + self.assertEqual(memory.LIST[2], "two") + self.assertEqual(memory.LIST[3], "three") + + async def test_this_memory_scope_should_return_active_dialogs_state(self): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + scope = ThisMemoryScope() + await dialog_context.begin_dialog("test") + memory = scope.get_memory(dialog_context) + self.assertIsNotNone(memory, "state not returned") + self.assertTrue(memory["is_dialog"]) + + async def test_this_memory_scope_should_overwrite_active_dialogs_memory(self): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + container = TestContainer("container") + dialogs.add(container) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + scope = ThisMemoryScope() + await dialog_context.begin_dialog("container") + foo_cls = namedtuple("TestObject", "foo") + scope.set_memory(dialog_context, foo_cls("bar")) + memory = scope.get_memory(dialog_context) + self.assertIsNotNone(memory, "state not returned") + self.assertEqual(memory.foo, "bar") + + async def test_this_memory_scope_should_raise_error_if_set_memory_called_without_memory( + self, + ): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + container = TestContainer("container") + dialogs.add(container) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + with self.assertRaises(Exception): + scope = ThisMemoryScope() + await dialog_context.begin_dialog("container") + scope.set_memory(dialog_context, None) + + async def test_this_memory_scope_should_raise_error_if_set_memory_called_without_active_dialog( + self, + ): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + container = TestContainer("container") + dialogs.add(container) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + with self.assertRaises(Exception): + scope = ThisMemoryScope() + foo_cls = namedtuple("TestObject", "foo") + scope.set_memory(dialog_context, foo_cls("bar")) + + async def test_turn_memory_scope_should_persist_changes_to_turn_state(self): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + scope = TurnMemoryScope() + memory = scope.get_memory(dialog_context) + self.assertIsNotNone(memory, "state not returned") + memory["foo"] = "bar" + memory = scope.get_memory(dialog_context) + self.assertEqual(memory["foo"], "bar") + + async def test_turn_memory_scope_should_overwrite_values_in_turn_state(self): + # Create a DialogState property, DialogSet and register the dialogs. + storage = MemoryStorage() + conversation_state = ConversationState(storage) + dialog_state = conversation_state.create_property("dialogs") + dialogs = DialogSet(dialog_state) + dialog = TestDialog("test", "test message") + dialogs.add(dialog) + + # Create test context + context = TurnContext(TestAdapter(), MemoryScopesTests.begin_message) + dialog_context = await dialogs.create_context(context) + + # Run test + scope = TurnMemoryScope() + foo_cls = namedtuple("TestObject", "foo") + scope.set_memory(dialog_context, foo_cls("bar")) + memory = scope.get_memory(dialog_context) + self.assertIsNotNone(memory, "state not returned") + self.assertEqual(memory.foo, "bar") diff --git a/libraries/botbuilder-dialogs/tests/memory/scopes/test_settings.py b/libraries/botbuilder-dialogs/tests/memory/scopes/test_settings.py new file mode 100644 index 000000000..9e21a99a6 --- /dev/null +++ b/libraries/botbuilder-dialogs/tests/memory/scopes/test_settings.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os + + +class DefaultConfig: + """Bot Configuration""" + + STRING = os.environ.get("STRING", "test") + INT = os.environ.get("INT", 3) + LIST = os.environ.get("LIST", ["zero", "one", "two", "three"]) + NOT_TO_BE_OVERRIDDEN = os.environ.get("NOT_TO_BE_OVERRIDDEN", "one") + TO_BE_OVERRIDDEN = os.environ.get("TO_BE_OVERRIDDEN", "one") diff --git a/libraries/botbuilder-dialogs/tests/test_activity_prompt.py b/libraries/botbuilder-dialogs/tests/test_activity_prompt.py index ab8fa4971..2f2019c91 100644 --- a/libraries/botbuilder-dialogs/tests/test_activity_prompt.py +++ b/libraries/botbuilder-dialogs/tests/test_activity_prompt.py @@ -215,3 +215,91 @@ async def aux_validator(prompt_context: PromptValidatorContext): step1 = await adapter.send("hello") step2 = await step1.assert_reply("please send an event.") await step2.assert_reply("please send an event.") + + async def test_activity_prompt_onerror_should_return_dialogcontext(self): + # Create ConversationState with MemoryStorage and register the state as middleware. + convo_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet and AttachmentPrompt. + dialog_state = convo_state.create_property("dialog_state") + dialogs = DialogSet(dialog_state) + dialogs.add(SimpleActivityPrompt("EventActivityPrompt", validator)) + + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + options = PromptOptions( + prompt=Activity( + type=ActivityTypes.message, text="please send an event." + ) + ) + + try: + await dialog_context.prompt("EventActivityPrompt", options) + await dialog_context.prompt("Non existent id", options) + except Exception as err: + self.assertIsNotNone( + err.data["DialogContext"] # pylint: disable=no-member + ) + self.assertEqual( + err.data["DialogContext"][ # pylint: disable=no-member + "active_dialog" + ], + "EventActivityPrompt", + ) + else: + raise Exception("Should have thrown an error.") + + elif results.status == DialogTurnStatus.Complete: + await turn_context.send_activity(results.result) + + await convo_state.save_changes(turn_context) + + # Initialize TestAdapter. + adapter = TestAdapter(exec_test) + + await adapter.send("hello") + + async def test_activity_replace_dialog_onerror_should_return_dialogcontext(self): + # Create ConversationState with MemoryStorage and register the state as middleware. + convo_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet and AttachmentPrompt. + dialog_state = convo_state.create_property("dialog_state") + dialogs = DialogSet(dialog_state) + dialogs.add(SimpleActivityPrompt("EventActivityPrompt", validator)) + + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + options = PromptOptions( + prompt=Activity( + type=ActivityTypes.message, text="please send an event." + ) + ) + + try: + await dialog_context.prompt("EventActivityPrompt", options) + await dialog_context.replace_dialog("Non existent id", options) + except Exception as err: + self.assertIsNotNone( + err.data["DialogContext"] # pylint: disable=no-member + ) + else: + raise Exception("Should have thrown an error.") + + elif results.status == DialogTurnStatus.Complete: + await turn_context.send_activity(results.result) + + await convo_state.save_changes(turn_context) + + # Initialize TestAdapter. + adapter = TestAdapter(exec_test) + + await adapter.send("hello") diff --git a/libraries/botbuilder-dialogs/tests/test_attachment_prompt.py b/libraries/botbuilder-dialogs/tests/test_attachment_prompt.py index 19ae1e429..62b0c8905 100644 --- a/libraries/botbuilder-dialogs/tests/test_attachment_prompt.py +++ b/libraries/botbuilder-dialogs/tests/test_attachment_prompt.py @@ -1,13 +1,14 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +import copy import aiounittest from botbuilder.dialogs.prompts import ( AttachmentPrompt, PromptOptions, PromptValidatorContext, ) -from botbuilder.schema import Activity, ActivityTypes, Attachment +from botbuilder.schema import Activity, ActivityTypes, Attachment, InputHints from botbuilder.core import ( TurnContext, @@ -71,6 +72,42 @@ async def exec_test(turn_context: TurnContext): step3 = await step2.send(attachment_activity) await step3.assert_reply("some content") + async def test_attachment_prompt_with_input_hint(self): + prompt_activity = Activity( + type=ActivityTypes.message, + text="please add an attachment.", + input_hint=InputHints.accepting_input, + ) + + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + options = PromptOptions(prompt=copy.copy(prompt_activity)) + await dialog_context.prompt("AttachmentPrompt", options) + elif results.status == DialogTurnStatus.Complete: + attachment = results.result[0] + content = MessageFactory.text(attachment.content) + await turn_context.send_activity(content) + + await convo_state.save_changes(turn_context) + + # Initialize TestAdapter. + adapter = TestAdapter(exec_test) + + # Create ConversationState with MemoryStorage and register the state as middleware. + convo_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet and AttachmentPrompt. + dialog_state = convo_state.create_property("dialog_state") + dialogs = DialogSet(dialog_state) + dialogs.add(AttachmentPrompt("AttachmentPrompt")) + + step1 = await adapter.send("hello") + await step1.assert_reply(prompt_activity) + async def test_attachment_prompt_with_validator(self): async def exec_test(turn_context: TurnContext): dialog_context = await dialogs.create_context(turn_context) diff --git a/libraries/botbuilder-dialogs/tests/test_choice_prompt.py b/libraries/botbuilder-dialogs/tests/test_choice_prompt.py index 16cb16c9e..995758888 100644 --- a/libraries/botbuilder-dialogs/tests/test_choice_prompt.py +++ b/libraries/botbuilder-dialogs/tests/test_choice_prompt.py @@ -15,9 +15,10 @@ ChoiceRecognizers, FindChoicesOptions, ) -from botbuilder.dialogs.choices import Choice, ListStyle +from botbuilder.dialogs.choices import Choice, ChoiceFactoryOptions, ListStyle from botbuilder.dialogs.prompts import ( ChoicePrompt, + PromptCultureModel, PromptOptions, PromptValidatorContext, ) @@ -370,6 +371,226 @@ async def validator(prompt: PromptValidatorContext) -> bool: step3 = await step2.send(_answer_message) await step3.assert_reply("red") + async def test_should_default_to_english_locale(self): + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results: DialogTurnResult = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + options = PromptOptions( + prompt=Activity( + type=ActivityTypes.message, text="Please choose a color." + ), + choices=_color_choices, + ) + await dialog_context.prompt("prompt", options) + elif results.status == DialogTurnStatus.Complete: + selected_choice = results.result + await turn_context.send_activity(selected_choice.value) + + await convo_state.save_changes(turn_context) + + async def validator(prompt: PromptValidatorContext) -> bool: + assert prompt + + if not prompt.recognized.succeeded: + await prompt.context.send_activity("Bad input.") + + return prompt.recognized.succeeded + + locales = [None, "", "not-supported"] + + for locale in locales: + adapter = TestAdapter(exec_test) + + convo_state = ConversationState(MemoryStorage()) + dialog_state = convo_state.create_property("dialogState") + dialogs = DialogSet(dialog_state) + + choice_prompt = ChoicePrompt("prompt", validator) + dialogs.add(choice_prompt) + + step1 = await adapter.send( + Activity(type=ActivityTypes.message, text="Hello", locale=locale) + ) + step2 = await step1.assert_reply( + "Please choose a color. (1) red, (2) green, or (3) blue" + ) + step3 = await step2.send(_answer_message) + await step3.assert_reply("red") + + async def test_should_recognize_locale_variations_of_correct_locales(self): + def cap_ending(locale: str) -> str: + return f"{locale.split('-')[0]}-{locale.split('-')[1].upper()}" + + def title_ending(locale: str) -> str: + return locale[:3] + locale[3].upper() + locale[4:] + + def cap_two_letter(locale: str) -> str: + return locale.split("-")[0].upper() + + def lower_two_letter(locale: str) -> str: + return locale.split("-")[0].upper() + + async def exec_test_for_locale(valid_locale: str, locale_variations: List): + # Hold the correct answer from when a valid locale is used + expected_answer = None + + def inspector(activity: Activity, description: str): + nonlocal expected_answer + + assert not description + + if valid_locale == test_locale: + expected_answer = activity.text + else: + # Ensure we're actually testing a variation. + assert activity.locale != valid_locale + + assert activity.text == expected_answer + return True + + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results: DialogTurnResult = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + options = PromptOptions( + prompt=Activity( + type=ActivityTypes.message, text="Please choose a color." + ), + choices=_color_choices, + ) + await dialog_context.prompt("prompt", options) + elif results.status == DialogTurnStatus.Complete: + selected_choice = results.result + await turn_context.send_activity(selected_choice.value) + + await convo_state.save_changes(turn_context) + + async def validator(prompt: PromptValidatorContext) -> bool: + assert prompt + + if not prompt.recognized.succeeded: + await prompt.context.send_activity("Bad input.") + + return prompt.recognized.succeeded + + test_locale = None + for test_locale in locale_variations: + adapter = TestAdapter(exec_test) + + convo_state = ConversationState(MemoryStorage()) + dialog_state = convo_state.create_property("dialogState") + dialogs = DialogSet(dialog_state) + + choice_prompt = ChoicePrompt("prompt", validator) + dialogs.add(choice_prompt) + + step1 = await adapter.send( + Activity( + type=ActivityTypes.message, text="Hello", locale=test_locale + ) + ) + await step1.assert_reply(inspector) + + locales = [ + "zh-cn", + "nl-nl", + "en-us", + "fr-fr", + "de-de", + "it-it", + "ja-jp", + "ko-kr", + "pt-br", + "es-es", + "tr-tr", + "de-de", + ] + + locale_tests = [] + for locale in locales: + locale_tests.append( + [ + locale, + cap_ending(locale), + title_ending(locale), + cap_two_letter(locale), + lower_two_letter(locale), + ] + ) + + # Test each valid locale + for locale_tests in locale_tests: + await exec_test_for_locale(locale_tests[0], locale_tests) + + async def test_should_recognize_and_use_custom_locale_dict( + self, + ): + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results: DialogTurnResult = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + options = PromptOptions( + prompt=Activity( + type=ActivityTypes.message, text="Please choose a color." + ), + choices=_color_choices, + ) + await dialog_context.prompt("prompt", options) + elif results.status == DialogTurnStatus.Complete: + selected_choice = results.result + await turn_context.send_activity(selected_choice.value) + + await convo_state.save_changes(turn_context) + + async def validator(prompt: PromptValidatorContext) -> bool: + assert prompt + + if not prompt.recognized.succeeded: + await prompt.context.send_activity("Bad input.") + + return prompt.recognized.succeeded + + adapter = TestAdapter(exec_test) + + convo_state = ConversationState(MemoryStorage()) + dialog_state = convo_state.create_property("dialogState") + dialogs = DialogSet(dialog_state) + + culture = PromptCultureModel( + locale="custom-locale", + no_in_language="customNo", + yes_in_language="customYes", + separator="customSeparator", + inline_or="customInlineOr", + inline_or_more="customInlineOrMore", + ) + + custom_dict = { + culture.locale: ChoiceFactoryOptions( + inline_or=culture.inline_or, + inline_or_more=culture.inline_or_more, + inline_separator=culture.separator, + include_numbers=True, + ) + } + + choice_prompt = ChoicePrompt("prompt", validator, choice_defaults=custom_dict) + dialogs.add(choice_prompt) + + step1 = await adapter.send( + Activity(type=ActivityTypes.message, text="Hello", locale=culture.locale) + ) + await step1.assert_reply( + "Please choose a color. (1) redcustomSeparator(2) greencustomInlineOrMore(3) blue" + ) + async def test_should_not_render_choices_if_list_style_none_is_specified(self): async def exec_test(turn_context: TurnContext): dialog_context = await dialogs.create_context(turn_context) diff --git a/libraries/botbuilder-dialogs/tests/test_confirm_prompt.py b/libraries/botbuilder-dialogs/tests/test_confirm_prompt.py index 4b464936a..cedf5f03a 100644 --- a/libraries/botbuilder-dialogs/tests/test_confirm_prompt.py +++ b/libraries/botbuilder-dialogs/tests/test_confirm_prompt.py @@ -1,6 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from typing import List import aiounittest from botbuilder.core import ( ConversationState, @@ -10,9 +11,13 @@ ) from botbuilder.core.adapters import TestAdapter from botbuilder.dialogs import DialogSet, DialogTurnResult, DialogTurnStatus -from botbuilder.dialogs.choices import ChoiceFactoryOptions, ListStyle -from botbuilder.dialogs.prompts import ConfirmPrompt -from botbuilder.dialogs.prompts import PromptOptions +from botbuilder.dialogs.choices import Choice, ChoiceFactoryOptions, ListStyle +from botbuilder.dialogs.prompts import ( + ConfirmPrompt, + PromptCultureModel, + PromptOptions, + PromptValidatorContext, +) from botbuilder.schema import Activity, ActivityTypes @@ -274,3 +279,222 @@ async def exec_test(turn_context: TurnContext): ) step5 = await step4.send("no") await step5.assert_reply("Not confirmed") + + async def test_confirm_prompt_should_default_to_english_locale(self): + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results: DialogTurnResult = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + options = PromptOptions( + prompt=Activity(type=ActivityTypes.message, text="Please confirm."), + retry_prompt=Activity( + type=ActivityTypes.message, + text="Please confirm, say 'yes' or 'no' or something like that.", + ), + ) + await dialog_context.prompt("ConfirmPrompt", options) + elif results.status == DialogTurnStatus.Complete: + message_text = "Confirmed" if results.result else "Not confirmed" + await turn_context.send_activity(MessageFactory.text(message_text)) + + await convo_state.save_changes(turn_context) + + locales = [None, "", "not-supported"] + + for locale in locales: + # Initialize TestAdapter. + adapter = TestAdapter(exec_test) + + # Create new ConversationState with MemoryStorage and register the state as middleware. + convo_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet, and ChoicePrompt. + dialog_state = convo_state.create_property("dialogState") + dialogs = DialogSet(dialog_state) + confirm_prompt = ConfirmPrompt("ConfirmPrompt") + confirm_prompt.choice_options = ChoiceFactoryOptions(include_numbers=True) + dialogs.add(confirm_prompt) + + step1 = await adapter.send( + Activity(type=ActivityTypes.message, text="Hello", locale=locale) + ) + step2 = await step1.assert_reply("Please confirm. (1) Yes or (2) No") + step3 = await step2.send("lala") + step4 = await step3.assert_reply( + "Please confirm, say 'yes' or 'no' or something like that. (1) Yes or (2) No" + ) + step5 = await step4.send("2") + await step5.assert_reply("Not confirmed") + + async def test_should_recognize_locale_variations_of_correct_locales(self): + def cap_ending(locale: str) -> str: + return f"{locale.split('-')[0]}-{locale.split('-')[1].upper()}" + + def title_ending(locale: str) -> str: + return locale[:3] + locale[3].upper() + locale[4:] + + def cap_two_letter(locale: str) -> str: + return locale.split("-")[0].upper() + + def lower_two_letter(locale: str) -> str: + return locale.split("-")[0].upper() + + async def exec_test_for_locale(valid_locale: str, locale_variations: List): + # Hold the correct answer from when a valid locale is used + expected_answer = None + + def inspector(activity: Activity, description: str): + nonlocal expected_answer + + assert not description + + if valid_locale == test_locale: + expected_answer = activity.text + else: + # Ensure we're actually testing a variation. + assert activity.locale != valid_locale + + assert activity.text == expected_answer + return True + + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results: DialogTurnResult = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + options = PromptOptions( + prompt=Activity( + type=ActivityTypes.message, text="Please confirm." + ) + ) + await dialog_context.prompt("prompt", options) + elif results.status == DialogTurnStatus.Complete: + confirmed = results.result + if confirmed: + await turn_context.send_activity("true") + else: + await turn_context.send_activity("false") + + await convo_state.save_changes(turn_context) + + async def validator(prompt: PromptValidatorContext) -> bool: + assert prompt + + if not prompt.recognized.succeeded: + await prompt.context.send_activity("Bad input.") + + return prompt.recognized.succeeded + + test_locale = None + for test_locale in locale_variations: + adapter = TestAdapter(exec_test) + + convo_state = ConversationState(MemoryStorage()) + dialog_state = convo_state.create_property("dialogState") + dialogs = DialogSet(dialog_state) + + choice_prompt = ConfirmPrompt("prompt", validator) + dialogs.add(choice_prompt) + + step1 = await adapter.send( + Activity( + type=ActivityTypes.message, text="Hello", locale=test_locale + ) + ) + await step1.assert_reply(inspector) + + locales = [ + "zh-cn", + "nl-nl", + "en-us", + "fr-fr", + "de-de", + "it-it", + "ja-jp", + "ko-kr", + "pt-br", + "es-es", + "tr-tr", + "de-de", + ] + + locale_tests = [] + for locale in locales: + locale_tests.append( + [ + locale, + cap_ending(locale), + title_ending(locale), + cap_two_letter(locale), + lower_two_letter(locale), + ] + ) + + # Test each valid locale + for locale_tests in locale_tests: + await exec_test_for_locale(locale_tests[0], locale_tests) + + async def test_should_recognize_and_use_custom_locale_dict( + self, + ): + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results: DialogTurnResult = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + options = PromptOptions( + prompt=Activity(type=ActivityTypes.message, text="Please confirm.") + ) + await dialog_context.prompt("prompt", options) + elif results.status == DialogTurnStatus.Complete: + selected_choice = results.result + await turn_context.send_activity(selected_choice.value) + + await convo_state.save_changes(turn_context) + + async def validator(prompt: PromptValidatorContext) -> bool: + assert prompt + + if not prompt.recognized.succeeded: + await prompt.context.send_activity("Bad input.") + + return prompt.recognized.succeeded + + adapter = TestAdapter(exec_test) + + convo_state = ConversationState(MemoryStorage()) + dialog_state = convo_state.create_property("dialogState") + dialogs = DialogSet(dialog_state) + + culture = PromptCultureModel( + locale="custom-locale", + no_in_language="customNo", + yes_in_language="customYes", + separator="customSeparator", + inline_or="customInlineOr", + inline_or_more="customInlineOrMore", + ) + + custom_dict = { + culture.locale: ( + Choice(culture.yes_in_language), + Choice(culture.no_in_language), + ChoiceFactoryOptions( + culture.separator, culture.inline_or, culture.inline_or_more, True + ), + ) + } + + confirm_prompt = ConfirmPrompt("prompt", validator, choice_defaults=custom_dict) + dialogs.add(confirm_prompt) + + step1 = await adapter.send( + Activity(type=ActivityTypes.message, text="Hello", locale=culture.locale) + ) + await step1.assert_reply( + "Please confirm. (1) customYescustomInlineOr(2) customNo" + ) diff --git a/libraries/botbuilder-dialogs/tests/test_date_time_prompt.py b/libraries/botbuilder-dialogs/tests/test_date_time_prompt.py index f3ea4d950..765ef4c3c 100644 --- a/libraries/botbuilder-dialogs/tests/test_date_time_prompt.py +++ b/libraries/botbuilder-dialogs/tests/test_date_time_prompt.py @@ -32,7 +32,6 @@ async def exec_test(turn_context: TurnContext) -> None: results = await dialog_context.continue_dialog() if results.status == DialogTurnStatus.Empty: - options = PromptOptions(prompt=MessageFactory.text(prompt_msg)) await dialog_context.begin_dialog("DateTimePrompt", options) else: diff --git a/libraries/botbuilder-dialogs/tests/test_dialog_manager.py b/libraries/botbuilder-dialogs/tests/test_dialog_manager.py new file mode 100644 index 000000000..3c5a4b34a --- /dev/null +++ b/libraries/botbuilder-dialogs/tests/test_dialog_manager.py @@ -0,0 +1,329 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +# pylint: disable=pointless-string-statement + +from enum import Enum +from typing import Callable, List, Tuple + +import aiounittest + +from botbuilder.core import ( + AutoSaveStateMiddleware, + BotAdapter, + ConversationState, + MemoryStorage, + MessageFactory, + UserState, + TurnContext, +) +from botbuilder.core.adapters import TestAdapter +from botbuilder.core.skills import SkillHandler, SkillConversationReference +from botbuilder.dialogs import ( + ComponentDialog, + Dialog, + DialogContext, + DialogEvents, + DialogInstance, + DialogReason, + TextPrompt, + WaterfallDialog, + DialogManager, + DialogManagerResult, + DialogTurnStatus, + WaterfallStepContext, +) +from botbuilder.dialogs.prompts import PromptOptions +from botbuilder.schema import ( + Activity, + ActivityTypes, + ChannelAccount, + ConversationAccount, + EndOfConversationCodes, + InputHints, +) +from botframework.connector.auth import AuthenticationConstants, ClaimsIdentity +from botframework.connector import Channels + + +class SkillFlowTestCase(str, Enum): + # DialogManager is executing on a root bot with no skills (typical standalone bot). + root_bot_only = "RootBotOnly" + + # DialogManager is executing on a root bot handling replies from a skill. + root_bot_consuming_skill = "RootBotConsumingSkill" + + # DialogManager is executing in a skill that is called from a root and calling another skill. + middle_skill = "MiddleSkill" + + # DialogManager is executing in a skill that is called from a parent (a root or another skill) but doesn"t call + # another skill. + leaf_skill = "LeafSkill" + + +class SimpleComponentDialog(ComponentDialog): + # An App ID for a parent bot. + parent_bot_id = "00000000-0000-0000-0000-0000000000PARENT" + + # An App ID for a skill bot. + skill_bot_id = "00000000-0000-0000-0000-00000000000SKILL" + + # Captures an EndOfConversation if it was sent to help with assertions. + eoc_sent: Activity = None + + # Property to capture the DialogManager turn results and do assertions. + dm_turn_result: DialogManagerResult = None + + def __init__( + self, id: str = None, prop: str = None + ): # pylint: disable=unused-argument + super().__init__(id or "SimpleComponentDialog") + self.text_prompt = "TextPrompt" + self.waterfall_dialog = "WaterfallDialog" + self.add_dialog(TextPrompt(self.text_prompt)) + self.add_dialog( + WaterfallDialog( + self.waterfall_dialog, + [ + self.prompt_for_name, + self.final_step, + ], + ) + ) + self.initial_dialog_id = self.waterfall_dialog + self.end_reason = None + + @staticmethod + async def create_test_flow( + dialog: Dialog, + test_case: SkillFlowTestCase = SkillFlowTestCase.root_bot_only, + enabled_trace=False, + ) -> TestAdapter: + conversation_id = "testFlowConversationId" + storage = MemoryStorage() + conversation_state = ConversationState(storage) + user_state = UserState(storage) + + activity = Activity( + channel_id=Channels.test, + service_url="https://test.com", + from_property=ChannelAccount(id="user1", name="User1"), + recipient=ChannelAccount(id="bot", name="Bot"), + conversation=ConversationAccount( + is_group=False, conversation_type=conversation_id, id=conversation_id + ), + ) + + dialog_manager = DialogManager(dialog) + dialog_manager.user_state = user_state + dialog_manager.conversation_state = conversation_state + + async def logic(context: TurnContext): + if test_case != SkillFlowTestCase.root_bot_only: + # Create a skill ClaimsIdentity and put it in turn_state so isSkillClaim() returns True. + claims_identity = ClaimsIdentity({}, False) + claims_identity.claims["ver"] = ( + "2.0" # AuthenticationConstants.VersionClaim + ) + claims_identity.claims["aud"] = ( + SimpleComponentDialog.skill_bot_id + ) # AuthenticationConstants.AudienceClaim + claims_identity.claims["azp"] = ( + SimpleComponentDialog.parent_bot_id + ) # AuthenticationConstants.AuthorizedParty + context.turn_state[BotAdapter.BOT_IDENTITY_KEY] = claims_identity + + if test_case == SkillFlowTestCase.root_bot_consuming_skill: + # Simulate the SkillConversationReference with a channel OAuthScope stored in turn_state. + # This emulates a response coming to a root bot through SkillHandler. + context.turn_state[ + SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY + ] = SkillConversationReference( + None, AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + ) + + if test_case == SkillFlowTestCase.middle_skill: + # Simulate the SkillConversationReference with a parent Bot ID stored in turn_state. + # This emulates a response coming to a skill from another skill through SkillHandler. + context.turn_state[ + SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY + ] = SkillConversationReference( + None, SimpleComponentDialog.parent_bot_id + ) + + async def aux( + turn_context: TurnContext, # pylint: disable=unused-argument + activities: List[Activity], + next: Callable, + ): + for activity in activities: + if activity.type == ActivityTypes.end_of_conversation: + SimpleComponentDialog.eoc_sent = activity + break + + return await next() + + # Interceptor to capture the EoC activity if it was sent so we can assert it in the tests. + context.on_send_activities(aux) + + SimpleComponentDialog.dm_turn_result = await dialog_manager.on_turn(context) + + adapter = TestAdapter(logic, activity, enabled_trace) + adapter.use(AutoSaveStateMiddleware([user_state, conversation_state])) + + return adapter + + async def on_end_dialog( + self, context: DialogContext, instance: DialogInstance, reason: DialogReason + ): + self.end_reason = reason + return await super().on_end_dialog(context, instance, reason) + + async def prompt_for_name(self, step: WaterfallStepContext): + return await step.prompt( + self.text_prompt, + PromptOptions( + prompt=MessageFactory.text( + "Hello, what is your name?", None, InputHints.expecting_input + ), + retry_prompt=MessageFactory.text( + "Hello, what is your name again?", None, InputHints.expecting_input + ), + ), + ) + + async def final_step(self, step: WaterfallStepContext): + await step.context.send_activity(f"Hello { step.result }, nice to meet you!") + return await step.end_dialog(step.result) + + +class DialogManagerTests(aiounittest.AsyncTestCase): + """ + self.beforeEach(() => { + _dmTurnResult = undefined + }) + """ + + async def test_handles_bot_and_skills(self): + construction_data: List[Tuple[SkillFlowTestCase, bool]] = [ + (SkillFlowTestCase.root_bot_only, False), + (SkillFlowTestCase.root_bot_consuming_skill, False), + (SkillFlowTestCase.middle_skill, True), + (SkillFlowTestCase.leaf_skill, True), + ] + + for test_case, should_send_eoc in construction_data: + with self.subTest(test_case=test_case, should_send_eoc=should_send_eoc): + SimpleComponentDialog.dm_turn_result = None + SimpleComponentDialog.eoc_sent = None + dialog = SimpleComponentDialog() + test_flow = await SimpleComponentDialog.create_test_flow( + dialog, test_case + ) + step1 = await test_flow.send("Hi") + step2 = await step1.assert_reply("Hello, what is your name?") + step3 = await step2.send("SomeName") + await step3.assert_reply("Hello SomeName, nice to meet you!") + + self.assertEqual( + SimpleComponentDialog.dm_turn_result.turn_result.status, + DialogTurnStatus.Complete, + ) + + self.assertEqual(dialog.end_reason, DialogReason.EndCalled) + if should_send_eoc: + self.assertTrue( + bool(SimpleComponentDialog.eoc_sent), + "Skills should send EndConversation to channel", + ) + self.assertEqual( + SimpleComponentDialog.eoc_sent.type, + ActivityTypes.end_of_conversation, + ) + self.assertEqual( + SimpleComponentDialog.eoc_sent.code, + EndOfConversationCodes.completed_successfully, + ) + self.assertEqual(SimpleComponentDialog.eoc_sent.value, "SomeName") + else: + self.assertIsNone( + SimpleComponentDialog.eoc_sent, + "Root bot should not send EndConversation to channel", + ) + + async def test_skill_handles_eoc_from_parent(self): + SimpleComponentDialog.dm_turn_result = None + dialog = SimpleComponentDialog() + test_flow = await SimpleComponentDialog.create_test_flow( + dialog, SkillFlowTestCase.leaf_skill + ) + + step1 = await test_flow.send("Hi") + step2 = await step1.assert_reply("Hello, what is your name?") + await step2.send(Activity(type=ActivityTypes.end_of_conversation)) + + self.assertEqual( + SimpleComponentDialog.dm_turn_result.turn_result.status, + DialogTurnStatus.Cancelled, + ) + + async def test_skill_handles_reprompt_from_parent(self): + SimpleComponentDialog.dm_turn_result = None + dialog = SimpleComponentDialog() + test_flow = await SimpleComponentDialog.create_test_flow( + dialog, SkillFlowTestCase.leaf_skill + ) + + step1 = await test_flow.send("Hi") + step2 = await step1.assert_reply("Hello, what is your name?") + step3 = await step2.send( + Activity(type=ActivityTypes.event, name=DialogEvents.reprompt_dialog) + ) + await step3.assert_reply("Hello, what is your name?") + + self.assertEqual( + SimpleComponentDialog.dm_turn_result.turn_result.status, + DialogTurnStatus.Waiting, + ) + + async def test_skill_should_return_empty_on_reprompt_with_no_dialog(self): + SimpleComponentDialog.dm_turn_result = None + dialog = SimpleComponentDialog() + test_flow = await SimpleComponentDialog.create_test_flow( + dialog, SkillFlowTestCase.leaf_skill + ) + + await test_flow.send( + Activity(type=ActivityTypes.event, name=DialogEvents.reprompt_dialog) + ) + + self.assertEqual( + SimpleComponentDialog.dm_turn_result.turn_result.status, + DialogTurnStatus.Empty, + ) + + async def test_trace_bot_state(self): + SimpleComponentDialog.dm_turn_result = None + dialog = SimpleComponentDialog() + + def assert_is_trace(activity, description): # pylint: disable=unused-argument + assert activity.type == ActivityTypes.trace + + def assert_is_trace_and_label(activity, description): + assert_is_trace(activity, description) + assert activity.label == "Bot State" + + test_flow = await SimpleComponentDialog.create_test_flow( + dialog, SkillFlowTestCase.root_bot_only, True + ) + + step1 = await test_flow.send("Hi") + step2 = await step1.assert_reply("Hello, what is your name?") + step3 = await step2.assert_reply(assert_is_trace_and_label) + step4 = await step3.send("SomeName") + step5 = await step4.assert_reply("Hello SomeName, nice to meet you!") + await step5.assert_reply(assert_is_trace_and_label) + + self.assertEqual( + SimpleComponentDialog.dm_turn_result.turn_result.status, + DialogTurnStatus.Complete, + ) diff --git a/libraries/botbuilder-dialogs/tests/test_dialog_set.py b/libraries/botbuilder-dialogs/tests/test_dialog_set.py index a58d12da0..993ed207a 100644 --- a/libraries/botbuilder-dialogs/tests/test_dialog_set.py +++ b/libraries/botbuilder-dialogs/tests/test_dialog_set.py @@ -2,8 +2,15 @@ # Licensed under the MIT License. import aiounittest -from botbuilder.dialogs import DialogSet, ComponentDialog -from botbuilder.core import ConversationState, MemoryStorage +from botbuilder.dialogs import DialogSet, ComponentDialog, WaterfallDialog +from botbuilder.core import ConversationState, MemoryStorage, NullTelemetryClient + + +class MyBotTelemetryClient(NullTelemetryClient): + # pylint: disable=useless-return + def __init__(self): + super().__init__() + return class DialogSetTests(aiounittest.AsyncTestCase): @@ -18,3 +25,85 @@ def test_dialogset_constructor_null_property(self): def test_dialogset_constructor_null_from_componentdialog(self): ComponentDialog("MyId") + + def test_dialogset_telemetryset(self): + convo_state = ConversationState(MemoryStorage()) + dialog_state_property = convo_state.create_property("dialogstate") + dialog_set = DialogSet(dialog_state_property) + + dialog_set.add(WaterfallDialog("A")) + dialog_set.add(WaterfallDialog("B")) + + self.assertTrue( + isinstance( + dialog_set.find_dialog("A").telemetry_client, NullTelemetryClient + ) + ) + self.assertTrue( + isinstance( + dialog_set.find_dialog("B").telemetry_client, NullTelemetryClient + ) + ) + + dialog_set.telemetry_client = MyBotTelemetryClient() + + self.assertTrue( + isinstance( + dialog_set.find_dialog("A").telemetry_client, MyBotTelemetryClient + ) + ) + self.assertTrue( + isinstance( + dialog_set.find_dialog("B").telemetry_client, MyBotTelemetryClient + ) + ) + + def test_dialogset_nulltelemetryset(self): + convo_state = ConversationState(MemoryStorage()) + dialog_state_property = convo_state.create_property("dialogstate") + dialog_set = DialogSet(dialog_state_property) + + dialog_set.add(WaterfallDialog("A")) + dialog_set.add(WaterfallDialog("B")) + + dialog_set.telemetry_client = MyBotTelemetryClient() + dialog_set.telemetry_client = None + + self.assertFalse( + isinstance( + dialog_set.find_dialog("A").telemetry_client, MyBotTelemetryClient + ) + ) + self.assertFalse( + isinstance( + dialog_set.find_dialog("B").telemetry_client, MyBotTelemetryClient + ) + ) + self.assertTrue( + isinstance( + dialog_set.find_dialog("A").telemetry_client, NullTelemetryClient + ) + ) + self.assertTrue( + isinstance( + dialog_set.find_dialog("B").telemetry_client, NullTelemetryClient + ) + ) + + # pylint: disable=pointless-string-statement + """ + This test will be enabled when telematry tests are fixed for DialogSet telemetry + def test_dialogset_addtelemetryset(self): + convo_state = ConversationState(MemoryStorage()) + dialog_state_property = convo_state.create_property("dialogstate") + dialog_set = DialogSet(dialog_state_property) + + dialog_set.add(WaterfallDialog("A")) + dialog_set.add(WaterfallDialog("B")) + + dialog_set.telemetry_client = MyBotTelemetryClient() + + dialog_set.add(WaterfallDialog("C")) + + self.assertTrue(isinstance(dialog_set.find_dialog("C").telemetry_client, MyBotTelemetryClient)) + """ diff --git a/libraries/botbuilder-dialogs/tests/test_dialogextensions.py b/libraries/botbuilder-dialogs/tests/test_dialogextensions.py new file mode 100644 index 000000000..533f4384c --- /dev/null +++ b/libraries/botbuilder-dialogs/tests/test_dialogextensions.py @@ -0,0 +1,230 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +# pylint: disable=ungrouped-imports +import enum +from typing import List +import uuid + +import aiounittest + +from botframework.connector.auth import ClaimsIdentity, AuthenticationConstants +from botbuilder.core import ( + TurnContext, + MessageFactory, + MemoryStorage, + ConversationState, + UserState, + AdapterExtensions, + BotAdapter, +) +from botbuilder.core.adapters import ( + TestFlow, + TestAdapter, +) +from botbuilder.core.skills import ( + SkillHandler, + SkillConversationReference, +) +from botbuilder.core.transcript_logger import ( + TranscriptLoggerMiddleware, + ConsoleTranscriptLogger, +) +from botbuilder.schema import ActivityTypes, Activity, EndOfConversationCodes +from botbuilder.dialogs import ( + ComponentDialog, + TextPrompt, + WaterfallDialog, + DialogInstance, + DialogReason, + WaterfallStepContext, + PromptOptions, + Dialog, + DialogExtensions, + DialogEvents, +) + + +class SimpleComponentDialog(ComponentDialog): + def __init__(self): + super().__init__("SimpleComponentDialog") + + self.add_dialog(TextPrompt("TextPrompt")) + self.add_dialog( + WaterfallDialog("WaterfallDialog", [self.prompt_for_name, self.final_step]) + ) + + self.initial_dialog_id = "WaterfallDialog" + self.end_reason = DialogReason.BeginCalled + + async def end_dialog( + self, context: TurnContext, instance: DialogInstance, reason: DialogReason + ) -> None: + self.end_reason = reason + return await super().end_dialog(context, instance, reason) + + async def prompt_for_name(self, step_context: WaterfallStepContext): + return await step_context.prompt( + "TextPrompt", + PromptOptions( + prompt=MessageFactory.text("Hello, what is your name?"), + retry_prompt=MessageFactory.text("Hello, what is your name again?"), + ), + ) + + async def final_step(self, step_context: WaterfallStepContext): + await step_context.context.send_activity( + f"Hello {step_context.result}, nice to meet you!" + ) + return await step_context.end_dialog(step_context.result) + + +class FlowTestCase(enum.Enum): + root_bot_only = 1 + root_bot_consuming_skill = 2 + middle_skill = 3 + leaf_skill = 4 + + +class DialogExtensionsTests(aiounittest.AsyncTestCase): + def __init__(self, methodName): + super().__init__(methodName) + self.eoc_sent: Activity = None + self.skill_bot_id = str(uuid.uuid4()) + self.parent_bot_id = str(uuid.uuid4()) + + async def handles_bot_and_skills_test_cases( + self, test_case: FlowTestCase, send_eoc: bool + ): + dialog = SimpleComponentDialog() + + test_flow = self.create_test_flow(dialog, test_case) + + await test_flow.send("Hi") + await test_flow.assert_reply("Hello, what is your name?") + await test_flow.send("SomeName") + await test_flow.assert_reply("Hello SomeName, nice to meet you!") + + assert dialog.end_reason == DialogReason.EndCalled + + if send_eoc: + self.assertIsNotNone( + self.eoc_sent, "Skills should send EndConversation to channel" + ) + assert ActivityTypes.end_of_conversation == self.eoc_sent.type + assert EndOfConversationCodes.completed_successfully == self.eoc_sent.code + assert self.eoc_sent.value == "SomeName" + else: + self.assertIsNone( + self.eoc_sent, "Root bot should not send EndConversation to channel" + ) + + async def test_handles_root_bot_only(self): + return await self.handles_bot_and_skills_test_cases( + FlowTestCase.root_bot_only, False + ) + + async def test_handles_root_bot_consuming_skill(self): + return await self.handles_bot_and_skills_test_cases( + FlowTestCase.root_bot_consuming_skill, False + ) + + async def test_handles_middle_skill(self): + return await self.handles_bot_and_skills_test_cases( + FlowTestCase.middle_skill, True + ) + + async def test_handles_leaf_skill(self): + return await self.handles_bot_and_skills_test_cases( + FlowTestCase.leaf_skill, True + ) + + async def test_skill_handles_eoc_from_parent(self): + dialog = SimpleComponentDialog() + test_flow = self.create_test_flow(dialog, FlowTestCase.leaf_skill) + + await test_flow.send("Hi") + await test_flow.assert_reply("Hello, what is your name?") + await test_flow.send( + Activity( + type=ActivityTypes.end_of_conversation, + caller_id=self.parent_bot_id, + ) + ) + + self.assertIsNone( + self.eoc_sent, + "Skill should not send back EoC when an EoC is sent from a parent", + ) + assert dialog.end_reason == DialogReason.CancelCalled + + async def test_skill_handles_reprompt_from_parent(self): + dialog = SimpleComponentDialog() + test_flow = self.create_test_flow(dialog, FlowTestCase.leaf_skill) + + await test_flow.send("Hi") + await test_flow.assert_reply("Hello, what is your name?") + await test_flow.send( + Activity( + type=ActivityTypes.event, + caller_id=self.parent_bot_id, + name=DialogEvents.reprompt_dialog, + ) + ) + await test_flow.assert_reply("Hello, what is your name?") + + assert dialog.end_reason == DialogReason.BeginCalled + + def create_test_flow(self, dialog: Dialog, test_case: FlowTestCase) -> TestFlow: + conversation_id = str(uuid.uuid4()) + storage = MemoryStorage() + convo_state = ConversationState(storage) + user_state = UserState(storage) + + async def logic(context: TurnContext): + if test_case != FlowTestCase.root_bot_only: + claims_identity = ClaimsIdentity( + { + AuthenticationConstants.VERSION_CLAIM: "2.0", + AuthenticationConstants.AUDIENCE_CLAIM: self.skill_bot_id, + AuthenticationConstants.AUTHORIZED_PARTY: self.parent_bot_id, + }, + True, + ) + context.turn_state[BotAdapter.BOT_IDENTITY_KEY] = claims_identity + + if test_case == FlowTestCase.root_bot_consuming_skill: + context.turn_state[ + SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY + ] = SkillConversationReference( + None, AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + ) + + if test_case == FlowTestCase.middle_skill: + context.turn_state[ + SkillHandler.SKILL_CONVERSATION_REFERENCE_KEY + ] = SkillConversationReference(None, self.parent_bot_id) + + async def capture_eoc( + inner_context: TurnContext, activities: List[Activity], next + ): # pylint: disable=unused-argument + for activity in activities: + if activity.type == ActivityTypes.end_of_conversation: + self.eoc_sent = activity + break + return await next() + + context.on_send_activities(capture_eoc) + + await DialogExtensions.run_dialog( + dialog, context, convo_state.create_property("DialogState") + ) + + adapter = TestAdapter( + logic, TestAdapter.create_conversation_reference(conversation_id) + ) + AdapterExtensions.use_storage(adapter, storage) + AdapterExtensions.use_bot_state(adapter, user_state, convo_state) + adapter.use(TranscriptLoggerMiddleware(ConsoleTranscriptLogger())) + + return TestFlow(None, adapter) diff --git a/libraries/botbuilder-dialogs/tests/test_number_prompt.py b/libraries/botbuilder-dialogs/tests/test_number_prompt.py index 1b9510017..52fda0eac 100644 --- a/libraries/botbuilder-dialogs/tests/test_number_prompt.py +++ b/libraries/botbuilder-dialogs/tests/test_number_prompt.py @@ -180,7 +180,6 @@ async def test_number_uses_locale_specified_in_constructor(self): dialogs.add(number_prompt) async def exec_test(turn_context: TurnContext) -> None: - dialog_context = await dialogs.create_context(turn_context) results = await dialog_context.continue_dialog() diff --git a/libraries/botbuilder-dialogs/tests/test_oauth_prompt.py b/libraries/botbuilder-dialogs/tests/test_oauth_prompt.py index a5802103a..0c5fac1e7 100644 --- a/libraries/botbuilder-dialogs/tests/test_oauth_prompt.py +++ b/libraries/botbuilder-dialogs/tests/test_oauth_prompt.py @@ -9,6 +9,7 @@ ChannelAccount, ConversationAccount, InputHints, + SignInConstants, TokenResponse, ) @@ -75,7 +76,6 @@ async def callback_handler(turn_context: TurnContext): async def inspector( activity: Activity, description: str = None ): # pylint: disable=unused-argument - self.assertTrue(len(activity.attachments) == 1) self.assertTrue( activity.attachments[0].content_type @@ -183,7 +183,6 @@ async def exec_test(turn_context: TurnContext): results = await dialog_context.continue_dialog() if results.status == DialogTurnStatus.Empty: - # If magicCode is detected when prompting, this will end the dialog and return the token in tokenResult token_result = await dialog_context.prompt("prompt", PromptOptions()) if isinstance(token_result.result, TokenResponse): @@ -260,3 +259,161 @@ async def callback_handler(turn_context: TurnContext): await adapter.send("Hello") self.assertTrue(called) + + async def test_should_end_oauth_prompt_on_invalid_message_when_end_on_invalid_message( + self, + ): + connection_name = "myConnection" + token = "abc123" + magic_code = "888999" + + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + await dialog_context.prompt("prompt", PromptOptions()) + elif results.status == DialogTurnStatus.Complete: + if results.result and results.result.token: + await turn_context.send_activity("Failed") + + else: + await turn_context.send_activity("Ended") + + await convo_state.save_changes(turn_context) + + # Initialize TestAdapter. + adapter = TestAdapter(exec_test) + + # Create ConversationState with MemoryStorage and register the state as middleware. + convo_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet and AttachmentPrompt. + dialog_state = convo_state.create_property("dialog_state") + dialogs = DialogSet(dialog_state) + dialogs.add( + OAuthPrompt( + "prompt", + OAuthPromptSettings(connection_name, "Login", None, 300000, None, True), + ) + ) + + def inspector( + activity: Activity, description: str = None + ): # pylint: disable=unused-argument + assert len(activity.attachments) == 1 + assert ( + activity.attachments[0].content_type + == CardFactory.content_types.oauth_card + ) + + # send a mock EventActivity back to the bot with the token + adapter.add_user_token( + connection_name, + activity.channel_id, + activity.recipient.id, + token, + magic_code, + ) + + step1 = await adapter.send("Hello") + step2 = await step1.assert_reply(inspector) + step3 = await step2.send("test invalid message") + await step3.assert_reply("Ended") + + async def test_should_timeout_oauth_prompt_with_message_activity( + self, + ): + activity = Activity(type=ActivityTypes.message, text="any") + await self.run_timeout_test(activity) + + async def test_should_timeout_oauth_prompt_with_token_response_event_activity( + self, + ): + activity = Activity( + type=ActivityTypes.event, name=SignInConstants.token_response_event_name + ) + await self.run_timeout_test(activity) + + async def test_should_timeout_oauth_prompt_with_verify_state_operation_activity( + self, + ): + activity = Activity( + type=ActivityTypes.invoke, name=SignInConstants.verify_state_operation_name + ) + await self.run_timeout_test(activity) + + async def test_should_not_timeout_oauth_prompt_with_custom_event_activity( + self, + ): + activity = Activity(type=ActivityTypes.event, name="custom event name") + await self.run_timeout_test(activity, False, "Ended", "Failed") + + async def run_timeout_test( + self, + activity: Activity, + should_succeed: bool = True, + token_response: str = "Failed", + no_token_resonse="Ended", + ): + connection_name = "myConnection" + token = "abc123" + magic_code = "888999" + + async def exec_test(turn_context: TurnContext): + dialog_context = await dialogs.create_context(turn_context) + + results = await dialog_context.continue_dialog() + + if results.status == DialogTurnStatus.Empty: + await dialog_context.prompt("prompt", PromptOptions()) + elif results.status == DialogTurnStatus.Complete or ( + results.status == DialogTurnStatus.Waiting and not should_succeed + ): + if results.result and results.result.token: + await turn_context.send_activity(token_response) + + else: + await turn_context.send_activity(no_token_resonse) + + await convo_state.save_changes(turn_context) + + # Initialize TestAdapter. + adapter = TestAdapter(exec_test) + + # Create ConversationState with MemoryStorage and register the state as middleware. + convo_state = ConversationState(MemoryStorage()) + + # Create a DialogState property, DialogSet and AttachmentPrompt. + dialog_state = convo_state.create_property("dialog_state") + dialogs = DialogSet(dialog_state) + dialogs.add( + OAuthPrompt( + "prompt", + OAuthPromptSettings(connection_name, "Login", None, 1), + ) + ) + + def inspector( + activity: Activity, description: str = None + ): # pylint: disable=unused-argument + assert len(activity.attachments) == 1 + assert ( + activity.attachments[0].content_type + == CardFactory.content_types.oauth_card + ) + + # send a mock EventActivity back to the bot with the token + adapter.add_user_token( + connection_name, + activity.channel_id, + activity.recipient.id, + token, + magic_code, + ) + + step1 = await adapter.send("Hello") + step2 = await step1.assert_reply(inspector) + step3 = await step2.send(activity) + await step3.assert_reply(no_token_resonse) diff --git a/libraries/botbuilder-dialogs/tests/test_object_path.py b/libraries/botbuilder-dialogs/tests/test_object_path.py new file mode 100644 index 000000000..8ff6ff543 --- /dev/null +++ b/libraries/botbuilder-dialogs/tests/test_object_path.py @@ -0,0 +1,254 @@ +import aiounittest + +from botbuilder.dialogs import ObjectPath + + +class Location: + def __init__(self, lat: float = None, long: float = None): + self.lat = lat + self.long = long + + +class Options: + def __init__( + self, + first_name: str = None, + last_name: str = None, + age: int = None, + boolean: bool = None, + dictionary: dict = None, + location: Location = None, + ): + self.first_name = first_name + self.last_name = last_name + self.age = age + self.boolean = boolean + self.dictionary = dictionary + self.location = location + + +class ObjectPathTests(aiounittest.AsyncTestCase): + async def test_typed_only_default(self): + default_options = Options( + last_name="Smith", + first_name="Fred", + age=22, + location=Location( + lat=1.2312312, + long=3.234234, + ), + ) + + overlay = Options() + + result = ObjectPath.assign(default_options, overlay) + assert result.last_name == default_options.last_name + assert result.first_name == default_options.first_name + assert result.age == default_options.age + assert result.boolean == default_options.boolean + assert result.location.lat == default_options.location.lat + assert result.location.long == default_options.location.long + + async def test_typed_only_overlay(self): + default_options = Options() + + overlay = Options( + last_name="Smith", + first_name="Fred", + age=22, + location=Location( + lat=1.2312312, + long=3.234234, + ), + ) + + result = ObjectPath.assign(default_options, overlay) + assert result.last_name == overlay.last_name + assert result.first_name == overlay.first_name + assert result.age == overlay.age + assert result.boolean == overlay.boolean + assert result.location.lat == overlay.location.lat + assert result.location.long == overlay.location.long + + async def test_typed_full_overlay(self): + default_options = Options( + last_name="Smith", + first_name="Fred", + age=22, + location=Location( + lat=1.2312312, + long=3.234234, + ), + dictionary={"one": 1, "two": 2}, + ) + + overlay = Options( + last_name="Grant", + first_name="Eddit", + age=32, + location=Location( + lat=2.2312312, + long=2.234234, + ), + dictionary={"one": 99, "three": 3}, + ) + + result = ObjectPath.assign(default_options, overlay) + assert result.last_name == overlay.last_name + assert result.first_name == overlay.first_name + assert result.age == overlay.age + assert result.boolean == overlay.boolean + assert result.location.lat == overlay.location.lat + assert result.location.long == overlay.location.long + assert "one" in result.dictionary + assert result.dictionary["one"] == 99 + assert "two" in result.dictionary + assert "three" in result.dictionary + + async def test_typed_partial_overlay(self): + default_options = Options( + last_name="Smith", + first_name="Fred", + age=22, + location=Location( + lat=1.2312312, + long=3.234234, + ), + ) + + overlay = Options( + last_name="Grant", + ) + + result = ObjectPath.assign(default_options, overlay) + assert result.last_name == overlay.last_name + assert result.first_name == default_options.first_name + assert result.age == default_options.age + assert result.boolean == default_options.boolean + assert result.location.lat == default_options.location.lat + assert result.location.long == default_options.location.long + + async def test_typed_no_target(self): + overlay = Options( + last_name="Smith", + first_name="Fred", + age=22, + location=Location( + lat=1.2312312, + long=3.234234, + ), + ) + + result = ObjectPath.assign(None, overlay) + assert result.last_name == overlay.last_name + assert result.first_name == overlay.first_name + assert result.age == overlay.age + assert result.boolean == overlay.boolean + assert result.location.lat == overlay.location.lat + assert result.location.long == overlay.location.long + + async def test_typed_no_overlay(self): + default_options = Options( + last_name="Smith", + first_name="Fred", + age=22, + location=Location( + lat=1.2312312, + long=3.234234, + ), + ) + + result = ObjectPath.assign(default_options, None) + assert result.last_name == default_options.last_name + assert result.first_name == default_options.first_name + assert result.age == default_options.age + assert result.boolean == default_options.boolean + assert result.location.lat == default_options.location.lat + assert result.location.long == default_options.location.long + + async def test_no_target_or_overlay(self): + result = ObjectPath.assign(None, None, Options) + assert result + + async def test_dict_partial_overlay(self): + default_options = { + "last_name": "Smith", + "first_name": "Fred", + "age": 22, + "location": Location( + lat=1.2312312, + long=3.234234, + ), + } + + overlay = { + "last_name": "Grant", + } + + result = ObjectPath.assign(default_options, overlay) + assert result["last_name"] == overlay["last_name"] + assert result["first_name"] == default_options["first_name"] + assert result["age"] == default_options["age"] + assert result["location"].lat == default_options["location"].lat + assert result["location"].long == default_options["location"].long + + async def test_dict_to_typed_overlay(self): + default_options = Options( + last_name="Smith", + first_name="Fred", + age=22, + location=Location( + lat=1.2312312, + long=3.234234, + ), + ) + + overlay = { + "last_name": "Grant", + } + + result = ObjectPath.assign(default_options, overlay) + assert result.last_name == overlay["last_name"] + assert result.first_name == default_options.first_name + assert result.age == default_options.age + assert result.boolean == default_options.boolean + assert result.location.lat == default_options.location.lat + assert result.location.long == default_options.location.long + + async def test_set_value(self): + test = {} + ObjectPath.set_path_value(test, "x.y.z", 15) + ObjectPath.set_path_value(test, "x.p", "hello") + ObjectPath.set_path_value(test, "foo", {"Bar": 15, "Blat": "yo"}) + ObjectPath.set_path_value(test, "x.a[1]", "yabba") + ObjectPath.set_path_value(test, "x.a[0]", "dabba") + ObjectPath.set_path_value(test, "null", None) + + assert ObjectPath.get_path_value(test, "x.y.z") == 15 + assert ObjectPath.get_path_value(test, "x.p") == "hello" + assert ObjectPath.get_path_value(test, "foo.bar") == 15 + + assert not ObjectPath.try_get_path_value(test, "foo.Blatxxx") + assert ObjectPath.try_get_path_value(test, "x.a[1]") == "yabba" + assert ObjectPath.try_get_path_value(test, "x.a[0]") == "dabba" + + assert not ObjectPath.try_get_path_value(test, "null") + + async def test_remove_path_value(self): + test = {} + ObjectPath.set_path_value(test, "x.y.z", 15) + ObjectPath.set_path_value(test, "x.p", "hello") + ObjectPath.set_path_value(test, "foo", {"Bar": 15, "Blat": "yo"}) + ObjectPath.set_path_value(test, "x.a[1]", "yabba") + ObjectPath.set_path_value(test, "x.a[0]", "dabba") + + ObjectPath.remove_path_value(test, "x.y.z") + with self.assertRaises(KeyError): + ObjectPath.get_path_value(test, "x.y.z") + + assert ObjectPath.get_path_value(test, "x.y.z", 99) == 99 + + ObjectPath.remove_path_value(test, "x.a[1]") + assert not ObjectPath.try_get_path_value(test, "x.a[1]") + + assert ObjectPath.try_get_path_value(test, "x.a[0]") == "dabba" diff --git a/libraries/botbuilder-dialogs/tests/test_skill_dialog.py b/libraries/botbuilder-dialogs/tests/test_skill_dialog.py index cafa17c88..3c2a3d1b4 100644 --- a/libraries/botbuilder-dialogs/tests/test_skill_dialog.py +++ b/libraries/botbuilder-dialogs/tests/test_skill_dialog.py @@ -1,10 +1,12 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. import uuid -from typing import Callable, Union +from http import HTTPStatus +from typing import Callable, Union, List from unittest.mock import Mock import aiounittest +from botframework.connector.token_api.models import TokenExchangeResource from botbuilder.core import ( ConversationState, MemoryStorage, @@ -12,6 +14,7 @@ TurnContext, MessageFactory, ) +from botbuilder.core.card_factory import ContentTypes from botbuilder.core.skills import ( BotFrameworkSkill, ConversationIdFactoryBase, @@ -19,7 +22,17 @@ SkillConversationReference, BotFrameworkClient, ) -from botbuilder.schema import Activity, ActivityTypes, ConversationReference +from botbuilder.schema import ( + Activity, + ActivityTypes, + ConversationReference, + OAuthCard, + Attachment, + ConversationAccount, + ChannelAccount, + ExpectedReplies, + DeliveryModes, +) from botbuilder.testing import DialogTestClient from botbuilder.dialogs import ( @@ -30,9 +43,12 @@ ) -class SimpleConversationIdFactory(ConversationIdFactoryBase): +class SimpleConversationIdFactory( + ConversationIdFactoryBase +): # pylint: disable=abstract-method def __init__(self): self.conversation_refs = {} + self.create_count = 0 async def create_skill_conversation_id( self, @@ -40,6 +56,7 @@ async def create_skill_conversation_id( SkillConversationIdFactoryOptions, ConversationReference ], ) -> str: + self.create_count += 1 key = ( options_or_conversation_reference.activity.conversation.id + options_or_conversation_reference.activity.service_url @@ -53,13 +70,14 @@ async def create_skill_conversation_id( ) return key - async def get_conversation_reference( + async def get_skill_conversation_reference( self, skill_conversation_id: str - ) -> Union[SkillConversationReference, ConversationReference]: + ) -> SkillConversationReference: return self.conversation_refs[skill_conversation_id] async def delete_conversation_reference(self, skill_conversation_id: str): - raise NotImplementedError() + self.conversation_refs.pop(skill_conversation_id, None) + return class SkillDialogTests(aiounittest.AsyncTestCase): @@ -91,16 +109,78 @@ async def test_begin_dialog_options_validation(self): with self.assertRaises(TypeError): await client.send_activity("irrelevant") - # Only Message and Event activities are supported + async def test_begin_dialog_calls_skill_no_deliverymode(self): + return await self.begin_dialog_calls_skill(None) + + async def test_begin_dialog_calls_skill_expect_replies(self): + return await self.begin_dialog_calls_skill(DeliveryModes.expect_replies) + + async def begin_dialog_calls_skill(self, deliver_mode: str): + activity_sent = None + from_bot_id_sent = None + to_bot_id_sent = None + to_url_sent = None + + async def capture( + from_bot_id: str, + to_bot_id: str, + to_url: str, + service_url: str, # pylint: disable=unused-argument + conversation_id: str, # pylint: disable=unused-argument + activity: Activity, + ): + nonlocal from_bot_id_sent, to_bot_id_sent, to_url_sent, activity_sent + from_bot_id_sent = from_bot_id + to_bot_id_sent = to_bot_id + to_url_sent = to_url + activity_sent = activity + + mock_skill_client = self._create_mock_skill_client(capture) + + conversation_state = ConversationState(MemoryStorage()) + dialog_options = SkillDialogTests.create_skill_dialog_options( + conversation_state, mock_skill_client + ) + + sut = SkillDialog(dialog_options, "dialog_id") + activity_to_send = MessageFactory.text(str(uuid.uuid4())) + activity_to_send.delivery_mode = deliver_mode + client = DialogTestClient( "test", sut, - BeginSkillDialogOptions(Activity(type=ActivityTypes.conversation_update)), + BeginSkillDialogOptions(activity=activity_to_send), + conversation_state=conversation_state, ) - with self.assertRaises(TypeError): - await client.send_activity("irrelevant") - async def test_begin_dialog_calls_skill(self): + assert len(dialog_options.conversation_id_factory.conversation_refs) == 0 + + # Send something to the dialog to start it + await client.send_activity(MessageFactory.text("irrelevant")) + + # Assert results and data sent to the SkillClient for fist turn + assert len(dialog_options.conversation_id_factory.conversation_refs) == 1 + assert dialog_options.bot_id == from_bot_id_sent + assert dialog_options.skill.app_id == to_bot_id_sent + assert dialog_options.skill.skill_endpoint == to_url_sent + assert activity_to_send.text == activity_sent.text + assert DialogTurnStatus.Waiting == client.dialog_turn_result.status + + # Send a second message to continue the dialog + await client.send_activity(MessageFactory.text("Second message")) + + # Assert results for second turn + assert len(dialog_options.conversation_id_factory.conversation_refs) == 1 + assert activity_sent.text == "Second message" + assert DialogTurnStatus.Waiting == client.dialog_turn_result.status + + # Send EndOfConversation to the dialog + await client.send_activity(Activity(type=ActivityTypes.end_of_conversation)) + + # Assert we are done. + assert DialogTurnStatus.Complete == client.dialog_turn_result.status + + async def test_should_handle_invoke_activities(self): activity_sent = None from_bot_id_sent = None to_bot_id_sent = None @@ -123,12 +203,15 @@ async def capture( mock_skill_client = self._create_mock_skill_client(capture) conversation_state = ConversationState(MemoryStorage()) - dialog_options = self._create_skill_dialog_options( + dialog_options = SkillDialogTests.create_skill_dialog_options( conversation_state, mock_skill_client ) sut = SkillDialog(dialog_options, "dialog_id") - activity_to_send = MessageFactory.text(str(uuid.uuid4())) + activity_to_send = Activity( + type=ActivityTypes.invoke, + name=str(uuid.uuid4()), + ) client = DialogTestClient( "test", @@ -137,21 +220,27 @@ async def capture( conversation_state=conversation_state, ) + # Send something to the dialog to start it await client.send_activity(MessageFactory.text("irrelevant")) + # Assert results and data sent to the SkillClient for fist turn assert dialog_options.bot_id == from_bot_id_sent assert dialog_options.skill.app_id == to_bot_id_sent assert dialog_options.skill.skill_endpoint == to_url_sent assert activity_to_send.text == activity_sent.text assert DialogTurnStatus.Waiting == client.dialog_turn_result.status + # Send a second message to continue the dialog await client.send_activity(MessageFactory.text("Second message")) + # Assert results for second turn assert activity_sent.text == "Second message" assert DialogTurnStatus.Waiting == client.dialog_turn_result.status + # Send EndOfConversation to the dialog await client.send_activity(Activity(type=ActivityTypes.end_of_conversation)) + # Assert we are done. assert DialogTurnStatus.Complete == client.dialog_turn_result.status async def test_cancel_dialog_sends_eoc(self): @@ -171,7 +260,7 @@ async def capture( mock_skill_client = self._create_mock_skill_client(capture) conversation_state = ConversationState(MemoryStorage()) - dialog_options = self._create_skill_dialog_options( + dialog_options = SkillDialogTests.create_skill_dialog_options( conversation_state, mock_skill_client ) @@ -199,7 +288,7 @@ async def test_should_throw_on_post_failure(self): mock_skill_client = self._create_mock_skill_client(None, 500) conversation_state = ConversationState(MemoryStorage()) - dialog_options = self._create_skill_dialog_options( + dialog_options = SkillDialogTests.create_skill_dialog_options( conversation_state, mock_skill_client ) @@ -217,8 +306,285 @@ async def test_should_throw_on_post_failure(self): with self.assertRaises(Exception): await client.send_activity("irrelevant") - def _create_skill_dialog_options( - self, conversation_state: ConversationState, skill_client: BotFrameworkClient + async def test_should_intercept_oauth_cards_for_sso(self): + connection_name = "connectionName" + first_response = ExpectedReplies( + activities=[ + SkillDialogTests.create_oauth_card_attachment_activity("https://test") + ] + ) + + sequence = 0 + + async def post_return(): + nonlocal sequence + if sequence == 0: + result = InvokeResponse(body=first_response, status=HTTPStatus.OK) + else: + result = InvokeResponse(status=HTTPStatus.OK) + sequence += 1 + return result + + mock_skill_client = self._create_mock_skill_client(None, post_return) + conversation_state = ConversationState(MemoryStorage()) + + dialog_options = SkillDialogTests.create_skill_dialog_options( + conversation_state, mock_skill_client, connection_name + ) + sut = SkillDialog(dialog_options, dialog_id="dialog") + activity_to_send = SkillDialogTests.create_send_activity() + + client = DialogTestClient( + "test", + sut, + BeginSkillDialogOptions( + activity=activity_to_send, + ), + conversation_state=conversation_state, + ) + + client.test_adapter.add_exchangeable_token( + connection_name, "test", "User1", "https://test", "https://test1" + ) + + final_activity = await client.send_activity(MessageFactory.text("irrelevant")) + self.assertIsNone(final_activity) + + async def test_should_not_intercept_oauth_cards_for_empty_connection_name(self): + connection_name = "connectionName" + first_response = ExpectedReplies( + activities=[ + SkillDialogTests.create_oauth_card_attachment_activity("https://test") + ] + ) + + sequence = 0 + + async def post_return(): + nonlocal sequence + if sequence == 0: + result = InvokeResponse(body=first_response, status=HTTPStatus.OK) + else: + result = InvokeResponse(status=HTTPStatus.OK) + sequence += 1 + return result + + mock_skill_client = self._create_mock_skill_client(None, post_return) + conversation_state = ConversationState(MemoryStorage()) + + dialog_options = SkillDialogTests.create_skill_dialog_options( + conversation_state, mock_skill_client + ) + sut = SkillDialog(dialog_options, dialog_id="dialog") + activity_to_send = SkillDialogTests.create_send_activity() + + client = DialogTestClient( + "test", + sut, + BeginSkillDialogOptions( + activity=activity_to_send, + ), + conversation_state=conversation_state, + ) + + client.test_adapter.add_exchangeable_token( + connection_name, "test", "User1", "https://test", "https://test1" + ) + + final_activity = await client.send_activity(MessageFactory.text("irrelevant")) + self.assertIsNotNone(final_activity) + self.assertEqual(len(final_activity.attachments), 1) + + async def test_should_not_intercept_oauth_cards_for_empty_token(self): + first_response = ExpectedReplies( + activities=[ + SkillDialogTests.create_oauth_card_attachment_activity("https://test") + ] + ) + + sequence = 0 + + async def post_return(): + nonlocal sequence + if sequence == 0: + result = InvokeResponse(body=first_response, status=HTTPStatus.OK) + else: + result = InvokeResponse(status=HTTPStatus.OK) + sequence += 1 + return result + + mock_skill_client = self._create_mock_skill_client(None, post_return) + conversation_state = ConversationState(MemoryStorage()) + + dialog_options = SkillDialogTests.create_skill_dialog_options( + conversation_state, mock_skill_client + ) + sut = SkillDialog(dialog_options, dialog_id="dialog") + activity_to_send = SkillDialogTests.create_send_activity() + + client = DialogTestClient( + "test", + sut, + BeginSkillDialogOptions( + activity=activity_to_send, + ), + conversation_state=conversation_state, + ) + + # Don't add exchangeable token to test adapter + + final_activity = await client.send_activity(MessageFactory.text("irrelevant")) + self.assertIsNotNone(final_activity) + self.assertEqual(len(final_activity.attachments), 1) + + async def test_should_not_intercept_oauth_cards_for_token_exception(self): + connection_name = "connectionName" + first_response = ExpectedReplies( + activities=[ + SkillDialogTests.create_oauth_card_attachment_activity("https://test") + ] + ) + + sequence = 0 + + async def post_return(): + nonlocal sequence + if sequence == 0: + result = InvokeResponse(body=first_response, status=HTTPStatus.OK) + else: + result = InvokeResponse(status=HTTPStatus.OK) + sequence += 1 + return result + + mock_skill_client = self._create_mock_skill_client(None, post_return) + conversation_state = ConversationState(MemoryStorage()) + + dialog_options = SkillDialogTests.create_skill_dialog_options( + conversation_state, mock_skill_client, connection_name + ) + sut = SkillDialog(dialog_options, dialog_id="dialog") + activity_to_send = SkillDialogTests.create_send_activity() + initial_dialog_options = BeginSkillDialogOptions( + activity=activity_to_send, + ) + + client = DialogTestClient( + "test", + sut, + initial_dialog_options, + conversation_state=conversation_state, + ) + client.test_adapter.throw_on_exchange_request( + connection_name, "test", "User1", "https://test" + ) + + final_activity = await client.send_activity(MessageFactory.text("irrelevant")) + self.assertIsNotNone(final_activity) + self.assertEqual(len(final_activity.attachments), 1) + + async def test_should_not_intercept_oauth_cards_for_bad_request(self): + connection_name = "connectionName" + first_response = ExpectedReplies( + activities=[ + SkillDialogTests.create_oauth_card_attachment_activity("https://test") + ] + ) + + sequence = 0 + + async def post_return(): + nonlocal sequence + if sequence == 0: + result = InvokeResponse(body=first_response, status=HTTPStatus.OK) + else: + result = InvokeResponse(status=HTTPStatus.CONFLICT) + sequence += 1 + return result + + mock_skill_client = self._create_mock_skill_client(None, post_return) + conversation_state = ConversationState(MemoryStorage()) + + dialog_options = SkillDialogTests.create_skill_dialog_options( + conversation_state, mock_skill_client, connection_name + ) + sut = SkillDialog(dialog_options, dialog_id="dialog") + activity_to_send = SkillDialogTests.create_send_activity() + + client = DialogTestClient( + "test", + sut, + BeginSkillDialogOptions( + activity=activity_to_send, + ), + conversation_state=conversation_state, + ) + + client.test_adapter.add_exchangeable_token( + connection_name, "test", "User1", "https://test", "https://test1" + ) + + final_activity = await client.send_activity(MessageFactory.text("irrelevant")) + self.assertIsNotNone(final_activity) + self.assertEqual(len(final_activity.attachments), 1) + + async def test_end_of_conversation_from_expect_replies_calls_delete_conversation_reference( + self, + ): + activity_sent: Activity = None + + # Callback to capture the parameters sent to the skill + async def capture_action( + from_bot_id: str, # pylint: disable=unused-argument + to_bot_id: str, # pylint: disable=unused-argument + to_uri: str, # pylint: disable=unused-argument + service_url: str, # pylint: disable=unused-argument + conversation_id: str, # pylint: disable=unused-argument + activity: Activity, + ): + # Capture values sent to the skill so we can assert the right parameters were used. + nonlocal activity_sent + activity_sent = activity + + eoc = Activity.create_end_of_conversation_activity() + expected_replies = list([eoc]) + + # Create a mock skill client to intercept calls and capture what is sent. + mock_skill_client = self._create_mock_skill_client( + capture_action, expected_replies=expected_replies + ) + + # Use Memory for conversation state + conversation_state = ConversationState(MemoryStorage()) + dialog_options = self.create_skill_dialog_options( + conversation_state, mock_skill_client + ) + + # Create the SkillDialogInstance and the activity to send. + sut = SkillDialog(dialog_options, dialog_id="dialog") + activity_to_send = Activity.create_message_activity() + activity_to_send.delivery_mode = DeliveryModes.expect_replies + activity_to_send.text = str(uuid.uuid4()) + client = DialogTestClient( + "test", + sut, + BeginSkillDialogOptions(activity_to_send), + conversation_state=conversation_state, + ) + + # Send something to the dialog to start it + await client.send_activity("hello") + + simple_id_factory: SimpleConversationIdFactory = ( + dialog_options.conversation_id_factory + ) + self.assertEqual(0, len(simple_id_factory.conversation_refs)) + self.assertEqual(1, simple_id_factory.create_count) + + @staticmethod + def create_skill_dialog_options( + conversation_state: ConversationState, + skill_client: BotFrameworkClient, + connection_name: str = None, ): return SkillDialogOptions( bot_id=str(uuid.uuid4()), @@ -230,12 +596,41 @@ def _create_skill_dialog_options( app_id=str(uuid.uuid4()), skill_endpoint="http://testskill.contoso.com/api/messages", ), + connection_name=connection_name, + ) + + @staticmethod + def create_send_activity() -> Activity: + return Activity( + type=ActivityTypes.message, + delivery_mode=DeliveryModes.expect_replies, + text=str(uuid.uuid4()), + ) + + @staticmethod + def create_oauth_card_attachment_activity(uri: str) -> Activity: + oauth_card = OAuthCard(token_exchange_resource=TokenExchangeResource(uri=uri)) + attachment = Attachment( + content_type=ContentTypes.oauth_card, + content=oauth_card, ) + attachment_activity = MessageFactory.attachment(attachment) + attachment_activity.conversation = ConversationAccount(id=str(uuid.uuid4())) + attachment_activity.from_property = ChannelAccount(id="blah", name="name") + + return attachment_activity + def _create_mock_skill_client( - self, callback: Callable, return_status: int = 200 + self, + callback: Callable, + return_status: Union[Callable, int] = 200, + expected_replies: List[Activity] = None, ) -> BotFrameworkClient: mock_client = Mock() + activity_list = ExpectedReplies( + activities=expected_replies or [MessageFactory.text("dummy activity")] + ) async def mock_post_activity( from_bot_id: str, @@ -255,7 +650,10 @@ async def mock_post_activity( conversation_id, activity, ) - return InvokeResponse(status=return_status) + + if isinstance(return_status, Callable): + return await return_status() + return InvokeResponse(status=return_status, body=activity_list) mock_client.post_activity.side_effect = mock_post_activity diff --git a/libraries/botbuilder-dialogs/tests/test_waterfall.py b/libraries/botbuilder-dialogs/tests/test_waterfall.py index c26f6ee01..1290cedc4 100644 --- a/libraries/botbuilder-dialogs/tests/test_waterfall.py +++ b/libraries/botbuilder-dialogs/tests/test_waterfall.py @@ -80,7 +80,6 @@ async def step2(step) -> DialogTurnResult: # Initialize TestAdapter async def exec_test(turn_context: TurnContext) -> None: - dialog_context = await dialogs.create_context(turn_context) results = await dialog_context.continue_dialog() if results.status == DialogTurnStatus.Empty: diff --git a/libraries/botbuilder-integration-aiohttp/README.rst b/libraries/botbuilder-integration-aiohttp/README.rst index f92429436..eb32dd702 100644 --- a/libraries/botbuilder-integration-aiohttp/README.rst +++ b/libraries/botbuilder-integration-aiohttp/README.rst @@ -3,12 +3,12 @@ BotBuilder-Integration-Aiohttp for Python ========================================= -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch -.. image:: https://badge.fury.io/py/botbuilder-core.svg - :target: https://badge.fury.io/py/botbuilder-core +.. image:: https://badge.fury.io/py/botbuilder-integration-aiohttp.svg + :target: https://badge.fury.io/py/botbuilder-integration-aiohttp :alt: Latest PyPI package version Within the Bot Framework, This library enables you to integrate your bot within an aiohttp web application. diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/__init__.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/__init__.py index 1bb31e665..afefa5646 100644 --- a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/__init__.py +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/__init__.py @@ -8,9 +8,21 @@ from .aiohttp_channel_service import aiohttp_channel_service_routes from .aiohttp_channel_service_exception_middleware import aiohttp_error_middleware from .bot_framework_http_client import BotFrameworkHttpClient +from .bot_framework_http_adapter import BotFrameworkHttpAdapter +from .cloud_adapter import CloudAdapter +from .configuration_service_client_credential_factory import ( + ConfigurationServiceClientCredentialFactory, +) +from .configuration_bot_framework_authentication import ( + ConfigurationBotFrameworkAuthentication, +) __all__ = [ "aiohttp_channel_service_routes", "aiohttp_error_middleware", "BotFrameworkHttpClient", + "BotFrameworkHttpAdapter", + "CloudAdapter", + "ConfigurationServiceClientCredentialFactory", + "ConfigurationBotFrameworkAuthentication", ] diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/about.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/about.py index c0bfc2c92..e5cd51eee 100644 --- a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/about.py +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/about.py @@ -5,7 +5,7 @@ __title__ = "botbuilder-integration-aiohttp" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/aiohttp_channel_service.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/aiohttp_channel_service.py index af2545d89..8a31f3342 100644 --- a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/aiohttp_channel_service.py +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/aiohttp_channel_service.py @@ -127,7 +127,8 @@ async def get_conversation(request: Request): @routes.get(base_url + "/v3/conversations/{conversation_id}/members") async def get_conversation_members(request: Request): result = await handler.handle_get_conversation_members( - request.headers.get("Authorization"), request.match_info["conversation_id"], + request.headers.get("Authorization"), + request.match_info["conversation_id"], ) return get_serialized_response(result) @@ -136,7 +137,8 @@ async def get_conversation_members(request: Request): async def get_conversation_paged_members(request: Request): # TODO: continuation token? page size? result = await handler.handle_get_conversation_paged_members( - request.headers.get("Authorization"), request.match_info["conversation_id"], + request.headers.get("Authorization"), + request.match_info["conversation_id"], ) return get_serialized_response(result) diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/aiohttp_channel_service_exception_middleware.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/aiohttp_channel_service_exception_middleware.py index 7c5091121..40b0d105d 100644 --- a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/aiohttp_channel_service_exception_middleware.py +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/aiohttp_channel_service_exception_middleware.py @@ -3,6 +3,7 @@ from aiohttp.web import ( middleware, + HTTPError, HTTPNotImplemented, HTTPUnauthorized, HTTPNotFound, @@ -25,5 +26,8 @@ async def aiohttp_error_middleware(request, handler): raise HTTPUnauthorized() except KeyError: raise HTTPNotFound() + except HTTPError as error: + # In the case the integration adapter raises a specific HTTPError + raise error except Exception: raise HTTPInternalServerError() diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_adapter.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_adapter.py new file mode 100644 index 000000000..879bdeacd --- /dev/null +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_adapter.py @@ -0,0 +1,199 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Optional + +from aiohttp import ClientSession +from aiohttp.web import ( + Request, + Response, + json_response, + WebSocketResponse, + HTTPBadRequest, + HTTPUnauthorized, + HTTPUnsupportedMediaType, +) +from botbuilder.core import Bot, BotFrameworkAdapterSettings +from botbuilder.core.streaming import ( + BotFrameworkHttpAdapterBase, + StreamingRequestHandler, +) +from botbuilder.schema import Activity, ResourceResponse +from botbuilder.integration.aiohttp.streaming import AiohttpWebSocket +from botframework.connector.auth import AuthenticationConstants, JwtTokenValidation + + +class BotFrameworkHttpAdapter(BotFrameworkHttpAdapterBase): + def __init__(self, settings: BotFrameworkAdapterSettings): + # pylint: disable=invalid-name + super().__init__(settings) + + self._AUTH_HEADER_NAME = "authorization" + self._CHANNEL_ID_HEADER_NAME = "channelid" + + async def process( + self, request: Request, ws_response: WebSocketResponse, bot: Bot + ) -> Optional[Response]: + # TODO: maybe it's not necessary to expose the ws_response + if not request: + raise TypeError("request can't be None") + # if ws_response is None: + # raise TypeError("ws_response can't be None") + if not bot: + raise TypeError("bot can't be None") + + if request.method == "GET": + await self._connect_web_socket(bot, request, ws_response) + else: + # Deserialize the incoming Activity + if "application/json" in request.headers["Content-Type"]: + body = await request.json() + else: + raise HTTPUnsupportedMediaType() + + activity = Activity().deserialize(body) + auth_header = ( + request.headers["Authorization"] + if "Authorization" in request.headers + else "" + ) + + # Process the inbound activity with the bot + invoke_response = await self.process_activity( + activity, auth_header, bot.on_turn + ) + if invoke_response: + return json_response( + data=invoke_response.body, status=invoke_response.status + ) + return Response(status=201) + + async def send_streaming_activity(self, activity: Activity) -> ResourceResponse: + # Check to see if any of this adapter's StreamingRequestHandlers is associated with this conversation. + possible_handlers = [ + handler + for handler in self.request_handlers + if handler.service_url == activity.service_url + and handler.has_conversation(activity.conversation.id) + ] + + if possible_handlers: + if len(possible_handlers) > 1: + # The conversation has moved to a new connection and the former + # StreamingRequestHandler needs to be told to forget about it. + possible_handlers.sort( + key=lambda handler: handler.conversation_added_time( + activity.conversation.id + ) + ) + correct_handler = possible_handlers[-1] + for handler in possible_handlers: + if handler is not correct_handler: + handler.forget_conversation(activity.conversation.id) + + return await correct_handler.send_activity(activity) + + return await possible_handlers[0].send_activity(activity) + + if self.connected_bot: + # This is a proactive message that will need a new streaming connection opened. + # The ServiceUrl of a streaming connection follows the pattern "url:[ChannelName]:[Protocol]:[Host]". + + uri = activity.service_url.split(":") + protocol = uri[len(uri) - 2] + host = uri[len(uri) - 1] + # TODO: discuss if should abstract this from current package + # TODO: manage life cycle of sessions (when should we close them) + session = ClientSession() + aiohttp_ws = await session.ws_connect(protocol + host + "/api/messages") + web_socket = AiohttpWebSocket(aiohttp_ws, session) + handler = StreamingRequestHandler(self.connected_bot, self, web_socket) + + if self.request_handlers is None: + self.request_handlers = [] + + self.request_handlers.append(handler) + + return await handler.send_activity(activity) + + return None + + async def _connect_web_socket( + self, bot: Bot, request: Request, ws_response: WebSocketResponse + ): + if not request: + raise TypeError("request can't be None") + if ws_response is None: + raise TypeError("ws_response can't be None") + + if not bot: + raise TypeError(f"'bot: {bot.__class__.__name__}' argument can't be None") + + if not ws_response.can_prepare(request): + raise HTTPBadRequest(text="Upgrade to WebSocket is required.") + + if not await self._http_authenticate_request(request): + raise HTTPUnauthorized(text="Request authentication failed.") + + try: + await ws_response.prepare(request) + + bf_web_socket = AiohttpWebSocket(ws_response) + + request_handler = StreamingRequestHandler(bot, self, bf_web_socket) + + if self.request_handlers is None: + self.request_handlers = [] + + self.request_handlers.append(request_handler) + + await request_handler.listen() + except Exception as error: + import traceback # pylint: disable=import-outside-toplevel + + traceback.print_exc() + raise Exception(f"Unable to create transport server. Error: {str(error)}") + + async def _http_authenticate_request(self, request: Request) -> bool: + # pylint: disable=no-member + try: + if not await self._credential_provider.is_authentication_disabled(): + auth_header = request.headers.get(self._AUTH_HEADER_NAME) + channel_id = request.headers.get(self._CHANNEL_ID_HEADER_NAME) + + if not auth_header: + await self._write_unauthorized_response(self._AUTH_HEADER_NAME) + return False + if not channel_id: + await self._write_unauthorized_response( + self._CHANNEL_ID_HEADER_NAME + ) + return False + + claims_identity = await JwtTokenValidation.validate_auth_header( + auth_header, + self._credential_provider, + self._channel_provider, + channel_id, + ) + + if not claims_identity.is_authenticated: + raise HTTPUnauthorized() + + self._credentials = ( + self._credentials + or await self._BotFrameworkAdapter__get_app_credentials( + self.settings.app_id, + AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + ) + ) + + self.claims_identity = claims_identity + return True + except Exception as error: + raise error + + async def _write_unauthorized_response(self, header_name: str): + raise HTTPUnauthorized( + text=f"Unable to authenticate. Missing header: {header_name}" + ) diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_adapter_integration_base.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_adapter_integration_base.py new file mode 100644 index 000000000..02db8a1f8 --- /dev/null +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_adapter_integration_base.py @@ -0,0 +1,21 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod +from typing import Optional + +from aiohttp.web import ( + Request, + Response, + WebSocketResponse, +) + +from botbuilder.core import Bot + + +class BotFrameworkHttpAdapterIntegrationBase(ABC): + @abstractmethod + async def process( + self, request: Request, bot: Bot, ws_response: WebSocketResponse = None + ) -> Optional[Response]: + raise NotImplementedError() diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_client.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_client.py index 3fa2f448d..c57c042c2 100644 --- a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_client.py +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/bot_framework_http_client.py @@ -3,7 +3,7 @@ # pylint: disable=no-member import json -from typing import Dict +from typing import Dict, List, Tuple from logging import Logger import aiohttp @@ -14,20 +14,22 @@ ExpectedReplies, ConversationReference, ConversationAccount, + ChannelAccount, + RoleTypes, ) from botframework.connector.auth import ( ChannelProvider, CredentialProvider, - GovernmentConstants, MicrosoftAppCredentials, + AppCredentials, + MicrosoftGovernmentAppCredentials, ) class BotFrameworkHttpClient(BotFrameworkClient): - """ - A skill host adapter implements API to forward activity to a skill and - implements routing ChannelAPI calls from the Skill up through the bot/adapter. + A skill host adapter that implements the API to forward activity to a skill and + implements routing ChannelAPI calls from the skill up through the bot/adapter. """ INVOKE_ACTIVITY_NAME = "SkillEvents.ChannelApiInvoke" @@ -46,7 +48,6 @@ def __init__( self._credential_provider = credential_provider self._channel_provider = channel_provider self._logger = logger - self._session = aiohttp.ClientSession() async def post_activity( self, @@ -70,15 +71,12 @@ async def post_activity( ) # Capture current activity settings before changing them. - # TODO: DO we need to set the activity ID? (events that are created manually don't have it). original_conversation_id = activity.conversation.id original_service_url = activity.service_url - original_caller_id = activity.caller_id original_relates_to = activity.relates_to + original_recipient = activity.recipient try: - # TODO: The relato has to be ported to the adapter in the new integration library when - # resolving conflicts in merge activity.relates_to = ConversationReference( service_url=activity.service_url, activity_id=activity.id, @@ -97,33 +95,48 @@ async def post_activity( ) activity.conversation.id = conversation_id activity.service_url = service_url - activity.caller_id = f"urn:botframework:aadappid:{from_bot_id}" - - headers_dict = { - "Content-type": "application/json; charset=utf-8", - } - if token: - headers_dict.update( - {"Authorization": f"Bearer {token}",} - ) - - json_content = json.dumps(activity.serialize()) - resp = await self._session.post( - to_url, data=json_content.encode("utf-8"), headers=headers_dict, - ) - resp.raise_for_status() - data = (await resp.read()).decode() - content = json.loads(data) if data else None + if not activity.recipient: + activity.recipient = ChannelAccount(role=RoleTypes.skill) + else: + activity.recipient.role = RoleTypes.skill - if content: - return InvokeResponse(status=resp.status, body=content) + status, content = await self._post_content(to_url, token, activity) + + return InvokeResponse(status=status, body=content) finally: # Restore activity properties. activity.conversation.id = original_conversation_id activity.service_url = original_service_url - activity.caller_id = original_caller_id activity.relates_to = original_relates_to + activity.recipient = original_recipient + + async def _post_content( + self, to_url: str, token: str, activity: Activity + ) -> Tuple[int, object]: + headers_dict = { + "Content-type": "application/json; charset=utf-8", + "x-ms-conversation-id": activity.conversation.id, + } + if token: + headers_dict.update( + { + "Authorization": f"Bearer {token}", + } + ) + + json_content = json.dumps(activity.serialize()) + + async with aiohttp.ClientSession() as session: + resp = await session.post( + to_url, + data=json_content.encode("utf-8"), + headers=headers_dict, + ) + + resp.raise_for_status() + data = (await resp.read()).decode() + return resp.status, json.loads(data) if data else None async def post_buffered_activity( self, @@ -133,7 +146,7 @@ async def post_buffered_activity( service_url: str, conversation_id: str, activity: Activity, - ) -> [Activity]: + ) -> List[Activity]: """ Helper method to return a list of activities when an Activity is being sent with DeliveryMode == expectReplies. @@ -147,27 +160,26 @@ async def post_buffered_activity( async def _get_app_credentials( self, app_id: str, oauth_scope: str - ) -> MicrosoftAppCredentials: + ) -> AppCredentials: if not app_id: - return MicrosoftAppCredentials(None, None) + return MicrosoftAppCredentials.empty() + # in the cache? cache_key = f"{app_id}{oauth_scope}" app_credentials = BotFrameworkHttpClient._APP_CREDENTIALS_CACHE.get(cache_key) - if app_credentials: return app_credentials + # create a new AppCredentials app_password = await self._credential_provider.get_app_password(app_id) - app_credentials = MicrosoftAppCredentials( - app_id, app_password, oauth_scope=oauth_scope + + app_credentials = ( + MicrosoftGovernmentAppCredentials(app_id, app_password, scope=oauth_scope) + if self._channel_provider and self._channel_provider.is_government() + else MicrosoftAppCredentials(app_id, app_password, oauth_scope=oauth_scope) ) - if self._channel_provider and self._channel_provider.is_government(): - app_credentials.oauth_endpoint = ( - GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL - ) - app_credentials.oauth_scope = ( - GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE - ) + # put it in the cache BotFrameworkHttpClient._APP_CREDENTIALS_CACHE[cache_key] = app_credentials + return app_credentials diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/cloud_adapter.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/cloud_adapter.py new file mode 100644 index 000000000..576c5125c --- /dev/null +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/cloud_adapter.py @@ -0,0 +1,203 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json + +from typing import Awaitable, Callable, Optional + +from aiohttp.web import ( + Request, + Response, + json_response, + WebSocketResponse, + HTTPBadRequest, + HTTPMethodNotAllowed, + HTTPUnauthorized, + HTTPUnsupportedMediaType, +) +from botbuilder.core import ( + Bot, + CloudAdapterBase, + InvokeResponse, + serializer_helper, + TurnContext, +) +from botbuilder.core.streaming import ( + StreamingActivityProcessor, + StreamingHttpDriver, + StreamingRequestHandler, +) +from botbuilder.schema import Activity +from botbuilder.integration.aiohttp.streaming import AiohttpWebSocket +from botframework.connector import AsyncBfPipeline, BotFrameworkConnectorConfiguration +from botframework.connector.aio import ConnectorClient +from botframework.connector.auth import ( + AuthenticateRequestResult, + BotFrameworkAuthentication, + BotFrameworkAuthenticationFactory, + ConnectorFactory, + MicrosoftAppCredentials, +) + +from .bot_framework_http_adapter_integration_base import ( + BotFrameworkHttpAdapterIntegrationBase, +) + + +class CloudAdapter(CloudAdapterBase, BotFrameworkHttpAdapterIntegrationBase): + def __init__(self, bot_framework_authentication: BotFrameworkAuthentication = None): + """ + Initializes a new instance of the CloudAdapter class. + + :param bot_framework_authentication: Optional BotFrameworkAuthentication instance + """ + # pylint: disable=invalid-name + if not bot_framework_authentication: + bot_framework_authentication = BotFrameworkAuthenticationFactory.create() + + self._AUTH_HEADER_NAME = "authorization" + self._CHANNEL_ID_HEADER_NAME = "channelid" + super().__init__(bot_framework_authentication) + + async def process( + self, request: Request, bot: Bot, ws_response: WebSocketResponse = None + ) -> Optional[Response]: + if not request: + raise TypeError("request can't be None") + # if ws_response is None: + # raise TypeError("ws_response can't be None") + if not bot: + raise TypeError("bot can't be None") + try: + # Only GET requests for web socket connects are allowed + if ( + request.method == "GET" + and ws_response + and ws_response.can_prepare(request) + ): + # All socket communication will be handled by the internal streaming-specific BotAdapter + await self._connect(bot, request, ws_response) + elif request.method == "POST": + # Deserialize the incoming Activity + if "application/json" in request.headers["Content-Type"]: + body = await request.json() + else: + raise HTTPUnsupportedMediaType() + + activity: Activity = Activity().deserialize(body) + + # A POST request must contain an Activity + if not activity.type: + raise HTTPBadRequest + + # Grab the auth header from the inbound http request + auth_header = ( + request.headers["Authorization"] + if "Authorization" in request.headers + else "" + ) + + # Process the inbound activity with the bot + invoke_response = await self.process_activity( + auth_header, activity, bot.on_turn + ) + + # Write the response, serializing the InvokeResponse + if invoke_response: + return json_response( + data=serializer_helper(invoke_response.body), + status=invoke_response.status, + ) + return Response(status=201) + else: + raise HTTPMethodNotAllowed + except PermissionError: + raise HTTPUnauthorized + + async def _connect( + self, bot: Bot, request: Request, ws_response: WebSocketResponse + ): + if ws_response is None: + raise TypeError("ws_response can't be None") + + # Grab the auth header from the inbound http request + auth_header = request.headers.get(self._AUTH_HEADER_NAME) + # Grab the channelId which should be in the http headers + channel_id = request.headers.get(self._CHANNEL_ID_HEADER_NAME) + + authentication_request_result = ( + await self.bot_framework_authentication.authenticate_streaming_request( + auth_header, channel_id + ) + ) + + # Transition the request to a WebSocket connection + await ws_response.prepare(request) + bf_web_socket = AiohttpWebSocket(ws_response) + + streaming_activity_processor = _StreamingActivityProcessor( + authentication_request_result, self, bot, bf_web_socket + ) + + await streaming_activity_processor.listen() + + +class _StreamingActivityProcessor(StreamingActivityProcessor): + def __init__( + self, + authenticate_request_result: AuthenticateRequestResult, + adapter: CloudAdapter, + bot: Bot, + web_socket: AiohttpWebSocket = None, + ) -> None: + self._authenticate_request_result = authenticate_request_result + self._adapter = adapter + + # Internal reuse of the existing StreamingRequestHandler class + self._request_handler = StreamingRequestHandler(bot, self, web_socket) + + # Fix up the connector factory so connector create from it will send over this connection + self._authenticate_request_result.connector_factory = ( + _StreamingConnectorFactory(self._request_handler) + ) + + async def listen(self): + await self._request_handler.listen() + + async def process_streaming_activity( + self, + activity: Activity, + bot_callback_handler: Callable[[TurnContext], Awaitable], + ) -> InvokeResponse: + return await self._adapter.process_activity( + self._authenticate_request_result, activity, bot_callback_handler + ) + + +class _StreamingConnectorFactory(ConnectorFactory): + def __init__(self, request_handler: StreamingRequestHandler) -> None: + self._request_handler = request_handler + self._service_url = None + + async def create( + self, service_url: str, audience: str # pylint: disable=unused-argument + ) -> ConnectorClient: + if not self._service_url: + self._service_url = service_url + elif service_url != self._service_url: + raise RuntimeError( + "This is a streaming scenario, all connectors from this factory must all be for the same url." + ) + + # TODO: investigate if Driver and pipeline should be moved here + streaming_driver = StreamingHttpDriver(self._request_handler) + config = BotFrameworkConnectorConfiguration( + MicrosoftAppCredentials.empty(), + service_url, + pipeline_type=AsyncBfPipeline, + driver=streaming_driver, + ) + streaming_driver.config = config + connector_client = ConnectorClient(None, custom_configuration=config) + + return connector_client diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/configuration_bot_framework_authentication.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/configuration_bot_framework_authentication.py new file mode 100644 index 000000000..8ef9d3bf1 --- /dev/null +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/configuration_bot_framework_authentication.py @@ -0,0 +1,98 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger +from typing import Any + +from botbuilder.integration.aiohttp import ConfigurationServiceClientCredentialFactory +from botbuilder.schema import Activity +from botframework.connector import HttpClientFactory +from botframework.connector.auth import ( + BotFrameworkAuthentication, + ClaimsIdentity, + UserTokenClient, + ConnectorFactory, + AuthenticateRequestResult, + ServiceClientCredentialsFactory, + AuthenticationConfiguration, + BotFrameworkAuthenticationFactory, +) +from botframework.connector.skills import BotFrameworkClient + + +class ConfigurationBotFrameworkAuthentication(BotFrameworkAuthentication): + def __init__( + self, + configuration: Any, + *, + credentials_factory: ServiceClientCredentialsFactory = None, + auth_configuration: AuthenticationConfiguration = None, + http_client_factory: HttpClientFactory = None, + logger: Logger = None + ): + self._inner: BotFrameworkAuthentication = ( + BotFrameworkAuthenticationFactory.create( + channel_service=getattr(configuration, "CHANNEL_SERVICE", None), + validate_authority=getattr(configuration, "VALIDATE_AUTHORITY", True), + to_channel_from_bot_login_url=getattr( + configuration, "TO_CHANNEL_FROM_BOT_LOGIN_URL", None + ), + to_channel_from_bot_oauth_scope=getattr( + configuration, "TO_CHANNEL_FROM_BOT_OAUTH_SCOPE", None + ), + to_bot_from_channel_token_issuer=getattr( + configuration, "TO_BOT_FROM_CHANNEL_TOKEN_ISSUER", None + ), + oauth_url=getattr(configuration, "OAUTH_URL", None), + to_bot_from_channel_open_id_metadata_url=getattr( + configuration, "TO_BOT_FROM_CHANNEL_OPENID_METADATA_URL", None + ), + to_bot_from_emulator_open_id_metadata_url=getattr( + configuration, "TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL", None + ), + caller_id=getattr(configuration, "CALLER_ID", None), + credential_factory=( + credentials_factory + if credentials_factory + else ConfigurationServiceClientCredentialFactory(configuration) + ), + auth_configuration=( + auth_configuration + if auth_configuration + else AuthenticationConfiguration() + ), + http_client_factory=http_client_factory, + logger=logger, + ) + ) + + async def authenticate_request( + self, activity: Activity, auth_header: str + ) -> AuthenticateRequestResult: + return await self._inner.authenticate_request(activity, auth_header) + + async def authenticate_streaming_request( + self, auth_header: str, channel_id_header: str + ) -> AuthenticateRequestResult: + return await self._inner.authenticate_streaming_request( + auth_header, channel_id_header + ) + + def create_connector_factory( + self, claims_identity: ClaimsIdentity + ) -> ConnectorFactory: + return self._inner.create_connector_factory(claims_identity) + + async def create_user_token_client( + self, claims_identity: ClaimsIdentity + ) -> UserTokenClient: + return await self._inner.create_user_token_client(claims_identity) + + def create_bot_framework_client(self) -> BotFrameworkClient: + return self._inner.create_bot_framework_client() + + def get_originating_audience(self) -> str: + return self._inner.get_originating_audience() + + async def authenticate_channel_request(self, auth_header: str) -> ClaimsIdentity: + return await self._inner.authenticate_channel_request(auth_header) diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/configuration_service_client_credential_factory.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/configuration_service_client_credential_factory.py new file mode 100644 index 000000000..79a6437b7 --- /dev/null +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/configuration_service_client_credential_factory.py @@ -0,0 +1,94 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger +from typing import Any + +from msrest.authentication import Authentication + +from botframework.connector.auth import PasswordServiceClientCredentialFactory +from botframework.connector.auth import ManagedIdentityServiceClientCredentialsFactory +from botframework.connector.auth import ServiceClientCredentialsFactory + + +class ConfigurationServiceClientCredentialFactory(ServiceClientCredentialsFactory): + def __init__(self, configuration: Any, *, logger: Logger = None) -> None: + self._inner = None + + app_type = ( + configuration.APP_TYPE + if hasattr(configuration, "APP_TYPE") + else "MultiTenant" + ).lower() + + app_id = configuration.APP_ID if hasattr(configuration, "APP_ID") else None + app_password = ( + configuration.APP_PASSWORD + if hasattr(configuration, "APP_PASSWORD") + else None + ) + app_tenantid = None + + if app_type == "userassignedmsi": + if not app_id: + raise Exception("Property 'APP_ID' is expected in configuration object") + + app_tenantid = ( + configuration.APP_TENANTID + if hasattr(configuration, "APP_TENANTID") + else None + ) + if not app_tenantid: + raise Exception( + "Property 'APP_TENANTID' is expected in configuration object" + ) + + self._inner = ManagedIdentityServiceClientCredentialsFactory( + app_id, logger=logger + ) + + elif app_type == "singletenant": + app_tenantid = ( + configuration.APP_TENANTID + if hasattr(configuration, "APP_TENANTID") + else None + ) + + if not app_id: + raise Exception("Property 'APP_ID' is expected in configuration object") + if not app_password: + raise Exception( + "Property 'APP_PASSWORD' is expected in configuration object" + ) + if not app_tenantid: + raise Exception( + "Property 'APP_TENANTID' is expected in configuration object" + ) + + self._inner = PasswordServiceClientCredentialFactory( + app_id, app_password, app_tenantid, logger=logger + ) + + # Default to MultiTenant + else: + # Specifically not checking for appId or password to allow auth disabled scenario + self._inner = PasswordServiceClientCredentialFactory( + app_id, app_password, None, logger=logger + ) + + async def is_valid_app_id(self, app_id: str) -> bool: + return await self._inner.is_valid_app_id(app_id) + + async def is_authentication_disabled(self) -> bool: + return await self._inner.is_authentication_disabled() + + async def create_credentials( + self, + app_id: str, + oauth_scope: str, + login_endpoint: str, + validate_authority: bool, + ) -> Authentication: + return await self._inner.create_credentials( + app_id, oauth_scope, login_endpoint, validate_authority + ) diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/__init__.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/__init__.py index 71aaa71cf..6c95b5619 100644 --- a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/__init__.py +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/__init__.py @@ -1,4 +1,5 @@ +from .aio_http_client_factory import AioHttpClientFactory from .skill_http_client import SkillHttpClient -__all__ = ["SkillHttpClient"] +__all__ = ["AioHttpClientFactory", "SkillHttpClient"] diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/aio_http_client_factory.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/aio_http_client_factory.py new file mode 100644 index 000000000..62d4ae539 --- /dev/null +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/aio_http_client_factory.py @@ -0,0 +1,45 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from aiohttp import ClientSession, ClientResponse, ClientResponseError + +from botframework.connector import ( + HttpClientBase, + HttpClientFactory, + HttpRequest, + HttpResponseBase, +) + + +class _HttpResponseImpl(HttpResponseBase): + def __init__(self, client_response: ClientResponse) -> None: + self._client_response = client_response + + @property + def status_code(self): + return self._client_response.status + + async def is_succesful(self) -> bool: + try: + self._client_response.raise_for_status() + return True + except ClientResponseError: + return False + + async def read_content_str(self) -> str: + return (await self._client_response.read()).decode() + + +class _HttpClientImplementation(HttpClientBase): + async def post(self, *, request: HttpRequest) -> HttpResponseBase: + async with ClientSession() as session: + aio_response = await session.post( + request.request_uri, data=request.content, headers=request.headers + ) + + return _HttpResponseImpl(aio_response) + + +class AioHttpClientFactory(HttpClientFactory): + def create_client(self) -> HttpClientBase: + return _HttpClientImplementation() diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/skill_http_client.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/skill_http_client.py index df875f734..542287af2 100644 --- a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/skill_http_client.py +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/skills/skill_http_client.py @@ -45,12 +45,11 @@ async def post_activity_to_skill( activity: Activity, originating_audience: str = None, ) -> InvokeResponse: - if originating_audience is None: originating_audience = ( GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE if self._channel_provider is not None - and self._channel_provider.IsGovernment() + and self._channel_provider.is_government() else AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE ) @@ -61,8 +60,10 @@ async def post_activity_to_skill( bot_framework_skill=to_skill, ) - skill_conversation_id = await self._skill_conversation_id_factory.create_skill_conversation_id( - options + skill_conversation_id = ( + await self._skill_conversation_id_factory.create_skill_conversation_id( + options + ) ) return await super().post_activity( diff --git a/scenarios/file-upload/bots/__init__.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/streaming/__init__.py similarity index 52% rename from scenarios/file-upload/bots/__init__.py rename to libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/streaming/__init__.py index 9c28a0532..4d380bf47 100644 --- a/scenarios/file-upload/bots/__init__.py +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/streaming/__init__.py @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from .teams_file_bot import TeamsFileBot +from .aiohttp_web_socket import AiohttpWebSocket -__all__ = ["TeamsFileBot"] +__all__ = [ + "AiohttpWebSocket", +] diff --git a/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/streaming/aiohttp_web_socket.py b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/streaming/aiohttp_web_socket.py new file mode 100644 index 000000000..aa4a94a8e --- /dev/null +++ b/libraries/botbuilder-integration-aiohttp/botbuilder/integration/aiohttp/streaming/aiohttp_web_socket.py @@ -0,0 +1,81 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import asyncio +import traceback + +from typing import Any, Optional, Union + +from aiohttp import ClientWebSocketResponse, WSMsgType, ClientSession +from aiohttp.web import WebSocketResponse + +from botframework.streaming.transport.web_socket import ( + WebSocket, + WebSocketMessage, + WebSocketCloseStatus, + WebSocketMessageType, + WebSocketState, +) + + +class AiohttpWebSocket(WebSocket): + def __init__( + self, + aiohttp_ws: Union[WebSocketResponse, ClientWebSocketResponse], + session: Optional[ClientSession] = None, + ): + self._aiohttp_ws = aiohttp_ws + self._session = session + + def dispose(self): + if self._session: + task = asyncio.create_task(self._session.close()) + + async def close(self, close_status: WebSocketCloseStatus, status_description: str): + await self._aiohttp_ws.close( + code=int(close_status), message=status_description.encode("utf8") + ) + + async def receive(self) -> WebSocketMessage: + try: + message = await self._aiohttp_ws.receive() + + message_data = None + + if message.type == WSMsgType.TEXT: + message_data = list(str(message.data).encode("ascii")) + elif message.type == WSMsgType.BINARY: + message_data = list(message.data) + elif isinstance(message.data, int): + message_data = [] + + # async for message in self._aiohttp_ws: + return WebSocketMessage( + message_type=WebSocketMessageType(int(message.type)), data=message_data + ) + except Exception as error: + traceback.print_exc() + raise error + + async def send( + self, buffer: Any, message_type: WebSocketMessageType, end_of_message: bool + ): + is_closing = self._aiohttp_ws.closed + try: + if message_type == WebSocketMessageType.BINARY: + # TODO: The clening buffer line should be removed, just for bypassing bug in POC + clean_buffer = bytes([byte for byte in buffer if byte is not None]) + await self._aiohttp_ws.send_bytes(clean_buffer) + elif message_type == WebSocketMessageType.TEXT: + await self._aiohttp_ws.send_str(buffer) + else: + raise RuntimeError( + f"AiohttpWebSocket - message_type: {message_type} currently not supported" + ) + except Exception as error: + traceback.print_exc() + raise error + + @property + def status(self) -> WebSocketState: + return WebSocketState.CLOSED if self._aiohttp_ws.closed else WebSocketState.OPEN diff --git a/libraries/botbuilder-integration-aiohttp/requirements.txt b/libraries/botbuilder-integration-aiohttp/requirements.txt index 1d6f7ab31..d66ba0327 100644 --- a/libraries/botbuilder-integration-aiohttp/requirements.txt +++ b/libraries/botbuilder-integration-aiohttp/requirements.txt @@ -1,4 +1,4 @@ -msrest==0.6.10 -botframework-connector>=4.7.1 -botbuilder-schema>=4.7.1 -aiohttp>=3.6.2 \ No newline at end of file +msrest== 0.7.* +botframework-connector==4.17.0 +botbuilder-schema==4.17.0 +aiohttp==3.*.* diff --git a/libraries/botbuilder-integration-aiohttp/setup.py b/libraries/botbuilder-integration-aiohttp/setup.py index df1778810..2624c9dc8 100644 --- a/libraries/botbuilder-integration-aiohttp/setup.py +++ b/libraries/botbuilder-integration-aiohttp/setup.py @@ -4,12 +4,13 @@ import os from setuptools import setup -VERSION = os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" +VERSION = os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" REQUIRES = [ - "botbuilder-schema>=4.7.1", - "botframework-connector>=4.7.1", - "botbuilder-core>=4.7.1", - "aiohttp==3.6.2", + "botbuilder-schema==4.17.0", + "botframework-connector==4.17.0", + "botbuilder-core==4.17.0", + "yarl>=1.8.1", + "aiohttp>=3.10,<4.0", ] root = os.path.abspath(os.path.dirname(__file__)) @@ -41,6 +42,7 @@ packages=[ "botbuilder.integration.aiohttp", "botbuilder.integration.aiohttp.skills", + "botbuilder.integration.aiohttp.streaming", ], install_requires=REQUIRES, classifiers=[ diff --git a/libraries/botbuilder-integration-aiohttp/tests/skills/test_skill_http_client.py b/libraries/botbuilder-integration-aiohttp/tests/skills/test_skill_http_client.py new file mode 100644 index 000000000..eba3352e1 --- /dev/null +++ b/libraries/botbuilder-integration-aiohttp/tests/skills/test_skill_http_client.py @@ -0,0 +1,204 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import uuid4 +from typing import Awaitable, Callable, Dict, Union + + +from unittest.mock import Mock +import aiounittest + +from botbuilder.core import MessageFactory, InvokeResponse +from botbuilder.core.skills import ( + BotFrameworkSkill, + ConversationIdFactoryBase, + SkillConversationIdFactoryOptions, + SkillConversationReference, +) +from botbuilder.integration.aiohttp.skills import SkillHttpClient +from botbuilder.schema import Activity, ConversationAccount, ConversationReference +from botframework.connector.auth import ( + AuthenticationConstants, + ChannelProvider, + GovernmentConstants, +) + + +class SimpleConversationIdFactory(ConversationIdFactoryBase): + def __init__(self, conversation_id: str): + self._conversation_id = conversation_id + self._conversation_refs: Dict[str, SkillConversationReference] = {} + # Public property to capture and assert the options passed to CreateSkillConversationIdAsync. + self.creation_options: SkillConversationIdFactoryOptions = None + + async def create_skill_conversation_id( + self, + options_or_conversation_reference: Union[ + SkillConversationIdFactoryOptions, ConversationReference + ], + ) -> str: + self.creation_options = options_or_conversation_reference + + key = self._conversation_id + self._conversation_refs[key] = self._conversation_refs.get( + key, + SkillConversationReference( + conversation_reference=options_or_conversation_reference.activity.get_conversation_reference(), + oauth_scope=options_or_conversation_reference.from_bot_oauth_scope, + ), + ) + return key + + async def get_skill_conversation_reference( + self, skill_conversation_id: str + ) -> SkillConversationReference: + return self._conversation_refs[skill_conversation_id] + + async def delete_conversation_reference(self, skill_conversation_id: str): + raise NotImplementedError() + + +class TestSkillHttpClientTests(aiounittest.AsyncTestCase): + async def test_post_activity_with_originating_audience(self): + conversation_id = str(uuid4()) + conversation_id_factory = SimpleConversationIdFactory(conversation_id) + test_activity = MessageFactory.text("some message") + test_activity.conversation = ConversationAccount() + skill = BotFrameworkSkill( + id="SomeSkill", + app_id="", + skill_endpoint="https://someskill.com/api/messages", + ) + + async def _mock_post_content( + to_url: str, + token: str, # pylint: disable=unused-argument + activity: Activity, + ) -> (int, object): + nonlocal self + self.assertEqual(skill.skill_endpoint, to_url) + # Assert that the activity being sent has what we expect. + self.assertEqual(conversation_id, activity.conversation.id) + self.assertEqual("https://parentbot.com/api/messages", activity.service_url) + + # Create mock response. + return 200, None + + sut = await self._create_http_client_with_mock_handler( + _mock_post_content, conversation_id_factory + ) + + result = await sut.post_activity_to_skill( + "", + skill, + "https://parentbot.com/api/messages", + test_activity, + "someOriginatingAudience", + ) + + # Assert factory options + self.assertEqual("", conversation_id_factory.creation_options.from_bot_id) + self.assertEqual( + "someOriginatingAudience", + conversation_id_factory.creation_options.from_bot_oauth_scope, + ) + self.assertEqual( + test_activity, conversation_id_factory.creation_options.activity + ) + self.assertEqual( + skill, conversation_id_factory.creation_options.bot_framework_skill + ) + + # Assert result + self.assertIsInstance(result, InvokeResponse) + self.assertEqual(200, result.status) + + async def test_post_activity_using_invoke_response(self): + for is_gov in [True, False]: + with self.subTest(is_government=is_gov): + # pylint: disable=undefined-variable + # pylint: disable=cell-var-from-loop + conversation_id = str(uuid4()) + conversation_id_factory = SimpleConversationIdFactory(conversation_id) + test_activity = MessageFactory.text("some message") + test_activity.conversation = ConversationAccount() + expected_oauth_scope = ( + AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + ) + mock_channel_provider: ChannelProvider = Mock(spec=ChannelProvider) + + def is_government_mock(): + nonlocal expected_oauth_scope + if is_government: + expected_oauth_scope = ( + GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + ) + + return is_government + + mock_channel_provider.is_government = Mock( + side_effect=is_government_mock + ) + + skill = BotFrameworkSkill( + id="SomeSkill", + app_id="", + skill_endpoint="https://someskill.com/api/messages", + ) + + async def _mock_post_content( + to_url: str, + token: str, # pylint: disable=unused-argument + activity: Activity, + ) -> (int, object): + nonlocal self + + self.assertEqual(skill.skill_endpoint, to_url) + # Assert that the activity being sent has what we expect. + self.assertEqual(conversation_id, activity.conversation.id) + self.assertEqual( + "https://parentbot.com/api/messages", activity.service_url + ) + + # Create mock response. + return 200, None + + sut = await self._create_http_client_with_mock_handler( + _mock_post_content, conversation_id_factory + ) + result = await sut.post_activity_to_skill( + "", skill, "https://parentbot.com/api/messages", test_activity + ) + + # Assert factory options + self.assertEqual( + "", conversation_id_factory.creation_options.from_bot_id + ) + self.assertEqual( + expected_oauth_scope, + conversation_id_factory.creation_options.from_bot_oauth_scope, + ) + self.assertEqual( + test_activity, conversation_id_factory.creation_options.activity + ) + self.assertEqual( + skill, conversation_id_factory.creation_options.bot_framework_skill + ) + + # Assert result + self.assertIsInstance(result, InvokeResponse) + self.assertEqual(200, result.status) + + # Helper to create an HttpClient with a mock message handler that executes function argument to validate the request + # and mock a response. + async def _create_http_client_with_mock_handler( + self, + value_function: Callable[[object], Awaitable[object]], + id_factory: ConversationIdFactoryBase, + channel_provider: ChannelProvider = None, + ) -> SkillHttpClient: + # pylint: disable=protected-access + client = SkillHttpClient(Mock(), id_factory, channel_provider) + client._post_content = value_function + + return client diff --git a/libraries/botbuilder-integration-aiohttp/tests/test_bot_framework_http_client.py b/libraries/botbuilder-integration-aiohttp/tests/test_bot_framework_http_client.py index 7e01f390b..89ea01539 100644 --- a/libraries/botbuilder-integration-aiohttp/tests/test_bot_framework_http_client.py +++ b/libraries/botbuilder-integration-aiohttp/tests/test_bot_framework_http_client.py @@ -1,8 +1,72 @@ +from unittest.mock import Mock + import aiounittest +from botbuilder.schema import ConversationAccount, ChannelAccount, RoleTypes from botbuilder.integration.aiohttp import BotFrameworkHttpClient +from botframework.connector.auth import CredentialProvider, Activity class TestBotFrameworkHttpClient(aiounittest.AsyncTestCase): async def test_should_create_connector_client(self): with self.assertRaises(TypeError): BotFrameworkHttpClient(None) + + async def test_adds_recipient_and_sets_it_back_to_null(self): + mock_credential_provider = Mock(spec=CredentialProvider) + + # pylint: disable=unused-argument + async def _mock_post_content( + to_url: str, token: str, activity: Activity + ) -> (int, object): + nonlocal self + self.assertIsNotNone(activity.recipient) + return 200, None + + client = BotFrameworkHttpClient(credential_provider=mock_credential_provider) + client._post_content = _mock_post_content # pylint: disable=protected-access + + activity = Activity(conversation=ConversationAccount()) + + await client.post_activity( + None, + None, + "https://skillbot.com/api/messages", + "https://parentbot.com/api/messages", + "NewConversationId", + activity, + ) + + assert activity.recipient is None + + async def test_does_not_overwrite_non_null_recipient_values(self): + skill_recipient_id = "skillBot" + mock_credential_provider = Mock(spec=CredentialProvider) + + # pylint: disable=unused-argument + async def _mock_post_content( + to_url: str, token: str, activity: Activity + ) -> (int, object): + nonlocal self + self.assertIsNotNone(activity.recipient) + self.assertEqual(skill_recipient_id, activity.recipient.id) + return 200, None + + client = BotFrameworkHttpClient(credential_provider=mock_credential_provider) + client._post_content = _mock_post_content # pylint: disable=protected-access + + activity = Activity( + conversation=ConversationAccount(), + recipient=ChannelAccount(id=skill_recipient_id), + ) + + await client.post_activity( + None, + None, + "https://skillbot.com/api/messages", + "https://parentbot.com/api/messages", + "NewConversationId", + activity, + ) + + assert activity.recipient.id == skill_recipient_id + assert activity.recipient.role is RoleTypes.skill diff --git a/libraries/botbuilder-integration-applicationinsights-aiohttp/README.rst b/libraries/botbuilder-integration-applicationinsights-aiohttp/README.rst index 8479d7ea1..1b2e58f0f 100644 --- a/libraries/botbuilder-integration-applicationinsights-aiohttp/README.rst +++ b/libraries/botbuilder-integration-applicationinsights-aiohttp/README.rst @@ -3,12 +3,12 @@ BotBuilder-ApplicationInsights SDK extension for aiohttp ======================================================== -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch -.. image:: https://badge.fury.io/py/botbuilder-applicationinsights.svg - :target: https://badge.fury.io/py/botbuilder-applicationinsights +.. image:: https://badge.fury.io/py/botbuilder-integration-applicationinsights-aiohttp.svg + :target: https://badge.fury.io/py/botbuilder-integration-applicationinsights-aiohttp :alt: Latest PyPI package version Within the Bot Framework, BotBuilder-ApplicationInsights enables the Azure Application Insights service. @@ -22,7 +22,7 @@ How to Install .. code-block:: python - pip install botbuilder-applicationinsights-aiohttp + pip install botbuilder-integration-applicationinsights-aiohttp Documentation/Wiki diff --git a/libraries/botbuilder-integration-applicationinsights-aiohttp/botbuilder/integration/applicationinsights/aiohttp/about.py b/libraries/botbuilder-integration-applicationinsights-aiohttp/botbuilder/integration/applicationinsights/aiohttp/about.py index 552d52e6e..cfaca1e0f 100644 --- a/libraries/botbuilder-integration-applicationinsights-aiohttp/botbuilder/integration/applicationinsights/aiohttp/about.py +++ b/libraries/botbuilder-integration-applicationinsights-aiohttp/botbuilder/integration/applicationinsights/aiohttp/about.py @@ -6,7 +6,7 @@ __title__ = "botbuilder-integration-applicationinsights-aiohttp" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botbuilder-integration-applicationinsights-aiohttp/botbuilder/integration/applicationinsights/aiohttp/aiohttp_telemetry_middleware.py b/libraries/botbuilder-integration-applicationinsights-aiohttp/botbuilder/integration/applicationinsights/aiohttp/aiohttp_telemetry_middleware.py index acc0c69cc..d5dc7e2eb 100644 --- a/libraries/botbuilder-integration-applicationinsights-aiohttp/botbuilder/integration/applicationinsights/aiohttp/aiohttp_telemetry_middleware.py +++ b/libraries/botbuilder-integration-applicationinsights-aiohttp/botbuilder/integration/applicationinsights/aiohttp/aiohttp_telemetry_middleware.py @@ -6,8 +6,9 @@ def retrieve_aiohttp_body(): - """ retrieve_flask_body + """ Retrieve the POST body text from temporary cache. + The POST body corresponds with the thread id and should resides in cache just for lifetime of request. """ @@ -18,7 +19,10 @@ def retrieve_aiohttp_body(): @middleware async def bot_telemetry_middleware(request, handler): """Process the incoming Flask request.""" - if "application/json" in request.headers["Content-Type"]: + if ( + "Content-Type" in request.headers + and request.headers["Content-Type"] == "application/json" + ): body = await request.json() _REQUEST_BODIES[current_thread().ident] = body diff --git a/libraries/botbuilder-integration-applicationinsights-aiohttp/setup.py b/libraries/botbuilder-integration-applicationinsights-aiohttp/setup.py index ea8c2f359..78c32e5eb 100644 --- a/libraries/botbuilder-integration-applicationinsights-aiohttp/setup.py +++ b/libraries/botbuilder-integration-applicationinsights-aiohttp/setup.py @@ -6,11 +6,11 @@ REQUIRES = [ "applicationinsights>=0.11.9", - "aiohttp==3.6.2", - "botbuilder-schema>=4.7.1", - "botframework-connector>=4.7.1", - "botbuilder-core>=4.7.1", - "botbuilder-applicationinsights>=4.7.1", + "aiohttp>=3.10,<4.0", + "botbuilder-schema==4.17.0", + "botframework-connector==4.17.0", + "botbuilder-core==4.17.0", + "botbuilder-applicationinsights==4.17.0", ] TESTS_REQUIRES = [ "aiounittest==1.3.0", diff --git a/libraries/botbuilder-schema/README.rst b/libraries/botbuilder-schema/README.rst index abf3ae738..ff3eac173 100644 --- a/libraries/botbuilder-schema/README.rst +++ b/libraries/botbuilder-schema/README.rst @@ -3,8 +3,8 @@ BotBuilder-Schema ================= -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch .. image:: https://badge.fury.io/py/botbuilder-schema.svg diff --git a/libraries/botbuilder-schema/botbuilder/schema/__init__.py b/libraries/botbuilder-schema/botbuilder/schema/__init__.py index 78a35caa5..24d431b76 100644 --- a/libraries/botbuilder-schema/botbuilder/schema/__init__.py +++ b/libraries/botbuilder-schema/botbuilder/schema/__init__.py @@ -1,113 +1,63 @@ -# coding=utf-8 -# -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. -# -------------------------------------------------------------------------- +# Licensed under the MIT License. -try: - from ._models_py3 import Activity - from ._models_py3 import AnimationCard - from ._models_py3 import Attachment - from ._models_py3 import AttachmentData - from ._models_py3 import AttachmentInfo - from ._models_py3 import AttachmentView - from ._models_py3 import AudioCard - from ._models_py3 import BasicCard - from ._models_py3 import CardAction - from ._models_py3 import CardImage - from ._models_py3 import ChannelAccount - from ._models_py3 import ConversationAccount - from ._models_py3 import ConversationMembers - from ._models_py3 import ConversationParameters - from ._models_py3 import ConversationReference - from ._models_py3 import ConversationResourceResponse - from ._models_py3 import ConversationsResult - from ._models_py3 import ExpectedReplies - from ._models_py3 import Entity - from ._models_py3 import Error - from ._models_py3 import ErrorResponse, ErrorResponseException - from ._models_py3 import Fact - from ._models_py3 import GeoCoordinates - from ._models_py3 import HeroCard - from ._models_py3 import InnerHttpError - from ._models_py3 import MediaCard - from ._models_py3 import MediaEventValue - from ._models_py3 import MediaUrl - from ._models_py3 import Mention - from ._models_py3 import MessageReaction - from ._models_py3 import OAuthCard - from ._models_py3 import PagedMembersResult - from ._models_py3 import Place - from ._models_py3 import ReceiptCard - from ._models_py3 import ReceiptItem - from ._models_py3 import ResourceResponse - from ._models_py3 import SemanticAction - from ._models_py3 import SigninCard - from ._models_py3 import SuggestedActions - from ._models_py3 import TextHighlight - from ._models_py3 import Thing - from ._models_py3 import ThumbnailCard - from ._models_py3 import ThumbnailUrl - from ._models_py3 import TokenExchangeInvokeRequest - from ._models_py3 import TokenExchangeInvokeResponse - from ._models_py3 import TokenExchangeState - from ._models_py3 import TokenRequest - from ._models_py3 import TokenResponse - from ._models_py3 import Transcript - from ._models_py3 import VideoCard -except (SyntaxError, ImportError): - from ._models import Activity - from ._models import AnimationCard - from ._models import Attachment - from ._models import AttachmentData - from ._models import AttachmentInfo - from ._models import AttachmentView - from ._models import AudioCard - from ._models import BasicCard - from ._models import CardAction - from ._models import CardImage - from ._models import ChannelAccount - from ._models import ConversationAccount - from ._models import ConversationMembers - from ._models import ConversationParameters - from ._models import ConversationReference - from ._models import ConversationResourceResponse - from ._models import ConversationsResult - from ._models import ExpectedReplies - from ._models import Entity - from ._models import Error - from ._models import ErrorResponse, ErrorResponseException - from ._models import Fact - from ._models import GeoCoordinates - from ._models import HeroCard - from ._models import InnerHttpError - from ._models import MediaCard - from ._models import MediaEventValue - from ._models import MediaUrl - from ._models import Mention - from ._models import MessageReaction - from ._models import OAuthCard - from ._models import PagedMembersResult - from ._models import Place - from ._models import ReceiptCard - from ._models import ReceiptItem - from ._models import ResourceResponse - from ._models import SemanticAction - from ._models import SigninCard - from ._models import SuggestedActions - from ._models import TextHighlight - from ._models import Thing - from ._models import ThumbnailCard - from ._models import ThumbnailUrl - from ._models import TokenRequest - from ._models import TokenResponse - from ._models import Transcript - from ._models import VideoCard +from warnings import warn + +from ._models_py3 import Activity +from ._models_py3 import ActivityEventNames +from ._models_py3 import AdaptiveCardInvokeAction +from ._models_py3 import AdaptiveCardInvokeResponse +from ._models_py3 import AdaptiveCardInvokeValue +from ._models_py3 import AnimationCard +from ._models_py3 import Attachment +from ._models_py3 import AttachmentData +from ._models_py3 import AttachmentInfo +from ._models_py3 import AttachmentView +from ._models_py3 import AudioCard +from ._models_py3 import BasicCard +from ._models_py3 import CardAction +from ._models_py3 import CardImage +from ._models_py3 import ChannelAccount +from ._models_py3 import ConversationAccount +from ._models_py3 import ConversationMembers +from ._models_py3 import ConversationParameters +from ._models_py3 import ConversationReference +from ._models_py3 import ConversationResourceResponse +from ._models_py3 import ConversationsResult +from ._models_py3 import ExpectedReplies +from ._models_py3 import Entity +from ._models_py3 import Error +from ._models_py3 import ErrorResponse, ErrorResponseException +from ._models_py3 import Fact +from ._models_py3 import GeoCoordinates +from ._models_py3 import HeroCard +from ._models_py3 import InnerHttpError +from ._models_py3 import InvokeResponse +from ._models_py3 import MediaCard +from ._models_py3 import MediaEventValue +from ._models_py3 import MediaUrl +from ._models_py3 import Mention +from ._models_py3 import MessageReaction +from ._models_py3 import OAuthCard +from ._models_py3 import PagedMembersResult +from ._models_py3 import Place +from ._models_py3 import ReceiptCard +from ._models_py3 import ReceiptItem +from ._models_py3 import ResourceResponse +from ._models_py3 import SemanticAction +from ._models_py3 import SigninCard +from ._models_py3 import SuggestedActions +from ._models_py3 import TextHighlight +from ._models_py3 import Thing +from ._models_py3 import ThumbnailCard +from ._models_py3 import ThumbnailUrl +from ._models_py3 import TokenExchangeInvokeRequest +from ._models_py3 import TokenExchangeInvokeResponse +from ._models_py3 import TokenExchangeState +from ._models_py3 import TokenRequest +from ._models_py3 import TokenResponse +from ._models_py3 import Transcript +from ._models_py3 import VideoCard from ._connector_client_enums import ( ActionTypes, ActivityImportance, @@ -124,9 +74,15 @@ ) from ._sign_in_enums import SignInConstants +from .callerid_constants import CallerIdConstants +from .speech_constants import SpeechConstants __all__ = [ "Activity", + "ActivityEventNames", + "AdaptiveCardInvokeAction", + "AdaptiveCardInvokeResponse", + "AdaptiveCardInvokeValue", "AnimationCard", "Attachment", "AttachmentData", @@ -152,6 +108,7 @@ "GeoCoordinates", "HeroCard", "InnerHttpError", + "InvokeResponse", "MediaCard", "MediaEventValue", "MediaUrl", @@ -190,4 +147,6 @@ "DeliveryModes", "ContactRelationUpdateActionTypes", "InstallationUpdateActionTypes", + "CallerIdConstants", + "SpeechConstants", ] diff --git a/libraries/botbuilder-schema/botbuilder/schema/_connector_client_enums.py b/libraries/botbuilder-schema/botbuilder/schema/_connector_client_enums.py index d2027a277..c32031efa 100644 --- a/libraries/botbuilder-schema/botbuilder/schema/_connector_client_enums.py +++ b/libraries/botbuilder-schema/botbuilder/schema/_connector_client_enums.py @@ -1,25 +1,16 @@ -# coding=utf-8 -# -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. -# -------------------------------------------------------------------------- +# Licensed under the MIT License. from enum import Enum class RoleTypes(str, Enum): - user = "user" bot = "bot" + skill = "skill" class ActivityTypes(str, Enum): - message = "message" contact_relation_update = "contactRelationUpdate" conversation_update = "conversationUpdate" @@ -36,36 +27,33 @@ class ActivityTypes(str, Enum): suggestion = "suggestion" trace = "trace" handoff = "handoff" + command = "command" + command_result = "commandResult" class TextFormatTypes(str, Enum): - markdown = "markdown" plain = "plain" xml = "xml" class AttachmentLayoutTypes(str, Enum): - list = "list" carousel = "carousel" class MessageReactionTypes(str, Enum): - like = "like" plus_one = "plusOne" class InputHints(str, Enum): - accepting_input = "acceptingInput" ignoring_input = "ignoringInput" expecting_input = "expectingInput" class ActionTypes(str, Enum): - open_url = "openUrl" im_back = "imBack" post_back = "postBack" @@ -79,7 +67,6 @@ class ActionTypes(str, Enum): class EndOfConversationCodes(str, Enum): - unknown = "unknown" completed_successfully = "completedSuccessfully" user_cancelled = "userCancelled" @@ -89,14 +76,12 @@ class EndOfConversationCodes(str, Enum): class ActivityImportance(str, Enum): - low = "low" normal = "normal" high = "high" class DeliveryModes(str, Enum): - normal = "normal" notification = "notification" expect_replies = "expectReplies" @@ -104,19 +89,16 @@ class DeliveryModes(str, Enum): class ContactRelationUpdateActionTypes(str, Enum): - add = "add" remove = "remove" class InstallationUpdateActionTypes(str, Enum): - add = "add" remove = "remove" class SemanticActionStates(str, Enum): - start_action = "start" continue_action = "continue" done_action = "done" diff --git a/libraries/botbuilder-schema/botbuilder/schema/_models.py b/libraries/botbuilder-schema/botbuilder/schema/_models.py deleted file mode 100644 index 1b9599a61..000000000 --- a/libraries/botbuilder-schema/botbuilder/schema/_models.py +++ /dev/null @@ -1,1618 +0,0 @@ -# coding=utf-8 -# -------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. -# -------------------------------------------------------------------------- - -from msrest.serialization import Model -from msrest.exceptions import HttpOperationError - - -class Activity(Model): - """An Activity is the basic communication type for the Bot Framework 3.0 - protocol. - - :param type: Contains the activity type. Possible values include: - 'message', 'contactRelationUpdate', 'conversationUpdate', 'typing', - 'endOfConversation', 'event', 'invoke', 'deleteUserData', 'messageUpdate', - 'messageDelete', 'installationUpdate', 'messageReaction', 'suggestion', - 'trace', 'handoff' - :type type: str or ~botframework.connector.models.ActivityTypes - :param id: Contains an ID that uniquely identifies the activity on the - channel. - :type id: str - :param timestamp: Contains the date and time that the message was sent, in - UTC, expressed in ISO-8601 format. - :type timestamp: datetime - :param local_timestamp: Contains the local date and time of the message - expressed in ISO-8601 format. - For example, 2016-09-23T13:07:49.4714686-07:00. - :type local_timestamp: datetime - :param local_timezone: Contains the name of the local timezone of the message, - expressed in IANA Time Zone database format. - For example, America/Los_Angeles. - :type local_timezone: str - :param service_url: Contains the URL that specifies the channel's service - endpoint. Set by the channel. - :type service_url: str - :param channel_id: Contains an ID that uniquely identifies the channel. - Set by the channel. - :type channel_id: str - :param from_property: Identifies the sender of the message. - :type from_property: ~botframework.connector.models.ChannelAccount - :param conversation: Identifies the conversation to which the activity - belongs. - :type conversation: ~botframework.connector.models.ConversationAccount - :param recipient: Identifies the recipient of the message. - :type recipient: ~botframework.connector.models.ChannelAccount - :param text_format: Format of text fields Default:markdown. Possible - values include: 'markdown', 'plain', 'xml' - :type text_format: str or ~botframework.connector.models.TextFormatTypes - :param attachment_layout: The layout hint for multiple attachments. - Default: list. Possible values include: 'list', 'carousel' - :type attachment_layout: str or - ~botframework.connector.models.AttachmentLayoutTypes - :param members_added: The collection of members added to the conversation. - :type members_added: list[~botframework.connector.models.ChannelAccount] - :param members_removed: The collection of members removed from the - conversation. - :type members_removed: list[~botframework.connector.models.ChannelAccount] - :param reactions_added: The collection of reactions added to the - conversation. - :type reactions_added: - list[~botframework.connector.models.MessageReaction] - :param reactions_removed: The collection of reactions removed from the - conversation. - :type reactions_removed: - list[~botframework.connector.models.MessageReaction] - :param topic_name: The updated topic name of the conversation. - :type topic_name: str - :param history_disclosed: Indicates whether the prior history of the - channel is disclosed. - :type history_disclosed: bool - :param locale: A locale name for the contents of the text field. - The locale name is a combination of an ISO 639 two- or three-letter - culture code associated with a language - and an ISO 3166 two-letter subculture code associated with a country or - region. - The locale name can also correspond to a valid BCP-47 language tag. - :type locale: str - :param text: The text content of the message. - :type text: str - :param speak: The text to speak. - :type speak: str - :param input_hint: Indicates whether your bot is accepting, - expecting, or ignoring user input after the message is delivered to the - client. Possible values include: 'acceptingInput', 'ignoringInput', - 'expectingInput' - :type input_hint: str or ~botframework.connector.models.InputHints - :param summary: The text to display if the channel cannot render cards. - :type summary: str - :param suggested_actions: The suggested actions for the activity. - :type suggested_actions: ~botframework.connector.models.SuggestedActions - :param attachments: Attachments - :type attachments: list[~botframework.connector.models.Attachment] - :param entities: Represents the entities that were mentioned in the - message. - :type entities: list[~botframework.connector.models.Entity] - :param channel_data: Contains channel-specific content. - :type channel_data: object - :param action: Indicates whether the recipient of a contactRelationUpdate - was added or removed from the sender's contact list. - :type action: str - :param reply_to_id: Contains the ID of the message to which this message - is a reply. - :type reply_to_id: str - :param label: A descriptive label for the activity. - :type label: str - :param value_type: The type of the activity's value object. - :type value_type: str - :param value: A value that is associated with the activity. - :type value: object - :param name: The name of the operation associated with an invoke or event - activity. - :type name: str - :param relates_to: A reference to another conversation or activity. - :type relates_to: ~botframework.connector.models.ConversationReference - :param code: The a code for endOfConversation activities that indicates - why the conversation ended. Possible values include: 'unknown', - 'completedSuccessfully', 'userCancelled', 'botTimedOut', - 'botIssuedInvalidMessage', 'channelFailed' - :type code: str or ~botframework.connector.models.EndOfConversationCodes - :param expiration: The time at which the activity should be considered to - be "expired" and should not be presented to the recipient. - :type expiration: datetime - :param importance: The importance of the activity. Possible values - include: 'low', 'normal', 'high' - :type importance: str or ~botframework.connector.models.ActivityImportance - :param delivery_mode: A delivery hint to signal to the recipient alternate - delivery paths for the activity. - The default delivery mode is "default". Possible values include: 'normal', - 'notification', 'expectReplies', 'ephemeral' - :type delivery_mode: str or ~botframework.connector.models.DeliveryModes - :param listen_for: List of phrases and references that speech and language - priming systems should listen for - :type listen_for: list[str] - :param text_highlights: The collection of text fragments to highlight when - the activity contains a ReplyToId value. - :type text_highlights: list[~botframework.connector.models.TextHighlight] - :param semantic_action: An optional programmatic action accompanying this - request - :type semantic_action: ~botframework.connector.models.SemanticAction - :param caller_id: A string containing an IRI identifying the caller of a - bot. This field is not intended to be transmitted over the wire, but is - instead populated by bots and clients based on cryptographically - verifiable data that asserts the identity of the callers (e.g. tokens). - :type caller_id: str - """ - - _attribute_map = { - "type": {"key": "type", "type": "str"}, - "id": {"key": "id", "type": "str"}, - "timestamp": {"key": "timestamp", "type": "iso-8601"}, - "local_timestamp": {"key": "localTimestamp", "type": "iso-8601"}, - "local_timezone": {"key": "localTimezone", "type": "str"}, - "service_url": {"key": "serviceUrl", "type": "str"}, - "channel_id": {"key": "channelId", "type": "str"}, - "from_property": {"key": "from", "type": "ChannelAccount"}, - "conversation": {"key": "conversation", "type": "ConversationAccount"}, - "recipient": {"key": "recipient", "type": "ChannelAccount"}, - "text_format": {"key": "textFormat", "type": "str"}, - "attachment_layout": {"key": "attachmentLayout", "type": "str"}, - "members_added": {"key": "membersAdded", "type": "[ChannelAccount]"}, - "members_removed": {"key": "membersRemoved", "type": "[ChannelAccount]"}, - "reactions_added": {"key": "reactionsAdded", "type": "[MessageReaction]"}, - "reactions_removed": {"key": "reactionsRemoved", "type": "[MessageReaction]"}, - "topic_name": {"key": "topicName", "type": "str"}, - "history_disclosed": {"key": "historyDisclosed", "type": "bool"}, - "locale": {"key": "locale", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "speak": {"key": "speak", "type": "str"}, - "input_hint": {"key": "inputHint", "type": "str"}, - "summary": {"key": "summary", "type": "str"}, - "suggested_actions": {"key": "suggestedActions", "type": "SuggestedActions"}, - "attachments": {"key": "attachments", "type": "[Attachment]"}, - "entities": {"key": "entities", "type": "[Entity]"}, - "channel_data": {"key": "channelData", "type": "object"}, - "action": {"key": "action", "type": "str"}, - "reply_to_id": {"key": "replyToId", "type": "str"}, - "label": {"key": "label", "type": "str"}, - "value_type": {"key": "valueType", "type": "str"}, - "value": {"key": "value", "type": "object"}, - "name": {"key": "name", "type": "str"}, - "relates_to": {"key": "relatesTo", "type": "ConversationReference"}, - "code": {"key": "code", "type": "str"}, - "expiration": {"key": "expiration", "type": "iso-8601"}, - "importance": {"key": "importance", "type": "str"}, - "delivery_mode": {"key": "deliveryMode", "type": "str"}, - "listen_for": {"key": "listenFor", "type": "[str]"}, - "text_highlights": {"key": "textHighlights", "type": "[TextHighlight]"}, - "semantic_action": {"key": "semanticAction", "type": "SemanticAction"}, - "caller_id": {"key": "callerId", "type": "str"}, - } - - def __init__(self, **kwargs): - super(Activity, self).__init__(**kwargs) - self.type = kwargs.get("type", None) - self.id = kwargs.get("id", None) - self.timestamp = kwargs.get("timestamp", None) - self.local_timestamp = kwargs.get("local_timestamp", None) - self.local_timezone = kwargs.get("local_timezone", None) - self.service_url = kwargs.get("service_url", None) - self.channel_id = kwargs.get("channel_id", None) - self.from_property = kwargs.get("from_property", None) - self.conversation = kwargs.get("conversation", None) - self.recipient = kwargs.get("recipient", None) - self.text_format = kwargs.get("text_format", None) - self.attachment_layout = kwargs.get("attachment_layout", None) - self.members_added = kwargs.get("members_added", None) - self.members_removed = kwargs.get("members_removed", None) - self.reactions_added = kwargs.get("reactions_added", None) - self.reactions_removed = kwargs.get("reactions_removed", None) - self.topic_name = kwargs.get("topic_name", None) - self.history_disclosed = kwargs.get("history_disclosed", None) - self.locale = kwargs.get("locale", None) - self.text = kwargs.get("text", None) - self.speak = kwargs.get("speak", None) - self.input_hint = kwargs.get("input_hint", None) - self.summary = kwargs.get("summary", None) - self.suggested_actions = kwargs.get("suggested_actions", None) - self.attachments = kwargs.get("attachments", None) - self.entities = kwargs.get("entities", None) - self.channel_data = kwargs.get("channel_data", None) - self.action = kwargs.get("action", None) - self.reply_to_id = kwargs.get("reply_to_id", None) - self.label = kwargs.get("label", None) - self.value_type = kwargs.get("value_type", None) - self.value = kwargs.get("value", None) - self.name = kwargs.get("name", None) - self.relates_to = kwargs.get("relates_to", None) - self.code = kwargs.get("code", None) - self.expiration = kwargs.get("expiration", None) - self.importance = kwargs.get("importance", None) - self.delivery_mode = kwargs.get("delivery_mode", None) - self.listen_for = kwargs.get("listen_for", None) - self.text_highlights = kwargs.get("text_highlights", None) - self.semantic_action = kwargs.get("semantic_action", None) - self.caller_id = kwargs.get("caller_id", None) - - -class AnimationCard(Model): - """An animation card (Ex: gif or short video clip). - - :param title: Title of this card - :type title: str - :param subtitle: Subtitle of this card - :type subtitle: str - :param text: Text of this card - :type text: str - :param image: Thumbnail placeholder - :type image: ~botframework.connector.models.ThumbnailUrl - :param media: Media URLs for this card. When this field contains more than - one URL, each URL is an alternative format of the same content. - :type media: list[~botframework.connector.models.MediaUrl] - :param buttons: Actions on this card - :type buttons: list[~botframework.connector.models.CardAction] - :param shareable: This content may be shared with others (default:true) - :type shareable: bool - :param autoloop: Should the client loop playback at end of content - (default:true) - :type autoloop: bool - :param autostart: Should the client automatically start playback of media - in this card (default:true) - :type autostart: bool - :param aspect: Aspect ratio of thumbnail/media placeholder. Allowed values - are "16:9" and "4:3" - :type aspect: str - :param duration: Describes the length of the media content without - requiring a receiver to open the content. Formatted as an ISO 8601 - Duration field. - :type duration: str - :param value: Supplementary parameter for this card - :type value: object - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "subtitle": {"key": "subtitle", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "image": {"key": "image", "type": "ThumbnailUrl"}, - "media": {"key": "media", "type": "[MediaUrl]"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - "shareable": {"key": "shareable", "type": "bool"}, - "autoloop": {"key": "autoloop", "type": "bool"}, - "autostart": {"key": "autostart", "type": "bool"}, - "aspect": {"key": "aspect", "type": "str"}, - "duration": {"key": "duration", "type": "str"}, - "value": {"key": "value", "type": "object"}, - } - - def __init__(self, **kwargs): - super(AnimationCard, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.subtitle = kwargs.get("subtitle", None) - self.text = kwargs.get("text", None) - self.image = kwargs.get("image", None) - self.media = kwargs.get("media", None) - self.buttons = kwargs.get("buttons", None) - self.shareable = kwargs.get("shareable", None) - self.autoloop = kwargs.get("autoloop", None) - self.autostart = kwargs.get("autostart", None) - self.aspect = kwargs.get("aspect", None) - self.duration = kwargs.get("duration", None) - self.value = kwargs.get("value", None) - - -class Attachment(Model): - """An attachment within an activity. - - :param content_type: mimetype/Contenttype for the file - :type content_type: str - :param content_url: Content Url - :type content_url: str - :param content: Embedded content - :type content: object - :param name: (OPTIONAL) The name of the attachment - :type name: str - :param thumbnail_url: (OPTIONAL) Thumbnail associated with attachment - :type thumbnail_url: str - """ - - _attribute_map = { - "content_type": {"key": "contentType", "type": "str"}, - "content_url": {"key": "contentUrl", "type": "str"}, - "content": {"key": "content", "type": "object"}, - "name": {"key": "name", "type": "str"}, - "thumbnail_url": {"key": "thumbnailUrl", "type": "str"}, - } - - def __init__(self, **kwargs): - super(Attachment, self).__init__(**kwargs) - self.content_type = kwargs.get("content_type", None) - self.content_url = kwargs.get("content_url", None) - self.content = kwargs.get("content", None) - self.name = kwargs.get("name", None) - self.thumbnail_url = kwargs.get("thumbnail_url", None) - - -class AttachmentData(Model): - """Attachment data. - - :param type: Content-Type of the attachment - :type type: str - :param name: Name of the attachment - :type name: str - :param original_base64: Attachment content - :type original_base64: bytearray - :param thumbnail_base64: Attachment thumbnail - :type thumbnail_base64: bytearray - """ - - _attribute_map = { - "type": {"key": "type", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "original_base64": {"key": "originalBase64", "type": "bytearray"}, - "thumbnail_base64": {"key": "thumbnailBase64", "type": "bytearray"}, - } - - def __init__(self, **kwargs): - super(AttachmentData, self).__init__(**kwargs) - self.type = kwargs.get("type", None) - self.name = kwargs.get("name", None) - self.original_base64 = kwargs.get("original_base64", None) - self.thumbnail_base64 = kwargs.get("thumbnail_base64", None) - - -class AttachmentInfo(Model): - """Metadata for an attachment. - - :param name: Name of the attachment - :type name: str - :param type: ContentType of the attachment - :type type: str - :param views: attachment views - :type views: list[~botframework.connector.models.AttachmentView] - """ - - _attribute_map = { - "name": {"key": "name", "type": "str"}, - "type": {"key": "type", "type": "str"}, - "views": {"key": "views", "type": "[AttachmentView]"}, - } - - def __init__(self, **kwargs): - super(AttachmentInfo, self).__init__(**kwargs) - self.name = kwargs.get("name", None) - self.type = kwargs.get("type", None) - self.views = kwargs.get("views", None) - - -class AttachmentView(Model): - """Attachment View name and size. - - :param view_id: Id of the attachment - :type view_id: str - :param size: Size of the attachment - :type size: int - """ - - _attribute_map = { - "view_id": {"key": "viewId", "type": "str"}, - "size": {"key": "size", "type": "int"}, - } - - def __init__(self, **kwargs): - super(AttachmentView, self).__init__(**kwargs) - self.view_id = kwargs.get("view_id", None) - self.size = kwargs.get("size", None) - - -class AudioCard(Model): - """Audio card. - - :param title: Title of this card - :type title: str - :param subtitle: Subtitle of this card - :type subtitle: str - :param text: Text of this card - :type text: str - :param image: Thumbnail placeholder - :type image: ~botframework.connector.models.ThumbnailUrl - :param media: Media URLs for this card. When this field contains more than - one URL, each URL is an alternative format of the same content. - :type media: list[~botframework.connector.models.MediaUrl] - :param buttons: Actions on this card - :type buttons: list[~botframework.connector.models.CardAction] - :param shareable: This content may be shared with others (default:true) - :type shareable: bool - :param autoloop: Should the client loop playback at end of content - (default:true) - :type autoloop: bool - :param autostart: Should the client automatically start playback of media - in this card (default:true) - :type autostart: bool - :param aspect: Aspect ratio of thumbnail/media placeholder. Allowed values - are "16:9" and "4:3" - :type aspect: str - :param duration: Describes the length of the media content without - requiring a receiver to open the content. Formatted as an ISO 8601 - Duration field. - :type duration: str - :param value: Supplementary parameter for this card - :type value: object - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "subtitle": {"key": "subtitle", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "image": {"key": "image", "type": "ThumbnailUrl"}, - "media": {"key": "media", "type": "[MediaUrl]"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - "shareable": {"key": "shareable", "type": "bool"}, - "autoloop": {"key": "autoloop", "type": "bool"}, - "autostart": {"key": "autostart", "type": "bool"}, - "aspect": {"key": "aspect", "type": "str"}, - "duration": {"key": "duration", "type": "str"}, - "value": {"key": "value", "type": "object"}, - } - - def __init__(self, **kwargs): - super(AudioCard, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.subtitle = kwargs.get("subtitle", None) - self.text = kwargs.get("text", None) - self.image = kwargs.get("image", None) - self.media = kwargs.get("media", None) - self.buttons = kwargs.get("buttons", None) - self.shareable = kwargs.get("shareable", None) - self.autoloop = kwargs.get("autoloop", None) - self.autostart = kwargs.get("autostart", None) - self.aspect = kwargs.get("aspect", None) - self.duration = kwargs.get("duration", None) - self.value = kwargs.get("value", None) - - -class BasicCard(Model): - """A basic card. - - :param title: Title of the card - :type title: str - :param subtitle: Subtitle of the card - :type subtitle: str - :param text: Text for the card - :type text: str - :param images: Array of images for the card - :type images: list[~botframework.connector.models.CardImage] - :param buttons: Set of actions applicable to the current card - :type buttons: list[~botframework.connector.models.CardAction] - :param tap: This action will be activated when user taps on the card - itself - :type tap: ~botframework.connector.models.CardAction - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "subtitle": {"key": "subtitle", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "images": {"key": "images", "type": "[CardImage]"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - "tap": {"key": "tap", "type": "CardAction"}, - } - - def __init__(self, **kwargs): - super(BasicCard, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.subtitle = kwargs.get("subtitle", None) - self.text = kwargs.get("text", None) - self.images = kwargs.get("images", None) - self.buttons = kwargs.get("buttons", None) - self.tap = kwargs.get("tap", None) - - -class CardAction(Model): - """A clickable action. - - :param type: The type of action implemented by this button. Possible - values include: 'openUrl', 'imBack', 'postBack', 'playAudio', 'playVideo', - 'showImage', 'downloadFile', 'signin', 'call', 'messageBack' - :type type: str or ~botframework.connector.models.ActionTypes - :param title: Text description which appears on the button - :type title: str - :param image: Image URL which will appear on the button, next to text - label - :type image: str - :param text: Text for this action - :type text: str - :param display_text: (Optional) text to display in the chat feed if the - button is clicked - :type display_text: str - :param value: Supplementary parameter for action. Content of this property - depends on the ActionType - :type value: object - :param channel_data: Channel-specific data associated with this action - :type channel_data: object - """ - - _attribute_map = { - "type": {"key": "type", "type": "str"}, - "title": {"key": "title", "type": "str"}, - "image": {"key": "image", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "display_text": {"key": "displayText", "type": "str"}, - "value": {"key": "value", "type": "object"}, - "channel_data": {"key": "channelData", "type": "object"}, - } - - def __init__(self, **kwargs): - super(CardAction, self).__init__(**kwargs) - self.type = kwargs.get("type", None) - self.title = kwargs.get("title", None) - self.image = kwargs.get("image", None) - self.text = kwargs.get("text", None) - self.display_text = kwargs.get("display_text", None) - self.value = kwargs.get("value", None) - self.channel_data = kwargs.get("channel_data", None) - - -class CardImage(Model): - """An image on a card. - - :param url: URL thumbnail image for major content property - :type url: str - :param alt: Image description intended for screen readers - :type alt: str - :param tap: Action assigned to specific Attachment - :type tap: ~botframework.connector.models.CardAction - """ - - _attribute_map = { - "url": {"key": "url", "type": "str"}, - "alt": {"key": "alt", "type": "str"}, - "tap": {"key": "tap", "type": "CardAction"}, - } - - def __init__(self, **kwargs): - super(CardImage, self).__init__(**kwargs) - self.url = kwargs.get("url", None) - self.alt = kwargs.get("alt", None) - self.tap = kwargs.get("tap", None) - - -class ChannelAccount(Model): - """Channel account information needed to route a message. - - :param id: Channel id for the user or bot on this channel (Example: - joe@smith.com, or @joesmith or 123456) - :type id: str - :param name: Display friendly name - :type name: str - :param aad_object_id: This account's object ID within Azure Active - Directory (AAD) - :type aad_object_id: str - :param role: Role of the entity behind the account (Example: User, Bot, - etc.). Possible values include: 'user', 'bot' - :type role: str or ~botframework.connector.models.RoleTypes - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "aad_object_id": {"key": "aadObjectId", "type": "str"}, - "role": {"key": "role", "type": "str"}, - } - - def __init__(self, **kwargs): - super(ChannelAccount, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - self.name = kwargs.get("name", None) - self.aad_object_id = kwargs.get("aadObjectId", None) - self.role = kwargs.get("role", None) - - -class ConversationAccount(Model): - """Conversation account represents the identity of the conversation within a channel. - - :param is_group: Indicates whether the conversation contains more than two - participants at the time the activity was generated - :type is_group: bool - :param conversation_type: Indicates the type of the conversation in - channels that distinguish between conversation types - :type conversation_type: str - :param id: Channel id for the user or bot on this channel (Example: - joe@smith.com, or @joesmith or 123456) - :type id: str - :param name: Display friendly name - :type name: str - :param aad_object_id: This account's object ID within Azure Active - Directory (AAD) - :type aad_object_id: str - :param role: Role of the entity behind the account (Example: User, Bot, - etc.). Possible values include: 'user', 'bot' - :type role: str or ~botframework.connector.models.RoleTypes - :param tenant_id: This conversation's tenant ID - :type tenant_id: str - """ - - _attribute_map = { - "is_group": {"key": "isGroup", "type": "bool"}, - "conversation_type": {"key": "conversationType", "type": "str"}, - "id": {"key": "id", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "aad_object_id": {"key": "aadObjectId", "type": "str"}, - "role": {"key": "role", "type": "str"}, - "tenant_id": {"key": "tenantID", "type": "str"}, - } - - def __init__(self, **kwargs): - super(ConversationAccount, self).__init__(**kwargs) - self.is_group = kwargs.get("is_group", None) - self.conversation_type = kwargs.get("conversation_type", None) - self.id = kwargs.get("id", None) - self.name = kwargs.get("name", None) - self.aad_object_id = kwargs.get("aad_object_id", None) - self.role = kwargs.get("role", None) - self.tenant_id = kwargs.get("tenant_id", None) - - -class ConversationMembers(Model): - """Conversation and its members. - - :param id: Conversation ID - :type id: str - :param members: List of members in this conversation - :type members: list[~botframework.connector.models.ChannelAccount] - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - "members": {"key": "members", "type": "[ChannelAccount]"}, - } - - def __init__(self, **kwargs): - super(ConversationMembers, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - self.members = kwargs.get("members", None) - - -class ConversationParameters(Model): - """Parameters for creating a new conversation. - - :param is_group: IsGroup - :type is_group: bool - :param bot: The bot address for this conversation - :type bot: ~botframework.connector.models.ChannelAccount - :param members: Members to add to the conversation - :type members: list[~botframework.connector.models.ChannelAccount] - :param topic_name: (Optional) Topic of the conversation (if supported by - the channel) - :type topic_name: str - :param activity: (Optional) When creating a new conversation, use this - activity as the initial message to the conversation - :type activity: ~botframework.connector.models.Activity - :param channel_data: Channel specific payload for creating the - conversation - :type channel_data: object - :param tenant_id: (Optional) The tenant ID in which the conversation should be created - :type tenant_id: str - """ - - _attribute_map = { - "is_group": {"key": "isGroup", "type": "bool"}, - "bot": {"key": "bot", "type": "ChannelAccount"}, - "members": {"key": "members", "type": "[ChannelAccount]"}, - "topic_name": {"key": "topicName", "type": "str"}, - "activity": {"key": "activity", "type": "Activity"}, - "channel_data": {"key": "channelData", "type": "object"}, - "tenant_id": {"key": "tenantID", "type": "str"}, - } - - def __init__(self, **kwargs): - super(ConversationParameters, self).__init__(**kwargs) - self.is_group = kwargs.get("is_group", None) - self.bot = kwargs.get("bot", None) - self.members = kwargs.get("members", None) - self.topic_name = kwargs.get("topic_name", None) - self.activity = kwargs.get("activity", None) - self.channel_data = kwargs.get("channel_data", None) - self.tenant_id = kwargs.get("tenant_id", None) - - -class ConversationReference(Model): - """An object relating to a particular point in a conversation. - - :param activity_id: (Optional) ID of the activity to refer to - :type activity_id: str - :param user: (Optional) User participating in this conversation - :type user: ~botframework.connector.models.ChannelAccount - :param bot: Bot participating in this conversation - :type bot: ~botframework.connector.models.ChannelAccount - :param conversation: Conversation reference - :type conversation: ~botframework.connector.models.ConversationAccount - :param channel_id: Channel ID - :type channel_id: str - :param service_url: Service endpoint where operations concerning the - referenced conversation may be performed - :type service_url: str - """ - - _attribute_map = { - "activity_id": {"key": "activityId", "type": "str"}, - "user": {"key": "user", "type": "ChannelAccount"}, - "bot": {"key": "bot", "type": "ChannelAccount"}, - "conversation": {"key": "conversation", "type": "ConversationAccount"}, - "channel_id": {"key": "channelId", "type": "str"}, - "service_url": {"key": "serviceUrl", "type": "str"}, - } - - def __init__(self, **kwargs): - super(ConversationReference, self).__init__(**kwargs) - self.activity_id = kwargs.get("activity_id", None) - self.user = kwargs.get("user", None) - self.bot = kwargs.get("bot", None) - self.conversation = kwargs.get("conversation", None) - self.channel_id = kwargs.get("channel_id", None) - self.service_url = kwargs.get("service_url", None) - - -class ConversationResourceResponse(Model): - """A response containing a resource. - - :param activity_id: ID of the Activity (if sent) - :type activity_id: str - :param service_url: Service endpoint where operations concerning the - conversation may be performed - :type service_url: str - :param id: Id of the resource - :type id: str - """ - - _attribute_map = { - "activity_id": {"key": "activityId", "type": "str"}, - "service_url": {"key": "serviceUrl", "type": "str"}, - "id": {"key": "id", "type": "str"}, - } - - def __init__(self, **kwargs): - super(ConversationResourceResponse, self).__init__(**kwargs) - self.activity_id = kwargs.get("activity_id", None) - self.service_url = kwargs.get("service_url", None) - self.id = kwargs.get("id", None) - - -class ConversationsResult(Model): - """Conversations result. - - :param continuation_token: Paging token - :type continuation_token: str - :param conversations: List of conversations - :type conversations: - list[~botframework.connector.models.ConversationMembers] - """ - - _attribute_map = { - "continuation_token": {"key": "continuationToken", "type": "str"}, - "conversations": {"key": "conversations", "type": "[ConversationMembers]"}, - } - - def __init__(self, **kwargs): - super(ConversationsResult, self).__init__(**kwargs) - self.continuation_token = kwargs.get("continuation_token", None) - self.conversations = kwargs.get("conversations", None) - - -class ExpectedReplies(Model): - """ExpectedReplies. - - :param activities: A collection of Activities that conforms to the - ExpectedReplies schema. - :type activities: list[~botframework.connector.models.Activity] - """ - - _attribute_map = {"activities": {"key": "activities", "type": "[Activity]"}} - - def __init__(self, **kwargs): - super(ExpectedReplies, self).__init__(**kwargs) - self.activities = kwargs.get("activities", None) - - -class Entity(Model): - """Metadata object pertaining to an activity. - - :param type: Type of this entity (RFC 3987 IRI) - :type type: str - """ - - _attribute_map = {"type": {"key": "type", "type": "str"}} - - def __init__(self, **kwargs): - super(Entity, self).__init__(**kwargs) - self.type = kwargs.get("type", None) - - -class Error(Model): - """Object representing error information. - - :param code: Error code - :type code: str - :param message: Error message - :type message: str - :param inner_http_error: Error from inner http call - :type inner_http_error: ~botframework.connector.models.InnerHttpError - """ - - _attribute_map = { - "code": {"key": "code", "type": "str"}, - "message": {"key": "message", "type": "str"}, - "inner_http_error": {"key": "innerHttpError", "type": "InnerHttpError"}, - } - - def __init__(self, **kwargs): - super(Error, self).__init__(**kwargs) - self.code = kwargs.get("code", None) - self.message = kwargs.get("message", None) - self.inner_http_error = kwargs.get("inner_http_error", None) - - -class ErrorResponse(Model): - """An HTTP API response. - - :param error: Error message - :type error: ~botframework.connector.models.Error - """ - - _attribute_map = {"error": {"key": "error", "type": "Error"}} - - def __init__(self, **kwargs): - super(ErrorResponse, self).__init__(**kwargs) - self.error = kwargs.get("error", None) - - -class ErrorResponseException(HttpOperationError): - """Server responsed with exception of type: 'ErrorResponse'. - - :param deserialize: A deserializer - :param response: Server response to be deserialized. - """ - - def __init__(self, deserialize, response, *args): - - super(ErrorResponseException, self).__init__( - deserialize, response, "ErrorResponse", *args - ) - - -class Fact(Model): - """Set of key-value pairs. Advantage of this section is that key and value - properties will be - rendered with default style information with some delimiter between them. - So there is no need for developer to specify style information. - - :param key: The key for this Fact - :type key: str - :param value: The value for this Fact - :type value: str - """ - - _attribute_map = { - "key": {"key": "key", "type": "str"}, - "value": {"key": "value", "type": "str"}, - } - - def __init__(self, **kwargs): - super(Fact, self).__init__(**kwargs) - self.key = kwargs.get("key", None) - self.value = kwargs.get("value", None) - - -class GeoCoordinates(Model): - """GeoCoordinates (entity type: "https://schema.org/GeoCoordinates"). - - :param elevation: Elevation of the location [WGS - 84](https://en.wikipedia.org/wiki/World_Geodetic_System) - :type elevation: float - :param latitude: Latitude of the location [WGS - 84](https://en.wikipedia.org/wiki/World_Geodetic_System) - :type latitude: float - :param longitude: Longitude of the location [WGS - 84](https://en.wikipedia.org/wiki/World_Geodetic_System) - :type longitude: float - :param type: The type of the thing - :type type: str - :param name: The name of the thing - :type name: str - """ - - _attribute_map = { - "elevation": {"key": "elevation", "type": "float"}, - "latitude": {"key": "latitude", "type": "float"}, - "longitude": {"key": "longitude", "type": "float"}, - "type": {"key": "type", "type": "str"}, - "name": {"key": "name", "type": "str"}, - } - - def __init__(self, **kwargs): - super(GeoCoordinates, self).__init__(**kwargs) - self.elevation = kwargs.get("elevation", None) - self.latitude = kwargs.get("latitude", None) - self.longitude = kwargs.get("longitude", None) - self.type = kwargs.get("type", None) - self.name = kwargs.get("name", None) - - -class HeroCard(Model): - """A Hero card (card with a single, large image). - - :param title: Title of the card - :type title: str - :param subtitle: Subtitle of the card - :type subtitle: str - :param text: Text for the card - :type text: str - :param images: Array of images for the card - :type images: list[~botframework.connector.models.CardImage] - :param buttons: Set of actions applicable to the current card - :type buttons: list[~botframework.connector.models.CardAction] - :param tap: This action will be activated when user taps on the card - itself - :type tap: ~botframework.connector.models.CardAction - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "subtitle": {"key": "subtitle", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "images": {"key": "images", "type": "[CardImage]"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - "tap": {"key": "tap", "type": "CardAction"}, - } - - def __init__(self, **kwargs): - super(HeroCard, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.subtitle = kwargs.get("subtitle", None) - self.text = kwargs.get("text", None) - self.images = kwargs.get("images", None) - self.buttons = kwargs.get("buttons", None) - self.tap = kwargs.get("tap", None) - - -class InnerHttpError(Model): - """Object representing inner http error. - - :param status_code: HttpStatusCode from failed request - :type status_code: int - :param body: Body from failed request - :type body: object - """ - - _attribute_map = { - "status_code": {"key": "statusCode", "type": "int"}, - "body": {"key": "body", "type": "object"}, - } - - def __init__(self, **kwargs): - super(InnerHttpError, self).__init__(**kwargs) - self.status_code = kwargs.get("status_code", None) - self.body = kwargs.get("body", None) - - -class MediaCard(Model): - """Media card. - - :param title: Title of this card - :type title: str - :param subtitle: Subtitle of this card - :type subtitle: str - :param text: Text of this card - :type text: str - :param image: Thumbnail placeholder - :type image: ~botframework.connector.models.ThumbnailUrl - :param media: Media URLs for this card. When this field contains more than - one URL, each URL is an alternative format of the same content. - :type media: list[~botframework.connector.models.MediaUrl] - :param buttons: Actions on this card - :type buttons: list[~botframework.connector.models.CardAction] - :param shareable: This content may be shared with others (default:true) - :type shareable: bool - :param autoloop: Should the client loop playback at end of content - (default:true) - :type autoloop: bool - :param autostart: Should the client automatically start playback of media - in this card (default:true) - :type autostart: bool - :param aspect: Aspect ratio of thumbnail/media placeholder. Allowed values - are "16:9" and "4:3" - :type aspect: str - :param duration: Describes the length of the media content without - requiring a receiver to open the content. Formatted as an ISO 8601 - Duration field. - :type duration: str - :param value: Supplementary parameter for this card - :type value: object - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "subtitle": {"key": "subtitle", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "image": {"key": "image", "type": "ThumbnailUrl"}, - "media": {"key": "media", "type": "[MediaUrl]"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - "shareable": {"key": "shareable", "type": "bool"}, - "autoloop": {"key": "autoloop", "type": "bool"}, - "autostart": {"key": "autostart", "type": "bool"}, - "aspect": {"key": "aspect", "type": "str"}, - "duration": {"key": "duration", "type": "str"}, - "value": {"key": "value", "type": "object"}, - } - - def __init__(self, **kwargs): - super(MediaCard, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.subtitle = kwargs.get("subtitle", None) - self.text = kwargs.get("text", None) - self.image = kwargs.get("image", None) - self.media = kwargs.get("media", None) - self.buttons = kwargs.get("buttons", None) - self.shareable = kwargs.get("shareable", None) - self.autoloop = kwargs.get("autoloop", None) - self.autostart = kwargs.get("autostart", None) - self.aspect = kwargs.get("aspect", None) - self.duration = kwargs.get("duration", None) - self.value = kwargs.get("value", None) - - -class MediaEventValue(Model): - """Supplementary parameter for media events. - - :param card_value: Callback parameter specified in the Value field of the - MediaCard that originated this event - :type card_value: object - """ - - _attribute_map = {"card_value": {"key": "cardValue", "type": "object"}} - - def __init__(self, **kwargs): - super(MediaEventValue, self).__init__(**kwargs) - self.card_value = kwargs.get("card_value", None) - - -class MediaUrl(Model): - """Media URL. - - :param url: Url for the media - :type url: str - :param profile: Optional profile hint to the client to differentiate - multiple MediaUrl objects from each other - :type profile: str - """ - - _attribute_map = { - "url": {"key": "url", "type": "str"}, - "profile": {"key": "profile", "type": "str"}, - } - - def __init__(self, **kwargs): - super(MediaUrl, self).__init__(**kwargs) - self.url = kwargs.get("url", None) - self.profile = kwargs.get("profile", None) - - -class Mention(Model): - """Mention information (entity type: "mention"). - - :param mentioned: The mentioned user - :type mentioned: ~botframework.connector.models.ChannelAccount - :param text: Sub Text which represents the mention (can be null or empty) - :type text: str - :param type: Type of this entity (RFC 3987 IRI) - :type type: str - """ - - _attribute_map = { - "mentioned": {"key": "mentioned", "type": "ChannelAccount"}, - "text": {"key": "text", "type": "str"}, - "type": {"key": "type", "type": "str"}, - } - - def __init__(self, **kwargs): - super(Mention, self).__init__(**kwargs) - self.mentioned = kwargs.get("mentioned", None) - self.text = kwargs.get("text", None) - self.type = kwargs.get("type", None) - - -class MessageReaction(Model): - """Message reaction object. - - :param type: Message reaction type. Possible values include: 'like', - 'plusOne' - :type type: str or ~botframework.connector.models.MessageReactionTypes - """ - - _attribute_map = {"type": {"key": "type", "type": "str"}} - - def __init__(self, **kwargs): - super(MessageReaction, self).__init__(**kwargs) - self.type = kwargs.get("type", None) - - -class OAuthCard(Model): - """A card representing a request to perform a sign in via OAuth. - - :param text: Text for signin request - :type text: str - :param connection_name: The name of the registered connection - :type connection_name: str - :param buttons: Action to use to perform signin - :type buttons: list[~botframework.connector.models.CardAction] - """ - - _attribute_map = { - "text": {"key": "text", "type": "str"}, - "connection_name": {"key": "connectionName", "type": "str"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - } - - def __init__(self, **kwargs): - super(OAuthCard, self).__init__(**kwargs) - self.text = kwargs.get("text", None) - self.connection_name = kwargs.get("connection_name", None) - self.buttons = kwargs.get("buttons", None) - - -class PagedMembersResult(Model): - """Page of members. - - :param continuation_token: Paging token - :type continuation_token: str - :param members: The Channel Accounts. - :type members: list[~botframework.connector.models.ChannelAccount] - """ - - _attribute_map = { - "continuation_token": {"key": "continuationToken", "type": "str"}, - "members": {"key": "members", "type": "[ChannelAccount]"}, - } - - def __init__(self, **kwargs): - super(PagedMembersResult, self).__init__(**kwargs) - self.continuation_token = kwargs.get("continuation_token", None) - self.members = kwargs.get("members", None) - - -class Place(Model): - """Place (entity type: "https://schema.org/Place"). - - :param address: Address of the place (may be `string` or complex object of - type `PostalAddress`) - :type address: object - :param geo: Geo coordinates of the place (may be complex object of type - `GeoCoordinates` or `GeoShape`) - :type geo: object - :param has_map: Map to the place (may be `string` (URL) or complex object - of type `Map`) - :type has_map: object - :param type: The type of the thing - :type type: str - :param name: The name of the thing - :type name: str - """ - - _attribute_map = { - "address": {"key": "address", "type": "object"}, - "geo": {"key": "geo", "type": "object"}, - "has_map": {"key": "hasMap", "type": "object"}, - "type": {"key": "type", "type": "str"}, - "name": {"key": "name", "type": "str"}, - } - - def __init__(self, **kwargs): - super(Place, self).__init__(**kwargs) - self.address = kwargs.get("address", None) - self.geo = kwargs.get("geo", None) - self.has_map = kwargs.get("has_map", None) - self.type = kwargs.get("type", None) - self.name = kwargs.get("name", None) - - -class ReceiptCard(Model): - """A receipt card. - - :param title: Title of the card - :type title: str - :param facts: Array of Fact objects - :type facts: list[~botframework.connector.models.Fact] - :param items: Array of Receipt Items - :type items: list[~botframework.connector.models.ReceiptItem] - :param tap: This action will be activated when user taps on the card - :type tap: ~botframework.connector.models.CardAction - :param total: Total amount of money paid (or to be paid) - :type total: str - :param tax: Total amount of tax paid (or to be paid) - :type tax: str - :param vat: Total amount of VAT paid (or to be paid) - :type vat: str - :param buttons: Set of actions applicable to the current card - :type buttons: list[~botframework.connector.models.CardAction] - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "facts": {"key": "facts", "type": "[Fact]"}, - "items": {"key": "items", "type": "[ReceiptItem]"}, - "tap": {"key": "tap", "type": "CardAction"}, - "total": {"key": "total", "type": "str"}, - "tax": {"key": "tax", "type": "str"}, - "vat": {"key": "vat", "type": "str"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - } - - def __init__(self, **kwargs): - super(ReceiptCard, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.facts = kwargs.get("facts", None) - self.items = kwargs.get("items", None) - self.tap = kwargs.get("tap", None) - self.total = kwargs.get("total", None) - self.tax = kwargs.get("tax", None) - self.vat = kwargs.get("vat", None) - self.buttons = kwargs.get("buttons", None) - - -class ReceiptItem(Model): - """An item on a receipt card. - - :param title: Title of the Card - :type title: str - :param subtitle: Subtitle appears just below Title field, differs from - Title in font styling only - :type subtitle: str - :param text: Text field appears just below subtitle, differs from Subtitle - in font styling only - :type text: str - :param image: Image - :type image: ~botframework.connector.models.CardImage - :param price: Amount with currency - :type price: str - :param quantity: Number of items of given kind - :type quantity: str - :param tap: This action will be activated when user taps on the Item - bubble. - :type tap: ~botframework.connector.models.CardAction - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "subtitle": {"key": "subtitle", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "image": {"key": "image", "type": "CardImage"}, - "price": {"key": "price", "type": "str"}, - "quantity": {"key": "quantity", "type": "str"}, - "tap": {"key": "tap", "type": "CardAction"}, - } - - def __init__(self, **kwargs): - super(ReceiptItem, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.subtitle = kwargs.get("subtitle", None) - self.text = kwargs.get("text", None) - self.image = kwargs.get("image", None) - self.price = kwargs.get("price", None) - self.quantity = kwargs.get("quantity", None) - self.tap = kwargs.get("tap", None) - - -class ResourceResponse(Model): - """A response containing a resource ID. - - :param id: Id of the resource - :type id: str - """ - - _attribute_map = {"id": {"key": "id", "type": "str"}} - - def __init__(self, **kwargs): - super(ResourceResponse, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - - -class SemanticAction(Model): - """Represents a reference to a programmatic action. - - :param id: ID of this action - :type id: str - :param entities: Entities associated with this action - :type entities: dict[str, ~botframework.connector.models.Entity] - :param state: State of this action. Allowed values: `start`, `continue`, `done` - :type state: str or ~botframework.connector.models.SemanticActionStates - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - "entities": {"key": "entities", "type": "{Entity}"}, - "state": {"key": "state", "type": "str"}, - } - - def __init__(self, **kwargs): - super(SemanticAction, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - self.entities = kwargs.get("entities", None) - self.state = kwargs.get("state", None) - - -class SigninCard(Model): - """A card representing a request to sign in. - - :param text: Text for signin request - :type text: str - :param buttons: Action to use to perform signin - :type buttons: list[~botframework.connector.models.CardAction] - """ - - _attribute_map = { - "text": {"key": "text", "type": "str"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - } - - def __init__(self, **kwargs): - super(SigninCard, self).__init__(**kwargs) - self.text = kwargs.get("text", None) - self.buttons = kwargs.get("buttons", None) - - -class SuggestedActions(Model): - """SuggestedActions that can be performed. - - :param to: Ids of the recipients that the actions should be shown to. - These Ids are relative to the channelId and a subset of all recipients of - the activity - :type to: list[str] - :param actions: Actions that can be shown to the user - :type actions: list[~botframework.connector.models.CardAction] - """ - - _attribute_map = { - "to": {"key": "to", "type": "[str]"}, - "actions": {"key": "actions", "type": "[CardAction]"}, - } - - def __init__(self, **kwargs): - super(SuggestedActions, self).__init__(**kwargs) - self.to = kwargs.get("to", None) - self.actions = kwargs.get("actions", None) - - -class TextHighlight(Model): - """Refers to a substring of content within another field. - - :param text: Defines the snippet of text to highlight - :type text: str - :param occurrence: Occurrence of the text field within the referenced - text, if multiple exist. - :type occurrence: int - """ - - _attribute_map = { - "text": {"key": "text", "type": "str"}, - "occurrence": {"key": "occurrence", "type": "int"}, - } - - def __init__(self, **kwargs): - super(TextHighlight, self).__init__(**kwargs) - self.text = kwargs.get("text", None) - self.occurrence = kwargs.get("occurrence", None) - - -class Thing(Model): - """Thing (entity type: "https://schema.org/Thing"). - - :param type: The type of the thing - :type type: str - :param name: The name of the thing - :type name: str - """ - - _attribute_map = { - "type": {"key": "type", "type": "str"}, - "name": {"key": "name", "type": "str"}, - } - - def __init__(self, **kwargs): - super(Thing, self).__init__(**kwargs) - self.type = kwargs.get("type", None) - self.name = kwargs.get("name", None) - - -class ThumbnailCard(Model): - """A thumbnail card (card with a single, small thumbnail image). - - :param title: Title of the card - :type title: str - :param subtitle: Subtitle of the card - :type subtitle: str - :param text: Text for the card - :type text: str - :param images: Array of images for the card - :type images: list[~botframework.connector.models.CardImage] - :param buttons: Set of actions applicable to the current card - :type buttons: list[~botframework.connector.models.CardAction] - :param tap: This action will be activated when user taps on the card - itself - :type tap: ~botframework.connector.models.CardAction - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "subtitle": {"key": "subtitle", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "images": {"key": "images", "type": "[CardImage]"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - "tap": {"key": "tap", "type": "CardAction"}, - } - - def __init__(self, **kwargs): - super(ThumbnailCard, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.subtitle = kwargs.get("subtitle", None) - self.text = kwargs.get("text", None) - self.images = kwargs.get("images", None) - self.buttons = kwargs.get("buttons", None) - self.tap = kwargs.get("tap", None) - - -class ThumbnailUrl(Model): - """Thumbnail URL. - - :param url: URL pointing to the thumbnail to use for media content - :type url: str - :param alt: HTML alt text to include on this thumbnail image - :type alt: str - """ - - _attribute_map = { - "url": {"key": "url", "type": "str"}, - "alt": {"key": "alt", "type": "str"}, - } - - def __init__(self, **kwargs): - super(ThumbnailUrl, self).__init__(**kwargs) - self.url = kwargs.get("url", None) - self.alt = kwargs.get("alt", None) - - -class TokenRequest(Model): - """A request to receive a user token. - - :param provider: The provider to request a user token from - :type provider: str - :param settings: A collection of settings for the specific provider for - this request - :type settings: dict[str, object] - """ - - _attribute_map = { - "provider": {"key": "provider", "type": "str"}, - "settings": {"key": "settings", "type": "{object}"}, - } - - def __init__(self, **kwargs): - super(TokenRequest, self).__init__(**kwargs) - self.provider = kwargs.get("provider", None) - self.settings = kwargs.get("settings", None) - - -class TokenResponse(Model): - """A response that includes a user token. - - :param connection_name: The connection name - :type connection_name: str - :param token: The user token - :type token: str - :param expiration: Expiration for the token, in ISO 8601 format (e.g. - "2007-04-05T14:30Z") - :type expiration: str - :param channel_id: The channelId of the TokenResponse - :type channel_id: str - """ - - _attribute_map = { - "connection_name": {"key": "connectionName", "type": "str"}, - "token": {"key": "token", "type": "str"}, - "expiration": {"key": "expiration", "type": "str"}, - "channel_id": {"key": "channelId", "type": "str"}, - } - - def __init__(self, **kwargs): - super(TokenResponse, self).__init__(**kwargs) - self.connection_name = kwargs.get("connection_name", None) - self.token = kwargs.get("token", None) - self.expiration = kwargs.get("expiration", None) - self.channel_id = kwargs.get("channel_id", None) - - -class Transcript(Model): - """Transcript. - - :param activities: A collection of Activities that conforms to the - Transcript schema. - :type activities: list[~botframework.connector.models.Activity] - """ - - _attribute_map = {"activities": {"key": "activities", "type": "[Activity]"}} - - def __init__(self, **kwargs): - super(Transcript, self).__init__(**kwargs) - self.activities = kwargs.get("activities", None) - - -class VideoCard(Model): - """Video card. - - :param title: Title of this card - :type title: str - :param subtitle: Subtitle of this card - :type subtitle: str - :param text: Text of this card - :type text: str - :param image: Thumbnail placeholder - :type image: ~botframework.connector.models.ThumbnailUrl - :param media: Media URLs for this card. When this field contains more than - one URL, each URL is an alternative format of the same content. - :type media: list[~botframework.connector.models.MediaUrl] - :param buttons: Actions on this card - :type buttons: list[~botframework.connector.models.CardAction] - :param shareable: This content may be shared with others (default:true) - :type shareable: bool - :param autoloop: Should the client loop playback at end of content - (default:true) - :type autoloop: bool - :param autostart: Should the client automatically start playback of media - in this card (default:true) - :type autostart: bool - :param aspect: Aspect ratio of thumbnail/media placeholder. Allowed values - are "16:9" and "4:3" - :type aspect: str - :param duration: Describes the length of the media content without - requiring a receiver to open the content. Formatted as an ISO 8601 - Duration field. - :type duration: str - :param value: Supplementary parameter for this card - :type value: object - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "subtitle": {"key": "subtitle", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "image": {"key": "image", "type": "ThumbnailUrl"}, - "media": {"key": "media", "type": "[MediaUrl]"}, - "buttons": {"key": "buttons", "type": "[CardAction]"}, - "shareable": {"key": "shareable", "type": "bool"}, - "autoloop": {"key": "autoloop", "type": "bool"}, - "autostart": {"key": "autostart", "type": "bool"}, - "aspect": {"key": "aspect", "type": "str"}, - "duration": {"key": "duration", "type": "str"}, - "value": {"key": "value", "type": "object"}, - } - - def __init__(self, **kwargs): - super(VideoCard, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.subtitle = kwargs.get("subtitle", None) - self.text = kwargs.get("text", None) - self.image = kwargs.get("image", None) - self.media = kwargs.get("media", None) - self.buttons = kwargs.get("buttons", None) - self.shareable = kwargs.get("shareable", None) - self.autoloop = kwargs.get("autoloop", None) - self.autostart = kwargs.get("autostart", None) - self.aspect = kwargs.get("aspect", None) - self.duration = kwargs.get("duration", None) - self.value = kwargs.get("value", None) diff --git a/libraries/botbuilder-schema/botbuilder/schema/_models_py3.py b/libraries/botbuilder-schema/botbuilder/schema/_models_py3.py index f95185d1e..e7dd1f789 100644 --- a/libraries/botbuilder-schema/botbuilder/schema/_models_py3.py +++ b/libraries/botbuilder-schema/botbuilder/schema/_models_py3.py @@ -1,18 +1,116 @@ -# coding=utf-8 -# -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. -# -------------------------------------------------------------------------- +# Licensed under the MIT License. +from typing import List + +from botbuilder.schema._connector_client_enums import ActivityTypes +from datetime import datetime, timezone +from enum import Enum from msrest.serialization import Model from msrest.exceptions import HttpOperationError +class ActivityEventNames(str, Enum): + continue_conversation = "ContinueConversation" + create_conversation = "CreateConversation" + + +class ConversationReference(Model): + """An object relating to a particular point in a conversation. + + :param activity_id: (Optional) ID of the activity to refer to + :type activity_id: str + :param user: (Optional) User participating in this conversation + :type user: ~botframework.connector.models.ChannelAccount + :param bot: Bot participating in this conversation + :type bot: ~botframework.connector.models.ChannelAccount + :param conversation: Conversation reference + :type conversation: ~botframework.connector.models.ConversationAccount + :param channel_id: Channel ID + :type channel_id: str + :param locale: A locale name for the contents of the text field. + The locale name is a combination of an ISO 639 two- or three-letter + culture code associated with a language and an ISO 3166 two-letter + subculture code associated with a country or region. + The locale name can also correspond to a valid BCP-47 language tag. + :type locale: str + :param service_url: Service endpoint where operations concerning the + referenced conversation may be performed + :type service_url: str + """ + + _attribute_map = { + "activity_id": {"key": "activityId", "type": "str"}, + "user": {"key": "user", "type": "ChannelAccount"}, + "bot": {"key": "bot", "type": "ChannelAccount"}, + "conversation": {"key": "conversation", "type": "ConversationAccount"}, + "channel_id": {"key": "channelId", "type": "str"}, + "locale": {"key": "locale", "type": "str"}, + "service_url": {"key": "serviceUrl", "type": "str"}, + } + + def __init__( + self, + *, + activity_id: str = None, + user=None, + bot=None, + conversation=None, + channel_id: str = None, + locale: str = None, + service_url: str = None, + **kwargs + ) -> None: + super(ConversationReference, self).__init__(**kwargs) + self.activity_id = activity_id + self.user = user + self.bot = bot + self.conversation = conversation + self.channel_id = channel_id + self.locale = locale + self.service_url = service_url + + +class Mention(Model): + """Mention information (entity type: "mention"). + + :param mentioned: The mentioned user + :type mentioned: ~botframework.connector.models.ChannelAccount + :param text: Sub Text which represents the mention (can be null or empty) + :type text: str + :param type: Type of this entity (RFC 3987 IRI) + :type type: str + """ + + _attribute_map = { + "mentioned": {"key": "mentioned", "type": "ChannelAccount"}, + "text": {"key": "text", "type": "str"}, + "type": {"key": "type", "type": "str"}, + } + + def __init__( + self, *, mentioned=None, text: str = None, type: str = None, **kwargs + ) -> None: + super(Mention, self).__init__(**kwargs) + self.mentioned = mentioned + self.text = text + self.type = type + + +class ResourceResponse(Model): + """A response containing a resource ID. + + :param id: Id of the resource + :type id: str + """ + + _attribute_map = {"id": {"key": "id", "type": "str"}} + + def __init__(self, *, id: str = None, **kwargs) -> None: + super(ResourceResponse, self).__init__(**kwargs) + self.id = id + + class Activity(Model): """An Activity is the basic communication type for the Bot Framework 3.0 protocol. @@ -146,7 +244,7 @@ class Activity(Model): :type semantic_action: ~botframework.connector.models.SemanticAction :param caller_id: A string containing an IRI identifying the caller of a bot. This field is not intended to be transmitted over the wire, but is - instead populated by bots and clients based on cryptographically + instead populated by bots and clients based on cryptographically verifiable data that asserts the identity of the callers (e.g. tokens). :type caller_id: str """ @@ -287,6 +385,475 @@ def __init__( self.semantic_action = semantic_action self.caller_id = caller_id + def apply_conversation_reference( + self, reference: ConversationReference, is_incoming: bool = False + ): + """ + Updates this activity with the delivery information from an existing ConversationReference + + :param reference: The existing conversation reference. + :param is_incoming: Optional, True to treat the activity as an + incoming activity, where the bot is the recipient; otherwise, False. + Default is False, and the activity will show the bot as the sender. + + :returns: his activity, updated with the delivery information. + + .. remarks:: + Call GetConversationReference on an incoming + activity to get a conversation reference that you can then use to update an + outgoing activity with the correct delivery information. + """ + self.channel_id = reference.channel_id + self.service_url = reference.service_url + self.conversation = reference.conversation + + if reference.locale is not None: + self.locale = reference.locale + + if is_incoming: + self.from_property = reference.user + self.recipient = reference.bot + + if reference.activity_id is not None: + self.id = reference.activity_id + else: + self.from_property = reference.bot + self.recipient = reference.user + + if reference.activity_id is not None: + self.reply_to_id = reference.activity_id + + return self + + def as_contact_relation_update_activity(self): + """ + Returns this activity as a ContactRelationUpdateActivity object; + or None, if this is not that type of activity. + + :returns: This activity as a message activity; or None. + """ + return ( + self if self.__is_activity(ActivityTypes.contact_relation_update) else None + ) + + def as_conversation_update_activity(self): + """ + Returns this activity as a ConversationUpdateActivity object; + or None, if this is not that type of activity. + + :returns: This activity as a conversation update activity; or None. + """ + return self if self.__is_activity(ActivityTypes.conversation_update) else None + + def as_end_of_conversation_activity(self): + """ + Returns this activity as an EndOfConversationActivity object; + or None, if this is not that type of activity. + + :returns: This activity as an end of conversation activity; or None. + """ + return self if self.__is_activity(ActivityTypes.end_of_conversation) else None + + def as_event_activity(self): + """ + Returns this activity as an EventActivity object; + or None, if this is not that type of activity. + + :returns: This activity as an event activity; or None. + """ + return self if self.__is_activity(ActivityTypes.event) else None + + def as_handoff_activity(self): + """ + Returns this activity as a HandoffActivity object; + or None, if this is not that type of activity. + + :returns: This activity as a handoff activity; or None. + """ + return self if self.__is_activity(ActivityTypes.handoff) else None + + def as_installation_update_activity(self): + """ + Returns this activity as an InstallationUpdateActivity object; + or None, if this is not that type of activity. + + :returns: This activity as an installation update activity; or None. + """ + return self if self.__is_activity(ActivityTypes.installation_update) else None + + def as_invoke_activity(self): + """ + Returns this activity as an InvokeActivity object; + or None, if this is not that type of activity. + + :returns: This activity as an invoke activity; or None. + """ + return self if self.__is_activity(ActivityTypes.invoke) else None + + def as_message_activity(self): + """ + Returns this activity as a MessageActivity object; + or None, if this is not that type of activity. + + :returns: This activity as a message activity; or None. + """ + return self if self.__is_activity(ActivityTypes.message) else None + + def as_message_delete_activity(self): + """ + Returns this activity as a MessageDeleteActivity object; + or None, if this is not that type of activity. + + :returns: This activity as a message delete request; or None. + """ + return self if self.__is_activity(ActivityTypes.message_delete) else None + + def as_message_reaction_activity(self): + """ + Returns this activity as a MessageReactionActivity object; + or None, if this is not that type of activity. + + :return: This activity as a message reaction activity; or None. + """ + return self if self.__is_activity(ActivityTypes.message_reaction) else None + + def as_message_update_activity(self): + """ + Returns this activity as an MessageUpdateActivity object; + or None, if this is not that type of activity. + + :returns: This activity as a message update request; or None. + """ + return self if self.__is_activity(ActivityTypes.message_update) else None + + def as_suggestion_activity(self): + """ + Returns this activity as a SuggestionActivity object; + or None, if this is not that type of activity. + + :returns: This activity as a suggestion activity; or None. + """ + return self if self.__is_activity(ActivityTypes.suggestion) else None + + def as_trace_activity(self): + """ + Returns this activity as a TraceActivity object; + or None, if this is not that type of activity. + + :returns: This activity as a trace activity; or None. + """ + return self if self.__is_activity(ActivityTypes.trace) else None + + def as_typing_activity(self): + """ + Returns this activity as a TypingActivity object; + or null, if this is not that type of activity. + + :returns: This activity as a typing activity; or null. + """ + return self if self.__is_activity(ActivityTypes.typing) else None + + @staticmethod + def create_contact_relation_update_activity(): + """ + Creates an instance of the :class:`Activity` class as aContactRelationUpdateActivity object. + + :returns: The new contact relation update activity. + """ + return Activity(type=ActivityTypes.contact_relation_update) + + @staticmethod + def create_conversation_update_activity(): + """ + Creates an instance of the :class:`Activity` class as a ConversationUpdateActivity object. + + :returns: The new conversation update activity. + """ + return Activity(type=ActivityTypes.conversation_update) + + @staticmethod + def create_end_of_conversation_activity(): + """ + Creates an instance of the :class:`Activity` class as an EndOfConversationActivity object. + + :returns: The new end of conversation activity. + """ + return Activity(type=ActivityTypes.end_of_conversation) + + @staticmethod + def create_event_activity(): + """ + Creates an instance of the :class:`Activity` class as an EventActivity object. + + :returns: The new event activity. + """ + return Activity(type=ActivityTypes.event) + + @staticmethod + def create_handoff_activity(): + """ + Creates an instance of the :class:`Activity` class as a HandoffActivity object. + + :returns: The new handoff activity. + """ + return Activity(type=ActivityTypes.handoff) + + @staticmethod + def create_invoke_activity(): + """ + Creates an instance of the :class:`Activity` class as an InvokeActivity object. + + :returns: The new invoke activity. + """ + return Activity(type=ActivityTypes.invoke) + + @staticmethod + def create_message_activity(): + """ + Creates an instance of the :class:`Activity` class as a MessageActivity object. + + :returns: The new message activity. + """ + return Activity(type=ActivityTypes.message) + + def create_reply(self, text: str = None, locale: str = None): + """ + Creates a new message activity as a response to this activity. + + :param text: The text of the reply. + :param locale: The language code for the text. + + :returns: The new message activity. + + .. remarks:: + The new activity sets up routing information based on this activity. + """ + return Activity( + type=ActivityTypes.message, + timestamp=datetime.now(timezone.utc), + from_property=ChannelAccount( + id=self.recipient.id if self.recipient else None, + name=self.recipient.name if self.recipient else None, + ), + recipient=ChannelAccount( + id=self.from_property.id if self.from_property else None, + name=self.from_property.name if self.from_property else None, + ), + reply_to_id=( + self.id + if type != ActivityTypes.conversation_update + or self.channel_id not in ["directline", "webchat"] + else None + ), + service_url=self.service_url, + channel_id=self.channel_id, + conversation=ConversationAccount( + is_group=self.conversation.is_group, + id=self.conversation.id, + name=self.conversation.name, + ), + text=text if text else "", + locale=locale if locale else self.locale, + attachments=[], + entities=[], + ) + + def create_trace( + self, name: str, value: object = None, value_type: str = None, label: str = None + ): + """ + Creates a new trace activity based on this activity. + + :param name: The name of the trace operation to create. + :param value: Optional, the content for this trace operation. + :param value_type: Optional, identifier for the format of the value + Default is the name of type of the value. + :param label: Optional, a descriptive label for this trace operation. + + :returns: The new trace activity. + """ + if not value_type and value: + value_type = type(value) + + return Activity( + type=ActivityTypes.trace, + timestamp=datetime.now(timezone.utc), + from_property=ChannelAccount( + id=self.recipient.id if self.recipient else None, + name=self.recipient.name if self.recipient else None, + ), + recipient=ChannelAccount( + id=self.from_property.id if self.from_property else None, + name=self.from_property.name if self.from_property else None, + ), + reply_to_id=( + self.id + if type != ActivityTypes.conversation_update + or self.channel_id not in ["directline", "webchat"] + else None + ), + service_url=self.service_url, + channel_id=self.channel_id, + conversation=ConversationAccount( + is_group=self.conversation.is_group, + id=self.conversation.id, + name=self.conversation.name, + ), + name=name, + label=label, + value_type=value_type, + value=value, + ).as_trace_activity() + + @staticmethod + def create_trace_activity( + name: str, value: object = None, value_type: str = None, label: str = None + ): + """ + Creates an instance of the :class:`Activity` class as a TraceActivity object. + + :param name: The name of the trace operation to create. + :param value: Optional, the content for this trace operation. + :param value_type: Optional, identifier for the format of the value. + Default is the name of type of the value. + :param label: Optional, a descriptive label for this trace operation. + + :returns: The new trace activity. + """ + if not value_type and value: + value_type = type(value) + + return Activity( + type=ActivityTypes.trace, + name=name, + label=label, + value_type=value_type, + value=value, + ) + + @staticmethod + def create_typing_activity(): + """ + Creates an instance of the :class:`Activity` class as a TypingActivity object. + + :returns: The new typing activity. + """ + return Activity(type=ActivityTypes.typing) + + def get_conversation_reference(self): + """ + Creates a ConversationReference based on this activity. + + :returns: A conversation reference for the conversation that contains this activity. + """ + return ConversationReference( + activity_id=( + self.id + if type != ActivityTypes.conversation_update + or self.channel_id not in ["directline", "webchat"] + else None + ), + user=self.from_property, + bot=self.recipient, + conversation=self.conversation, + channel_id=self.channel_id, + locale=self.locale, + service_url=self.service_url, + ) + + def get_mentions(self) -> List[Mention]: + """ + Resolves the mentions from the entities of this activity. + + :returns: The array of mentions; or an empty array, if none are found. + + .. remarks:: + This method is defined on the :class:`Activity` class, but is only intended + for use with a message activity, where the activity Activity.Type is set to + ActivityTypes.Message. + """ + _list = self.entities + return [x for x in _list if str(x.type).lower() == "mention"] + + def get_reply_conversation_reference( + self, reply: ResourceResponse + ) -> ConversationReference: + """ + Create a ConversationReference based on this Activity's Conversation info + and the ResourceResponse from sending an activity. + + :param reply: ResourceResponse returned from send_activity. + + :return: A ConversationReference that can be stored and used later to delete or update the activity. + """ + reference = self.get_conversation_reference() + reference.activity_id = reply.id + return reference + + def has_content(self) -> bool: + """ + Indicates whether this activity has content. + + :returns: True, if this activity has any content to send; otherwise, false. + + .. remarks:: + This method is defined on the :class:`Activity` class, but is only intended + for use with a message activity, where the activity Activity.Type is set to + ActivityTypes.Message. + """ + if self.text and self.text.strip(): + return True + + if self.summary and self.summary.strip(): + return True + + if self.attachments and len(self.attachments) > 0: + return True + + if self.channel_data: + return True + + return False + + def is_from_streaming_connection(self) -> bool: + """ + Determine if the Activity was sent via an Http/Https connection or Streaming + This can be determined by looking at the service_url property: + (1) All channels that send messages via http/https are not streaming + (2) Channels that send messages via streaming have a ServiceUrl that does not begin with http/https. + + :returns: True if the Activity originated from a streaming connection. + """ + if self.service_url: + return not self.service_url.lower().startswith("http") + return False + + def __is_activity(self, activity_type: str) -> bool: + """ + Indicates whether this activity is of a specified activity type. + + :param activity_type: The activity type to check for. + :return: True if this activity is of the specified activity type; otherwise, False. + """ + if self.type is None: + return False + + type_attribute = str(self.type).lower() + activity_type = str(activity_type).lower() + + result = type_attribute.startswith(activity_type) + + if result: + result = len(type_attribute) == len(activity_type) + + if not result: + result = ( + len(type_attribute) > len(activity_type) + and type_attribute[len(activity_type)] == "/" + ) + + return result + class AnimationCard(Model): """An animation card (Ex: gif or short video clip). @@ -644,6 +1211,8 @@ class CardAction(Model): :type value: object :param channel_data: Channel-specific data associated with this action :type channel_data: object + :param image_alt_text: Alternate image text to be used in place of the `image` field + :type image_alt_text: str """ _attribute_map = { @@ -654,6 +1223,7 @@ class CardAction(Model): "display_text": {"key": "displayText", "type": "str"}, "value": {"key": "value", "type": "object"}, "channel_data": {"key": "channelData", "type": "object"}, + "image_alt_text": {"key": "imageAltText", "type": "str"}, } def __init__( @@ -666,6 +1236,7 @@ def __init__( display_text: str = None, value=None, channel_data=None, + image_alt_text: str = None, **kwargs ) -> None: super(CardAction, self).__init__(**kwargs) @@ -676,6 +1247,7 @@ def __init__( self.display_text = display_text self.value = value self.channel_data = channel_data + self.image_alt_text = image_alt_text class CardImage(Model): @@ -723,6 +1295,7 @@ class ChannelAccount(Model): "name": {"key": "name", "type": "str"}, "aad_object_id": {"key": "aadObjectId", "type": "str"}, "role": {"key": "role", "type": "str"}, + "properties": {"key": "properties", "type": "object"}, } def __init__( @@ -732,6 +1305,7 @@ def __init__( name: str = None, aad_object_id: str = None, role=None, + properties=None, **kwargs ) -> None: super(ChannelAccount, self).__init__(**kwargs) @@ -739,6 +1313,7 @@ def __init__( self.name = name self.aad_object_id = aad_object_id self.role = role + self.properties = properties class ConversationAccount(Model): @@ -758,8 +1333,8 @@ class ConversationAccount(Model): :param aad_object_id: This account's object ID within Azure Active Directory (AAD) :type aad_object_id: str - :param role: Role of the entity behind the account (Example: User, Bot, - etc.). Possible values include: 'user', 'bot' + :param role: Role of the entity behind the account (Example: User, Bot, Skill + etc.). Possible values include: 'user', 'bot', 'skill' :type role: str or ~botframework.connector.models.RoleTypes :param tenant_id: This conversation's tenant ID :type tenant_id: str @@ -876,53 +1451,6 @@ def __init__( self.tenant_id = tenant_id -class ConversationReference(Model): - """An object relating to a particular point in a conversation. - - :param activity_id: (Optional) ID of the activity to refer to - :type activity_id: str - :param user: (Optional) User participating in this conversation - :type user: ~botframework.connector.models.ChannelAccount - :param bot: Bot participating in this conversation - :type bot: ~botframework.connector.models.ChannelAccount - :param conversation: Conversation reference - :type conversation: ~botframework.connector.models.ConversationAccount - :param channel_id: Channel ID - :type channel_id: str - :param service_url: Service endpoint where operations concerning the - referenced conversation may be performed - :type service_url: str - """ - - _attribute_map = { - "activity_id": {"key": "activityId", "type": "str"}, - "user": {"key": "user", "type": "ChannelAccount"}, - "bot": {"key": "bot", "type": "ChannelAccount"}, - "conversation": {"key": "conversation", "type": "ConversationAccount"}, - "channel_id": {"key": "channelId", "type": "str"}, - "service_url": {"key": "serviceUrl", "type": "str"}, - } - - def __init__( - self, - *, - activity_id: str = None, - user=None, - bot=None, - conversation=None, - channel_id: str = None, - service_url: str = None, - **kwargs - ) -> None: - super(ConversationReference, self).__init__(**kwargs) - self.activity_id = activity_id - self.user = user - self.bot = bot - self.conversation = conversation - self.channel_id = channel_id - self.service_url = service_url - - class ConversationResourceResponse(Model): """A response containing a resource. @@ -1055,7 +1583,6 @@ class ErrorResponseException(HttpOperationError): """ def __init__(self, deserialize, response, *args): - super(ErrorResponseException, self).__init__( deserialize, response, "ErrorResponse", *args ) @@ -1195,6 +1722,44 @@ def __init__(self, *, status_code: int = None, body=None, **kwargs) -> None: self.body = body +class InvokeResponse(Model): + """ + Tuple class containing an HTTP Status Code and a JSON serializable + object. The HTTP Status code is, in the invoke activity scenario, what will + be set in the resulting POST. The Body of the resulting POST will be + JSON serialized content. + + The body content is defined by the producer. The caller must know what + the content is and deserialize as needed. + """ + + _attribute_map = { + "status": {"key": "status", "type": "int"}, + "body": {"key": "body", "type": "object"}, + } + + def __init__(self, *, status: int = None, body: object = None, **kwargs): + """ + Gets or sets the HTTP status and/or body code for the response + :param status: The HTTP status code. + :param body: The JSON serializable body content for the response. This object + must be serializable by the core Python json routines. The caller is responsible + for serializing more complex/nested objects into native classes (lists and + dictionaries of strings are acceptable). + """ + super().__init__(**kwargs) + self.status = status + self.body = body + + def is_successful_status_code(self) -> bool: + """ + Gets a value indicating whether the invoke response was successful. + :return: A value that indicates if the HTTP response was successful. true if status is in + the Successful range (200-299); otherwise false. + """ + return 200 <= self.status <= 299 + + class MediaCard(Model): """Media card. @@ -1313,32 +1878,6 @@ def __init__(self, *, url: str = None, profile: str = None, **kwargs) -> None: self.profile = profile -class Mention(Model): - """Mention information (entity type: "mention"). - - :param mentioned: The mentioned user - :type mentioned: ~botframework.connector.models.ChannelAccount - :param text: Sub Text which represents the mention (can be null or empty) - :type text: str - :param type: Type of this entity (RFC 3987 IRI) - :type type: str - """ - - _attribute_map = { - "mentioned": {"key": "mentioned", "type": "ChannelAccount"}, - "text": {"key": "text", "type": "str"}, - "type": {"key": "type", "type": "str"}, - } - - def __init__( - self, *, mentioned=None, text: str = None, type: str = None, **kwargs - ) -> None: - super(Mention, self).__init__(**kwargs) - self.mentioned = mentioned - self.text = text - self.type = type - - class MessageReaction(Model): """Message reaction object. @@ -1370,6 +1909,7 @@ class OAuthCard(Model): "connection_name": {"key": "connectionName", "type": "str"}, "buttons": {"key": "buttons", "type": "[CardAction]"}, "token_exchange_resource": {"key": "tokenExchangeResource", "type": "object"}, + "token_post_resource": {"key": "tokenPostResource", "type": "object"}, } def __init__( @@ -1379,6 +1919,7 @@ def __init__( connection_name: str = None, buttons=None, token_exchange_resource=None, + token_post_resource=None, **kwargs ) -> None: super(OAuthCard, self).__init__(**kwargs) @@ -1386,6 +1927,7 @@ def __init__( self.connection_name = connection_name self.buttons = buttons self.token_exchange_resource = token_exchange_resource + self.token_post_resource = token_post_resource class PagedMembersResult(Model): @@ -1564,20 +2106,6 @@ def __init__( self.tap = tap -class ResourceResponse(Model): - """A response containing a resource ID. - - :param id: Id of the resource - :type id: str - """ - - _attribute_map = {"id": {"key": "id", "type": "str"}} - - def __init__(self, *, id: str = None, **kwargs) -> None: - super(ResourceResponse, self).__init__(**kwargs) - self.id = id - - class SemanticAction(Model): """Represents a reference to a programmatic action. @@ -1897,7 +2425,7 @@ class TokenResponse(Model): "2007-04-05T14:30Z") :type expiration: str :param channel_id: The channelId of the TokenResponse - :type channel_id: str + :type channel_id: str """ _attribute_map = { @@ -2018,3 +2546,92 @@ def __init__( self.aspect = aspect self.duration = duration self.value = value + + +class AdaptiveCardInvokeAction(Model): + """AdaptiveCardInvokeAction. + + Defines the structure that arrives in the Activity.Value.Action for Invoke activity with + name of 'adaptiveCard/action'. + + :param type: The Type of this Adaptive Card Invoke Action. + :type type: str + :param id: The Id of this Adaptive Card Invoke Action. + :type id: str + :param verb: The Verb of this Adaptive Card Invoke Action. + :type verb: str + :param data: The data of this Adaptive Card Invoke Action. + :type data: dict[str, object] + """ + + _attribute_map = { + "type": {"key": "type", "type": "str"}, + "id": {"key": "id", "type": "str"}, + "verb": {"key": "verb", "type": "str"}, + "data": {"key": "data", "type": "{object}"}, + } + + def __init__( + self, *, type: str = None, id: str = None, verb: str = None, data=None, **kwargs + ) -> None: + super(AdaptiveCardInvokeAction, self).__init__(**kwargs) + self.type = type + self.id = id + self.verb = verb + self.data = data + + +class AdaptiveCardInvokeResponse(Model): + """AdaptiveCardInvokeResponse. + + Defines the structure that is returned as the result of an Invoke activity with Name of 'adaptiveCard/action'. + + :param status_code: The Card Action Response StatusCode. + :type status_code: int + :param type: The type of this Card Action Response. + :type type: str + :param value: The JSON response object. + :type value: dict[str, object] + """ + + _attribute_map = { + "status_code": {"key": "statusCode", "type": "int"}, + "type": {"key": "type", "type": "str"}, + "value": {"key": "value", "type": "{object}"}, + } + + def __init__( + self, *, status_code: int = None, type: str = None, value=None, **kwargs + ) -> None: + super(AdaptiveCardInvokeResponse, self).__init__(**kwargs) + self.status_code = status_code + self.type = type + self.value = value + + +class AdaptiveCardInvokeValue(Model): + """AdaptiveCardInvokeResponse. + + Defines the structure that arrives in the Activity.Value for Invoke activity with Name of 'adaptiveCard/action'. + + :param action: The action of this adaptive card invoke action value. + :type action: :class:`botframework.schema.models.AdaptiveCardInvokeAction` + :param authentication: The TokenExchangeInvokeRequest for this adaptive card invoke action value. + :type authentication: :class:`botframework.schema.models.TokenExchangeInvokeRequest` + :param state: The 'state' or magic code for an OAuth flow. + :type state: str + """ + + _attribute_map = { + "action": {"key": "action", "type": "{object}"}, + "authentication": {"key": "authentication", "type": "{object}"}, + "state": {"key": "state", "type": "str"}, + } + + def __init__( + self, *, action=None, authentication=None, state: str = None, **kwargs + ) -> None: + super(AdaptiveCardInvokeValue, self).__init__(**kwargs) + self.action = action + self.authentication = authentication + self.state = state diff --git a/libraries/botbuilder-schema/botbuilder/schema/_sign_in_enums.py b/libraries/botbuilder-schema/botbuilder/schema/_sign_in_enums.py index 4e411687a..3bc6f6b61 100644 --- a/libraries/botbuilder-schema/botbuilder/schema/_sign_in_enums.py +++ b/libraries/botbuilder-schema/botbuilder/schema/_sign_in_enums.py @@ -1,15 +1,10 @@ -# coding=utf-8 -# -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -------------------------------------------------------------------------- +# Licensed under the MIT License. from enum import Enum class SignInConstants(str, Enum): - # Name for the signin invoke to verify the 6-digit authentication code as part of sign-in. verify_state_operation_name = "signin/verifyState" # Name for signin invoke to perform a token exchange. diff --git a/libraries/botbuilder-schema/botbuilder/schema/callerid_constants.py b/libraries/botbuilder-schema/botbuilder/schema/callerid_constants.py new file mode 100644 index 000000000..3b2131306 --- /dev/null +++ b/libraries/botbuilder-schema/botbuilder/schema/callerid_constants.py @@ -0,0 +1,22 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from enum import Enum + + +class CallerIdConstants(str, Enum): + public_azure_channel = "urn:botframework:azure" + """ + The caller ID for any Bot Framework channel. + """ + + us_gov_channel = "urn:botframework:azureusgov" + """ + The caller ID for any Bot Framework US Government cloud channel. + """ + + bot_to_bot_prefix = "urn:botframework:aadappid:" + """ + The caller ID prefix when a bot initiates a request to another bot. + This prefix will be followed by the Azure Active Directory App ID of the bot that initiated the call. + """ diff --git a/libraries/botbuilder-schema/botbuilder/schema/speech_constants.py b/libraries/botbuilder-schema/botbuilder/schema/speech_constants.py new file mode 100644 index 000000000..0fbc396e6 --- /dev/null +++ b/libraries/botbuilder-schema/botbuilder/schema/speech_constants.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +class SpeechConstants: + """ + Defines constants that can be used in the processing of speech interactions. + """ + + EMPTY_SPEAK_TAG = '' + """ + The xml tag structure to indicate an empty speak tag, to be used in the 'speak' property of an Activity. + When set this indicates to the channel that speech should not be generated. + """ diff --git a/libraries/botbuilder-schema/botbuilder/schema/teams/__init__.py b/libraries/botbuilder-schema/botbuilder/schema/teams/__init__.py index 0f1f0edfe..be9aa11ce 100644 --- a/libraries/botbuilder-schema/botbuilder/schema/teams/__init__.py +++ b/libraries/botbuilder-schema/botbuilder/schema/teams/__init__.py @@ -1,130 +1,94 @@ -# coding=utf-8 -# -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. -# -------------------------------------------------------------------------- +# Licensed under the MIT License. -try: - from ._models_py3 import AppBasedLinkQuery - from ._models_py3 import ChannelInfo - from ._models_py3 import ConversationList - from ._models_py3 import FileConsentCard - from ._models_py3 import FileConsentCardResponse - from ._models_py3 import FileDownloadInfo - from ._models_py3 import FileInfoCard - from ._models_py3 import FileUploadInfo - from ._models_py3 import MessageActionsPayload - from ._models_py3 import MessageActionsPayloadApp - from ._models_py3 import MessageActionsPayloadAttachment - from ._models_py3 import MessageActionsPayloadBody - from ._models_py3 import MessageActionsPayloadConversation - from ._models_py3 import MessageActionsPayloadFrom - from ._models_py3 import MessageActionsPayloadMention - from ._models_py3 import MessageActionsPayloadReaction - from ._models_py3 import MessageActionsPayloadUser - from ._models_py3 import MessagingExtensionAction - from ._models_py3 import MessagingExtensionActionResponse - from ._models_py3 import MessagingExtensionAttachment - from ._models_py3 import MessagingExtensionParameter - from ._models_py3 import MessagingExtensionQuery - from ._models_py3 import MessagingExtensionQueryOptions - from ._models_py3 import MessagingExtensionResponse - from ._models_py3 import MessagingExtensionResult - from ._models_py3 import MessagingExtensionSuggestedAction - from ._models_py3 import NotificationInfo - from ._models_py3 import O365ConnectorCard - from ._models_py3 import O365ConnectorCardActionBase - from ._models_py3 import O365ConnectorCardActionCard - from ._models_py3 import O365ConnectorCardActionQuery - from ._models_py3 import O365ConnectorCardDateInput - from ._models_py3 import O365ConnectorCardFact - from ._models_py3 import O365ConnectorCardHttpPOST - from ._models_py3 import O365ConnectorCardImage - from ._models_py3 import O365ConnectorCardInputBase - from ._models_py3 import O365ConnectorCardMultichoiceInput - from ._models_py3 import O365ConnectorCardMultichoiceInputChoice - from ._models_py3 import O365ConnectorCardOpenUri - from ._models_py3 import O365ConnectorCardOpenUriTarget - from ._models_py3 import O365ConnectorCardSection - from ._models_py3 import O365ConnectorCardTextInput - from ._models_py3 import O365ConnectorCardViewAction - from ._models_py3 import SigninStateVerificationQuery - from ._models_py3 import TaskModuleContinueResponse - from ._models_py3 import TaskModuleMessageResponse - from ._models_py3 import TaskModuleRequest - from ._models_py3 import TaskModuleRequestContext - from ._models_py3 import TaskModuleResponse - from ._models_py3 import TaskModuleResponseBase - from ._models_py3 import TaskModuleTaskInfo - from ._models_py3 import TeamDetails - from ._models_py3 import TeamInfo - from ._models_py3 import TeamsChannelAccount - from ._models_py3 import TeamsChannelData - from ._models_py3 import TeamsPagedMembersResult - from ._models_py3 import TenantInfo -except (SyntaxError, ImportError): - from ._models import AppBasedLinkQuery - from ._models import ChannelInfo - from ._models import ConversationList - from ._models import FileConsentCard - from ._models import FileConsentCardResponse - from ._models import FileDownloadInfo - from ._models import FileInfoCard - from ._models import FileUploadInfo - from ._models import MessageActionsPayload - from ._models import MessageActionsPayloadApp - from ._models import MessageActionsPayloadAttachment - from ._models import MessageActionsPayloadBody - from ._models import MessageActionsPayloadConversation - from ._models import MessageActionsPayloadFrom - from ._models import MessageActionsPayloadMention - from ._models import MessageActionsPayloadReaction - from ._models import MessageActionsPayloadUser - from ._models import MessagingExtensionAction - from ._models import MessagingExtensionActionResponse - from ._models import MessagingExtensionAttachment - from ._models import MessagingExtensionParameter - from ._models import MessagingExtensionQuery - from ._models import MessagingExtensionQueryOptions - from ._models import MessagingExtensionResponse - from ._models import MessagingExtensionResult - from ._models import MessagingExtensionSuggestedAction - from ._models import NotificationInfo - from ._models import O365ConnectorCard - from ._models import O365ConnectorCardActionBase - from ._models import O365ConnectorCardActionCard - from ._models import O365ConnectorCardActionQuery - from ._models import O365ConnectorCardDateInput - from ._models import O365ConnectorCardFact - from ._models import O365ConnectorCardHttpPOST - from ._models import O365ConnectorCardImage - from ._models import O365ConnectorCardInputBase - from ._models import O365ConnectorCardMultichoiceInput - from ._models import O365ConnectorCardMultichoiceInputChoice - from ._models import O365ConnectorCardOpenUri - from ._models import O365ConnectorCardOpenUriTarget - from ._models import O365ConnectorCardSection - from ._models import O365ConnectorCardTextInput - from ._models import O365ConnectorCardViewAction - from ._models import SigninStateVerificationQuery - from ._models import TaskModuleContinueResponse - from ._models import TaskModuleMessageResponse - from ._models import TaskModuleRequest - from ._models import TaskModuleRequestContext - from ._models import TaskModuleResponse - from ._models import TaskModuleResponseBase - from ._models import TaskModuleTaskInfo - from ._models import TeamDetails - from ._models import TeamInfo - from ._models import TeamsChannelAccount - from ._models import TeamsChannelData - from ._models import TeamsPagedMembersResult - from ._models import TenantInfo +from ._models_py3 import AppBasedLinkQuery +from ._models_py3 import ChannelInfo +from ._models_py3 import ConversationList +from ._models_py3 import FileConsentCard +from ._models_py3 import FileConsentCardResponse +from ._models_py3 import FileDownloadInfo +from ._models_py3 import FileInfoCard +from ._models_py3 import FileUploadInfo +from ._models_py3 import MeetingDetails +from ._models_py3 import MeetingInfo +from ._models_py3 import MeetingStartEventDetails +from ._models_py3 import MeetingEndEventDetails +from ._models_py3 import MessageActionsPayload +from ._models_py3 import MessageActionsPayloadApp +from ._models_py3 import MessageActionsPayloadAttachment +from ._models_py3 import MessageActionsPayloadBody +from ._models_py3 import MessageActionsPayloadConversation +from ._models_py3 import MessageActionsPayloadFrom +from ._models_py3 import MessageActionsPayloadMention +from ._models_py3 import MessageActionsPayloadReaction +from ._models_py3 import MessageActionsPayloadUser +from ._models_py3 import MessagingExtensionAction +from ._models_py3 import MessagingExtensionActionResponse +from ._models_py3 import MessagingExtensionAttachment +from ._models_py3 import MessagingExtensionParameter +from ._models_py3 import MessagingExtensionQuery +from ._models_py3 import MessagingExtensionQueryOptions +from ._models_py3 import MessagingExtensionResponse +from ._models_py3 import MessagingExtensionResult +from ._models_py3 import MessagingExtensionSuggestedAction +from ._models_py3 import NotificationInfo +from ._models_py3 import O365ConnectorCard +from ._models_py3 import O365ConnectorCardActionBase +from ._models_py3 import O365ConnectorCardActionCard +from ._models_py3 import O365ConnectorCardActionQuery +from ._models_py3 import O365ConnectorCardDateInput +from ._models_py3 import O365ConnectorCardFact +from ._models_py3 import O365ConnectorCardHttpPOST +from ._models_py3 import O365ConnectorCardImage +from ._models_py3 import O365ConnectorCardInputBase +from ._models_py3 import O365ConnectorCardMultichoiceInput +from ._models_py3 import O365ConnectorCardMultichoiceInputChoice +from ._models_py3 import O365ConnectorCardOpenUri +from ._models_py3 import O365ConnectorCardOpenUriTarget +from ._models_py3 import O365ConnectorCardSection +from ._models_py3 import O365ConnectorCardTextInput +from ._models_py3 import O365ConnectorCardViewAction +from ._models_py3 import SigninStateVerificationQuery +from ._models_py3 import TaskModuleContinueResponse +from ._models_py3 import TaskModuleMessageResponse +from ._models_py3 import TaskModuleRequest +from ._models_py3 import TaskModuleRequestContext +from ._models_py3 import TaskModuleResponse +from ._models_py3 import TaskModuleResponseBase +from ._models_py3 import TaskModuleTaskInfo +from ._models_py3 import TeamDetails +from ._models_py3 import TeamInfo +from ._models_py3 import TeamsChannelAccount +from ._models_py3 import TeamsChannelDataSettings +from ._models_py3 import TeamsChannelData +from ._models_py3 import TeamsPagedMembersResult +from ._models_py3 import TenantInfo +from ._models_py3 import TeamsMeetingInfo +from ._models_py3 import TeamsMeetingParticipant +from ._models_py3 import MeetingParticipantInfo +from ._models_py3 import CacheInfo +from ._models_py3 import TabContext +from ._models_py3 import TabEntityContext +from ._models_py3 import TabRequest +from ._models_py3 import TabResponseCard +from ._models_py3 import TabResponseCards +from ._models_py3 import TabResponsePayload +from ._models_py3 import TabResponse +from ._models_py3 import TabSubmit +from ._models_py3 import TabSubmitData +from ._models_py3 import TabSuggestedActions +from ._models_py3 import TaskModuleCardResponse +from ._models_py3 import UserMeetingDetails +from ._models_py3 import TeamsMeetingMember +from ._models_py3 import MeetingParticipantsEventDetails +from ._models_py3 import ReadReceiptInfo +from ._models_py3 import BotConfigAuth +from ._models_py3 import ConfigAuthResponse +from ._models_py3 import ConfigResponse +from ._models_py3 import ConfigTaskResponse +from ._models_py3 import MeetingNotificationBase +from ._models_py3 import MeetingNotificationResponse +from ._models_py3 import OnBehalfOf __all__ = [ "AppBasedLinkQuery", @@ -135,6 +99,10 @@ "FileDownloadInfo", "FileInfoCard", "FileUploadInfo", + "MeetingDetails", + "MeetingInfo", + "MeetingStartEventDetails", + "MeetingEndEventDetails", "MessageActionsPayload", "MessageActionsPayloadApp", "MessageActionsPayloadAttachment", @@ -181,7 +149,34 @@ "TeamDetails", "TeamInfo", "TeamsChannelAccount", + "TeamsChannelDataSettings", "TeamsChannelData", "TeamsPagedMembersResult", "TenantInfo", + "TeamsMeetingInfo", + "TeamsMeetingParticipant", + "MeetingParticipantInfo", + "CacheInfo", + "TabContext", + "TabEntityContext", + "TabRequest", + "TabResponseCard", + "TabResponseCards", + "TabResponsePayload", + "TabResponse", + "TabSubmit", + "TabSubmitData", + "TabSuggestedActions", + "TaskModuleCardResponse", + "UserMeetingDetails", + "TeamsMeetingMember", + "MeetingParticipantsEventDetails", + "ReadReceiptInfo", + "BotConfigAuth", + "ConfigAuthResponse", + "ConfigResponse", + "ConfigTaskResponse", + "MeetingNotificationBase", + "MeetingNotificationResponse", + "OnBehalfOf", ] diff --git a/libraries/botbuilder-schema/botbuilder/schema/teams/_models.py b/libraries/botbuilder-schema/botbuilder/schema/teams/_models.py deleted file mode 100644 index 29372b73b..000000000 --- a/libraries/botbuilder-schema/botbuilder/schema/teams/_models.py +++ /dev/null @@ -1,1627 +0,0 @@ -# coding=utf-8 -# -------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. -# -------------------------------------------------------------------------- - -from msrest.serialization import Model -from botbuilder.schema import Activity, PagedMembersResult, TeamsChannelAccount - - -class AppBasedLinkQuery(Model): - """Invoke request body type for app-based link query. - - :param url: Url queried by user - :type url: str - """ - - _attribute_map = { - "url": {"key": "url", "type": "str"}, - } - - def __init__(self, *, url: str = None, **kwargs) -> None: - super(AppBasedLinkQuery, self).__init__(**kwargs) - self.url = url - - -class ChannelInfo(Model): - """A channel info object which describes the channel. - - :param id: Unique identifier representing a channel - :type id: str - :param name: Name of the channel - :type name: str - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - "name": {"key": "name", "type": "str"}, - } - - def __init__(self, **kwargs): - super(ChannelInfo, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - self.name = kwargs.get("name", None) - - -class ConversationList(Model): - """List of channels under a team. - - :param conversations: - :type conversations: - list[~botframework.connector.teams.models.ChannelInfo] - """ - - _attribute_map = { - "conversations": {"key": "conversations", "type": "[ChannelInfo]"}, - } - - def __init__(self, **kwargs): - super(ConversationList, self).__init__(**kwargs) - self.conversations = kwargs.get("conversations", None) - - -class FileConsentCard(Model): - """File consent card attachment. - - :param description: File description. - :type description: str - :param size_in_bytes: Size of the file to be uploaded in Bytes. - :type size_in_bytes: long - :param accept_context: Context sent back to the Bot if user consented to - upload. This is free flow schema and is sent back in Value field of - Activity. - :type accept_context: object - :param decline_context: Context sent back to the Bot if user declined. - This is free flow schema and is sent back in Value field of Activity. - :type decline_context: object - """ - - _attribute_map = { - "description": {"key": "description", "type": "str"}, - "size_in_bytes": {"key": "sizeInBytes", "type": "long"}, - "accept_context": {"key": "acceptContext", "type": "object"}, - "decline_context": {"key": "declineContext", "type": "object"}, - } - - def __init__(self, **kwargs): - super(FileConsentCard, self).__init__(**kwargs) - self.description = kwargs.get("description", None) - self.size_in_bytes = kwargs.get("size_in_bytes", None) - self.accept_context = kwargs.get("accept_context", None) - self.decline_context = kwargs.get("decline_context", None) - - -class FileConsentCardResponse(Model): - """Represents the value of the invoke activity sent when the user acts on a - file consent card. - - :param action: The action the user took. Possible values include: - 'accept', 'decline' - :type action: str or ~botframework.connector.teams.models.enum - :param context: The context associated with the action. - :type context: object - :param upload_info: If the user accepted the file, contains information - about the file to be uploaded. - :type upload_info: ~botframework.connector.teams.models.FileUploadInfo - """ - - _attribute_map = { - "action": {"key": "action", "type": "str"}, - "context": {"key": "context", "type": "object"}, - "upload_info": {"key": "uploadInfo", "type": "FileUploadInfo"}, - } - - def __init__(self, **kwargs): - super(FileConsentCardResponse, self).__init__(**kwargs) - self.action = kwargs.get("action", None) - self.context = kwargs.get("context", None) - self.upload_info = kwargs.get("upload_info", None) - - -class FileDownloadInfo(Model): - """File download info attachment. - - :param download_url: File download url. - :type download_url: str - :param unique_id: Unique Id for the file. - :type unique_id: str - :param file_type: Type of file. - :type file_type: str - :param etag: ETag for the file. - :type etag: object - """ - - _attribute_map = { - "download_url": {"key": "downloadUrl", "type": "str"}, - "unique_id": {"key": "uniqueId", "type": "str"}, - "file_type": {"key": "fileType", "type": "str"}, - "etag": {"key": "etag", "type": "object"}, - } - - def __init__(self, **kwargs): - super(FileDownloadInfo, self).__init__(**kwargs) - self.download_url = kwargs.get("download_url", None) - self.unique_id = kwargs.get("unique_id", None) - self.file_type = kwargs.get("file_type", None) - self.etag = kwargs.get("etag", None) - - -class FileInfoCard(Model): - """File info card. - - :param unique_id: Unique Id for the file. - :type unique_id: str - :param file_type: Type of file. - :type file_type: str - :param etag: ETag for the file. - :type etag: object - """ - - _attribute_map = { - "unique_id": {"key": "uniqueId", "type": "str"}, - "file_type": {"key": "fileType", "type": "str"}, - "etag": {"key": "etag", "type": "object"}, - } - - def __init__(self, **kwargs): - super(FileInfoCard, self).__init__(**kwargs) - self.unique_id = kwargs.get("unique_id", None) - self.file_type = kwargs.get("file_type", None) - self.etag = kwargs.get("etag", None) - - -class FileUploadInfo(Model): - """Information about the file to be uploaded. - - :param name: Name of the file. - :type name: str - :param upload_url: URL to an upload session that the bot can use to set - the file contents. - :type upload_url: str - :param content_url: URL to file. - :type content_url: str - :param unique_id: ID that uniquely identifies the file. - :type unique_id: str - :param file_type: Type of the file. - :type file_type: str - """ - - _attribute_map = { - "name": {"key": "name", "type": "str"}, - "upload_url": {"key": "uploadUrl", "type": "str"}, - "content_url": {"key": "contentUrl", "type": "str"}, - "unique_id": {"key": "uniqueId", "type": "str"}, - "file_type": {"key": "fileType", "type": "str"}, - } - - def __init__(self, **kwargs): - super(FileUploadInfo, self).__init__(**kwargs) - self.name = kwargs.get("name", None) - self.upload_url = kwargs.get("upload_url", None) - self.content_url = kwargs.get("content_url", None) - self.unique_id = kwargs.get("unique_id", None) - self.file_type = kwargs.get("file_type", None) - - -class MessageActionsPayloadApp(Model): - """Represents an application entity. - - :param application_identity_type: The type of application. Possible values - include: 'aadApplication', 'bot', 'tenantBot', 'office365Connector', - 'webhook' - :type application_identity_type: str or - ~botframework.connector.teams.models.enum - :param id: The id of the application. - :type id: str - :param display_name: The plaintext display name of the application. - :type display_name: str - """ - - _attribute_map = { - "application_identity_type": {"key": "applicationIdentityType", "type": "str"}, - "id": {"key": "id", "type": "str"}, - "display_name": {"key": "displayName", "type": "str"}, - } - - def __init__(self, **kwargs): - super(MessageActionsPayloadApp, self).__init__(**kwargs) - self.application_identity_type = kwargs.get("application_identity_type", None) - self.id = kwargs.get("id", None) - self.display_name = kwargs.get("display_name", None) - - -class MessageActionsPayloadAttachment(Model): - """Represents the attachment in a message. - - :param id: The id of the attachment. - :type id: str - :param content_type: The type of the attachment. - :type content_type: str - :param content_url: The url of the attachment, in case of a external link. - :type content_url: str - :param content: The content of the attachment, in case of a code snippet, - email, or file. - :type content: object - :param name: The plaintext display name of the attachment. - :type name: str - :param thumbnail_url: The url of a thumbnail image that might be embedded - in the attachment, in case of a card. - :type thumbnail_url: str - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - "content_type": {"key": "contentType", "type": "str"}, - "content_url": {"key": "contentUrl", "type": "str"}, - "content": {"key": "content", "type": "object"}, - "name": {"key": "name", "type": "str"}, - "thumbnail_url": {"key": "thumbnailUrl", "type": "str"}, - } - - def __init__(self, **kwargs): - super(MessageActionsPayloadAttachment, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - self.content_type = kwargs.get("content_type", None) - self.content_url = kwargs.get("content_url", None) - self.content = kwargs.get("content", None) - self.name = kwargs.get("name", None) - self.thumbnail_url = kwargs.get("thumbnail_url", None) - - -class MessageActionsPayloadBody(Model): - """Plaintext/HTML representation of the content of the message. - - :param content_type: Type of the content. Possible values include: 'html', - 'text' - :type content_type: str or ~botframework.connector.teams.models.enum - :param content: The content of the body. - :type content: str - """ - - _attribute_map = { - "content_type": {"key": "contentType", "type": "str"}, - "content": {"key": "content", "type": "str"}, - } - - def __init__(self, **kwargs): - super(MessageActionsPayloadBody, self).__init__(**kwargs) - self.content_type = kwargs.get("content_type", None) - self.content = kwargs.get("content", None) - - -class MessageActionsPayloadConversation(Model): - """Represents a team or channel entity. - - :param conversation_identity_type: The type of conversation, whether a - team or channel. Possible values include: 'team', 'channel' - :type conversation_identity_type: str or - ~botframework.connector.teams.models.enum - :param id: The id of the team or channel. - :type id: str - :param display_name: The plaintext display name of the team or channel - entity. - :type display_name: str - """ - - _attribute_map = { - "conversation_identity_type": { - "key": "conversationIdentityType", - "type": "str", - }, - "id": {"key": "id", "type": "str"}, - "display_name": {"key": "displayName", "type": "str"}, - } - - def __init__(self, **kwargs): - super(MessageActionsPayloadConversation, self).__init__(**kwargs) - self.conversation_identity_type = kwargs.get("conversation_identity_type", None) - self.id = kwargs.get("id", None) - self.display_name = kwargs.get("display_name", None) - - -class MessageActionsPayloadFrom(Model): - """Represents a user, application, or conversation type that either sent or - was referenced in a message. - - :param user: Represents details of the user. - :type user: ~botframework.connector.teams.models.MessageActionsPayloadUser - :param application: Represents details of the app. - :type application: - ~botframework.connector.teams.models.MessageActionsPayloadApp - :param conversation: Represents details of the converesation. - :type conversation: - ~botframework.connector.teams.models.MessageActionsPayloadConversation - """ - - _attribute_map = { - "user": {"key": "user", "type": "MessageActionsPayloadUser"}, - "application": {"key": "application", "type": "MessageActionsPayloadApp"}, - "conversation": { - "key": "conversation", - "type": "MessageActionsPayloadConversation", - }, - } - - def __init__(self, **kwargs): - super(MessageActionsPayloadFrom, self).__init__(**kwargs) - self.user = kwargs.get("user", None) - self.application = kwargs.get("application", None) - self.conversation = kwargs.get("conversation", None) - - -class MessageActionsPayloadMention(Model): - """Represents the entity that was mentioned in the message. - - :param id: The id of the mentioned entity. - :type id: int - :param mention_text: The plaintext display name of the mentioned entity. - :type mention_text: str - :param mentioned: Provides more details on the mentioned entity. - :type mentioned: - ~botframework.connector.teams.models.MessageActionsPayloadFrom - """ - - _attribute_map = { - "id": {"key": "id", "type": "int"}, - "mention_text": {"key": "mentionText", "type": "str"}, - "mentioned": {"key": "mentioned", "type": "MessageActionsPayloadFrom"}, - } - - def __init__(self, **kwargs): - super(MessageActionsPayloadMention, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - self.mention_text = kwargs.get("mention_text", None) - self.mentioned = kwargs.get("mentioned", None) - - -class MessageActionsPayloadReaction(Model): - """Represents the reaction of a user to a message. - - :param reaction_type: The type of reaction given to the message. Possible - values include: 'like', 'heart', 'laugh', 'surprised', 'sad', 'angry' - :type reaction_type: str or ~botframework.connector.teams.models.enum - :param created_date_time: Timestamp of when the user reacted to the - message. - :type created_date_time: str - :param user: The user with which the reaction is associated. - :type user: ~botframework.connector.teams.models.MessageActionsPayloadFrom - """ - - _attribute_map = { - "reaction_type": {"key": "reactionType", "type": "str"}, - "created_date_time": {"key": "createdDateTime", "type": "str"}, - "user": {"key": "user", "type": "MessageActionsPayloadFrom"}, - } - - def __init__(self, **kwargs): - super(MessageActionsPayloadReaction, self).__init__(**kwargs) - self.reaction_type = kwargs.get("reaction_type", None) - self.created_date_time = kwargs.get("created_date_time", None) - self.user = kwargs.get("user", None) - - -class MessageActionsPayloadUser(Model): - """Represents a user entity. - - :param user_identity_type: The identity type of the user. Possible values - include: 'aadUser', 'onPremiseAadUser', 'anonymousGuest', 'federatedUser' - :type user_identity_type: str or ~botframework.connector.teams.models.enum - :param id: The id of the user. - :type id: str - :param display_name: The plaintext display name of the user. - :type display_name: str - """ - - _attribute_map = { - "user_identity_type": {"key": "userIdentityType", "type": "str"}, - "id": {"key": "id", "type": "str"}, - "display_name": {"key": "displayName", "type": "str"}, - } - - def __init__(self, **kwargs): - super(MessageActionsPayloadUser, self).__init__(**kwargs) - self.user_identity_type = kwargs.get("user_identity_type", None) - self.id = kwargs.get("id", None) - self.display_name = kwargs.get("display_name", None) - - -class MessageActionsPayload(Model): - """Represents the individual message within a chat or channel where a message - actions is taken. - - :param id: Unique id of the message. - :type id: str - :param reply_to_id: Id of the parent/root message of the thread. - :type reply_to_id: str - :param message_type: Type of message - automatically set to message. - Possible values include: 'message' - :type message_type: str or ~botframework.connector.teams.models.enum - :param created_date_time: Timestamp of when the message was created. - :type created_date_time: str - :param last_modified_date_time: Timestamp of when the message was edited - or updated. - :type last_modified_date_time: str - :param deleted: Indicates whether a message has been soft deleted. - :type deleted: bool - :param subject: Subject line of the message. - :type subject: str - :param summary: Summary text of the message that could be used for - notifications. - :type summary: str - :param importance: The importance of the message. Possible values include: - 'normal', 'high', 'urgent' - :type importance: str or ~botframework.connector.teams.models.enum - :param locale: Locale of the message set by the client. - :type locale: str - :param from_property: Sender of the message. - :type from_property: - ~botframework.connector.teams.models.MessageActionsPayloadFrom - :param body: Plaintext/HTML representation of the content of the message. - :type body: ~botframework.connector.teams.models.MessageActionsPayloadBody - :param attachment_layout: How the attachment(s) are displayed in the - message. - :type attachment_layout: str - :param attachments: Attachments in the message - card, image, file, etc. - :type attachments: - list[~botframework.connector.teams.models.MessageActionsPayloadAttachment] - :param mentions: List of entities mentioned in the message. - :type mentions: - list[~botframework.connector.teams.models.MessageActionsPayloadMention] - :param reactions: Reactions for the message. - :type reactions: - list[~botframework.connector.teams.models.MessageActionsPayloadReaction] - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - "reply_to_id": {"key": "replyToId", "type": "str"}, - "message_type": {"key": "messageType", "type": "str"}, - "created_date_time": {"key": "createdDateTime", "type": "str"}, - "last_modified_date_time": {"key": "lastModifiedDateTime", "type": "str"}, - "deleted": {"key": "deleted", "type": "bool"}, - "subject": {"key": "subject", "type": "str"}, - "summary": {"key": "summary", "type": "str"}, - "importance": {"key": "importance", "type": "str"}, - "locale": {"key": "locale", "type": "str"}, - "from_property": {"key": "from", "type": "MessageActionsPayloadFrom"}, - "body": {"key": "body", "type": "MessageActionsPayloadBody"}, - "attachment_layout": {"key": "attachmentLayout", "type": "str"}, - "attachments": { - "key": "attachments", - "type": "[MessageActionsPayloadAttachment]", - }, - "mentions": {"key": "mentions", "type": "[MessageActionsPayloadMention]"}, - "reactions": {"key": "reactions", "type": "[MessageActionsPayloadReaction]"}, - } - - def __init__(self, **kwargs): - super(MessageActionsPayload, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - self.reply_to_id = kwargs.get("reply_to_id", None) - self.message_type = kwargs.get("message_type", None) - self.created_date_time = kwargs.get("created_date_time", None) - self.last_modified_date_time = kwargs.get("last_modified_date_time", None) - self.deleted = kwargs.get("deleted", None) - self.subject = kwargs.get("subject", None) - self.summary = kwargs.get("summary", None) - self.importance = kwargs.get("importance", None) - self.locale = kwargs.get("locale", None) - self.from_property = kwargs.get("from_property", None) - self.body = kwargs.get("body", None) - self.attachment_layout = kwargs.get("attachment_layout", None) - self.attachments = kwargs.get("attachments", None) - self.mentions = kwargs.get("mentions", None) - self.reactions = kwargs.get("reactions", None) - - -class MessagingExtensionAction(TaskModuleRequest): - """Messaging extension action. - - :param data: User input data. Free payload with key-value pairs. - :type data: object - :param context: Current user context, i.e., the current theme - :type context: - ~botframework.connector.teams.models.TaskModuleRequestContext - :param command_id: Id of the command assigned by Bot - :type command_id: str - :param command_context: The context from which the command originates. - Possible values include: 'message', 'compose', 'commandbox' - :type command_context: str or ~botframework.connector.teams.models.enum - :param bot_message_preview_action: Bot message preview action taken by - user. Possible values include: 'edit', 'send' - :type bot_message_preview_action: str or - ~botframework.connector.teams.models.enum - :param bot_activity_preview: - :type bot_activity_preview: - list[~botframework.connector.teams.models.Activity] - :param message_payload: Message content sent as part of the command - request. - :type message_payload: - ~botframework.connector.teams.models.MessageActionsPayload - """ - - _attribute_map = { - "data": {"key": "data", "type": "object"}, - "context": {"key": "context", "type": "TaskModuleRequestContext"}, - "command_id": {"key": "commandId", "type": "str"}, - "command_context": {"key": "commandContext", "type": "str"}, - "bot_message_preview_action": {"key": "botMessagePreviewAction", "type": "str"}, - "bot_activity_preview": {"key": "botActivityPreview", "type": "[Activity]"}, - "message_payload": {"key": "messagePayload", "type": "MessageActionsPayload"}, - } - - def __init__(self, **kwargs): - super(MessagingExtensionAction, self).__init__(**kwargs) - self.command_id = kwargs.get("command_id", None) - self.command_context = kwargs.get("command_context", None) - self.bot_message_preview_action = kwargs.get("bot_message_preview_action", None) - self.bot_activity_preview = kwargs.get("bot_activity_preview", None) - self.message_payload = kwargs.get("message_payload", None) - - -class MessagingExtensionActionResponse(Model): - """Response of messaging extension action. - - :param task: The JSON for the Adaptive card to appear in the task module. - :type task: ~botframework.connector.teams.models.TaskModuleResponseBase - :param compose_extension: - :type compose_extension: - ~botframework.connector.teams.models.MessagingExtensionResult - """ - - _attribute_map = { - "task": {"key": "task", "type": "TaskModuleResponseBase"}, - "compose_extension": { - "key": "composeExtension", - "type": "MessagingExtensionResult", - }, - } - - def __init__(self, **kwargs): - super(MessagingExtensionActionResponse, self).__init__(**kwargs) - self.task = kwargs.get("task", None) - self.compose_extension = kwargs.get("compose_extension", None) - - -class MessagingExtensionAttachment(Attachment): - """Messaging extension attachment. - - :param content_type: mimetype/Contenttype for the file - :type content_type: str - :param content_url: Content Url - :type content_url: str - :param content: Embedded content - :type content: object - :param name: (OPTIONAL) The name of the attachment - :type name: str - :param thumbnail_url: (OPTIONAL) Thumbnail associated with attachment - :type thumbnail_url: str - :param preview: - :type preview: ~botframework.connector.teams.models.Attachment - """ - - _attribute_map = { - "content_type": {"key": "contentType", "type": "str"}, - "content_url": {"key": "contentUrl", "type": "str"}, - "content": {"key": "content", "type": "object"}, - "name": {"key": "name", "type": "str"}, - "thumbnail_url": {"key": "thumbnailUrl", "type": "str"}, - "preview": {"key": "preview", "type": "Attachment"}, - } - - def __init__(self, **kwargs): - super(MessagingExtensionAttachment, self).__init__(**kwargs) - self.preview = kwargs.get("preview", None) - - -class MessagingExtensionParameter(Model): - """Messaging extension query parameters. - - :param name: Name of the parameter - :type name: str - :param value: Value of the parameter - :type value: object - """ - - _attribute_map = { - "name": {"key": "name", "type": "str"}, - "value": {"key": "value", "type": "object"}, - } - - def __init__(self, **kwargs): - super(MessagingExtensionParameter, self).__init__(**kwargs) - self.name = kwargs.get("name", None) - self.value = kwargs.get("value", None) - - -class MessagingExtensionQuery(Model): - """Messaging extension query. - - :param command_id: Id of the command assigned by Bot - :type command_id: str - :param parameters: Parameters for the query - :type parameters: - list[~botframework.connector.teams.models.MessagingExtensionParameter] - :param query_options: - :type query_options: - ~botframework.connector.teams.models.MessagingExtensionQueryOptions - :param state: State parameter passed back to the bot after - authentication/configuration flow - :type state: str - """ - - _attribute_map = { - "command_id": {"key": "commandId", "type": "str"}, - "parameters": {"key": "parameters", "type": "[MessagingExtensionParameter]"}, - "query_options": { - "key": "queryOptions", - "type": "MessagingExtensionQueryOptions", - }, - "state": {"key": "state", "type": "str"}, - } - - def __init__(self, **kwargs): - super(MessagingExtensionQuery, self).__init__(**kwargs) - self.command_id = kwargs.get("command_id", None) - self.parameters = kwargs.get("parameters", None) - self.query_options = kwargs.get("query_options", None) - self.state = kwargs.get("state", None) - - -class MessagingExtensionQueryOptions(Model): - """Messaging extension query options. - - :param skip: Number of entities to skip - :type skip: int - :param count: Number of entities to fetch - :type count: int - """ - - _attribute_map = { - "skip": {"key": "skip", "type": "int"}, - "count": {"key": "count", "type": "int"}, - } - - def __init__(self, **kwargs): - super(MessagingExtensionQueryOptions, self).__init__(**kwargs) - self.skip = kwargs.get("skip", None) - self.count = kwargs.get("count", None) - - -class MessagingExtensionResponse(Model): - """Messaging extension response. - - :param compose_extension: - :type compose_extension: - ~botframework.connector.teams.models.MessagingExtensionResult - """ - - _attribute_map = { - "compose_extension": { - "key": "composeExtension", - "type": "MessagingExtensionResult", - }, - } - - def __init__(self, **kwargs): - super(MessagingExtensionResponse, self).__init__(**kwargs) - self.compose_extension = kwargs.get("compose_extension", None) - - -class MessagingExtensionResult(Model): - """Messaging extension result. - - :param attachment_layout: Hint for how to deal with multiple attachments. - Possible values include: 'list', 'grid' - :type attachment_layout: str or ~botframework.connector.teams.models.enum - :param type: The type of the result. Possible values include: 'result', - 'auth', 'config', 'message', 'botMessagePreview' - :type type: str or ~botframework.connector.teams.models.enum - :param attachments: (Only when type is result) Attachments - :type attachments: - list[~botframework.connector.teams.models.MessagingExtensionAttachment] - :param suggested_actions: - :type suggested_actions: - ~botframework.connector.teams.models.MessagingExtensionSuggestedAction - :param text: (Only when type is message) Text - :type text: str - :param activity_preview: (Only when type is botMessagePreview) Message - activity to preview - :type activity_preview: ~botframework.connector.teams.models.Activity - """ - - _attribute_map = { - "attachment_layout": {"key": "attachmentLayout", "type": "str"}, - "type": {"key": "type", "type": "str"}, - "attachments": {"key": "attachments", "type": "[MessagingExtensionAttachment]"}, - "suggested_actions": { - "key": "suggestedActions", - "type": "MessagingExtensionSuggestedAction", - }, - "text": {"key": "text", "type": "str"}, - "activity_preview": {"key": "activityPreview", "type": "Activity"}, - } - - def __init__(self, **kwargs): - super(MessagingExtensionResult, self).__init__(**kwargs) - self.attachment_layout = kwargs.get("attachment_layout", None) - self.type = kwargs.get("type", None) - self.attachments = kwargs.get("attachments", None) - self.suggested_actions = kwargs.get("suggested_actions", None) - self.text = kwargs.get("text", None) - self.activity_preview = kwargs.get("activity_preview", None) - - -class MessagingExtensionSuggestedAction(Model): - """Messaging extension Actions (Only when type is auth or config). - - :param actions: Actions - :type actions: list[~botframework.connector.teams.models.CardAction] - """ - - _attribute_map = { - "actions": {"key": "actions", "type": "[CardAction]"}, - } - - def __init__(self, **kwargs): - super(MessagingExtensionSuggestedAction, self).__init__(**kwargs) - self.actions = kwargs.get("actions", None) - - -class NotificationInfo(Model): - """Specifies if a notification is to be sent for the mentions. - - :param alert: true if notification is to be sent to the user, false - otherwise. - :type alert: bool - """ - - _attribute_map = { - "alert": {"key": "alert", "type": "bool"}, - } - - def __init__(self, **kwargs): - super(NotificationInfo, self).__init__(**kwargs) - self.alert = kwargs.get("alert", None) - - -class O365ConnectorCard(Model): - """O365 connector card. - - :param title: Title of the item - :type title: str - :param text: Text for the card - :type text: str - :param summary: Summary for the card - :type summary: str - :param theme_color: Theme color for the card - :type theme_color: str - :param sections: Set of sections for the current card - :type sections: - list[~botframework.connector.teams.models.O365ConnectorCardSection] - :param potential_action: Set of actions for the current card - :type potential_action: - list[~botframework.connector.teams.models.O365ConnectorCardActionBase] - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "summary": {"key": "summary", "type": "str"}, - "theme_color": {"key": "themeColor", "type": "str"}, - "sections": {"key": "sections", "type": "[O365ConnectorCardSection]"}, - "potential_action": { - "key": "potentialAction", - "type": "[O365ConnectorCardActionBase]", - }, - } - - def __init__(self, **kwargs): - super(O365ConnectorCard, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.text = kwargs.get("text", None) - self.summary = kwargs.get("summary", None) - self.theme_color = kwargs.get("theme_color", None) - self.sections = kwargs.get("sections", None) - self.potential_action = kwargs.get("potential_action", None) - - -class O365ConnectorCardActionBase(Model): - """O365 connector card action base. - - :param type: Type of the action. Possible values include: 'ViewAction', - 'OpenUri', 'HttpPOST', 'ActionCard' - :type type: str or ~botframework.connector.teams.models.enum - :param name: Name of the action that will be used as button title - :type name: str - :param id: Action Id - :type id: str - """ - - _attribute_map = { - "type": {"key": "@type", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "id": {"key": "@id", "type": "str"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardActionBase, self).__init__(**kwargs) - self.type = kwargs.get("type", None) - self.name = kwargs.get("name", None) - self.id = kwargs.get("id", None) - - -class O365ConnectorCardActionCard(O365ConnectorCardActionBase): - """O365 connector card ActionCard action. - - :param type: Type of the action. Possible values include: 'ViewAction', - 'OpenUri', 'HttpPOST', 'ActionCard' - :type type: str or ~botframework.connector.teams.models.enum - :param name: Name of the action that will be used as button title - :type name: str - :param id: Action Id - :type id: str - :param inputs: Set of inputs contained in this ActionCard whose each item - can be in any subtype of O365ConnectorCardInputBase - :type inputs: - list[~botframework.connector.teams.models.O365ConnectorCardInputBase] - :param actions: Set of actions contained in this ActionCard whose each - item can be in any subtype of O365ConnectorCardActionBase except - O365ConnectorCardActionCard, as nested ActionCard is forbidden. - :type actions: - list[~botframework.connector.teams.models.O365ConnectorCardActionBase] - """ - - _attribute_map = { - "type": {"key": "@type", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "id": {"key": "@id", "type": "str"}, - "inputs": {"key": "inputs", "type": "[O365ConnectorCardInputBase]"}, - "actions": {"key": "actions", "type": "[O365ConnectorCardActionBase]"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardActionCard, self).__init__(**kwargs) - self.inputs = kwargs.get("inputs", None) - self.actions = kwargs.get("actions", None) - - -class O365ConnectorCardActionQuery(Model): - """O365 connector card HttpPOST invoke query. - - :param body: The results of body string defined in - IO365ConnectorCardHttpPOST with substituted input values - :type body: str - :param action_id: Action Id associated with the HttpPOST action button - triggered, defined in O365ConnectorCardActionBase. - :type action_id: str - """ - - _attribute_map = { - "body": {"key": "body", "type": "str"}, - "action_id": {"key": "actionId", "type": "str"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardActionQuery, self).__init__(**kwargs) - self.body = kwargs.get("body", None) - # This is how it comes in from Teams - self.action_id = kwargs.get("actionId", None) - - -class O365ConnectorCardDateInput(O365ConnectorCardInputBase): - """O365 connector card date input. - - :param type: Input type name. Possible values include: 'textInput', - 'dateInput', 'multichoiceInput' - :type type: str or ~botframework.connector.teams.models.enum - :param id: Input Id. It must be unique per entire O365 connector card. - :type id: str - :param is_required: Define if this input is a required field. Default - value is false. - :type is_required: bool - :param title: Input title that will be shown as the placeholder - :type title: str - :param value: Default value for this input field - :type value: str - :param include_time: Include time input field. Default value is false - (date only). - :type include_time: bool - """ - - _attribute_map = { - "type": {"key": "@type", "type": "str"}, - "id": {"key": "id", "type": "str"}, - "is_required": {"key": "isRequired", "type": "bool"}, - "title": {"key": "title", "type": "str"}, - "value": {"key": "value", "type": "str"}, - "include_time": {"key": "includeTime", "type": "bool"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardDateInput, self).__init__(**kwargs) - self.include_time = kwargs.get("include_time", None) - - -class O365ConnectorCardFact(Model): - """O365 connector card fact. - - :param name: Display name of the fact - :type name: str - :param value: Display value for the fact - :type value: str - """ - - _attribute_map = { - "name": {"key": "name", "type": "str"}, - "value": {"key": "value", "type": "str"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardFact, self).__init__(**kwargs) - self.name = kwargs.get("name", None) - self.value = kwargs.get("value", None) - - -class O365ConnectorCardHttpPOST(O365ConnectorCardActionBase): - """O365 connector card HttpPOST action. - - :param type: Type of the action. Possible values include: 'ViewAction', - 'OpenUri', 'HttpPOST', 'ActionCard' - :type type: str or ~botframework.connector.teams.models.enum - :param name: Name of the action that will be used as button title - :type name: str - :param id: Action Id - :type id: str - :param body: Content to be posted back to bots via invoke - :type body: str - """ - - _attribute_map = { - "type": {"key": "@type", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "id": {"key": "@id", "type": "str"}, - "body": {"key": "body", "type": "str"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardHttpPOST, self).__init__(**kwargs) - self.body = kwargs.get("body", None) - - -class O365ConnectorCardImage(Model): - """O365 connector card image. - - :param image: URL for the image - :type image: str - :param title: Alternative text for the image - :type title: str - """ - - _attribute_map = { - "image": {"key": "image", "type": "str"}, - "title": {"key": "title", "type": "str"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardImage, self).__init__(**kwargs) - self.image = kwargs.get("image", None) - self.title = kwargs.get("title", None) - - -class O365ConnectorCardInputBase(Model): - """O365 connector card input for ActionCard action. - - :param type: Input type name. Possible values include: 'textInput', - 'dateInput', 'multichoiceInput' - :type type: str or ~botframework.connector.teams.models.enum - :param id: Input Id. It must be unique per entire O365 connector card. - :type id: str - :param is_required: Define if this input is a required field. Default - value is false. - :type is_required: bool - :param title: Input title that will be shown as the placeholder - :type title: str - :param value: Default value for this input field - :type value: str - """ - - _attribute_map = { - "type": {"key": "@type", "type": "str"}, - "id": {"key": "id", "type": "str"}, - "is_required": {"key": "isRequired", "type": "bool"}, - "title": {"key": "title", "type": "str"}, - "value": {"key": "value", "type": "str"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardInputBase, self).__init__(**kwargs) - self.type = kwargs.get("type", None) - self.id = kwargs.get("id", None) - self.is_required = kwargs.get("is_required", None) - self.title = kwargs.get("title", None) - self.value = kwargs.get("value", None) - - -class O365ConnectorCardMultichoiceInput(O365ConnectorCardInputBase): - """O365 connector card multiple choice input. - - :param type: Input type name. Possible values include: 'textInput', - 'dateInput', 'multichoiceInput' - :type type: str or ~botframework.connector.teams.models.enum - :param id: Input Id. It must be unique per entire O365 connector card. - :type id: str - :param is_required: Define if this input is a required field. Default - value is false. - :type is_required: bool - :param title: Input title that will be shown as the placeholder - :type title: str - :param value: Default value for this input field - :type value: str - :param choices: Set of choices whose each item can be in any subtype of - O365ConnectorCardMultichoiceInputChoice. - :type choices: - list[~botframework.connector.teams.models.O365ConnectorCardMultichoiceInputChoice] - :param style: Choice item rendering style. Default value is 'compact'. - Possible values include: 'compact', 'expanded' - :type style: str or ~botframework.connector.teams.models.enum - :param is_multi_select: Define if this input field allows multiple - selections. Default value is false. - :type is_multi_select: bool - """ - - _attribute_map = { - "type": {"key": "@type", "type": "str"}, - "id": {"key": "id", "type": "str"}, - "is_required": {"key": "isRequired", "type": "bool"}, - "title": {"key": "title", "type": "str"}, - "value": {"key": "value", "type": "str"}, - "choices": { - "key": "choices", - "type": "[O365ConnectorCardMultichoiceInputChoice]", - }, - "style": {"key": "style", "type": "str"}, - "is_multi_select": {"key": "isMultiSelect", "type": "bool"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardMultichoiceInput, self).__init__(**kwargs) - self.choices = kwargs.get("choices", None) - self.style = kwargs.get("style", None) - self.is_multi_select = kwargs.get("is_multi_select", None) - - -class O365ConnectorCardMultichoiceInputChoice(Model): - """O365O365 connector card multiple choice input item. - - :param display: The text rendered on ActionCard. - :type display: str - :param value: The value received as results. - :type value: str - """ - - _attribute_map = { - "display": {"key": "display", "type": "str"}, - "value": {"key": "value", "type": "str"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardMultichoiceInputChoice, self).__init__(**kwargs) - self.display = kwargs.get("display", None) - self.value = kwargs.get("value", None) - - -class O365ConnectorCardOpenUriTarget(Model): - """O365 connector card OpenUri target. - - :param os: Target operating system. Possible values include: 'default', - 'iOS', 'android', 'windows' - :type os: str or ~botframework.connector.teams.models.enum - :param uri: Target url - :type uri: str - """ - - _attribute_map = { - "os": {"key": "os", "type": "str"}, - "uri": {"key": "uri", "type": "str"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardOpenUriTarget, self).__init__(**kwargs) - self.os = kwargs.get("os", None) - self.uri = kwargs.get("uri", None) - - -class O365ConnectorCardOpenUri(O365ConnectorCardActionBase): - """O365 connector card OpenUri action. - - :param type: Type of the action. Possible values include: 'ViewAction', - 'OpenUri', 'HttpPOST', 'ActionCard' - :type type: str or ~botframework.connector.teams.models.enum - :param name: Name of the action that will be used as button title - :type name: str - :param id: Action Id - :type id: str - :param targets: Target os / urls - :type targets: - list[~botframework.connector.teams.models.O365ConnectorCardOpenUriTarget] - """ - - _attribute_map = { - "type": {"key": "@type", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "id": {"key": "@id", "type": "str"}, - "targets": {"key": "targets", "type": "[O365ConnectorCardOpenUriTarget]"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardOpenUri, self).__init__(**kwargs) - self.targets = kwargs.get("targets", None) - - -class O365ConnectorCardSection(Model): - """O365 connector card section. - - :param title: Title of the section - :type title: str - :param text: Text for the section - :type text: str - :param activity_title: Activity title - :type activity_title: str - :param activity_subtitle: Activity subtitle - :type activity_subtitle: str - :param activity_text: Activity text - :type activity_text: str - :param activity_image: Activity image - :type activity_image: str - :param activity_image_type: Describes how Activity image is rendered. - Possible values include: 'avatar', 'article' - :type activity_image_type: str or - ~botframework.connector.teams.models.enum - :param markdown: Use markdown for all text contents. Default value is - true. - :type markdown: bool - :param facts: Set of facts for the current section - :type facts: - list[~botframework.connector.teams.models.O365ConnectorCardFact] - :param images: Set of images for the current section - :type images: - list[~botframework.connector.teams.models.O365ConnectorCardImage] - :param potential_action: Set of actions for the current section - :type potential_action: - list[~botframework.connector.teams.models.O365ConnectorCardActionBase] - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "text": {"key": "text", "type": "str"}, - "activity_title": {"key": "activityTitle", "type": "str"}, - "activity_subtitle": {"key": "activitySubtitle", "type": "str"}, - "activity_text": {"key": "activityText", "type": "str"}, - "activity_image": {"key": "activityImage", "type": "str"}, - "activity_image_type": {"key": "activityImageType", "type": "str"}, - "markdown": {"key": "markdown", "type": "bool"}, - "facts": {"key": "facts", "type": "[O365ConnectorCardFact]"}, - "images": {"key": "images", "type": "[O365ConnectorCardImage]"}, - "potential_action": { - "key": "potentialAction", - "type": "[O365ConnectorCardActionBase]", - }, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardSection, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.text = kwargs.get("text", None) - self.activity_title = kwargs.get("activity_title", None) - self.activity_subtitle = kwargs.get("activity_subtitle", None) - self.activity_text = kwargs.get("activity_text", None) - self.activity_image = kwargs.get("activity_image", None) - self.activity_image_type = kwargs.get("activity_image_type", None) - self.markdown = kwargs.get("markdown", None) - self.facts = kwargs.get("facts", None) - self.images = kwargs.get("images", None) - self.potential_action = kwargs.get("potential_action", None) - - -class O365ConnectorCardTextInput(O365ConnectorCardInputBase): - """O365 connector card text input. - - :param type: Input type name. Possible values include: 'textInput', - 'dateInput', 'multichoiceInput' - :type type: str or ~botframework.connector.teams.models.enum - :param id: Input Id. It must be unique per entire O365 connector card. - :type id: str - :param is_required: Define if this input is a required field. Default - value is false. - :type is_required: bool - :param title: Input title that will be shown as the placeholder - :type title: str - :param value: Default value for this input field - :type value: str - :param is_multiline: Define if text input is allowed for multiple lines. - Default value is false. - :type is_multiline: bool - :param max_length: Maximum length of text input. Default value is - unlimited. - :type max_length: float - """ - - _attribute_map = { - "type": {"key": "@type", "type": "str"}, - "id": {"key": "id", "type": "str"}, - "is_required": {"key": "isRequired", "type": "bool"}, - "title": {"key": "title", "type": "str"}, - "value": {"key": "value", "type": "str"}, - "is_multiline": {"key": "isMultiline", "type": "bool"}, - "max_length": {"key": "maxLength", "type": "float"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardTextInput, self).__init__(**kwargs) - self.is_multiline = kwargs.get("is_multiline", None) - self.max_length = kwargs.get("max_length", None) - - -class O365ConnectorCardViewAction(O365ConnectorCardActionBase): - """O365 connector card ViewAction action. - - :param type: Type of the action. Possible values include: 'ViewAction', - 'OpenUri', 'HttpPOST', 'ActionCard' - :type type: str or ~botframework.connector.teams.models.enum - :param name: Name of the action that will be used as button title - :type name: str - :param id: Action Id - :type id: str - :param target: Target urls, only the first url effective for card button - :type target: list[str] - """ - - _attribute_map = { - "type": {"key": "@type", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "id": {"key": "@id", "type": "str"}, - "target": {"key": "target", "type": "[str]"}, - } - - def __init__(self, **kwargs): - super(O365ConnectorCardViewAction, self).__init__(**kwargs) - self.target = kwargs.get("target", None) - - -class SigninStateVerificationQuery(Model): - """Signin state (part of signin action auth flow) verification invoke query. - - :param state: The state string originally received when the signin web - flow is finished with a state posted back to client via tab SDK - microsoftTeams.authentication.notifySuccess(state) - :type state: str - """ - - _attribute_map = { - "state": {"key": "state", "type": "str"}, - } - - def __init__(self, **kwargs): - super(SigninStateVerificationQuery, self).__init__(**kwargs) - self.state = kwargs.get("state", None) - - -class TaskModuleContinueResponse(TaskModuleResponseBase): - """Task Module Response with continue action. - - :param type: Choice of action options when responding to the task/submit - message. Possible values include: 'message', 'continue' - :type type: str or ~botframework.connector.teams.models.enum - :param value: The JSON for the Adaptive card to appear in the task module. - :type value: ~botframework.connector.teams.models.TaskModuleTaskInfo - """ - - _attribute_map = { - "type": {"key": "type", "type": "str"}, - "value": {"key": "value", "type": "TaskModuleTaskInfo"}, - } - - def __init__(self, **kwargs): - super(TaskModuleContinueResponse, self).__init__(**kwargs) - self.value = kwargs.get("value", None) - - -class TaskModuleMessageResponse(TaskModuleResponseBase): - """Task Module response with message action. - - :param type: Choice of action options when responding to the task/submit - message. Possible values include: 'message', 'continue' - :type type: str or ~botframework.connector.teams.models.enum - :param value: Teams will display the value of value in a popup message - box. - :type value: str - """ - - _attribute_map = { - "type": {"key": "type", "type": "str"}, - "value": {"key": "value", "type": "str"}, - } - - def __init__(self, **kwargs): - super(TaskModuleMessageResponse, self).__init__(**kwargs) - self.value = kwargs.get("value", None) - - -class TaskModuleRequest(Model): - """Task module invoke request value payload. - - :param data: User input data. Free payload with key-value pairs. - :type data: object - :param context: Current user context, i.e., the current theme - :type context: - ~botframework.connector.teams.models.TaskModuleRequestContext - """ - - _attribute_map = { - "data": {"key": "data", "type": "object"}, - "context": {"key": "context", "type": "TaskModuleRequestContext"}, - } - - def __init__(self, **kwargs): - super(TaskModuleRequest, self).__init__(**kwargs) - self.data = kwargs.get("data", None) - self.context = kwargs.get("context", None) - - -class TaskModuleRequestContext(Model): - """Current user context, i.e., the current theme. - - :param theme: - :type theme: str - """ - - _attribute_map = { - "theme": {"key": "theme", "type": "str"}, - } - - def __init__(self, **kwargs): - super(TaskModuleRequestContext, self).__init__(**kwargs) - self.theme = kwargs.get("theme", None) - - -class TaskModuleResponse(Model): - """Envelope for Task Module Response. - - :param task: The JSON for the Adaptive card to appear in the task module. - :type task: ~botframework.connector.teams.models.TaskModuleResponseBase - """ - - _attribute_map = { - "task": {"key": "task", "type": "TaskModuleResponseBase"}, - } - - def __init__(self, **kwargs): - super(TaskModuleResponse, self).__init__(**kwargs) - self.task = kwargs.get("task", None) - - -class TaskModuleResponseBase(Model): - """Base class for Task Module responses. - - :param type: Choice of action options when responding to the task/submit - message. Possible values include: 'message', 'continue' - :type type: str or ~botframework.connector.teams.models.enum - """ - - _attribute_map = { - "type": {"key": "type", "type": "str"}, - } - - def __init__(self, **kwargs): - super(TaskModuleResponseBase, self).__init__(**kwargs) - self.type = kwargs.get("type", None) - - -class TaskModuleTaskInfo(Model): - """Metadata for a Task Module. - - :param title: Appears below the app name and to the right of the app icon. - :type title: str - :param height: This can be a number, representing the task module's height - in pixels, or a string, one of: small, medium, large. - :type height: object - :param width: This can be a number, representing the task module's width - in pixels, or a string, one of: small, medium, large. - :type width: object - :param url: The URL of what is loaded as an iframe inside the task module. - One of url or card is required. - :type url: str - :param card: The JSON for the Adaptive card to appear in the task module. - :type card: ~botframework.connector.teams.models.Attachment - :param fallback_url: If a client does not support the task module feature, - this URL is opened in a browser tab. - :type fallback_url: str - :param completion_bot_id: If a client does not support the task module - feature, this URL is opened in a browser tab. - :type completion_bot_id: str - """ - - _attribute_map = { - "title": {"key": "title", "type": "str"}, - "height": {"key": "height", "type": "object"}, - "width": {"key": "width", "type": "object"}, - "url": {"key": "url", "type": "str"}, - "card": {"key": "card", "type": "Attachment"}, - "fallback_url": {"key": "fallbackUrl", "type": "str"}, - "completion_bot_id": {"key": "completionBotId", "type": "str"}, - } - - def __init__(self, **kwargs): - super(TaskModuleTaskInfo, self).__init__(**kwargs) - self.title = kwargs.get("title", None) - self.height = kwargs.get("height", None) - self.width = kwargs.get("width", None) - self.url = kwargs.get("url", None) - self.card = kwargs.get("card", None) - self.fallback_url = kwargs.get("fallback_url", None) - self.completion_bot_id = kwargs.get("completion_bot_id", None) - - -class TeamDetails(Model): - """Details related to a team. - - :param id: Unique identifier representing a team - :type id: str - :param name: Name of team. - :type name: str - :param aad_group_id: Azure Active Directory (AAD) Group Id for the team. - :type aad_group_id: str - :param channel_count: The count of channels in the team. - :type chanel_count: int - :param member_count: The count of members in the team. - :type member_count: int - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "aad_group_id": {"key": "aadGroupId", "type": "str"}, - "channel_count": {"key": "channelCount", "type": "int"}, - "member_count": {"key": "memberCount", "type": "int"}, - } - - def __init__(self, **kwargs): - super(TeamDetails, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - self.name = kwargs.get("name", None) - self.aad_group_id = kwargs.get("aad_group_id", None) - self.channel_count = kwargs.get("channel_count", None) - self.member_count = kwargs.get("member_count", None) - - -class TeamInfo(Model): - """Describes a team. - - :param id: Unique identifier representing a team - :type id: str - :param name: Name of team. - :type name: str - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - "name": {"key": "name", "type": "str"}, - } - - def __init__(self, **kwargs): - super(TeamInfo, self).__init__(**kwargs) - self.id = kwargs.get("id", None) - self.name = kwargs.get("name", None) - - -class TeamsChannelAccount(ChannelAccount): - """Teams channel account detailing user Azure Active Directory details. - - :param id: Channel id for the user or bot on this channel (Example: - joe@smith.com, or @joesmith or 123456) - :type id: str - :param name: Display friendly name - :type name: str - :param given_name: Given name part of the user name. - :type given_name: str - :param surname: Surname part of the user name. - :type surname: str - :param email: Email Id of the user. - :type email: str - :param user_principal_name: Unique user principal name - :type user_principal_name: str - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - "name": {"key": "name", "type": "str"}, - "given_name": {"key": "givenName", "type": "str"}, - "surname": {"key": "surname", "type": "str"}, - "email": {"key": "email", "type": "str"}, - "userPrincipalName": {"key": "userPrincipalName", "type": "str"}, - "aad_object_id": {"key": "objectId", "type": "str"}, - } - - def __init__(self, **kwargs): - super(TeamsChannelAccount, self).__init__(**kwargs) - self.given_name = kwargs.get("given_name", None) - self.surname = kwargs.get("surname", None) - self.email = kwargs.get("email", None) - self.user_principal_name = kwargs.get("userPrincipalName", None) - - -class TeamsPagedMembersResult(PagedMembersResult): - """Page of members for Teams. - - :param continuation_token: Paging token - :type continuation_token: str - :param members: The Teams Channel Accounts. - :type members: list[~botframework.connector.models.TeamsChannelAccount] - """ - - _attribute_map = { - "continuation_token": {"key": "continuationToken", "type": "str"}, - "members": {"key": "members", "type": "[TeamsChannelAccount]"}, - } - - def __init__(self, **kwargs): - super(TeamsPagedMembersResult, self).__init__(**kwargs) - self.continuation_token = kwargs.get("continuation_token", None) - self.members = kwargs.get("members", None) - - -class TeamsChannelData(Model): - """Channel data specific to messages received in Microsoft Teams. - - :param channel: Information about the channel in which the message was - sent - :type channel: ~botframework.connector.teams.models.ChannelInfo - :param event_type: Type of event. - :type event_type: str - :param team: Information about the team in which the message was sent - :type team: ~botframework.connector.teams.models.TeamInfo - :param notification: Notification settings for the message - :type notification: ~botframework.connector.teams.models.NotificationInfo - :param tenant: Information about the tenant in which the message was sent - :type tenant: ~botframework.connector.teams.models.TenantInfo - """ - - _attribute_map = { - "channel": {"key": "channel", "type": "ChannelInfo"}, - "event_type": {"key": "eventType", "type": "str"}, - "team": {"key": "team", "type": "TeamInfo"}, - "notification": {"key": "notification", "type": "NotificationInfo"}, - "tenant": {"key": "tenant", "type": "TenantInfo"}, - } - - def __init__(self, **kwargs): - super(TeamsChannelData, self).__init__(**kwargs) - self.channel = kwargs.get("channel", None) - # doing camel case here since that's how the data comes in - self.event_type = kwargs.get("event_type", None) - self.team = kwargs.get("team", None) - self.notification = kwargs.get("notification", None) - self.tenant = kwargs.get("tenant", None) - - -class TenantInfo(Model): - """Describes a tenant. - - :param id: Unique identifier representing a tenant - :type id: str - """ - - _attribute_map = { - "id": {"key": "id", "type": "str"}, - } - - def __init__(self, **kwargs): - super(TenantInfo, self).__init__(**kwargs) - self.id = kwargs.get("id", None) diff --git a/libraries/botbuilder-schema/botbuilder/schema/teams/_models_py3.py b/libraries/botbuilder-schema/botbuilder/schema/teams/_models_py3.py index 181cbd367..0b6e0e899 100644 --- a/libraries/botbuilder-schema/botbuilder/schema/teams/_models_py3.py +++ b/libraries/botbuilder-schema/botbuilder/schema/teams/_models_py3.py @@ -1,16 +1,36 @@ -# coding=utf-8 -# -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. -# -------------------------------------------------------------------------- +# Licensed under the MIT License. +from enum import Enum +from typing import List from msrest.serialization import Model -from botbuilder.schema import Activity, Attachment, ChannelAccount, PagedMembersResult +from botbuilder.schema import ( + Attachment, + ChannelAccount, + PagedMembersResult, + ConversationAccount, +) + + +class TabEntityContext(Model): + """ + Current TabRequest entity context, or 'tabEntityId'. + + :param tab_entity_id: Gets or sets the entity id of the tab. + :type tab_entity_id: str + """ + + _attribute_map = { + "tab_entity_id": {"key": "tabEntityId", "type": "str"}, + } + + def __init__(self, *, tab_entity_id=None, **kwargs) -> None: + super(TabEntityContext, self).__init__(**kwargs) + self.tab_entity_id = tab_entity_id + self._custom_init() + + def _custom_init(self): + return class TaskModuleRequest(Model): @@ -21,17 +41,24 @@ class TaskModuleRequest(Model): :param context: Current user context, i.e., the current theme :type context: ~botframework.connector.teams.models.TaskModuleRequestContext + :param tab_entity_context: Gets or sets current tab request context. + :type tab_entity_context: + ~botframework.connector.teams.models.TabEntityContext """ _attribute_map = { "data": {"key": "data", "type": "object"}, "context": {"key": "context", "type": "TaskModuleRequestContext"}, + "tab_entity_context": {"key": "tabContext", "type": "TabEntityContext"}, } - def __init__(self, *, data=None, context=None, **kwargs) -> None: + def __init__( + self, *, data=None, context=None, tab_entity_context=None, **kwargs + ) -> None: super(TaskModuleRequest, self).__init__(**kwargs) self.data = data self.context = context + self.tab_entity_context = tab_entity_context class AppBasedLinkQuery(Model): @@ -39,15 +66,19 @@ class AppBasedLinkQuery(Model): :param url: Url queried by user :type url: str + :param state: The magic code for OAuth Flow + :type state: str """ _attribute_map = { "url": {"key": "url", "type": "str"}, + "state": {"key": "state", "type": "str"}, } - def __init__(self, *, url: str = None, **kwargs) -> None: + def __init__(self, *, url: str = None, state: str = None, **kwargs) -> None: super(AppBasedLinkQuery, self).__init__(**kwargs) self.url = url + self.state = state class ChannelInfo(Model): @@ -57,17 +88,45 @@ class ChannelInfo(Model): :type id: str :param name: Name of the channel :type name: str + :param type: The channel type + :type type: str """ _attribute_map = { "id": {"key": "id", "type": "str"}, "name": {"key": "name", "type": "str"}, + "type": {"key": "type", "type": "str"}, } - def __init__(self, *, id: str = None, name: str = None, **kwargs) -> None: + def __init__( + self, *, id: str = None, name: str = None, type: str = None, **kwargs + ) -> None: super(ChannelInfo, self).__init__(**kwargs) self.id = id self.name = name + self.type = type + + +class CacheInfo(Model): + """A cache info object which notifies Teams how long an object should be cached for. + + :param cache_type: Type of Cache Info + :type cache_type: str + :param cache_duration: Duration of the Cached Info. + :type cache_duration: int + """ + + _attribute_map = { + "cache_type": {"key": "cacheType", "type": "str"}, + "cache_duration": {"key": "cacheDuration", "type": "int"}, + } + + def __init__( + self, *, cache_type: str = None, cache_duration: int = None, **kwargs + ) -> None: + super(CacheInfo, self).__init__(**kwargs) + self.cache_type = cache_type + self.cache_duration = cache_duration class ConversationList(Model): @@ -546,6 +605,8 @@ class MessageActionsPayload(Model): :type importance: str :param locale: Locale of the message set by the client. :type locale: str + :param link_to_message: Link back to the message. + :type link_to_message: str :param from_property: Sender of the message. :type from_property: ~botframework.connector.teams.models.MessageActionsPayloadFrom @@ -576,6 +637,7 @@ class MessageActionsPayload(Model): "summary": {"key": "summary", "type": "str"}, "importance": {"key": "importance", "type": "str"}, "locale": {"key": "locale", "type": "str"}, + "link_to_message": {"key": "linkToMessage", "type": "str"}, "from_property": {"key": "from", "type": "MessageActionsPayloadFrom"}, "body": {"key": "body", "type": "MessageActionsPayloadBody"}, "attachment_layout": {"key": "attachmentLayout", "type": "str"}, @@ -600,6 +662,7 @@ def __init__( summary: str = None, importance=None, locale: str = None, + link_to_message: str = None, from_property=None, body=None, attachment_layout: str = None, @@ -619,6 +682,7 @@ def __init__( self.summary = summary self.importance = importance self.locale = locale + self.link_to_message = link_to_message self.from_property = from_property self.body = body self.attachment_layout = attachment_layout @@ -693,6 +757,8 @@ class MessagingExtensionActionResponse(Model): :param compose_extension: :type compose_extension: ~botframework.connector.teams.models.MessagingExtensionResult + :param cache_info: CacheInfo for this MessagingExtensionActionResponse. + :type cache_info: ~botframework.connector.teams.models.CacheInfo """ _attribute_map = { @@ -701,12 +767,21 @@ class MessagingExtensionActionResponse(Model): "key": "composeExtension", "type": "MessagingExtensionResult", }, + "cache_info": {"key": "cacheInfo", "type": "CacheInfo"}, } - def __init__(self, *, task=None, compose_extension=None, **kwargs) -> None: + def __init__( + self, + *, + task=None, + compose_extension=None, + cache_info: CacheInfo = None, + **kwargs + ) -> None: super(MessagingExtensionActionResponse, self).__init__(**kwargs) self.task = task self.compose_extension = compose_extension + self.cache_info = cache_info class MessagingExtensionAttachment(Attachment): @@ -843,8 +918,9 @@ class MessagingExtensionResponse(Model): """Messaging extension response. :param compose_extension: - :type compose_extension: - ~botframework.connector.teams.models.MessagingExtensionResult + :type compose_extension: ~botframework.connector.teams.models.MessagingExtensionResult + :param cache_info: CacheInfo for this MessagingExtensionResponse. + :type cache_info: ~botframework.connector.teams.models.CacheInfo """ _attribute_map = { @@ -852,11 +928,13 @@ class MessagingExtensionResponse(Model): "key": "composeExtension", "type": "MessagingExtensionResult", }, + "cache_info": {"key": "cacheInfo", "type": CacheInfo}, } - def __init__(self, *, compose_extension=None, **kwargs) -> None: + def __init__(self, *, compose_extension=None, cache_info=None, **kwargs) -> None: super(MessagingExtensionResponse, self).__init__(**kwargs) self.compose_extension = compose_extension + self.cache_info = cache_info class MessagingExtensionResult(Model): @@ -939,11 +1017,22 @@ class NotificationInfo(Model): _attribute_map = { "alert": {"key": "alert", "type": "bool"}, + "alert_in_meeting": {"key": "alertInMeeting", "type": "bool"}, + "external_resource_url": {"key": "externalResourceUrl", "type": "str"}, } - def __init__(self, *, alert: bool = None, **kwargs) -> None: + def __init__( + self, + *, + alert: bool = None, + alert_in_meeting: bool = None, + external_resource_url: str = None, + **kwargs + ) -> None: super(NotificationInfo, self).__init__(**kwargs) self.alert = alert + self.alert_in_meeting = alert_in_meeting + self.external_resource_url = external_resource_url class O365ConnectorCard(Model): @@ -1654,15 +1743,19 @@ class TaskModuleResponse(Model): :param task: The JSON for the Adaptive card to appear in the task module. :type task: ~botframework.connector.teams.models.TaskModuleResponseBase + :param cache_info: CacheInfo for this TaskModuleResponse. + :type cache_info: ~botframework.connector.teams.models.CacheInfo """ _attribute_map = { "task": {"key": "task", "type": "TaskModuleResponseBase"}, + "cache_info": {"key": "cacheInfo", "type": "CacheInfo"}, } - def __init__(self, *, task=None, **kwargs) -> None: + def __init__(self, *, task=None, cache_info=None, **kwargs) -> None: super(TaskModuleResponse, self).__init__(**kwargs) self.task = task + self.cache_info = cache_info class TaskModuleTaskInfo(Model): @@ -1734,6 +1827,8 @@ class TeamDetails(Model): :type channel_count: int :param member_count: The count of members in the team. :type member_count: int + :param type: The team type + :type type: str """ _attribute_map = { @@ -1742,6 +1837,7 @@ class TeamDetails(Model): "aad_group_id": {"key": "aadGroupId", "type": "str"}, "channel_count": {"key": "channelCount", "type": "int"}, "member_count": {"key": "memberCount", "type": "int"}, + "type": {"key": "type", "type": "str"}, } def __init__( @@ -1752,6 +1848,7 @@ def __init__( aad_group_id: str = None, member_count: int = None, channel_count: int = None, + type: str = None, **kwargs ) -> None: super(TeamDetails, self).__init__(**kwargs) @@ -1760,6 +1857,7 @@ def __init__( self.aad_group_id = aad_group_id self.channel_count = channel_count self.member_count = member_count + self.type = type class TeamInfo(Model): @@ -1769,17 +1867,23 @@ class TeamInfo(Model): :type id: str :param name: Name of team. :type name: str + :param name: Azure AD Teams group ID. + :type name: str """ _attribute_map = { "id": {"key": "id", "type": "str"}, "name": {"key": "name", "type": "str"}, + "aad_group_id": {"key": "aadGroupId", "type": "str"}, } - def __init__(self, *, id: str = None, name: str = None, **kwargs) -> None: + def __init__( + self, *, id: str = None, name: str = None, aad_group_id: str = None, **kwargs + ) -> None: super(TeamInfo, self).__init__(**kwargs) self.id = id self.name = name + self.aad_group_id = aad_group_id class TeamsChannelAccount(ChannelAccount): @@ -1796,8 +1900,12 @@ class TeamsChannelAccount(ChannelAccount): :type surname: str :param email: Email Id of the user. :type email: str - :param user_principal_name: Unique user principal name + :param user_principal_name: Unique user principal name. :type user_principal_name: str + :param tenant_id: Tenant Id of the user. + :type tenant_id: str + :param user_role: User Role of the user. + :type user_role: str """ _attribute_map = { @@ -1807,7 +1915,9 @@ class TeamsChannelAccount(ChannelAccount): "surname": {"key": "surname", "type": "str"}, "email": {"key": "email", "type": "str"}, "user_principal_name": {"key": "userPrincipalName", "type": "str"}, - "aad_object_id": {"key": "objectId", "type": "str"}, + "aad_object_id": {"key": "aadObjectId", "type": "str"}, + "tenant_id": {"key": "tenantId", "type": "str"}, + "user_role": {"key": "userRole", "type": "str"}, } def __init__( @@ -1819,6 +1929,8 @@ def __init__( surname: str = None, email: str = None, user_principal_name: str = None, + tenant_id: str = None, + user_role: str = None, **kwargs ) -> None: super(TeamsChannelAccount, self).__init__(id=id, name=name, **kwargs) @@ -1826,6 +1938,8 @@ def __init__( self.surname = surname self.email = email self.user_principal_name = user_principal_name + self.tenant_id = tenant_id + self.user_role = user_role class TeamsPagedMembersResult(PagedMembersResult): @@ -1846,7 +1960,7 @@ def __init__( self, *, continuation_token: str = None, - members: [TeamsChannelAccount] = None, + members: List[TeamsChannelAccount] = None, **kwargs ) -> None: super(TeamsPagedMembersResult, self).__init__( @@ -1856,6 +1970,26 @@ def __init__( self.members = members +class TeamsChannelDataSettings(Model): + """ + Represents the settings information for a Teams channel data. + + :param selected_channel: Information about the selected Teams channel. + :type selected_channel: ~botframework.connector.teams.models.ChannelInfo + :param additional_properties: Gets or sets properties that are not otherwise defined by the + type but that might appear in the REST JSON object. + :type additional_properties: object + """ + + _attribute_map = { + "selected_channel": {"key": "selectedChannel", "type": "ChannelInfo"}, + } + + def __init__(self, *, selected_channel=None, **kwargs) -> None: + super(TeamsChannelDataSettings, self).__init__(**kwargs) + self.selected_channel = selected_channel + + class TeamsChannelData(Model): """Channel data specific to messages received in Microsoft Teams. @@ -1870,33 +2004,48 @@ class TeamsChannelData(Model): :type notification: ~botframework.connector.teams.models.NotificationInfo :param tenant: Information about the tenant in which the message was sent :type tenant: ~botframework.connector.teams.models.TenantInfo + :param meeting: Information about the meeting in which the message was sent + :type meeting: ~botframework.connector.teams.models.TeamsMeetingInfo + :param settings: Information about the about the settings in which the message was sent + :type settings: ~botframework.connector.teams.models.TeamsChannelDataSettings + :param on_behalf_of: The OnBehalfOf list for user attribution + :type on_behalf_of: list[~botframework.connector.teams.models.OnBehalfOf] """ _attribute_map = { "channel": {"key": "channel", "type": "ChannelInfo"}, - "eventType": {"key": "eventType", "type": "str"}, + "event_type": {"key": "eventType", "type": "str"}, "team": {"key": "team", "type": "TeamInfo"}, "notification": {"key": "notification", "type": "NotificationInfo"}, "tenant": {"key": "tenant", "type": "TenantInfo"}, + "meeting": {"key": "meeting", "type": "TeamsMeetingInfo"}, + "settings": {"key": "settings", "type": "TeamsChannelDataSettings"}, + "on_behalf_of": {"key": "onBehalfOf", "type": "[OnBehalfOf]"}, } def __init__( self, *, channel=None, - eventType: str = None, + event_type: str = None, team=None, notification=None, tenant=None, + meeting=None, + settings: TeamsChannelDataSettings = None, + on_behalf_of: List["OnBehalfOf"] = None, **kwargs ) -> None: super(TeamsChannelData, self).__init__(**kwargs) self.channel = channel # doing camel case here since that's how the data comes in - self.event_type = eventType + self.event_type = event_type self.team = team self.notification = notification self.tenant = tenant + self.meeting = meeting + self.settings = settings + self.on_behalf_of = on_behalf_of if on_behalf_of is not None else [] class TenantInfo(Model): @@ -1913,3 +2062,970 @@ class TenantInfo(Model): def __init__(self, *, id: str = None, **kwargs) -> None: super(TenantInfo, self).__init__(**kwargs) self.id = id + + +class TeamsMeetingInfo(Model): + """Describes a Teams Meeting. + + :param id: Unique identifier representing a meeting + :type id: str + """ + + _attribute_map = { + "id": {"key": "id", "type": "str"}, + } + + def __init__(self, *, id: str = None, **kwargs) -> None: + super(TeamsMeetingInfo, self).__init__(**kwargs) + self.id = id + + +class MeetingParticipantInfo(Model): + """Teams meeting participant details. + + :param role: Role of the participant in the current meeting. + :type role: str + :param in_meeting: True, if the participant is in the meeting. + :type in_meeting: bool + """ + + _attribute_map = { + "role": {"key": "role", "type": "str"}, + "in_meeting": {"key": "inMeeting", "type": "bool"}, + } + + def __init__(self, *, role: str = None, in_meeting: bool = None, **kwargs) -> None: + super(MeetingParticipantInfo, self).__init__(**kwargs) + self.role = role + self.in_meeting = in_meeting + + +class TeamsMeetingParticipant(Model): + """Teams participant channel account detailing user Azure Active Directory and meeting participant details. + + :param user: Teams Channel Account information for this meeting participant + :type user: TeamsChannelAccount + :param meeting: >Information specific to this participant in the specific meeting. + :type meeting: MeetingParticipantInfo + :param conversation: Conversation Account for the meeting. + :type conversation: ConversationAccount + """ + + _attribute_map = { + "user": {"key": "user", "type": "TeamsChannelAccount"}, + "meeting": {"key": "meeting", "type": "MeetingParticipantInfo"}, + "conversation": {"key": "conversation", "type": "ConversationAccount"}, + } + + def __init__( + self, + *, + user: TeamsChannelAccount = None, + meeting: MeetingParticipantInfo = None, + conversation: ConversationAccount = None, + **kwargs + ) -> None: + super(TeamsMeetingParticipant, self).__init__(**kwargs) + self.user = user + self.meeting = meeting + self.conversation = conversation + + +class TabContext(Model): + """ + Current tab request context, i.e., the current theme. + + :param theme: Gets or sets the current user's theme. + :type theme: str + """ + + _attribute_map = { + "theme": {"key": "theme", "type": "str"}, + } + + def __init__(self, *, theme=None, **kwargs) -> None: + super(TabContext, self).__init__(**kwargs) + self.theme = theme + self._custom_init() + + def _custom_init(self): + return + + +class TabRequest(Model): + """ + Invoke ('tab/fetch') request value payload. + + :param tab_entity_context: Gets or sets current tab entity request context. + :type tab_entity_context: + ~botframework.connector.teams.models.TabEntityContext + :param context: Gets or sets current tab entity request context. + :type context: + ~botframework.connector.teams.models.TabContext + :param state: Gets or sets state, which is the magic code for OAuth Flow. + :type state: str + """ + + _attribute_map = { + "tab_entity_context": {"key": "tabContext", "type": "TabEntityContext"}, + "context": {"key": "context", "type": "TabContext"}, + "state": {"key": "state", "type": "str"}, + } + + def __init__( + self, *, tab_entity_context=None, context=None, state=None, **kwargs + ) -> None: + super(TabRequest, self).__init__(**kwargs) + self.tab_entity_context = tab_entity_context + self.context = context + self.state = state + self._custom_init() + + def _custom_init(self): + return + + +class TabResponseCard(Model): + """ + Envelope for cards for a Tab request. + + :param card: Gets or sets adaptive card for this card tab response. + :type card: object + """ + + _attribute_map = { + "card": {"key": "card", "type": "object"}, + } + + def __init__(self, *, card=None, **kwargs) -> None: + super(TabResponseCard, self).__init__(**kwargs) + self.card = card + self._custom_init() + + def _custom_init(self): + return + + +class TabResponseCards(Model): + """ + Envelope for cards for a TabResponse. + + :param cards: Gets or sets adaptive card for this card tab response. + :type cards: + list[ ~botframework.connector.teams.models.TabResponseCard] + """ + + _attribute_map = { + "cards": {"key": "cards", "type": "[TabResponseCard]"}, + } + + def __init__(self, *, cards=None, **kwargs) -> None: + super(TabResponseCards, self).__init__(**kwargs) + self.cards = cards + self._custom_init() + + def _custom_init(self): + return + + +class TabResponsePayload(Model): + """ + Initializes a new instance of the TabResponsePayload class. + + :param type: Gets or sets choice of action options when responding to the + tab/fetch message. Possible values include: 'continue', 'auth' or 'silentAuth' + :type type: str + :param value: Gets or sets the TabResponseCards when responding to + tab/fetch activity with type of 'continue'. + :type value: TabResponseCards + :param suggested_actions: Gets or sets the Suggested Actions for this card tab. + :type suggested_actions: TabSuggestedActions + """ + + _attribute_map = { + "type": {"key": "type", "type": "str"}, + "value": {"key": "value", "type": "TabResponseCards"}, + "suggested_actions": {"key": "suggestedActions", "type": "TabSuggestedActions"}, + } + + def __init__( + self, *, type=None, value=None, suggested_actions=None, **kwargs + ) -> None: + super(TabResponsePayload, self).__init__(**kwargs) + self.type = type + self.value = value + self.suggested_actions = suggested_actions + self._custom_init() + + def _custom_init(self): + return + + +class TabResponse(Model): + """ + Envelope for Card Tab Response Payload. + + :param tab: Possible values include: 'continue', 'auth' or 'silentAuth' + :type type: ~botframework.connector.teams.models.TabResponsePayload + """ + + _attribute_map = { + "tab": {"key": "tab", "type": "TabResponsePayload"}, + } + + def __init__(self, *, tab=None, **kwargs) -> None: + super(TabResponse, self).__init__(**kwargs) + self.tab = tab + self._custom_init() + + def _custom_init(self): + return + + +class TabSumit(Model): + """ + Invoke ('tab/submit') request value payload. + + :param tab_entity_context: Gets or sets current tab entity request context. + :type tab_entity_context: + ~botframework.connector.teams.models.TabEntityContext + :param context: Gets or sets current tab entity request context. + :type context: + ~botframework.connector.teams.models.TabContext + :param data: User input data. Free payload containing properties of key-value pairs. + :type data: + ~botframework.connector.teams.models.TabSubmitData + """ + + _attribute_map = { + "tab_entity_context": {"key": "tabContext", "type": "TabEntityContext"}, + "context": {"key": "context", "type": "TabContext"}, + "data": {"key": "data", "type": "TabSubmitData"}, + } + + def __init__( + self, *, tab_entity_context=None, context=None, data=None, **kwargs + ) -> None: + super(TabSumit, self).__init__(**kwargs) + self.tab_entity_context = tab_entity_context + self.context = context + self.data = data + self._custom_init() + + def _custom_init(self): + return + + +class TabSubmitData(Model): + """ + Invoke ('tab/submit') request value payload data. + + :param type: Currently, 'tab/submit'. + :type type: str + :param properties: Gets or sets properties that are not otherwise defined by the TabSubmit + type but that might appear in the serialized REST JSON object. + :type properties: object + """ + + _attribute_map = { + "type": {"key": "type", "type": "str"}, + "properties": {"key": "properties", "type": "{object}"}, + } + + def __init__(self, *, type=None, properties=None, **kwargs) -> None: + super(TabSubmitData, self).__init__(**kwargs) + self.type = type + self.properties = properties + self._custom_init() + + def _custom_init(self): + return + + +class TabSubmit(Model): + """ + Initializes a new instance of the TabSubmit class. + + :param tab_entity_context: Gets or sets current tab entity request context. + :type tab_entity_context: ~botframework.connector.teams.models.TabEntityContext + :param context: Gets or sets current user context, i.e., the current theme. + :type context: ~botframework.connector.teams.models.TabContext + :param data: User input data. Free payload containing properties of key-value pairs. + :type data: ~botframework.connector.teams.models.TabSubmitData + """ + + _attribute_map = { + "tab_entity_context": {"key": "tabContext", "type": "TabEntityContext"}, + "context": {"key": "context", "type": "TabContext"}, + "data": {"key": "data", "type": "TabSubmitData"}, + } + + def __init__( + self, *, tab_entity_context=None, context=None, data=None, **kwargs + ) -> None: + super(TabSubmit, self).__init__(**kwargs) + self.tab_entity_context = tab_entity_context + self.context = context + self.data = data + self._custom_init() + + def _custom_init(self): + return + + +class TabSuggestedActions(Model): + """ + Tab SuggestedActions (Only when type is 'auth' or 'silentAuth'). + + :param actions: Gets or sets adaptive card for this card tab response. + :type actions: list[~botframework.connector.models.CardAction] + """ + + _attribute_map = { + "actions": {"key": "actions", "type": "[CardAction]"}, + } + + def __init__(self, *, actions=None, **kwargs) -> None: + super(TabSuggestedActions, self).__init__(**kwargs) + self.actions = actions + self._custom_init() + + def _custom_init(self): + return + + +class TaskModuleCardResponse(TaskModuleResponseBase): + """ + Tab Response to 'task/submit' from a tab. + + :param value: The JSON for the Adaptive cards to appear in the tab. + :type value: ~botframework.connector.teams.models.TabResponse + """ + + _attribute_map = { + "value": {"key": "value", "type": "TabResponse"}, + } + + def __init__(self, *, value=None, **kwargs) -> None: + super(TaskModuleCardResponse, self).__init__("continue", **kwargs) + self.value = value + self._custom_init() + + def _custom_init(self): + return + + +class MeetingDetailsBase(Model): + """Specific details of a Teams meeting. + + :param id: The meeting's Id, encoded as a BASE64 string. + :type id: str + :param join_url: The URL used to join the meeting. + :type join_url: str + :param title: The title of the meeting. + :type title: str + """ + + _attribute_map = { + "id": {"key": "uniqueId", "type": "str"}, + "join_url": {"key": "joinUrl", "type": "str"}, + "title": {"key": "title", "type": "str"}, + } + + def __init__( + self, *, id: str = None, join_url: str = None, title: str = None, **kwargs + ) -> None: + super(MeetingDetailsBase, self).__init__(**kwargs) + self.id = id + self.join_url = join_url + self.title = title + + +class MeetingDetails(MeetingDetailsBase): + """Specific details of a Teams meeting. + + :param ms_graph_resource_id: The MsGraphResourceId, used specifically for MS Graph API calls. + :type ms_graph_resource_id: str + :param scheduled_start_time: The meeting's scheduled start time, in UTC. + :type scheduled_start_time: str + :param scheduled_end_time: The meeting's scheduled end time, in UTC. + :type scheduled_end_time: str + :param type: The meeting's type. + :type type: str + """ + + _attribute_map = { + "ms_graph_resource_id": {"key": "msGraphResourceId", "type": "str"}, + "scheduled_start_time": {"key": "scheduledStartTime", "type": "str"}, + "scheduled_end_time": {"key": "scheduledEndTime", "type": "str"}, + "type": {"key": "type", "type": "str"}, + } + + def __init__( + self, + *, + ms_graph_resource_id: str = None, + scheduled_start_time: str = None, + scheduled_end_time: str = None, + type: str = None, + **kwargs + ) -> None: + super(MeetingDetails, self).__init__(**kwargs) + self.ms_graph_resource_id = ms_graph_resource_id + self.scheduled_start_time = scheduled_start_time + self.scheduled_end_time = scheduled_end_time + self.type = type + + +class MeetingInfo(Model): + """General information about a Teams meeting. + + :param details: The specific details of a Teams meeting. + :type details: ~botframework.connector.teams.models.MeetingDetails + :param conversation: The Conversation Account for the meeting. + :type conversation: ~botbuilder.schema.models.ConversationAccount + :param organizer: The meeting's scheduled start time, in UTC. + :type organizer: ~botbuilder.schema.models.TeamsChannelAccount + """ + + _attribute_map = { + "details": {"key": "details", "type": "object"}, + "conversation": {"key": "conversation", "type": "object"}, + "organizer": {"key": "organizer", "type": "object"}, + } + + def __init__( + self, + *, + details: MeetingDetails = None, + conversation: ConversationAccount = None, + organizer: TeamsChannelAccount = None, + **kwargs + ) -> None: + super(MeetingInfo, self).__init__(**kwargs) + self.details = details + self.conversation = conversation + self.organizer = organizer + + +class MeetingEventDetails(MeetingDetailsBase): + """Base class for Teams meting start and end events. + + :param meeting_type: The meeting's type. + :type meeting_type: str + """ + + _attribute_map = {"meeting_type": {"key": "MeetingType", "type": "str"}} + + def __init__(self, *, meeting_type: str = None, **kwargs): + super(MeetingEventDetails, self).__init__(**kwargs) + self.meeting_type = meeting_type + + +class MeetingStartEventDetails(MeetingDetailsBase): + """Specific details of a Teams meeting start event. + + :param start_time: Timestamp for meeting start, in UTC. + :type start_time: str + """ + + _attribute_map = {"start_time": {"key": "StartTime", "type": "str"}} + + def __init__(self, *, start_time: str = None, **kwargs): + super(MeetingStartEventDetails, self).__init__(**kwargs) + self.start_time = start_time + + +class MeetingEndEventDetails(MeetingDetailsBase): + """Specific details of a Teams meeting end event. + + :param end_time: Timestamp for meeting end, in UTC. + :type end_time: str + """ + + _attribute_map = {"end_time": {"key": "EndTime", "type": "str"}} + + def __init__(self, *, end_time: str = None, **kwargs): + super(MeetingEndEventDetails, self).__init__(**kwargs) + self.end_time = end_time + + +class UserMeetingDetails(Model): + """Specific details of a user in a Teams meeting. + + :param role: Role of the participant in the current meeting. + :type role: str + :param in_meeting: True, if the participant is in the meeting. + :type in_meeting: bool + """ + + _attribute_map = { + "role": {"key": "role", "type": "str"}, + "in_meeting": {"key": "inMeeting", "type": "bool"}, + } + + def __init__(self, *, role: str = None, in_meeting: bool = None, **kwargs) -> None: + super(UserMeetingDetails, self).__init__(**kwargs) + self.in_meeting = in_meeting + self.role = role + + +class TeamsMeetingMember(Model): + """Data about the meeting participants. + + :param user: The channel user data. + :type user: TeamsChannelAccount + :param meeting: The user meeting details. + :type meeting: UserMeetingDetails + """ + + _attribute_map = { + "user": {"key": "user", "type": "TeamsChannelAccount"}, + "meeting": {"key": "meeting", "type": "UserMeetingDetails"}, + } + + def __init__( + self, + *, + user: TeamsChannelAccount = None, + meeting: UserMeetingDetails = None, + **kwargs + ) -> None: + super(TeamsMeetingMember, self).__init__(**kwargs) + self.user = user + self.meeting = meeting + + +class MeetingParticipantsEventDetails(Model): + """Data about the meeting participants. + + :param members: The members involved in the meeting event. + :type members: list[~botframework.connector.models.TeamsMeetingMember] + """ + + _attribute_map = { + "conversations": {"key": "members", "type": "[TeamsMeetingMember]"}, + } + + def __init__(self, *, members: List[TeamsMeetingMember] = None, **kwargs) -> None: + super(MeetingParticipantsEventDetails, self).__init__(**kwargs) + self.members = members + + +class ReadReceiptInfo(Model): + """General information about a read receipt. + + :param last_read_message_id: The id of the last read message. + :type last_read_message_id: str + """ + + _attribute_map = { + "last_read_message_id": {"key": "lastReadMessageId", "type": "str"}, + } + + def __init__(self, *, last_read_message_id: str = None, **kwargs) -> None: + super(ReadReceiptInfo, self).__init__(**kwargs) + self.last_read_message_id = last_read_message_id + + @staticmethod + def is_message_read(compare_message_id, last_read_message_id): + """ + Helper method useful for determining if a message has been read. + This method converts the strings to integers. If the compare_message_id is + less than or equal to the last_read_message_id, then the message has been read. + + :param compare_message_id: The id of the message to compare. + :param last_read_message_id: The id of the last message read by the user. + :return: True if the compare_message_id is less than or equal to the last_read_message_id. + """ + if not compare_message_id or not last_read_message_id: + return False + + try: + compare_message_id_long = int(compare_message_id) + last_read_message_id_long = int(last_read_message_id) + except ValueError: + return False + + return compare_message_id_long <= last_read_message_id_long + + def is_message_read_instance(self, compare_message_id): + """ + Helper method useful for determining if a message has been read. + If the compare_message_id is less than or equal to the last_read_message_id, + then the message has been read. + + :param compare_message_id: The id of the message to compare. + :return: True if the compare_message_id is less than or equal to the last_read_message_id. + """ + return ReadReceiptInfo.is_message_read( + compare_message_id, self.last_read_message_id + ) + + +class BotConfigAuth(Model): + """Specifies bot config auth, including type and suggestedActions. + + :param type: The type of bot config auth. + :type type: str + :param suggested_actions: The suggested actions of bot config auth. + :type suggested_actions: ~botframework.connector.models.SuggestedActions + """ + + _attribute_map = { + "type": {"key": "type", "type": "str"}, + "suggested_actions": {"key": "suggestedActions", "type": "SuggestedActions"}, + } + + def __init__(self, *, type: str = "auth", suggested_actions=None, **kwargs) -> None: + super(BotConfigAuth, self).__init__(**kwargs) + self.type = type + self.suggested_actions = suggested_actions + + +class ConfigResponseBase(Model): + """Specifies Invoke response base, including response type. + + :param response_type: Response type for invoke request + :type response_type: str + """ + + _attribute_map = { + "response_type": {"key": "responseType", "type": "str"}, + } + + def __init__(self, *, response_type: str = None, **kwargs) -> None: + super(ConfigResponseBase, self).__init__(**kwargs) + self.response_type = response_type + + +class ConfigResponse(ConfigResponseBase): + """Envelope for Config Response Payload. + + :param config: The response to the config message. Possible values: 'auth', 'task' + :type config: T + :param cache_info: Response cache info + :type cache_info: ~botframework.connector.teams.models.CacheInfo + """ + + _attribute_map = { + "config": {"key": "config", "type": "object"}, + "cache_info": {"key": "cacheInfo", "type": "CacheInfo"}, + } + + def __init__(self, *, config=None, cache_info=None, **kwargs) -> None: + super(ConfigResponse, self).__init__(response_type="config", **kwargs) + self.config = config + self.cache_info = cache_info + + +class ConfigTaskResponse(ConfigResponse): + """Envelope for Config Task Response. + + This class uses TaskModuleResponseBase as the type for the config parameter. + """ + + def __init__(self, *, config=None, **kwargs) -> None: + super(ConfigTaskResponse, self).__init__( + config=config or TaskModuleResponseBase(), **kwargs + ) + + +class ConfigAuthResponse(ConfigResponse): + """Envelope for Config Auth Response. + + This class uses BotConfigAuth as the type for the config parameter. + """ + + def __init__(self, *, config=None, **kwargs) -> None: + super(ConfigAuthResponse, self).__init__( + config=config or BotConfigAuth(), **kwargs + ) + + +class OnBehalfOf(Model): + """Specifies attribution for notifications. + + :param item_id: The identification of the item. Default is 0. + :type item_id: int + :param mention_type: The mention type. Default is "person". + :type mention_type: str + :param mri: Message resource identifier (MRI) of the person on whose behalf the message is sent. + :type mri: str + :param display_name: Name of the person. Used as fallback in case name resolution is unavailable. + :type display_name: str + """ + + _attribute_map = { + "item_id": {"key": "itemid", "type": "int"}, + "mention_type": {"key": "mentionType", "type": "str"}, + "mri": {"key": "mri", "type": "str"}, + "display_name": {"key": "displayName", "type": "str"}, + } + + def __init__( + self, + *, + item_id: int = 0, + mention_type: str = "person", + mri: str = None, + display_name: str = None, + **kwargs + ) -> None: + super(OnBehalfOf, self).__init__(**kwargs) + self.item_id = item_id + self.mention_type = mention_type + self.mri = mri + self.display_name = display_name + + +class SurfaceType(Enum): + """ + Defines Teams Surface type for use with a Surface object. + + :var Unknown: TeamsSurfaceType is Unknown. + :vartype Unknown: int + :var MeetingStage: TeamsSurfaceType is MeetingStage.. + :vartype MeetingStage: int + :var MeetingTabIcon: TeamsSurfaceType is MeetingTabIcon. + :vartype MeetingTabIcon: int + """ + + Unknown = 0 + + MeetingStage = 1 + + MeetingTabIcon = 2 + + +class ContentType(Enum): + """ + Defines content type. Depending on contentType, content field will have a different structure. + + :var Unknown: Content type is Unknown. + :vartype Unknown: int + :var Task: TContent type is Task. + :vartype Task: int + """ + + Unknown = 0 + + Task = 1 + + +class MeetingNotificationBase(Model): + """Specifies Bot meeting notification base including channel data and type. + + :param type: Type of Bot meeting notification. + :type type: str + """ + + _attribute_map = { + "type": {"key": "type", "type": "str"}, + } + + def __init__(self, *, type: str = None, **kwargs) -> None: + super(MeetingNotificationBase, self).__init__(**kwargs) + self.type = type + + +class MeetingNotification(MeetingNotificationBase): + """Specifies Bot meeting notification including meeting notification value. + + :param value: Teams Bot meeting notification value. + :type value: TargetedMeetingNotificationValue + """ + + _attribute_map = { + "value": {"key": "value", "type": "TargetedMeetingNotificationValue"}, + } + + def __init__( + self, *, value: "TargetedMeetingNotificationValue" = None, **kwargs + ) -> None: + super(MeetingNotification, self).__init__(**kwargs) + self.value = value + + +class MeetingNotificationChannelData(Model): + """Specify Teams Bot meeting notification channel data. + + :param on_behalf_of_list: The Teams Bot meeting notification's OnBehalfOf list. + :type on_behalf_of_list: list[~botframework.connector.teams.models.OnBehalfOf] + """ + + _attribute_map = { + "on_behalf_of_list": {"key": "OnBehalfOf", "type": "[OnBehalfOf]"} + } + + def __init__(self, *, on_behalf_of_list: List["OnBehalfOf"] = None, **kwargs): + super(MeetingNotificationChannelData, self).__init__(**kwargs) + self.on_behalf_of_list = on_behalf_of_list + + +class MeetingNotificationRecipientFailureInfo(Model): + """Information regarding failure to notify a recipient of a meeting notification. + + :param recipient_mri: The MRI for a recipient meeting notification failure. + :type recipient_mri: str + :param error_code: The error code for a meeting notification. + :type error_code: str + :param failure_reason: The reason why a participant meeting notification failed. + :type failure_reason: str + """ + + _attribute_map = { + "recipient_mri": {"key": "recipientMri", "type": "str"}, + "error_code": {"key": "errorcode", "type": "str"}, + "failure_reason": {"key": "failureReason", "type": "str"}, + } + + def __init__( + self, + *, + recipient_mri: str = None, + error_code: str = None, + failure_reason: str = None, + **kwargs + ): + super(MeetingNotificationRecipientFailureInfo, self).__init__(**kwargs) + self.recipient_mri = recipient_mri + self.error_code = error_code + self.failure_reason = failure_reason + + +class MeetingNotificationResponse(Model): + """Specifies Bot meeting notification response. + + Contains list of MeetingNotificationRecipientFailureInfo. + + :param recipients_failure_info: The list of MeetingNotificationRecipientFailureInfo. + :type recipients_failure_info: list[~botframework.connector.teams.models.MeetingNotificationRecipientFailureInfo] + """ + + _attribute_map = { + "recipients_failure_info": { + "key": "recipientsFailureInfo", + "type": "[MeetingNotificationRecipientFailureInfo]", + } + } + + def __init__( + self, + *, + recipients_failure_info: List["MeetingNotificationRecipientFailureInfo"] = None, + **kwargs + ): + super(MeetingNotificationResponse, self).__init__(**kwargs) + self.recipients_failure_info = recipients_failure_info + + +class Surface(Model): + """Specifies where the notification will be rendered in the meeting UX. + + :param type: The value indicating where the notification will be rendered in the meeting UX. + :type type: ~botframework.connector.teams.models.SurfaceType + """ + + _attribute_map = { + "type": {"key": "surface", "type": "SurfaceType"}, + } + + def __init__(self, *, type: SurfaceType = None, **kwargs): + super(Surface, self).__init__(**kwargs) + self.type = type + + +class MeetingStageSurface(Surface): + """Specifies meeting stage surface. + + :param content_type: The content type of this MeetingStageSurface. + :type content_type: ~botframework.connector.teams.models.ContentType + :param content: The content of this MeetingStageSurface. + :type content: object + """ + + _attribute_map = { + "content_type": {"key": "contentType", "type": "ContentType"}, + "content": {"key": "content", "type": "object"}, + } + + def __init__( + self, + *, + content_type: ContentType = ContentType.Task, + content: object = None, + **kwargs + ): + super(MeetingStageSurface, self).__init__(SurfaceType.MeetingStage, **kwargs) + self.content_type = content_type + self.content = content + + +class MeetingTabIconSurface(Surface): + """ + Specifies meeting tab icon surface. + + :param tab_entity_id: The tab entity Id of this MeetingTabIconSurface. + :type tab_entity_id: str + """ + + _attribute_map = { + "tab_entity_id": {"key": "tabEntityId", "type": "str"}, + } + + def __init__(self, *, tab_entity_id: str = None, **kwargs): + super(MeetingTabIconSurface, self).__init__( + SurfaceType.MeetingTabIcon, **kwargs + ) + self.tab_entity_id = tab_entity_id + + +class TargetedMeetingNotificationValue(Model): + """Specifies the targeted meeting notification value, including recipients and surfaces. + + :param recipients: The collection of recipients of the targeted meeting notification. + :type recipients: list[str] + :param surfaces: The collection of surfaces on which to show the notification. + :type surfaces: list[~botframework.connector.teams.models.Surface] + """ + + _attribute_map = { + "recipients": {"key": "recipients", "type": "[str]"}, + "surfaces": {"key": "surfaces", "type": "[Surface]"}, + } + + def __init__( + self, *, recipients: List[str] = None, surfaces: List[Surface] = None, **kwargs + ): + super(TargetedMeetingNotificationValue, self).__init__(**kwargs) + self.recipients = recipients + self.surfaces = surfaces + + +class TargetedMeetingNotification(MeetingNotification): + """Specifies Teams targeted meeting notification. + + :param value: The value of the TargetedMeetingNotification. + :type value: ~botframework.connector.teams.models.TargetedMeetingNotificationValue + :param channel_data: Teams Bot meeting notification channel data. + :type channel_data: ~botframework.connector.teams.models.MeetingNotificationChannelData + """ + + _attribute_map = { + "value": {"key": "value", "type": "TargetedMeetingNotificationValue"}, + "channel_data": { + "key": "channelData", + "type": "MeetingNotificationChannelData", + }, + } + + def __init__( + self, + *, + value: "TargetedMeetingNotificationValue" = None, + channel_data: "MeetingNotificationChannelData" = None, + **kwargs + ): + super(TargetedMeetingNotification, self).__init__(value=value, **kwargs) + self.channel_data = channel_data diff --git a/libraries/botbuilder-schema/requirements.txt b/libraries/botbuilder-schema/requirements.txt index 2969b6597..c6b07eaec 100644 --- a/libraries/botbuilder-schema/requirements.txt +++ b/libraries/botbuilder-schema/requirements.txt @@ -1 +1,2 @@ -msrest==0.6.10 \ No newline at end of file +aiounittest==1.3.0 +msrest== 0.7.* \ No newline at end of file diff --git a/libraries/botbuilder-schema/setup.py b/libraries/botbuilder-schema/setup.py index bb183e091..43855c655 100644 --- a/libraries/botbuilder-schema/setup.py +++ b/libraries/botbuilder-schema/setup.py @@ -5,8 +5,11 @@ from setuptools import setup NAME = "botbuilder-schema" -VERSION = os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" -REQUIRES = ["msrest==0.6.10"] +VERSION = os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" +REQUIRES = [ + "msrest== 0.7.*", + "urllib3", +] root = os.path.abspath(os.path.dirname(__file__)) @@ -24,7 +27,10 @@ long_description_content_type="text/x-rst", license="MIT", install_requires=REQUIRES, - packages=["botbuilder.schema", "botbuilder.schema.teams",], + packages=[ + "botbuilder.schema", + "botbuilder.schema.teams", + ], include_package_data=True, classifiers=[ "Programming Language :: Python :: 3.7", diff --git a/libraries/botbuilder-schema/tests/teams/test_bot_config_auth.py b/libraries/botbuilder-schema/tests/teams/test_bot_config_auth.py new file mode 100644 index 000000000..f6d771c4e --- /dev/null +++ b/libraries/botbuilder-schema/tests/teams/test_bot_config_auth.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botbuilder.schema.teams import BotConfigAuth + + +class TestBotConfigAuth(aiounittest.AsyncTestCase): + def test_bot_config_auth_inits_with_no_args(self): + bot_config_auth_response = BotConfigAuth() + + self.assertIsNotNone(bot_config_auth_response) + self.assertIsInstance(bot_config_auth_response, BotConfigAuth) + self.assertEqual("auth", bot_config_auth_response.type) diff --git a/libraries/botbuilder-schema/tests/teams/test_config_auth_response.py b/libraries/botbuilder-schema/tests/teams/test_config_auth_response.py new file mode 100644 index 000000000..54221399d --- /dev/null +++ b/libraries/botbuilder-schema/tests/teams/test_config_auth_response.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botbuilder.schema.teams import ConfigAuthResponse + + +class TestConfigAuthResponse(aiounittest.AsyncTestCase): + def test_config_auth_response_init_with_no_args(self): + config_auth_response = ConfigAuthResponse() + + self.assertIsNotNone(config_auth_response) + self.assertIsInstance(config_auth_response, ConfigAuthResponse) + self.assertEqual("config", config_auth_response.response_type) diff --git a/libraries/botbuilder-schema/tests/teams/test_config_response.py b/libraries/botbuilder-schema/tests/teams/test_config_response.py new file mode 100644 index 000000000..39d2ce0d5 --- /dev/null +++ b/libraries/botbuilder-schema/tests/teams/test_config_response.py @@ -0,0 +1,13 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botbuilder.schema.teams import ConfigResponse + + +class TestConfigResponse(aiounittest.AsyncTestCase): + def test_config_response_inits_with_no_args(self): + config_response = ConfigResponse() + + self.assertIsNotNone(config_response) + self.assertIsInstance(config_response, ConfigResponse) diff --git a/libraries/botbuilder-schema/tests/teams/test_config_task_response.py b/libraries/botbuilder-schema/tests/teams/test_config_task_response.py new file mode 100644 index 000000000..53126388d --- /dev/null +++ b/libraries/botbuilder-schema/tests/teams/test_config_task_response.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botbuilder.schema.teams import ConfigTaskResponse + + +class TestConfigTaskResponse(aiounittest.AsyncTestCase): + def test_config_task_response_init_with_no_args(self): + config_task_response = ConfigTaskResponse() + + self.assertIsNotNone(config_task_response) + self.assertIsInstance(config_task_response, ConfigTaskResponse) + self.assertEqual("config", config_task_response.response_type) diff --git a/libraries/botbuilder-schema/tests/teams/test_message_actions_payload.py b/libraries/botbuilder-schema/tests/teams/test_message_actions_payload.py new file mode 100644 index 000000000..ef0cc55ee --- /dev/null +++ b/libraries/botbuilder-schema/tests/teams/test_message_actions_payload.py @@ -0,0 +1,146 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botframework.connector.models import ( + MessageActionsPayloadFrom, + MessageActionsPayloadBody, + MessageActionsPayloadAttachment, + MessageActionsPayloadMention, + MessageActionsPayloadReaction, +) +from botbuilder.schema.teams import MessageActionsPayload + + +class TestingMessageActionsPayload(aiounittest.AsyncTestCase): + # Arrange + test_id = "01" + reply_to_id = "test_reply_to_id" + message_type = "test_message_type" + created_date_time = "01/01/2000" + last_modified_date_time = "01/01/2000" + deleted = False + subject = "test_subject" + summary = "test_summary" + importance = "high" + locale = "test_locale" + link_to_message = "https://teams.microsoft/com/l/message/testing-id" + from_property = MessageActionsPayloadFrom() + body = MessageActionsPayloadBody + attachment_layout = "test_attachment_layout" + attachments = [MessageActionsPayloadAttachment()] + mentions = [MessageActionsPayloadMention()] + reactions = [MessageActionsPayloadReaction()] + + # Act + message = MessageActionsPayload( + id=test_id, + reply_to_id=reply_to_id, + message_type=message_type, + created_date_time=created_date_time, + last_modified_date_time=last_modified_date_time, + deleted=deleted, + subject=subject, + summary=summary, + importance=importance, + locale=locale, + link_to_message=link_to_message, + from_property=from_property, + body=body, + attachment_layout=attachment_layout, + attachments=attachments, + mentions=mentions, + reactions=reactions, + ) + + def test_assign_id(self, message_action_payload=message, test_id=test_id): + # Assert + self.assertEqual(message_action_payload.id, test_id) + + def test_assign_reply_to_id( + self, message_action_payload=message, reply_to_id=reply_to_id + ): + # Assert + self.assertEqual(message_action_payload.reply_to_id, reply_to_id) + + def test_assign_message_type( + self, message_action_payload=message, message_type=message_type + ): + # Assert + self.assertEqual(message_action_payload.message_type, message_type) + + def test_assign_created_date_time( + self, message_action_payload=message, created_date_time=created_date_time + ): + # Assert + self.assertEqual(message_action_payload.created_date_time, created_date_time) + + def test_assign_last_modified_date_time( + self, + message_action_payload=message, + last_modified_date_time=last_modified_date_time, + ): + # Assert + self.assertEqual( + message_action_payload.last_modified_date_time, last_modified_date_time + ) + + def test_assign_deleted(self, message_action_payload=message, deleted=deleted): + # Assert + self.assertEqual(message_action_payload.deleted, deleted) + + def test_assign_subject(self, message_action_payload=message, subject=subject): + # Assert + self.assertEqual(message_action_payload.subject, subject) + + def test_assign_summary(self, message_action_payload=message, summary=summary): + # Assert + self.assertEqual(message_action_payload.summary, summary) + + def test_assign_importance( + self, message_action_payload=message, importance=importance + ): + # Assert + self.assertEqual(message_action_payload.importance, importance) + + def test_assign_locale(self, message_action_payload=message, locale=locale): + # Assert + self.assertEqual(message_action_payload.locale, locale) + + def test_assign_link_to_message( + self, message_action_payload=message, link_to_message=link_to_message + ): + # Assert + self.assertEqual(message_action_payload.link_to_message, link_to_message) + + def test_assign_from_property( + self, message_action_payload=message, from_property=from_property + ): + # Assert + self.assertEqual(message_action_payload.from_property, from_property) + + def test_assign_body(self, message_action_payload=message, body=body): + # Assert + self.assertEqual(message_action_payload.body, body) + + def test_assign_attachment_layout( + self, message_action_payload=message, attachment_layout=attachment_layout + ): + # Assert + self.assertEqual(message_action_payload.attachment_layout, attachment_layout) + + def test_assign_attachments( + self, message_action_payload=message, attachments=attachments + ): + # Assert + self.assertEqual(message_action_payload.attachments, attachments) + + def test_assign_mentions(self, message_action_payload=message, mentions=mentions): + # Assert + self.assertEqual(message_action_payload.mentions, mentions) + + def test_assign_reactions( + self, message_action_payload=message, reactions=reactions + ): + # Assert + self.assertEqual(message_action_payload.reactions, reactions) diff --git a/libraries/botbuilder-schema/tests/teams/test_read_receipt_info.py b/libraries/botbuilder-schema/tests/teams/test_read_receipt_info.py new file mode 100644 index 000000000..e6aad9bf3 --- /dev/null +++ b/libraries/botbuilder-schema/tests/teams/test_read_receipt_info.py @@ -0,0 +1,28 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botbuilder.schema.teams import ReadReceiptInfo + + +class TestReadReceiptInfo(aiounittest.AsyncTestCase): + def test_read_receipt_info(self): + # Arrange + test_cases = [ + ("1000", "1000", True), + ("1001", "1000", True), + ("1000", "1001", False), + ("1000", None, False), + (None, "1000", False), + ] + + for last_read, compare, is_read in test_cases: + # Act + info = ReadReceiptInfo(last_read_message_id=last_read) + + # Assert + self.assertEqual(info.last_read_message_id, last_read) + self.assertEqual(info.is_message_read_instance(compare), is_read) + self.assertEqual( + ReadReceiptInfo.is_message_read(compare, last_read), is_read + ) diff --git a/libraries/botbuilder-schema/tests/test_activity.py b/libraries/botbuilder-schema/tests/test_activity.py new file mode 100644 index 000000000..513ff06f9 --- /dev/null +++ b/libraries/botbuilder-schema/tests/test_activity.py @@ -0,0 +1,745 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botbuilder.schema import ( + Activity, + ConversationReference, + ConversationAccount, + ChannelAccount, + Entity, + ResourceResponse, + Attachment, +) +from botbuilder.schema._connector_client_enums import ActivityTypes + + +class TestActivity(aiounittest.AsyncTestCase): + def test_constructor(self): + # Arrange + activity = Activity() + + # Assert + self.assertIsNotNone(activity) + self.assertIsNone(activity.type) + self.assertIsNone(activity.id) + self.assertIsNone(activity.timestamp) + self.assertIsNone(activity.local_timestamp) + self.assertIsNone(activity.local_timezone) + self.assertIsNone(activity.service_url) + self.assertIsNone(activity.channel_id) + self.assertIsNone(activity.from_property) + self.assertIsNone(activity.conversation) + self.assertIsNone(activity.recipient) + self.assertIsNone(activity.text_format) + self.assertIsNone(activity.attachment_layout) + self.assertIsNone(activity.members_added) + self.assertIsNone(activity.members_removed) + self.assertIsNone(activity.reactions_added) + self.assertIsNone(activity.reactions_removed) + self.assertIsNone(activity.topic_name) + self.assertIsNone(activity.history_disclosed) + self.assertIsNone(activity.locale) + self.assertIsNone(activity.text) + self.assertIsNone(activity.speak) + self.assertIsNone(activity.input_hint) + self.assertIsNone(activity.summary) + self.assertIsNone(activity.suggested_actions) + self.assertIsNone(activity.attachments) + self.assertIsNone(activity.entities) + self.assertIsNone(activity.channel_data) + self.assertIsNone(activity.action) + self.assertIsNone(activity.reply_to_id) + self.assertIsNone(activity.label) + self.assertIsNone(activity.value_type) + self.assertIsNone(activity.value) + self.assertIsNone(activity.name) + self.assertIsNone(activity.relates_to) + self.assertIsNone(activity.code) + self.assertIsNone(activity.expiration) + self.assertIsNone(activity.importance) + self.assertIsNone(activity.delivery_mode) + self.assertIsNone(activity.listen_for) + self.assertIsNone(activity.text_highlights) + self.assertIsNone(activity.semantic_action) + self.assertIsNone(activity.caller_id) + + def test_apply_conversation_reference(self): + # Arrange + activity = self.__create_activity() + conversation_reference = ConversationReference( + channel_id="123", + service_url="serviceUrl", + conversation=ConversationAccount(id="456"), + user=ChannelAccount(id="abc"), + bot=ChannelAccount(id="def"), + activity_id="12345", + locale="en-uS", + ) + + # Act + activity.apply_conversation_reference(reference=conversation_reference) + + # Assert + self.assertEqual(conversation_reference.channel_id, activity.channel_id) + self.assertEqual(conversation_reference.locale, activity.locale) + self.assertEqual(conversation_reference.service_url, activity.service_url) + self.assertEqual( + conversation_reference.conversation.id, activity.conversation.id + ) + self.assertEqual(conversation_reference.bot.id, activity.from_property.id) + self.assertEqual(conversation_reference.user.id, activity.recipient.id) + self.assertEqual(conversation_reference.activity_id, activity.reply_to_id) + + def test_apply_conversation_reference_with_is_incoming_true(self): + # Arrange + activity = self.__create_activity() + conversation_reference = ConversationReference( + channel_id="cr_123", + service_url="cr_serviceUrl", + conversation=ConversationAccount(id="cr_456"), + user=ChannelAccount(id="cr_abc"), + bot=ChannelAccount(id="cr_def"), + activity_id="cr_12345", + locale="en-uS", + ) + + # Act + activity.apply_conversation_reference( + reference=conversation_reference, is_incoming=True + ) + + # Assert + self.assertEqual(conversation_reference.channel_id, activity.channel_id) + self.assertEqual(conversation_reference.locale, activity.locale) + self.assertEqual(conversation_reference.service_url, activity.service_url) + self.assertEqual( + conversation_reference.conversation.id, activity.conversation.id + ) + self.assertEqual(conversation_reference.user.id, activity.from_property.id) + self.assertEqual(conversation_reference.bot.id, activity.recipient.id) + self.assertEqual(conversation_reference.activity_id, activity.id) + + def test_as_contact_relation_update_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.contact_relation_update + + # Act + result = activity.as_contact_relation_update_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.contact_relation_update) + + def test_as_contact_relation_update_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_contact_relation_update_activity() + + # Assert + self.assertIsNone(result) + + def test_as_conversation_update_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.conversation_update + + # Act + result = activity.as_conversation_update_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.conversation_update) + + def test_as_conversation_update_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_conversation_update_activity() + + # Assert + self.assertIsNone(result) + + def test_as_end_of_conversation_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.end_of_conversation + + # Act + result = activity.as_end_of_conversation_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.end_of_conversation) + + def test_as_end_of_conversation_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_end_of_conversation_activity() + + # Assert + self.assertIsNone(result) + + def test_as_event_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.event + + # Act + result = activity.as_event_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.event) + + def test_as_event_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_event_activity() + + # Assert + self.assertIsNone(result) + + def test_as_handoff_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.handoff + + # Act + result = activity.as_handoff_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.handoff) + + def test_as_handoff_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_handoff_activity() + + # Assert + self.assertIsNone(result) + + def test_as_installation_update_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.installation_update + + # Act + result = activity.as_installation_update_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.installation_update) + + def test_as_installation_update_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_installation_update_activity() + + # Assert + self.assertIsNone(result) + + def test_as_invoke_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.invoke + + # Act + result = activity.as_invoke_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.invoke) + + def test_as_invoke_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_invoke_activity() + + # Assert + self.assertIsNone(result) + + def test_as_message_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_message_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.message) + + def test_as_message_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.invoke + + # Act + result = activity.as_message_activity() + + # Assert + self.assertIsNone(result) + + def test_as_message_activity_type_none(self): + # Arrange + activity = self.__create_activity() + activity.type = None + + # Act + result = activity.as_message_activity() + + # Assert + self.assertIsNone(result) + + def test_as_message_delete_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message_delete + + # Act + result = activity.as_message_delete_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.message_delete) + + def test_as_message_delete_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_message_delete_activity() + + # Assert + self.assertIsNone(result) + + def test_as_message_reaction_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message_reaction + + # Act + result = activity.as_message_reaction_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.message_reaction) + + def test_as_message_reaction_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_message_reaction_activity() + + # Assert + self.assertIsNone(result) + + def test_as_message_update_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message_update + + # Act + result = activity.as_message_update_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.message_update) + + def test_as_message_update_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_message_update_activity() + + # Assert + self.assertIsNone(result) + + def test_as_suggestion_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.suggestion + + # Act + result = activity.as_suggestion_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.suggestion) + + def test_as_suggestion_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_suggestion_activity() + + # Assert + self.assertIsNone(result) + + def test_as_trace_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.trace + + # Act + result = activity.as_trace_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.trace) + + def test_as_trace_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_trace_activity() + + # Assert + self.assertIsNone(result) + + def test_as_typing_activity_return_activity(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.typing + + # Act + result = activity.as_typing_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.typing) + + def test_as_typing_activity_return_none(self): + # Arrange + activity = self.__create_activity() + activity.type = ActivityTypes.message + + # Act + result = activity.as_typing_activity() + + # Assert + self.assertIsNone(result) + + def test_create_contact_relation_update_activity(self): + # Act + result = Activity.create_contact_relation_update_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.contact_relation_update) + + def test_create_conversation_update_activity(self): + # Act + result = Activity.create_conversation_update_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.conversation_update) + + def test_create_end_of_conversation_activity(self): + # Act + result = Activity.create_end_of_conversation_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.end_of_conversation) + + def test_create_event_activity(self): + # Act + result = Activity.create_event_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.event) + + def test_create_handoff_activity(self): + # Act + result = Activity.create_handoff_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.handoff) + + def test_create_invoke_activity(self): + # Act + result = Activity.create_invoke_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.invoke) + + def test_create_message_activity(self): + # Act + result = Activity.create_message_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.message) + + def test_create_reply(self): + # Arrange + activity = self.__create_activity() + text = "test reply" + locale = "en-us" + + # Act + result = activity.create_reply(text=text, locale=locale) + + # Assert + self.assertEqual(result.text, text) + self.assertEqual(result.locale, locale) + self.assertEqual(result.type, ActivityTypes.message) + + def test_create_reply_without_arguments(self): + # Arrange + activity = self.__create_activity() + + # Act + result = activity.create_reply() + + # Assert + self.assertEqual(result.type, ActivityTypes.message) + self.assertEqual(result.text, "") + self.assertEqual(result.locale, activity.locale) + + def test_create_trace(self): + # Arrange + activity = self.__create_activity() + name = "test-activity" + value_type = "string" + value = "test-value" + label = "test-label" + + # Act + result = activity.create_trace( + name=name, value_type=value_type, value=value, label=label + ) + + # Assert + self.assertEqual(result.type, ActivityTypes.trace) + self.assertEqual(result.name, name) + self.assertEqual(result.value_type, value_type) + self.assertEqual(result.value, value) + self.assertEqual(result.label, label) + + def test_create_trace_activity_no_recipient(self): + # Arrange + activity = self.__create_activity() + activity.recipient = None + + # Act + result = activity.create_trace("test") + + # Assert + self.assertIsNone(result.from_property.id) + self.assertIsNone(result.from_property.name) + + def test_crete_trace_activity_no_value_type(self): + # Arrange + name = "test-activity" + value = "test-value" + label = "test-label" + + # Act + result = Activity.create_trace_activity(name=name, value=value, label=label) + + # Assert + self.assertEqual(result.type, ActivityTypes.trace) + self.assertEqual(result.value_type, type(value)) + self.assertEqual(result.label, label) + + def test_create_trace_activity(self): + # Arrange + name = "test-activity" + value_type = "string" + value = "test-value" + label = "test-label" + + # Act + result = Activity.create_trace_activity( + name=name, value_type=value_type, value=value, label=label + ) + + # Assert + self.assertEqual(result.type, ActivityTypes.trace) + self.assertEqual(result.name, name) + self.assertEqual(result.value_type, value_type) + self.assertEqual(result.label, label) + + def test_create_typing_activity(self): + # Act + result = Activity.create_typing_activity() + + # Assert + self.assertEqual(result.type, ActivityTypes.typing) + + def test_get_conversation_reference(self): + # Arrange + activity = self.__create_activity() + + # Act + result = activity.get_conversation_reference() + + # Assert + self.assertEqual(activity.id, result.activity_id) + self.assertEqual(activity.from_property.id, result.user.id) + self.assertEqual(activity.recipient.id, result.bot.id) + self.assertEqual(activity.conversation.id, result.conversation.id) + self.assertEqual(activity.channel_id, result.channel_id) + self.assertEqual(activity.locale, result.locale) + self.assertEqual(activity.service_url, result.service_url) + + def test_get_mentions(self): + # Arrange + mentions = [Entity(type="mention"), Entity(type="reaction")] + activity = Activity(entities=mentions) + + # Act + result = Activity.get_mentions(activity) + + # Assert + self.assertEqual(len(result), 1) + self.assertEqual(result[0].type, "mention") + + def test_get_reply_conversation_reference(self): + # Arrange + activity = self.__create_activity() + reply = ResourceResponse(id="1234") + + # Act + result = activity.get_reply_conversation_reference(reply=reply) + + # Assert + self.assertEqual(reply.id, result.activity_id) + self.assertEqual(activity.from_property.id, result.user.id) + self.assertEqual(activity.recipient.id, result.bot.id) + self.assertEqual(activity.conversation.id, result.conversation.id) + self.assertEqual(activity.channel_id, result.channel_id) + self.assertEqual(activity.locale, result.locale) + self.assertEqual(activity.service_url, result.service_url) + + def test_has_content_empty(self): + # Arrange + activity_empty = Activity() + + # Act + result_empty = activity_empty.has_content() + + # Assert + self.assertEqual(result_empty, False) + + def test_has_content_with_text(self): + # Arrange + activity_with_text = Activity(text="test-text") + + # Act + result_with_text = activity_with_text.has_content() + + # Assert + self.assertEqual(result_with_text, True) + + def test_has_content_with_summary(self): + # Arrange + activity_with_summary = Activity(summary="test-summary") + + # Act + result_with_summary = activity_with_summary.has_content() + + # Assert + self.assertEqual(result_with_summary, True) + + def test_has_content_with_attachment(self): + # Arrange + activity_with_attachment = Activity(attachments=[Attachment()]) + + # Act + result_with_attachment = activity_with_attachment.has_content() + + # Assert + self.assertEqual(result_with_attachment, True) + + def test_has_content_with_channel_data(self): + # Arrange + activity_with_channel_data = Activity(channel_data="test-channel-data") + + # Act + result_with_channel_data = activity_with_channel_data.has_content() + + # Assert + self.assertEqual(result_with_channel_data, True) + + def test_is_from_streaming_connection(self): + # Arrange + non_streaming = [ + "http://yayay.com", + "https://yayay.com", + "HTTP://yayay.com", + "HTTPS://yayay.com", + ] + streaming = [ + "urn:botframework:WebSocket:wss://beep.com", + "urn:botframework:WebSocket:http://beep.com", + "URN:botframework:WebSocket:wss://beep.com", + "URN:botframework:WebSocket:http://beep.com", + ] + activity = self.__create_activity() + activity.service_url = None + + # Assert + self.assertEqual(activity.is_from_streaming_connection(), False) + + for s in non_streaming: + activity.service_url = s + self.assertEqual(activity.is_from_streaming_connection(), False) + + for s in streaming: + activity.service_url = s + self.assertEqual(activity.is_from_streaming_connection(), True) + + @staticmethod + def __create_activity() -> Activity: + account1 = ChannelAccount( + id="ChannelAccount_Id_1", + name="ChannelAccount_Name_1", + aad_object_id="ChannelAccount_aadObjectId_1", + role="ChannelAccount_Role_1", + ) + + account2 = ChannelAccount( + id="ChannelAccount_Id_2", + name="ChannelAccount_Name_2", + aad_object_id="ChannelAccount_aadObjectId_2", + role="ChannelAccount_Role_2", + ) + + conversation_account = ConversationAccount( + conversation_type="a", + id="123", + is_group=True, + name="Name", + role="ConversationAccount_Role", + ) + + activity = Activity( + id="123", + from_property=account1, + recipient=account2, + conversation=conversation_account, + channel_id="ChannelId123", + locale="en-uS", + service_url="ServiceUrl123", + ) + + return activity diff --git a/libraries/botbuilder-testing/README.rst b/libraries/botbuilder-testing/README.rst index 128ee8a8a..10ded9adb 100644 --- a/libraries/botbuilder-testing/README.rst +++ b/libraries/botbuilder-testing/README.rst @@ -3,8 +3,8 @@ BotBuilder-Testing SDK for Python ================================= -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch .. image:: https://badge.fury.io/py/botbuilder-testing.svg diff --git a/libraries/botbuilder-testing/botbuilder/testing/about.py b/libraries/botbuilder-testing/botbuilder/testing/about.py index 09585f325..dca57a9fa 100644 --- a/libraries/botbuilder-testing/botbuilder/testing/about.py +++ b/libraries/botbuilder-testing/botbuilder/testing/about.py @@ -6,7 +6,7 @@ __title__ = "botbuilder-testing" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botbuilder-testing/botbuilder/testing/storage_base_tests.py b/libraries/botbuilder-testing/botbuilder/testing/storage_base_tests.py index 96044f9de..e374a3401 100644 --- a/libraries/botbuilder-testing/botbuilder/testing/storage_base_tests.py +++ b/libraries/botbuilder-testing/botbuilder/testing/storage_base_tests.py @@ -8,6 +8,13 @@ All tests return true if assertions pass to indicate that the code ran to completion, passing internal assertions. Therefore, all tests using theses static tests should strictly check that the method returns true. +Note: Python cannot have dicts with properties with a None value like other SDKs can have properties with null values. + Because of this, StoreItem tests have "e_tag: *" where the tests in the other SDKs do not. + This has also caused us to comment out some parts of these tests where we assert that "e_tag" + is None for the same reason. A null e_tag should work just like a * e_tag when writing, + as far as the storage adapters are concerened, so this shouldn't cause issues. + + :Example: async def test_handle_null_keys_when_reading(self): await reset() @@ -17,7 +24,7 @@ async def test_handle_null_keys_when_reading(self): assert test_ran """ import pytest -from botbuilder.azure import CosmosDbStorage +from botbuilder.azure import CosmosDbPartitionedStorage from botbuilder.core import ( ConversationState, TurnContext, @@ -50,7 +57,7 @@ async def return_empty_object_when_reading_unknown_key(storage) -> bool: @staticmethod async def handle_null_keys_when_reading(storage) -> bool: - if isinstance(storage, (CosmosDbStorage, MemoryStorage)): + if isinstance(storage, (CosmosDbPartitionedStorage, MemoryStorage)): result = await storage.read(None) assert len(result.keys()) == 0 # Catch-all @@ -83,7 +90,7 @@ async def does_not_raise_when_writing_no_items(storage) -> bool: async def create_object(storage) -> bool: store_items = { "createPoco": {"id": 1}, - "createPocoStoreItem": {"id": 2}, + "createPocoStoreItem": {"id": 2, "e_tag": "*"}, } await storage.write(store_items) @@ -95,11 +102,10 @@ async def create_object(storage) -> bool: store_items["createPocoStoreItem"]["id"] == read_store_items["createPocoStoreItem"]["id"] ) - """ - If decided to validate e_tag integrity aagain, uncomment this code - assert read_store_items["createPoco"]["e_tag"] is not None + + # If decided to validate e_tag integrity again, uncomment this code + # assert read_store_items["createPoco"]["e_tag"] is not None assert read_store_items["createPocoStoreItem"]["e_tag"] is not None - """ return True @@ -122,7 +128,7 @@ async def handle_crazy_keys(storage) -> bool: async def update_object(storage) -> bool: original_store_items = { "pocoItem": {"id": 1, "count": 1}, - "pocoStoreItem": {"id": 1, "count": 1}, + "pocoStoreItem": {"id": 1, "count": 1, "e_tag": "*"}, } # 1st write should work @@ -131,9 +137,9 @@ async def update_object(storage) -> bool: loaded_store_items = await storage.read(["pocoItem", "pocoStoreItem"]) update_poco_item = loaded_store_items["pocoItem"] - # update_poco_item["e_tag"] = None + update_poco_item["e_tag"] = None update_poco_store_item = loaded_store_items["pocoStoreItem"] - # assert update_poco_store_item["e_tag"] is not None + assert update_poco_store_item["e_tag"] is not None # 2nd write should work update_poco_item["count"] += 1 @@ -153,11 +159,13 @@ async def update_object(storage) -> bool: update_poco_item["count"] = 123 await storage.write({"pocoItem": update_poco_item}) - """ - If decided to validate e_tag integrity aagain, uncomment this code # Write with old eTag should FAIL for storeItem update_poco_store_item["count"] = 123 + """ + This assert exists in the other SDKs but can't in python, currently + due to using "e_tag: *" above (see comment near the top of this file for details). + with pytest.raises(Exception) as err: await storage.write({"pocoStoreItem": update_poco_store_item}) assert err.value is not None @@ -166,7 +174,7 @@ async def update_object(storage) -> bool: reloaded_store_items2 = await storage.read(["pocoItem", "pocoStoreItem"]) reloaded_poco_item2 = reloaded_store_items2["pocoItem"] - # reloaded_poco_item2["e_tag"] = None + reloaded_poco_item2["e_tag"] = None reloaded_poco_store_item2 = reloaded_store_items2["pocoStoreItem"] assert reloaded_poco_item2["count"] == 123 @@ -175,7 +183,7 @@ async def update_object(storage) -> bool: # write with wildcard etag should work reloaded_poco_item2["count"] = 100 reloaded_poco_store_item2["count"] = 100 - # reloaded_poco_store_item2["e_tag"] = "*" + reloaded_poco_store_item2["e_tag"] = "*" wildcard_etag_dict = { "pocoItem": reloaded_poco_item2, @@ -195,15 +203,12 @@ async def update_object(storage) -> bool: assert reloaded_store_item4 is not None - """ - If decided to validate e_tag integrity aagain, uncomment this code reloaded_store_item4["e_tag"] = "" dict2 = {"pocoStoreItem": reloaded_store_item4} with pytest.raises(Exception) as err: await storage.write(dict2) assert err.value is not None - """ final_store_items = await storage.read(["pocoItem", "pocoStoreItem"]) assert final_store_items["pocoItem"]["count"] == 100 @@ -213,13 +218,13 @@ async def update_object(storage) -> bool: @staticmethod async def delete_object(storage) -> bool: - store_items = {"delete1": {"id": 1, "count": 1}} + store_items = {"delete1": {"id": 1, "count": 1, "e_tag": "*"}} await storage.write(store_items) read_store_items = await storage.read(["delete1"]) - # assert read_store_items["delete1"]["e_tag"] + assert read_store_items["delete1"]["e_tag"] assert read_store_items["delete1"]["count"] == 1 await storage.delete(["delete1"]) @@ -243,7 +248,11 @@ async def delete_unknown_object(storage) -> bool: @staticmethod async def perform_batch_operations(storage) -> bool: await storage.write( - {"batch1": {"count": 10}, "batch2": {"count": 20}, "batch3": {"count": 30},} + { + "batch1": {"count": 10}, + "batch2": {"count": 20}, + "batch3": {"count": 30}, + } ) result = await storage.read(["batch1", "batch2", "batch3"]) diff --git a/libraries/botbuilder-testing/requirements.txt b/libraries/botbuilder-testing/requirements.txt index 91ef96796..7bca77c2c 100644 --- a/libraries/botbuilder-testing/requirements.txt +++ b/libraries/botbuilder-testing/requirements.txt @@ -1,4 +1,4 @@ -botbuilder-schema>=4.7.1 -botbuilder-core>=4.7.1 -botbuilder-dialogs>=4.7.1 -aiounittest==1.3.0 +botbuilder-schema==4.17.0 +botbuilder-core==4.17.0 +botbuilder-dialogs==4.17.0 +aiounittest==1.4.0 diff --git a/libraries/botbuilder-testing/setup.py b/libraries/botbuilder-testing/setup.py index 433235306..9ee855a41 100644 --- a/libraries/botbuilder-testing/setup.py +++ b/libraries/botbuilder-testing/setup.py @@ -5,9 +5,11 @@ from setuptools import setup REQUIRES = [ - "botbuilder-schema>=4.7.1", - "botbuilder-core>=4.7.1", - "botbuilder-dialogs>=4.7.1", + "botbuilder-schema==4.17.0", + "botbuilder-core==4.17.0", + "botbuilder-dialogs==4.17.0", + "botbuilder-azure==4.17.0", + "pytest~=8.3.3", ] TESTS_REQUIRES = ["aiounittest==1.3.0"] diff --git a/libraries/botframework-connector/README.rst b/libraries/botframework-connector/README.rst index aec6b1e92..d19a47bba 100644 --- a/libraries/botframework-connector/README.rst +++ b/libraries/botframework-connector/README.rst @@ -3,8 +3,8 @@ Microsoft Bot Framework Connector for Python ============================================ -.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master - :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI +.. image:: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master + :target: https://dev.azure.com/FuseLabs/SDK_v4/_apis/build/status/Python/Python-CI-PR-yaml?branchName=master :align: right :alt: Azure DevOps status for master branch .. image:: https://badge.fury.io/py/botframework-connector.svg diff --git a/libraries/botframework-connector/azure_bdist_wheel.py b/libraries/botframework-connector/azure_bdist_wheel.py index a0e0049b5..56a1b0b20 100644 --- a/libraries/botframework-connector/azure_bdist_wheel.py +++ b/libraries/botframework-connector/azure_bdist_wheel.py @@ -75,7 +75,6 @@ def safer_version(version): class bdist_wheel(Command): - description = "create a wheel distribution" user_options = [ @@ -518,9 +517,9 @@ def skip(path): from distutils import log as logger import os.path + # from wheel.bdist_wheel import bdist_wheel class azure_bdist_wheel(bdist_wheel): - description = "Create an Azure wheel distribution" user_options = bdist_wheel.user_options + [ @@ -556,9 +555,7 @@ def write_record(self, bdist_dir, distinfo_dir): for azure_sub_package in folder_with_init: init_file = os.path.join(bdist_dir, azure_sub_package, "__init__.py") if os.path.isfile(init_file): - logger.info( - "manually remove {} while building the wheel".format(init_file) - ) + logger.info("manually remove %s while building the wheel", init_file) os.remove(init_file) else: raise ValueError( diff --git a/libraries/botframework-connector/botframework/connector/__init__.py b/libraries/botframework-connector/botframework/connector/__init__.py index 47e6ad952..e167a32ad 100644 --- a/libraries/botframework-connector/botframework/connector/__init__.py +++ b/libraries/botframework-connector/botframework/connector/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from .channels import Channels @@ -14,6 +10,23 @@ from .emulator_api_client import EmulatorApiClient from .version import VERSION -__all__ = ["Channels", "ConnectorClient", "EmulatorApiClient"] +from .aiohttp_bf_pipeline import AsyncBfPipeline +from .bot_framework_sdk_client_async import BotFrameworkConnectorConfiguration +from .http_client_base import HttpClientBase +from .http_client_factory import HttpClientFactory +from .http_request import HttpRequest +from .http_response_base import HttpResponseBase + +__all__ = [ + "AsyncBfPipeline", + "Channels", + "ConnectorClient", + "EmulatorApiClient", + "BotFrameworkConnectorConfiguration", + "HttpClientBase", + "HttpClientFactory", + "HttpRequest", + "HttpResponseBase", +] __version__ = VERSION diff --git a/libraries/botframework-connector/botframework/connector/_configuration.py b/libraries/botframework-connector/botframework/connector/_configuration.py index 33f23fd21..5dde8f9f8 100644 --- a/libraries/botframework-connector/botframework/connector/_configuration.py +++ b/libraries/botframework-connector/botframework/connector/_configuration.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest import Configuration @@ -26,7 +22,6 @@ class ConnectorClientConfiguration(Configuration): """ def __init__(self, credentials, base_url=None): - if credentials is None: raise ValueError("Parameter 'credentials' must not be None.") if not base_url: diff --git a/libraries/botframework-connector/botframework/connector/_not_implemented_http_client.py b/libraries/botframework-connector/botframework/connector/_not_implemented_http_client.py new file mode 100644 index 000000000..898df2f45 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/_not_implemented_http_client.py @@ -0,0 +1,15 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .http_client_base import HttpClientBase +from .http_request import HttpRequest +from .http_response_base import HttpResponseBase + + +class _NotImplementedHttpClient(HttpClientBase): + async def post( + self, *, request: HttpRequest # pylint: disable=unused-argument + ) -> HttpResponseBase: + raise RuntimeError( + "Please provide an http implementation for the skill BotFrameworkClient" + ) diff --git a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/about.py b/libraries/botframework-connector/botframework/connector/about.py similarity index 87% rename from samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/about.py rename to libraries/botframework-connector/botframework/connector/about.py index 7fb9c163e..7bda53edb 100644 --- a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/about.py +++ b/libraries/botframework-connector/botframework/connector/about.py @@ -3,9 +3,9 @@ import os -__title__ = "model_runtime_svc_corebot101" +__title__ = "botframework-connector" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "0.0.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botframework-connector/botframework/connector/aio/__init__.py b/libraries/botframework-connector/botframework/connector/aio/__init__.py index 04c1b91a5..e8f4fa483 100644 --- a/libraries/botframework-connector/botframework/connector/aio/__init__.py +++ b/libraries/botframework-connector/botframework/connector/aio/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from ._connector_client_async import ConnectorClient diff --git a/libraries/botframework-connector/botframework/connector/aio/_connector_client_async.py b/libraries/botframework-connector/botframework/connector/aio/_connector_client_async.py index ff6b9b314..e77867cb6 100644 --- a/libraries/botframework-connector/botframework/connector/aio/_connector_client_async.py +++ b/libraries/botframework-connector/botframework/connector/aio/_connector_client_async.py @@ -3,22 +3,27 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- -from msrest.async_client import SDKClientAsync +from typing import Optional, Type + +from msrest.universal_http.async_abc import AsyncHTTPSender as AsyncHttpDriver +from msrest.pipeline.aiohttp import AsyncHTTPSender +from msrest.async_client import AsyncPipeline from msrest import Serializer, Deserializer -from .._configuration import ConnectorClientConfiguration from .operations_async import AttachmentsOperations from .operations_async import ConversationsOperations from .. import models -class ConnectorClient(SDKClientAsync): +from ..bot_framework_sdk_client_async import ( + BotFrameworkSDKClientAsync, + BotFrameworkConnectorConfiguration, +) + + +class ConnectorClient(BotFrameworkSDKClientAsync): """The Bot Connector REST API allows your bot to send and receive messages to channels configured in the [Bot Framework Developer Portal](https://dev.botframework.com). The Connector service uses industry-standard REST and JSON over HTTPS. @@ -49,9 +54,26 @@ class ConnectorClient(SDKClientAsync): :param str base_url: Service URL """ - def __init__(self, credentials, base_url=None): - - self.config = ConnectorClientConfiguration(credentials, base_url) + def __init__( + self, + credentials, + base_url=None, + *, + pipeline_type: Optional[Type[AsyncPipeline]] = None, + sender: Optional[AsyncHTTPSender] = None, + driver: Optional[AsyncHttpDriver] = None, + custom_configuration: Optional[BotFrameworkConnectorConfiguration] = None, + ): + if custom_configuration: + self.config = custom_configuration + else: + self.config = BotFrameworkConnectorConfiguration( + credentials, + base_url, + pipeline_type=pipeline_type, + sender=sender, + driver=driver, + ) super(ConnectorClient, self).__init__(self.config) client_models = { diff --git a/libraries/botframework-connector/botframework/connector/aio/operations_async/__init__.py b/libraries/botframework-connector/botframework/connector/aio/operations_async/__init__.py index 6adc13e41..ca019f8e4 100644 --- a/libraries/botframework-connector/botframework/connector/aio/operations_async/__init__.py +++ b/libraries/botframework-connector/botframework/connector/aio/operations_async/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from ._attachments_operations_async import AttachmentsOperations diff --git a/libraries/botframework-connector/botframework/connector/aio/operations_async/_attachments_operations_async.py b/libraries/botframework-connector/botframework/connector/aio/operations_async/_attachments_operations_async.py index a46fa7da5..7694e1e6a 100644 --- a/libraries/botframework-connector/botframework/connector/aio/operations_async/_attachments_operations_async.py +++ b/libraries/botframework-connector/botframework/connector/aio/operations_async/_attachments_operations_async.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.pipeline import ClientRawResponse @@ -30,7 +26,6 @@ class AttachmentsOperations: models = models def __init__(self, client, config, serializer, deserializer) -> None: - self._client = client self._serialize = serializer self._deserialize = deserializer diff --git a/libraries/botframework-connector/botframework/connector/aio/operations_async/_conversations_operations_async.py b/libraries/botframework-connector/botframework/connector/aio/operations_async/_conversations_operations_async.py index db5e00ae0..553248342 100644 --- a/libraries/botframework-connector/botframework/connector/aio/operations_async/_conversations_operations_async.py +++ b/libraries/botframework-connector/botframework/connector/aio/operations_async/_conversations_operations_async.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.pipeline import ClientRawResponse @@ -31,7 +27,6 @@ class ConversationsOperations: models = models def __init__(self, client, config, serializer, deserializer) -> None: - self._client = client self._serialize = serializer self._deserialize = deserializer @@ -515,6 +510,7 @@ async def reply_to_activity( header_parameters = {} header_parameters["Accept"] = "application/json" header_parameters["Content-Type"] = "application/json; charset=utf-8" + header_parameters["x-ms-conversation-id"] = conversation_id if custom_headers: header_parameters.update(custom_headers) diff --git a/libraries/botframework-connector/botframework/connector/aiohttp_bf_pipeline.py b/libraries/botframework-connector/botframework/connector/aiohttp_bf_pipeline.py new file mode 100644 index 000000000..b46a40857 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/aiohttp_bf_pipeline.py @@ -0,0 +1,34 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from msrest.pipeline import AsyncPipeline, AsyncHTTPPolicy, SansIOHTTPPolicy +from msrest.universal_http.async_requests import AsyncRequestsHTTPSender as Driver +from msrest.pipeline.async_requests import ( + AsyncRequestsCredentialsPolicy, + AsyncPipelineRequestsHTTPSender, +) +from msrest.pipeline.universal import RawDeserializer + +from .bot_framework_sdk_client_async import BotFrameworkConnectorConfiguration + + +class AsyncBfPipeline(AsyncPipeline): + def __init__(self, config: BotFrameworkConnectorConfiguration): + creds = config.credentials + + policies = [ + config.user_agent_policy, # UserAgent policy + RawDeserializer(), # Deserialize the raw bytes + config.http_logger_policy, # HTTP request/response log + ] # type: List[Union[AsyncHTTPPolicy, SansIOHTTPPolicy]] + if creds: + if isinstance(creds, (AsyncHTTPPolicy, SansIOHTTPPolicy)): + policies.insert(1, creds) + else: + # Assume this is the old credentials class, and then requests. Wrap it. + policies.insert(1, AsyncRequestsCredentialsPolicy(creds)) + + sender = config.sender or AsyncPipelineRequestsHTTPSender( + config.driver or Driver(config) + ) + super().__init__(policies, sender) diff --git a/libraries/botframework-connector/botframework/connector/async_mixin/__init__.py b/libraries/botframework-connector/botframework/connector/async_mixin/__init__.py index 796bf96fe..76ba66e7a 100644 --- a/libraries/botframework-connector/botframework/connector/async_mixin/__init__.py +++ b/libraries/botframework-connector/botframework/connector/async_mixin/__init__.py @@ -1 +1,4 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + from .async_mixin import AsyncServiceClientMixin diff --git a/libraries/botframework-connector/botframework/connector/async_mixin/async_mixin.py b/libraries/botframework-connector/botframework/connector/async_mixin/async_mixin.py index 8a2313480..314642542 100644 --- a/libraries/botframework-connector/botframework/connector/async_mixin/async_mixin.py +++ b/libraries/botframework-connector/botframework/connector/async_mixin/async_mixin.py @@ -85,7 +85,6 @@ async def async_send(self, request, headers=None, content=None, **config): response = None try: - try: future = loop.run_in_executor( None, @@ -143,7 +142,7 @@ class _MsrestStopIteration(Exception): def _msrest_next(iterator): - """"To avoid: + """ "To avoid: TypeError: StopIteration interacts badly with generators and cannot be raised into a Future """ try: diff --git a/libraries/botframework-connector/botframework/connector/auth/__init__.py b/libraries/botframework-connector/botframework/connector/auth/__init__.py index e1f08743f..d58dcf5fa 100644 --- a/libraries/botframework-connector/botframework/connector/auth/__init__.py +++ b/libraries/botframework-connector/botframework/connector/auth/__init__.py @@ -3,24 +3,31 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- # pylint: disable=missing-docstring from .authentication_constants import * +from .authenticate_request_result import * +from .bot_framework_authentication import * +from .bot_framework_authentication_factory import * from .government_constants import * from .channel_provider import * +from .connector_factory import * from .simple_channel_provider import * from .app_credentials import * from .microsoft_app_credentials import * from .microsoft_government_app_credentials import * from .certificate_app_credentials import * +from .certificate_government_app_credentials import * +from .certificate_service_client_credential_factory import * from .claims_identity import * from .jwt_token_validation import * from .credential_provider import * from .channel_validation import * from .emulator_validation import * from .jwt_token_extractor import * +from .password_service_client_credential_factory import * +from .service_client_credentials_factory import * +from .user_token_client import * from .authentication_configuration import * +from .managedidentity_app_credentials import * +from .managedidentity_service_client_credential_factory import * diff --git a/libraries/botframework-connector/botframework/connector/auth/_bot_framework_client_impl.py b/libraries/botframework-connector/botframework/connector/auth/_bot_framework_client_impl.py new file mode 100644 index 000000000..df4313c0e --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/_bot_framework_client_impl.py @@ -0,0 +1,125 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from copy import deepcopy +from json import dumps, loads +from logging import Logger + +from botbuilder.schema import ( + Activity, + ConversationReference, + ConversationAccount, + ChannelAccount, + InvokeResponse, + RoleTypes, +) + +from ..http_client_factory import HttpClientFactory +from ..http_request import HttpRequest +from .._not_implemented_http_client import _NotImplementedHttpClient +from ..skills.bot_framework_client import BotFrameworkClient + +from .service_client_credentials_factory import ServiceClientCredentialsFactory + + +class _BotFrameworkClientImpl(BotFrameworkClient): + def __init__( + self, + credentials_factory: ServiceClientCredentialsFactory, + http_client_factory: HttpClientFactory, + login_endpoint: str, + logger: Logger = None, + ): + self._credentials_factory = credentials_factory + self._http_client = ( + http_client_factory.create_client() + if http_client_factory + else _NotImplementedHttpClient() + ) + self._login_endpoint = login_endpoint + self._logger = logger + + async def post_activity( + self, + from_bot_id: str, + to_bot_id: str, + to_url: str, + service_url: str, + conversation_id: str, + activity: Activity, + ) -> InvokeResponse: + if not to_url: + raise TypeError("to_url") + if not service_url: + raise TypeError("service_url") + if not conversation_id: + raise TypeError("conversation_id") + if not activity: + raise TypeError("activity") + + if self._logger: + self._logger.log(20, f"post to skill '{to_bot_id}' at '{to_url}'") + + credentials = await self._credentials_factory.create_credentials( + from_bot_id, to_bot_id, self._login_endpoint, True + ) + + # Get token for the skill call + token = credentials.get_access_token() if credentials.microsoft_app_id else None + + # Clone the activity so we can modify it before sending without impacting the original object. + activity_copy = deepcopy(activity) + + # Apply the appropriate addressing to the newly created Activity. + activity_copy.relates_to = ConversationReference( + service_url=activity_copy.service_url, + activity_id=activity_copy.id, + channel_id=activity_copy.channel_id, + conversation=ConversationAccount( + id=activity_copy.conversation.id, + name=activity_copy.conversation.name, + conversation_type=activity_copy.conversation.conversation_type, + aad_object_id=activity_copy.conversation.aad_object_id, + is_group=activity_copy.conversation.is_group, + role=activity_copy.conversation.role, + tenant_id=activity_copy.conversation.tenant_id, + properties=activity_copy.conversation.properties, + ), + bot=None, + ) + activity_copy.conversation.id = conversation_id + activity_copy.service_url = service_url + if not activity_copy.recipient: + activity_copy.recipient = ChannelAccount(role=RoleTypes.skill) + else: + activity_copy.recipient.role = RoleTypes.skill + + headers_dict = { + "Content-type": "application/json; charset=utf-8", + "x-ms-conversation-id": conversation_id, + } + if token: + headers_dict.update( + { + "Authorization": f"Bearer {token}", + } + ) + json_content = dumps(activity_copy.serialize()).encode("utf-8") + + request = HttpRequest( + request_uri=to_url, content=json_content, headers=headers_dict + ) + response = await self._http_client.post(request=request) + + data = await response.read_content_str() + + if not await response.is_succesful() and self._logger: + # Otherwise we can assume we don't have to deserialize - so just log the content so it's not lost. + self._logger.log( + 40, + f"Bot Framework call failed to '{to_url}' returning '{int(response.status_code)}' and '{data}'", + ) + + return InvokeResponse( + status=response.status_code, body=loads(data) if data else None + ) diff --git a/libraries/botframework-connector/botframework/connector/auth/_built_in_bot_framework_authentication.py b/libraries/botframework-connector/botframework/connector/auth/_built_in_bot_framework_authentication.py new file mode 100644 index 000000000..8be3b200f --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/_built_in_bot_framework_authentication.py @@ -0,0 +1,215 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger +from typing import Optional + +from botbuilder.schema import Activity + +from ..bot_framework_sdk_client_async import BotFrameworkConnectorConfiguration +from ..http_client_factory import HttpClientFactory +from ..skills.bot_framework_client import BotFrameworkClient + +from ._bot_framework_client_impl import _BotFrameworkClientImpl +from ._user_token_client_impl import _UserTokenClientImpl +from ._connector_factory_impl import _ConnectorFactoryImpl +from .authenticate_request_result import AuthenticateRequestResult +from .authentication_configuration import AuthenticationConfiguration +from .authentication_constants import AuthenticationConstants +from .bot_framework_authentication import BotFrameworkAuthentication +from .claims_identity import ClaimsIdentity +from .channel_provider import ChannelProvider +from .connector_factory import ConnectorFactory +from .credential_provider import _DelegatingCredentialProvider +from .jwt_token_validation import JwtTokenValidation +from .service_client_credentials_factory import ServiceClientCredentialsFactory +from .skill_validation import SkillValidation +from .simple_channel_provider import SimpleChannelProvider +from .user_token_client import UserTokenClient + + +class _BuiltinBotFrameworkAuthentication(BotFrameworkAuthentication): + def __init__( + self, + to_channel_from_bot_oauth_scope: str, + login_endpoint: str, + caller_id: str, + channel_service: str, + oauth_endpoint: str, + credentials_factory: ServiceClientCredentialsFactory, + auth_configuration: AuthenticationConfiguration, + http_client_factory: HttpClientFactory, + connector_client_configuration: BotFrameworkConnectorConfiguration, + logger: Logger, + ): + self._to_channel_from_bot_oauth_scope = to_channel_from_bot_oauth_scope + self._login_endpoint = login_endpoint + self._caller_id = caller_id + self._channel_service = channel_service + self._oauth_endpoint = oauth_endpoint + self._credentials_factory = credentials_factory + self._auth_configuration = auth_configuration + self._http_client_factory = http_client_factory + self._connector_client_configuration = connector_client_configuration + self._logger = logger + + @staticmethod + def get_app_id(claims_identity: ClaimsIdentity) -> str: + # For requests from channel App Id is in Audience claim of JWT token. For emulator it is in AppId claim. For + # unauthenticated requests we have anonymous claimsIdentity provided auth is disabled. + # For Activities coming from Emulator AppId claim contains the Bot's AAD AppId. + app_id = claims_identity.get_claim_value(AuthenticationConstants.AUDIENCE_CLAIM) + if app_id is None: + app_id = claims_identity.get_claim_value( + AuthenticationConstants.APP_ID_CLAIM + ) + return app_id + + async def authenticate_request( + self, activity: Activity, auth_header: str + ) -> AuthenticateRequestResult: + credential_provider = _DelegatingCredentialProvider(self._credentials_factory) + + claims_identity = await JwtTokenValidation.authenticate_request( + activity, + auth_header, + credential_provider, + self._get_channel_provider(), + self._auth_configuration, + ) + + outbound_audience = ( + JwtTokenValidation.get_app_id_from_claims(claims_identity.claims) + if SkillValidation.is_skill_claim(claims_identity.claims) + else self._to_channel_from_bot_oauth_scope + ) + + caller_id = await self.generate_caller_id( + credential_factory=self._credentials_factory, + claims_identity=claims_identity, + caller_id=self._caller_id, + ) + + connector_factory = _ConnectorFactoryImpl( + app_id=_BuiltinBotFrameworkAuthentication.get_app_id(claims_identity), + to_channel_from_bot_oauth_scope=self._to_channel_from_bot_oauth_scope, + login_endpoint=self._login_endpoint, + validate_authority=True, + credential_factory=self._credentials_factory, + connector_client_configuration=self._connector_client_configuration, + logger=self._logger, + ) + + result = AuthenticateRequestResult() + result.claims_identity = claims_identity + result.audience = outbound_audience + result.caller_id = caller_id + result.connector_factory = connector_factory + + return result + + async def authenticate_streaming_request( + self, auth_header: str, channel_id_header: str + ) -> AuthenticateRequestResult: + credential_provider = _DelegatingCredentialProvider(self._credentials_factory) + + if channel_id_header is None: + is_auth_disabled = ( + await self._credentials_factory.is_authentication_disabled() + ) + if not is_auth_disabled: + raise PermissionError("Unauthorized Access. Request is not authorized") + + claims_identity = await JwtTokenValidation.validate_auth_header( + auth_header, + credential_provider, + self._get_channel_provider(), + channel_id_header, + ) + + outbound_audience = ( + JwtTokenValidation.get_app_id_from_claims(claims_identity.claims) + if SkillValidation.is_skill_claim(claims_identity.claims) + else self._to_channel_from_bot_oauth_scope + ) + + caller_id = await self.generate_caller_id( + credential_factory=self._credentials_factory, + claims_identity=claims_identity, + caller_id=self._caller_id, + ) + + result = AuthenticateRequestResult() + result.claims_identity = claims_identity + result.audience = outbound_audience + result.caller_id = caller_id + + return result + + def create_connector_factory( + self, claims_identity: ClaimsIdentity + ) -> ConnectorFactory: + return _ConnectorFactoryImpl( + app_id=_BuiltinBotFrameworkAuthentication.get_app_id(claims_identity), + to_channel_from_bot_oauth_scope=self._to_channel_from_bot_oauth_scope, + login_endpoint=self._login_endpoint, + validate_authority=True, + credential_factory=self._credentials_factory, + connector_client_configuration=self._connector_client_configuration, + logger=self._logger, + ) + + async def create_user_token_client( + self, claims_identity: ClaimsIdentity + ) -> UserTokenClient: + app_id = _BuiltinBotFrameworkAuthentication.get_app_id(claims_identity) + + credentials = await self._credentials_factory.create_credentials( + app_id, + oauth_scope=self._to_channel_from_bot_oauth_scope, + login_endpoint=self._login_endpoint, + validate_authority=True, + ) + + return _UserTokenClientImpl(app_id, credentials, self._oauth_endpoint) + + def create_bot_framework_client(self) -> BotFrameworkClient: + return _BotFrameworkClientImpl( + self._credentials_factory, + self._http_client_factory, + self._login_endpoint, + self._logger, + ) + + def get_originating_audience(self) -> str: + return self._to_channel_from_bot_oauth_scope + + async def authenticate_channel_request(self, auth_header: str) -> ClaimsIdentity: + credential_provider = _DelegatingCredentialProvider(self._credentials_factory) + + if auth_header is None: + is_auth_disabled = await credential_provider.is_authentication_disabled() + if not is_auth_disabled: + # No auth header. Auth is required. Request is not authorized. + raise PermissionError("Unauthorized Access. Request is not authorized") + + # In the scenario where auth is disabled, we still want to have the + # IsAuthenticated flag set in the ClaimsIdentity. + # To do this requires adding in an empty claim. + # Since ChannelServiceHandler calls are always a skill callback call, we set the skill claim too. + return SkillValidation.create_anonymous_skill_claim() + + return await JwtTokenValidation.validate_auth_header( + auth_header, + credential_provider, + channel_service_or_provider=self._get_channel_provider(), + channel_id="unknown", + auth_configuration=self._auth_configuration, + ) + + def _get_channel_provider(self) -> Optional[ChannelProvider]: + return ( + SimpleChannelProvider(self._channel_service) + if self._channel_service is not None + else None + ) diff --git a/libraries/botframework-connector/botframework/connector/auth/_connector_factory_impl.py b/libraries/botframework-connector/botframework/connector/auth/_connector_factory_impl.py new file mode 100644 index 000000000..9c7d83af7 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/_connector_factory_impl.py @@ -0,0 +1,55 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger + +from botframework.connector.aio import ConnectorClient + +from ..about import __version__ +from ..bot_framework_sdk_client_async import BotFrameworkConnectorConfiguration +from .connector_factory import ConnectorFactory +from .service_client_credentials_factory import ServiceClientCredentialsFactory + +USER_AGENT = f"Microsoft-BotFramework/3.1 (BotBuilder Python/{__version__})" + + +class _ConnectorFactoryImpl(ConnectorFactory): + def __init__( + self, + app_id: str, + to_channel_from_bot_oauth_scope: str, + login_endpoint: str, + validate_authority: bool, + credential_factory: ServiceClientCredentialsFactory, + connector_client_configuration: BotFrameworkConnectorConfiguration = None, + logger: Logger = None, + ) -> None: + self._app_id = app_id + self._to_channel_from_bot_oauth_scope = to_channel_from_bot_oauth_scope + self._login_endpoint = login_endpoint + self._validate_authority = validate_authority + self._credential_factory = credential_factory + self._connector_client_configuration = connector_client_configuration + self._logger = logger + + async def create(self, service_url: str, audience: str = None) -> ConnectorClient: + # Use the credentials factory to create credentails specific to this particular cloud environment. + credentials = await self._credential_factory.create_credentials( + self._app_id, + audience or self._to_channel_from_bot_oauth_scope, + self._login_endpoint, + self._validate_authority, + ) + + # A new connector client for making calls against this serviceUrl using credentials derived + # from the current appId and the specified audience. + if self._connector_client_configuration: + client = ConnectorClient( + credentials, + base_url=service_url, + custom_configuration=self._connector_client_configuration, + ) + else: + client = ConnectorClient(credentials, base_url=service_url) + client.config.add_user_agent(USER_AGENT) + return client diff --git a/libraries/botframework-connector/botframework/connector/auth/_government_cloud_bot_framework_authentication.py b/libraries/botframework-connector/botframework/connector/auth/_government_cloud_bot_framework_authentication.py new file mode 100644 index 000000000..8cde743e5 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/_government_cloud_bot_framework_authentication.py @@ -0,0 +1,36 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger + +from botbuilder.schema import CallerIdConstants + +from ..bot_framework_sdk_client_async import BotFrameworkConnectorConfiguration +from ..http_client_factory import HttpClientFactory +from ._built_in_bot_framework_authentication import _BuiltinBotFrameworkAuthentication +from .authentication_configuration import AuthenticationConfiguration +from .government_constants import GovernmentConstants +from .service_client_credentials_factory import ServiceClientCredentialsFactory + + +class _GovernmentCloudBotFrameworkAuthentication(_BuiltinBotFrameworkAuthentication): + def __init__( + self, + credentials_factory: ServiceClientCredentialsFactory, + auth_configuration: AuthenticationConfiguration, + http_client_factory: HttpClientFactory, + connector_client_configuration: BotFrameworkConnectorConfiguration = None, + logger: Logger = None, + ): + super(_GovernmentCloudBotFrameworkAuthentication, self).__init__( + GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX, + CallerIdConstants.us_gov_channel, + GovernmentConstants.CHANNEL_SERVICE, + GovernmentConstants.OAUTH_URL_GOV, + credentials_factory, + auth_configuration, + http_client_factory, + connector_client_configuration, + logger, + ) diff --git a/libraries/botframework-connector/botframework/connector/auth/_parameterized_bot_framework_authentication.py b/libraries/botframework-connector/botframework/connector/auth/_parameterized_bot_framework_authentication.py new file mode 100644 index 000000000..3419c2099 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/_parameterized_bot_framework_authentication.py @@ -0,0 +1,514 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger +from typing import Dict, Optional + +from botbuilder.schema import Activity, RoleTypes + +from ..bot_framework_sdk_client_async import BotFrameworkConnectorConfiguration +from ..http_client_factory import HttpClientFactory +from ..channels import Channels +from ..skills.bot_framework_client import BotFrameworkClient + +from .bot_framework_authentication import BotFrameworkAuthentication +from .claims_identity import ClaimsIdentity +from .user_token_client import UserTokenClient +from .connector_factory import ConnectorFactory +from .authenticate_request_result import AuthenticateRequestResult +from .service_client_credentials_factory import ServiceClientCredentialsFactory +from .authentication_configuration import AuthenticationConfiguration +from .verify_options import VerifyOptions +from .jwt_token_validation import JwtTokenValidation +from .skill_validation import SkillValidation +from .authentication_constants import AuthenticationConstants +from .emulator_validation import EmulatorValidation +from .jwt_token_extractor import JwtTokenExtractor +from ._bot_framework_client_impl import _BotFrameworkClientImpl +from ._built_in_bot_framework_authentication import _BuiltinBotFrameworkAuthentication +from ._user_token_client_impl import _UserTokenClientImpl +from ._connector_factory_impl import _ConnectorFactoryImpl + + +class _ParameterizedBotFrameworkAuthentication(BotFrameworkAuthentication): + def __init__( + self, + validate_authority: bool, + to_channel_from_bot_login_url: str, + to_channel_from_bot_oauth_scope: str, + to_bot_from_channel_token_issuer: str, + oauth_url: str, + to_bot_from_channel_open_id_metadata_url: str, + to_bot_from_emulator_open_id_metadata_url: str, + caller_id: str, + credentials_factory: ServiceClientCredentialsFactory, + auth_configuration: AuthenticationConfiguration, + http_client_factory: HttpClientFactory, + connector_client_configuration: BotFrameworkConnectorConfiguration = None, + logger: Logger = None, + ): + self._validate_authority = validate_authority + self._to_channel_from_bot_login_url = to_channel_from_bot_login_url + self._to_channel_from_bot_oauth_scope = to_channel_from_bot_oauth_scope + self._to_bot_from_channel_token_issuer = to_bot_from_channel_token_issuer + self._oauth_url = oauth_url + self._to_bot_from_channel_open_id_metadata_url = ( + to_bot_from_channel_open_id_metadata_url + ) + self._to_bot_from_emulator_open_id_metadata_url = ( + to_bot_from_emulator_open_id_metadata_url + ) + self._caller_id = caller_id + self._credentials_factory = credentials_factory + self._auth_configuration = auth_configuration + self._http_client_factory = http_client_factory + self._connector_client_configuration = connector_client_configuration + self._logger = logger + + async def authenticate_request( + self, activity: Activity, auth_header: str + ) -> AuthenticateRequestResult: + claims_identity = await self._jwt_token_validation_authenticate_request( + activity, auth_header + ) + + outbound_audience = ( + JwtTokenValidation.get_app_id_from_claims(claims_identity.claims) + if SkillValidation.is_skill_claim(claims_identity.claims) + else self._to_channel_from_bot_oauth_scope + ) + + caller_id = await self.generate_caller_id( + credential_factory=self._credentials_factory, + claims_identity=claims_identity, + caller_id=self._caller_id, + ) + + connector_factory = _ConnectorFactoryImpl( + app_id=_BuiltinBotFrameworkAuthentication.get_app_id(claims_identity), + to_channel_from_bot_oauth_scope=self._to_channel_from_bot_oauth_scope, + login_endpoint=self._to_channel_from_bot_login_url, + validate_authority=self._validate_authority, + credential_factory=self._credentials_factory, + connector_client_configuration=self._connector_client_configuration, + logger=self._logger, + ) + + result = AuthenticateRequestResult() + result.claims_identity = claims_identity + result.audience = outbound_audience + result.caller_id = caller_id + result.connector_factory = connector_factory + + return result + + async def authenticate_streaming_request( + self, auth_header: str, channel_id_header: str + ) -> AuthenticateRequestResult: + if channel_id_header is None: + is_auth_disabled = ( + await self._credentials_factory.is_authentication_disabled() + ) + if not is_auth_disabled: + raise PermissionError("Unauthorized Access. Request is not authorized") + + claims_identity = await self._jwt_token_validation_validate_auth_header( + auth_header, channel_id_header + ) + + outbound_audience = ( + JwtTokenValidation.get_app_id_from_claims(claims_identity.claims) + if SkillValidation.is_skill_claim(claims_identity.claims) + else self._to_channel_from_bot_oauth_scope + ) + + caller_id = await self.generate_caller_id( + credential_factory=self._credentials_factory, + claims_identity=claims_identity, + caller_id=self._caller_id, + ) + + result = AuthenticateRequestResult() + result.claims_identity = claims_identity + result.audience = outbound_audience + result.caller_id = caller_id + + return result + + def create_connector_factory( + self, claims_identity: ClaimsIdentity + ) -> ConnectorFactory: + return _ConnectorFactoryImpl( + app_id=_BuiltinBotFrameworkAuthentication.get_app_id(claims_identity), + to_channel_from_bot_oauth_scope=self._to_channel_from_bot_oauth_scope, + login_endpoint=self._to_channel_from_bot_login_url, + validate_authority=self._validate_authority, + credential_factory=self._credentials_factory, + connector_client_configuration=self._connector_client_configuration, + logger=self._logger, + ) + + async def create_user_token_client( + self, claims_identity: ClaimsIdentity + ) -> UserTokenClient: + app_id = _BuiltinBotFrameworkAuthentication.get_app_id(claims_identity) + + credentials = await self._credentials_factory.create_credentials( + app_id, + oauth_scope=self._to_channel_from_bot_oauth_scope, + login_endpoint=self._to_channel_from_bot_login_url, + validate_authority=self._validate_authority, + ) + + return _UserTokenClientImpl(app_id, credentials, self._oauth_url) + + def create_bot_framework_client(self) -> BotFrameworkClient: + return _BotFrameworkClientImpl( + self._credentials_factory, + self._http_client_factory, + self._to_channel_from_bot_login_url, + self._logger, + ) + + def get_originating_audience(self) -> str: + return self._to_channel_from_bot_oauth_scope + + async def authenticate_channel_request(self, auth_header: str) -> ClaimsIdentity: + return await self._jwt_token_validation_validate_auth_header( + auth_header, channel_id="unknown" + ) + + async def _jwt_token_validation_authenticate_request( + self, activity: Activity, auth_header: str + ) -> ClaimsIdentity: + if auth_header is None: + is_auth_disabled = ( + await self._credentials_factory.is_authentication_disabled() + ) + if not is_auth_disabled: + # No Auth Header. Auth is required. Request is not authorized. + raise PermissionError("Unauthorized Access. Request is not authorized") + + # Check if the activity is for a skill call and is coming from the Emulator. + if ( + activity.channel_id == Channels.emulator + and activity.recipient.role == RoleTypes.skill + ): + # Return an anonymous claim with an anonymous skill AppId + return SkillValidation.create_anonymous_skill_claim() + + # In the scenario where Auth is disabled, we still want to have the + # IsAuthenticated flag set in the ClaimsIdentity. To do this requires + # adding in an empty claim. + return ClaimsIdentity({}, True, AuthenticationConstants.ANONYMOUS_AUTH_TYPE) + + # Validate the header and extract claims. + claims_identity = await self._jwt_token_validation_validate_auth_header( + auth_header, activity.channel_id, activity.service_url + ) + + return claims_identity + + async def _jwt_token_validation_validate_auth_header( + self, auth_header: str, channel_id: str, service_url: Optional[str] = None + ) -> ClaimsIdentity: + identity = await self._jwt_token_validation_authenticate_token( + auth_header, channel_id, service_url + ) + + await self._jwt_token_validation_validate_claims(identity.claims) + + return identity + + async def _jwt_token_validation_validate_claims(self, claims: Dict[str, object]): + if self._auth_configuration.claims_validator: + # Call the validation method if defined (it should throw an exception if the validation fails) + await self._auth_configuration.claims_validator([claims]) + elif SkillValidation.is_skill_claim(claims): + raise PermissionError( + "ClaimsValidator is required for validation of Skill Host calls." + ) + + async def _jwt_token_validation_authenticate_token( + self, auth_header: str, channel_id: str, service_url: str + ) -> ClaimsIdentity: + if SkillValidation.is_skill_token(auth_header): + return await self._skill_validation_authenticate_channel_token( + auth_header, channel_id + ) + + if EmulatorValidation.is_token_from_emulator(auth_header): + return await self._emulator_validation_authenticate_emulator_token( + auth_header, channel_id + ) + + return await self._government_channel_validation_authenticate_channel_token( + auth_header, service_url, channel_id + ) + + # // The following code is based on SkillValidation.authenticate_channel_token + async def _skill_validation_authenticate_channel_token( + self, auth_header: str, channel_id: str + ) -> Optional[ClaimsIdentity]: + if not auth_header: + return None + + validation_params = VerifyOptions( + issuer=[ + # TODO: presumably this table should also come from configuration + # Auth v3.1, 1.0 token + "https://sts.windows.net/d6d49420-f39b-4df7-a1dc-d59a935871db/", + # Auth v3.1, 2.0 token + "https://login.microsoftonline.com/d6d49420-f39b-4df7-a1dc-d59a935871db/v2.0", + # Auth v3.2, 1.0 token + "https://sts.windows.net/f8cdef31-a31e-4b4a-93e4-5f571e91255a/", + # Auth v3.2, 2.0 token + "https://login.microsoftonline.com/f8cdef31-a31e-4b4a-93e4-5f571e91255a/v2.0", + # Auth for US Gov, 1.0 token + "https://sts.windows.net/cab8a31a-1906-4287-a0d8-4eef66b95f6e/", + # Auth for US Gov, 2.0 token + "https://login.microsoftonline.us/cab8a31a-1906-4287-a0d8-4eef66b95f6e/v2.0", + ], + audience=None, # Audience validation takes place manually in code. + clock_tolerance=5 * 60, + ignore_expiration=False, + ) + + if self._auth_configuration.valid_token_issuers: + validation_params.issuer.append( + self._auth_configuration.valid_token_issuers + ) + + # TODO: what should the openIdMetadataUrl be here? + token_extractor = JwtTokenExtractor( + validation_params, + metadata_url=self._to_bot_from_emulator_open_id_metadata_url, + allowed_algorithms=AuthenticationConstants.ALLOWED_SIGNING_ALGORITHMS, + ) + + parts = auth_header.split(" ") + if len(parts) != 2: + return None + + identity = await token_extractor.get_identity( + schema=parts[0], + parameter=parts[1], + channel_id=channel_id, + required_endorsements=self._auth_configuration.required_endorsements, + ) + + await self._skill_validation_validate_identity(identity) + + return identity + + async def _skill_validation_validate_identity(self, identity: ClaimsIdentity): + if identity is None: + # No valid identity. Not Authorized. + raise PermissionError("Invalid Identity") + + if not identity.is_authenticated: + # The token is in some way invalid. Not Authorized. + raise PermissionError("Token Not Authenticated") + + version_claim = identity.get_claim_value(AuthenticationConstants.VERSION_CLAIM) + if not version_claim: + # No version claim + raise PermissionError( + f"'{AuthenticationConstants.VERSION_CLAIM}' claim is required on skill Tokens." + ) + + # Look for the "aud" claim, but only if issued from the Bot Framework + audience_claim = identity.get_claim_value( + AuthenticationConstants.AUDIENCE_CLAIM + ) + if not audience_claim: + # Claim is not present or doesn't have a value. Not Authorized. + raise PermissionError( + f"'{AuthenticationConstants.AUDIENCE_CLAIM}' claim is required on skill Tokens." + ) + + is_valid_app_id = await self._credentials_factory.is_valid_app_id( + audience_claim + ) + if not is_valid_app_id: + # The AppId is not valid. Not Authorized. + raise PermissionError("Invalid audience.") + + app_id = JwtTokenValidation.get_app_id_from_claims(identity.claims) + if not app_id: + # Invalid appId + raise PermissionError("Invalid appId.") + + # The following code is based on EmulatorValidation.authenticate_emulator_token + async def _emulator_validation_authenticate_emulator_token( + self, auth_header: str, channel_id: str + ) -> Optional[ClaimsIdentity]: + if not auth_header: + return None + + to_bot_from_emulator_validation_params = VerifyOptions( + issuer=[ + # TODO: presumably this table should also come from configuration + # Auth v3.1, 1.0 token + "https://sts.windows.net/d6d49420-f39b-4df7-a1dc-d59a935871db/", + # Auth v3.1, 2.0 token + "https://login.microsoftonline.com/d6d49420-f39b-4df7-a1dc-d59a935871db/v2.0", + # Auth v3.2, 1.0 token + "https://sts.windows.net/f8cdef31-a31e-4b4a-93e4-5f571e91255a/", + # Auth v3.2, 2.0 token + "https://login.microsoftonline.com/f8cdef31-a31e-4b4a-93e4-5f571e91255a/v2.0", + # Auth for US Gov, 1.0 token + "https://sts.windows.net/cab8a31a-1906-4287-a0d8-4eef66b95f6e/", + # Auth for US Gov, 2.0 token + "https://login.microsoftonline.us/cab8a31a-1906-4287-a0d8-4eef66b95f6e/v2.0", + ], + audience=None, # Audience validation takes place manually in code. + clock_tolerance=5 * 60, + ignore_expiration=False, + ) + + if self._auth_configuration.valid_token_issuers: + to_bot_from_emulator_validation_params.issuer.append( + self._auth_configuration.valid_token_issuers + ) + + token_extractor = JwtTokenExtractor( + to_bot_from_emulator_validation_params, + metadata_url=self._to_bot_from_emulator_open_id_metadata_url, + allowed_algorithms=AuthenticationConstants.ALLOWED_SIGNING_ALGORITHMS, + ) + + parts = auth_header.split(" ") + if len(parts) != 2: + return None + + identity = await token_extractor.get_identity( + schema=parts[0], + parameter=parts[1], + channel_id=channel_id, + required_endorsements=self._auth_configuration.required_endorsements, + ) + + if identity is None: + # No valid identity. Not Authorized. + raise PermissionError("Invalid Identity") + + if not identity.is_authenticated: + # The token is in some way invalid. Not Authorized. + raise PermissionError("Token Not Authenticated") + + # Now check that the AppID in the claim set matches + # what we're looking for. Note that in a multi-tenant bot, this value + # comes from developer code that may be reaching out to a service, hence the + # Async validation. + version_claim = identity.get_claim_value(AuthenticationConstants.VERSION_CLAIM) + if version_claim is None: + raise PermissionError("'ver' claim is required on Emulator Tokens.") + + # The Emulator, depending on Version, sends the AppId via either the + # appid claim (Version 1) or the Authorized Party claim (Version 2). + if not version_claim or version_claim == "1.0": + # either no Version or a version of "1.0" means we should look for + # the claim in the "appid" claim. + app_id = identity.get_claim_value(AuthenticationConstants.APP_ID_CLAIM) + if not app_id: + # No claim around AppID. Not Authorized. + raise PermissionError( + "'appid' claim is required on Emulator Token version '1.0'." + ) + elif version_claim == "2.0": + app_id = identity.get_claim_value(AuthenticationConstants.AUTHORIZED_PARTY) + if not app_id: + raise PermissionError( + "'azp' claim is required on Emulator Token version '2.0'." + ) + else: + # Unknown Version. Not Authorized. + raise PermissionError(f"Unknown Emulator Token version '{version_claim}'.") + + is_valid_app_id = await self._credentials_factory.is_valid_app_id(app_id) + if not is_valid_app_id: + raise PermissionError(f"Invalid AppId passed on token: {app_id}") + + return identity + + async def _government_channel_validation_authenticate_channel_token( + self, auth_header: str, service_url: str, channel_id: str + ) -> Optional[ClaimsIdentity]: + if not auth_header: + return None + + validation_params = VerifyOptions( + issuer=[self._to_bot_from_channel_token_issuer], + audience=None, # Audience validation takes place in JwtTokenExtractor + clock_tolerance=5 * 60, + ignore_expiration=False, + ) + + token_extractor = JwtTokenExtractor( + validation_params, + metadata_url=self._to_bot_from_channel_open_id_metadata_url, + allowed_algorithms=AuthenticationConstants.ALLOWED_SIGNING_ALGORITHMS, + ) + + parts = auth_header.split(" ") + if len(parts) != 2: + return None + + identity = await token_extractor.get_identity( + schema=parts[0], + parameter=parts[1], + channel_id=channel_id, + required_endorsements=self._auth_configuration.required_endorsements, + ) + + await self._government_channel_validation_validate_identity( + identity, service_url + ) + + return identity + + async def _government_channel_validation_validate_identity( + self, identity: ClaimsIdentity, service_url: str + ): + if identity is None: + # No valid identity. Not Authorized. + raise PermissionError("Identity missing") + + if not identity.is_authenticated: + # The token is in some way invalid. Not Authorized. + raise PermissionError("Invalid token") + + # Now check that the AppID in the claim set matches + # what we're looking for. Note that in a multi-tenant bot, this value + # comes from developer code that may be reaching out to a service, hence the + # Async validation. + + # Look for the "aud" claim, but only if issued from the Bot Framework + issuer = identity.get_claim_value(AuthenticationConstants.ISSUER_CLAIM) + if issuer != self._to_bot_from_channel_token_issuer: + raise PermissionError("'iss' claim missing") + + app_id = identity.get_claim_value(AuthenticationConstants.AUDIENCE_CLAIM) + if not app_id: + # The relevant audience Claim MUST be present. Not Authorized. + raise PermissionError("'aud' claim missing") + + # The AppId from the claim in the token must match the AppId specified by the developer. + # In this case, the token is destined for the app, so we find the app ID in the audience claim. + is_valid_app_id = await self._credentials_factory.is_valid_app_id(app_id) + if not is_valid_app_id: + # The AppId is not valid. Not Authorized. + raise PermissionError(f"Invalid AppId passed on token: {app_id}") + + if service_url is not None: + service_url_claim = identity.get_claim_value( + AuthenticationConstants.SERVICE_URL_CLAIM + ) + if not service_url_claim: + # Claim must be present. Not Authorized. + raise PermissionError("'serviceurl' claim missing") + + if service_url_claim != service_url: + # Claim must match. Not Authorized. + raise PermissionError("Invalid 'serviceurl' claim") diff --git a/libraries/botframework-connector/botframework/connector/auth/_public_cloud_bot_framework_authentication.py b/libraries/botframework-connector/botframework/connector/auth/_public_cloud_bot_framework_authentication.py new file mode 100644 index 000000000..1e34a0ab8 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/_public_cloud_bot_framework_authentication.py @@ -0,0 +1,37 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger + +from botbuilder.schema import CallerIdConstants + +from ..bot_framework_sdk_client_async import BotFrameworkConnectorConfiguration +from ..http_client_factory import HttpClientFactory + +from .service_client_credentials_factory import ServiceClientCredentialsFactory +from .authentication_configuration import AuthenticationConfiguration +from .authentication_constants import AuthenticationConstants +from ._built_in_bot_framework_authentication import _BuiltinBotFrameworkAuthentication + + +class _PublicCloudBotFrameworkAuthentication(_BuiltinBotFrameworkAuthentication): + def __init__( + self, + credentials_factory: ServiceClientCredentialsFactory, + auth_configuration: AuthenticationConfiguration, + http_client_factory: HttpClientFactory, + connector_client_configuration: BotFrameworkConnectorConfiguration = None, + logger: Logger = None, + ): + super(_PublicCloudBotFrameworkAuthentication, self).__init__( + AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + AuthenticationConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX, + CallerIdConstants.public_azure_channel, + "", # channel_service + AuthenticationConstants.OAUTH_URL, + credentials_factory, + auth_configuration, + http_client_factory, + connector_client_configuration, + logger, + ) diff --git a/libraries/botframework-connector/botframework/connector/auth/_user_token_client_impl.py b/libraries/botframework-connector/botframework/connector/auth/_user_token_client_impl.py new file mode 100644 index 000000000..10603542b --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/_user_token_client_impl.py @@ -0,0 +1,138 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Dict, List + +from botbuilder.schema import Activity, TokenResponse + +from botframework.connector.token_api import TokenApiClientConfiguration +from botframework.connector.token_api.aio import TokenApiClient +from botframework.connector.token_api.models import ( + SignInUrlResponse, + TokenExchangeRequest, + TokenStatus, +) + +from .app_credentials import AppCredentials +from .user_token_client import UserTokenClient + + +class _UserTokenClientImpl(UserTokenClient): + def __init__( + self, + app_id: str, + credentials: AppCredentials, + oauth_endpoint: str, + client_configuration: TokenApiClientConfiguration = None, + ) -> None: + super().__init__() + self._app_id = app_id + self._client = TokenApiClient(credentials, oauth_endpoint) + if client_configuration: + self._client.config = client_configuration + + async def get_user_token( + self, user_id: str, connection_name: str, channel_id: str, magic_code: str + ) -> TokenResponse: + if user_id is None or not isinstance(user_id, str): + raise TypeError("user_id") + if connection_name is None or not isinstance(connection_name, str): + raise TypeError("connection_name") + if channel_id is None or not isinstance(channel_id, str): + raise TypeError("channel_id") + + result = await self._client.user_token.get_token( + user_id, connection_name, channel_id=channel_id, code=magic_code + ) + + if result is None or result.token is None: + return None + + return result + + async def get_sign_in_resource( + self, connection_name: str, activity: Activity, final_redirect: str + ) -> SignInUrlResponse: + if connection_name is None or not isinstance(connection_name, str): + raise TypeError("connection_name") + if activity is None or not isinstance(activity, Activity): + raise TypeError("activity") + + result = await self._client.bot_sign_in.get_sign_in_resource( + UserTokenClient.create_token_exchange_state( + self._app_id, connection_name, activity + ), + final_redirect=final_redirect, + ) + + return result + + async def sign_out_user(self, user_id: str, connection_name: str, channel_id: str): + if user_id is None or not isinstance(user_id, str): + raise TypeError("user_id") + if connection_name is None or not isinstance(connection_name, str): + raise TypeError("connection_name") + if channel_id is None or not isinstance(channel_id, str): + raise TypeError("channel_id") + + await self._client.user_token.sign_out(user_id, connection_name, channel_id) + + async def get_token_status( + self, user_id: str, channel_id: str, include_filter: str + ) -> List[TokenStatus]: + if user_id is None or not isinstance(user_id, str): + raise TypeError("user_id") + if channel_id is None or not isinstance(channel_id, str): + raise TypeError("channel_id") + + result = await self._client.user_token.get_token_status( + user_id, channel_id, include_filter + ) + + return result + + async def get_aad_tokens( + self, + user_id: str, + connection_name: str, + resource_urls: List[str], + channel_id: str, + ) -> Dict[str, TokenResponse]: + if user_id is None or not isinstance(user_id, str): + raise TypeError("user_id") + if connection_name is None or not isinstance(connection_name, str): + raise TypeError("connection_name") + if channel_id is None or not isinstance(channel_id, str): + raise TypeError("channel_id") + + result = await self._client.user_token.get_aad_tokens( + user_id, connection_name, channel_id, resource_urls + ) + + return result + + async def exchange_token( + self, + user_id: str, + connection_name: str, + channel_id: str, + exchange_request: TokenExchangeRequest, + ) -> TokenResponse: + if user_id is None or not isinstance(user_id, str): + raise TypeError("user_id") + if connection_name is None or not isinstance(connection_name, str): + raise TypeError("connection_name") + if channel_id is None or not isinstance(channel_id, str): + raise TypeError("channel_id") + + (uri, token) = ( + (exchange_request.uri, exchange_request.token) + if exchange_request + else (None, None) + ) + + result = await self._client.user_token.exchange_async( + user_id, connection_name, channel_id, uri, token + ) + + return result diff --git a/libraries/botframework-connector/botframework/connector/auth/app_credentials.py b/libraries/botframework-connector/botframework/connector/auth/app_credentials.py index 148504c45..b054f0c2f 100644 --- a/libraries/botframework-connector/botframework/connector/auth/app_credentials.py +++ b/libraries/botframework-connector/botframework/connector/auth/app_credentials.py @@ -1,13 +1,10 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from datetime import datetime, timedelta -from urllib.parse import urlparse - import requests from msrest.authentication import Authentication -from botframework.connector.auth import AuthenticationConstants +from .authentication_constants import AuthenticationConstants class AppCredentials(Authentication): @@ -17,16 +14,8 @@ class AppCredentials(Authentication): """ schema = "Bearer" - - trustedHostNames = { - # "state.botframework.com": datetime.max, - # "state.botframework.azure.us": datetime.max, - "api.botframework.com": datetime.max, - "token.botframework.com": datetime.max, - "api.botframework.azure.us": datetime.max, - "token.botframework.azure.us": datetime.max, - } cache = {} + __tenant = None def __init__( self, @@ -38,50 +27,55 @@ def __init__( Initializes a new instance of MicrosoftAppCredentials class :param channel_auth_tenant: Optional. The oauth token tenant. """ - tenant = ( - channel_auth_tenant - if channel_auth_tenant - else AuthenticationConstants.DEFAULT_CHANNEL_AUTH_TENANT - ) + self.microsoft_app_id = app_id + self.tenant = channel_auth_tenant self.oauth_endpoint = ( - AuthenticationConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + tenant - ) - self.oauth_scope = ( - oauth_scope or AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + self._get_to_channel_from_bot_loginurl_prefix() + self.tenant ) + self.oauth_scope = oauth_scope or self._get_to_channel_from_bot_oauthscope() - self.microsoft_app_id = app_id + def _get_default_channelauth_tenant(self) -> str: + return AuthenticationConstants.DEFAULT_CHANNEL_AUTH_TENANT + + def _get_to_channel_from_bot_loginurl_prefix(self) -> str: + return AuthenticationConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + + def _get_to_channel_from_bot_oauthscope(self) -> str: + return AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + + @property + def tenant(self) -> str: + return self.__tenant + + @tenant.setter + def tenant(self, value: str): + self.__tenant = value or self._get_default_channelauth_tenant() @staticmethod def trust_service_url(service_url: str, expiration=None): """ + Obsolete: trust_service_url is not a required part of the security model. Checks if the service url is for a trusted host or not. :param service_url: The service url. :param expiration: The expiration time after which this service url is not trusted anymore. - :returns: True if the host of the service url is trusted; False otherwise. """ - if expiration is None: - expiration = datetime.now() + timedelta(days=1) - host = urlparse(service_url).hostname - if host is not None: - AppCredentials.trustedHostNames[host] = expiration @staticmethod - def is_trusted_service(service_url: str) -> bool: + def is_trusted_service(service_url: str) -> bool: # pylint: disable=unused-argument """ + Obsolete: is_trusted_service is not a required part of the security model. Checks if the service url is for a trusted host or not. :param service_url: The service url. :returns: True if the host of the service url is trusted; False otherwise. """ - host = urlparse(service_url).hostname - if host is not None: - return AppCredentials._is_trusted_url(host) - return False + return True @staticmethod - def _is_trusted_url(host: str) -> bool: - expiration = AppCredentials.trustedHostNames.get(host, datetime.min) - return expiration > (datetime.now() - timedelta(minutes=5)) + def _is_trusted_url(host: str) -> bool: # pylint: disable=unused-argument + """ + Obsolete: _is_trusted_url is not a required part of the security model. + """ + return True # pylint: disable=arguments-differ def signed_session(self, session: requests.Session = None) -> requests.Session: @@ -92,7 +86,7 @@ def signed_session(self, session: requests.Session = None) -> requests.Session: if not session: session = requests.Session() - if not self._should_authorize(session): + if not self._should_set_token(session): session.headers.pop("Authorization", None) else: auth_token = self.get_access_token() @@ -101,10 +95,14 @@ def signed_session(self, session: requests.Session = None) -> requests.Session: return session - def _should_authorize( + def _should_set_token( self, session: requests.Session # pylint: disable=unused-argument ) -> bool: - return True + # We don't set the token if the AppId is not set, since it means that we are in an un-authenticated scenario. + return ( + self.microsoft_app_id != AuthenticationConstants.ANONYMOUS_SKILL_APP_ID + and self.microsoft_app_id + ) def get_access_token(self, force_refresh: bool = False) -> str: """ diff --git a/libraries/botframework-connector/botframework/connector/auth/authenticate_request_result.py b/libraries/botframework-connector/botframework/connector/auth/authenticate_request_result.py new file mode 100644 index 000000000..4d9013abf --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/authenticate_request_result.py @@ -0,0 +1,17 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .claims_identity import ClaimsIdentity +from .connector_factory import ConnectorFactory + + +class AuthenticateRequestResult: + def __init__(self) -> None: + # A value for the Audience. + self.audience: str = None + # A value for the ClaimsIdentity. + self.claims_identity: ClaimsIdentity = None + # A value for the caller id. + self.caller_id: str = None + # A value for the ConnectorFactory. + self.connector_factory: ConnectorFactory = None diff --git a/libraries/botframework-connector/botframework/connector/auth/authentication_configuration.py b/libraries/botframework-connector/botframework/connector/auth/authentication_configuration.py index 59642d9ff..93314692d 100644 --- a/libraries/botframework-connector/botframework/connector/auth/authentication_configuration.py +++ b/libraries/botframework-connector/botframework/connector/auth/authentication_configuration.py @@ -3,12 +3,39 @@ from typing import Awaitable, Callable, Dict, List +from .authentication_constants import AuthenticationConstants + class AuthenticationConfiguration: def __init__( self, required_endorsements: List[str] = None, claims_validator: Callable[[List[Dict]], Awaitable] = None, + valid_token_issuers: List[str] = None, + tenant_id: str = None, ): self.required_endorsements = required_endorsements or [] self.claims_validator = claims_validator + self.valid_token_issuers = valid_token_issuers or [] + + if tenant_id: + self.add_tenant_issuers(self, tenant_id) + + @staticmethod + def add_tenant_issuers(authentication_configuration, tenant_id: str): + authentication_configuration.valid_token_issuers.append( + AuthenticationConstants.VALID_TOKEN_ISSUER_URL_TEMPLATE_V1.format(tenant_id) + ) + authentication_configuration.valid_token_issuers.append( + AuthenticationConstants.VALID_TOKEN_ISSUER_URL_TEMPLATE_V2.format(tenant_id) + ) + authentication_configuration.valid_token_issuers.append( + AuthenticationConstants.VALID_GOVERNMENT_TOKEN_ISSUER_URL_TEMPLATE_V1.format( + tenant_id + ) + ) + authentication_configuration.valid_token_issuers.append( + AuthenticationConstants.VALID_GOVERNMENT_TOKEN_ISSUER_URL_TEMPLATE_V2.format( + tenant_id + ) + ) diff --git a/libraries/botframework-connector/botframework/connector/auth/authentication_constants.py b/libraries/botframework-connector/botframework/connector/auth/authentication_constants.py index 429b7ccb6..90cb5656f 100644 --- a/libraries/botframework-connector/botframework/connector/auth/authentication_constants.py +++ b/libraries/botframework-connector/botframework/connector/auth/authentication_constants.py @@ -1,10 +1,10 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + from abc import ABC class AuthenticationConstants(ABC): - # TO CHANNEL FROM BOT: Login URL # # DEPRECATED: DO NOT USE @@ -22,11 +22,16 @@ class AuthenticationConstants(ABC): DEFAULT_CHANNEL_AUTH_TENANT = "botframework.com" # TO CHANNEL FROM BOT: OAuth scope to request - TO_CHANNEL_FROM_BOT_OAUTH_SCOPE = "https://api.botframework.com/.default" + TO_CHANNEL_FROM_BOT_OAUTH_SCOPE = "https://api.botframework.com" # TO BOT FROM CHANNEL: Token issuer TO_BOT_FROM_CHANNEL_TOKEN_ISSUER = "https://api.botframework.com" + """ + OAuth Url used to get a token from OAuthApiClient. + """ + OAUTH_URL = "https://api.botframework.com" + # Application Setting Key for the OpenIdMetadataUrl value. BOT_OPEN_ID_METADATA_KEY = "BotOpenIdMetadata" @@ -40,7 +45,7 @@ class AuthenticationConstants(ABC): EMULATE_OAUTH_CARDS_KEY = "EmulateOAuthCards" # TO BOT FROM CHANNEL: OpenID metadata document for tokens coming from MSA - TO_BOT_FROM_CHANNEL_OPEN_ID_METADATA_URL = ( + TO_BOT_FROM_CHANNEL_OPENID_METADATA_URL = ( "https://login.botframework.com/v1/.well-known/openidconfiguration" ) @@ -51,10 +56,30 @@ class AuthenticationConstants(ABC): ) # TO BOT FROM EMULATOR: OpenID metadata document for tokens coming from MSA - TO_BOT_FROM_EMULATOR_OPEN_ID_METADATA_URL = ( + TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL = ( "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration" ) + # The V1 Azure AD token issuer URL template that will contain the tenant id where + # the token was issued from. + VALID_TOKEN_ISSUER_URL_TEMPLATE_V1 = "https://sts.windows.net/{0}/" + + # The V2 Azure AD token issuer URL template that will contain the tenant id where + # the token was issued from. + VALID_TOKEN_ISSUER_URL_TEMPLATE_V2 = "https://login.microsoftonline.com/{0}/v2.0" + + # The Government V1 Azure AD token issuer URL template that will contain the tenant id + # where the token was issued from. + VALID_GOVERNMENT_TOKEN_ISSUER_URL_TEMPLATE_V1 = ( + "https://login.microsoftonline.us/{0}/" + ) + + # The Government V2 Azure AD token issuer URL template that will contain the tenant id + # where the token was issued from. + VALID_GOVERNMENT_TOKEN_ISSUER_URL_TEMPLATE_V2 = ( + "https://login.microsoftonline.us/{0}/v2.0" + ) + # Allowed token signing algorithms. Tokens come from channels to the bot. The code # that uses this also supports tokens coming from the emulator. ALLOWED_SIGNING_ALGORITHMS = ["RS256", "RS384", "RS512"] @@ -113,3 +138,9 @@ class AuthenticationConstants(ABC): # Service URL claim name. As used in Microsoft Bot Framework v3.1 auth. SERVICE_URL_CLAIM = "serviceurl" + + # AppId used for creating skill claims when there is no appId and password configured. + ANONYMOUS_SKILL_APP_ID = "AnonymousSkill" + + # Indicates that ClaimsIdentity.authentication_type is anonymous (no app Id and password were provided). + ANONYMOUS_AUTH_TYPE = "anonymous" diff --git a/libraries/botframework-connector/botframework/connector/auth/bot_framework_authentication.py b/libraries/botframework-connector/botframework/connector/auth/bot_framework_authentication.py new file mode 100644 index 000000000..ea1d96d62 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/bot_framework_authentication.py @@ -0,0 +1,121 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod + +from botbuilder.schema import Activity, CallerIdConstants + +from botframework.connector.skills import BotFrameworkClient + +from .authenticate_request_result import AuthenticateRequestResult +from .claims_identity import ClaimsIdentity +from .connector_factory import ConnectorFactory +from .jwt_token_validation import JwtTokenValidation +from .user_token_client import UserTokenClient +from .service_client_credentials_factory import ServiceClientCredentialsFactory +from .skill_validation import SkillValidation + + +class BotFrameworkAuthentication(ABC): + @abstractmethod + async def authenticate_request( + self, activity: Activity, auth_header: str + ) -> AuthenticateRequestResult: + """ + Validate Bot Framework Protocol requests. + + :param activity: The inbound Activity. + :param auth_header: The HTTP auth header. + :return: An AuthenticateRequestResult. + """ + raise NotImplementedError() + + @abstractmethod + async def authenticate_streaming_request( + self, auth_header: str, channel_id_header: str + ) -> AuthenticateRequestResult: + """ + Validate Bot Framework Protocol requests. + + :param auth_header: The HTTP auth header. + :param channel_id_header: The channel ID HTTP header. + :return: An AuthenticateRequestResult. + """ + raise NotImplementedError() + + @abstractmethod + def create_connector_factory( + self, claims_identity: ClaimsIdentity + ) -> ConnectorFactory: + """ + Creates a ConnectorFactory that can be used to create ConnectorClients that can use credentials + from this particular Cloud Environment. + + :param claims_identity: The inbound Activity's ClaimsIdentity. + :return: A ConnectorFactory. + """ + raise NotImplementedError() + + @abstractmethod + async def create_user_token_client( + self, claims_identity: ClaimsIdentity + ) -> UserTokenClient: + """ + Creates the appropriate UserTokenClient instance. + + :param claims_identity: The inbound Activity's ClaimsIdentity. + :return: An UserTokenClient. + """ + raise NotImplementedError() + + def create_bot_framework_client(self) -> BotFrameworkClient: + """ + Creates a BotFrameworkClient for calling Skills. + + :return: A BotFrameworkClient. + """ + raise Exception("NotImplemented") + + def get_originating_audience(self) -> str: + """ + Gets the originating audience from Bot OAuth scope. + + :return: The originating audience. + """ + raise Exception("NotImplemented") + + async def authenticate_channel_request(self, auth_header: str) -> ClaimsIdentity: + """ + Authenticate Bot Framework Protocol request to Skills. + + :param auth_header: The HTTP auth header in the skill request. + :return: A ClaimsIdentity. + """ + raise Exception("NotImplemented") + + async def generate_caller_id( + self, + *, + credential_factory: ServiceClientCredentialsFactory, + claims_identity: ClaimsIdentity, + caller_id: str, + ) -> str: + """ + Generates the appropriate caller_id to write onto the Activity, this might be None. + + :param credential_factory A ServiceClientCredentialsFactory to use. + :param claims_identity The inbound claims. + :param caller_id The default caller_id to use if this is not a skill. + :return: The caller_id, this might be None. + """ + # Is the bot accepting all incoming messages? + if await credential_factory.is_authentication_disabled(): + # Return None so that the caller_id is cleared. + return None + + # Is the activity from another bot? + return ( + f"{CallerIdConstants.bot_to_bot_prefix}{JwtTokenValidation.get_app_id_from_claims(claims_identity.claims)}" + if SkillValidation.is_skill_claim(claims_identity.claims) + else caller_id + ) diff --git a/libraries/botframework-connector/botframework/connector/auth/bot_framework_authentication_factory.py b/libraries/botframework-connector/botframework/connector/auth/bot_framework_authentication_factory.py new file mode 100644 index 000000000..45643d465 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/bot_framework_authentication_factory.py @@ -0,0 +1,112 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +from logging import Logger + +from ..bot_framework_sdk_client_async import BotFrameworkConnectorConfiguration +from ..http_client_factory import HttpClientFactory + +from ._government_cloud_bot_framework_authentication import ( + _GovernmentCloudBotFrameworkAuthentication, +) +from ._parameterized_bot_framework_authentication import ( + _ParameterizedBotFrameworkAuthentication, +) +from ._public_cloud_bot_framework_authentication import ( + _PublicCloudBotFrameworkAuthentication, +) + +from .authentication_configuration import AuthenticationConfiguration +from .bot_framework_authentication import BotFrameworkAuthentication +from .government_constants import GovernmentConstants +from .password_service_client_credential_factory import ( + PasswordServiceClientCredentialFactory, +) +from .service_client_credentials_factory import ServiceClientCredentialsFactory + + +class BotFrameworkAuthenticationFactory: + @staticmethod + def create( + *, + channel_service: str = None, + validate_authority: bool = False, + to_channel_from_bot_login_url: str = None, + to_channel_from_bot_oauth_scope: str = None, + to_bot_from_channel_token_issuer: str = None, + oauth_url: str = None, + to_bot_from_channel_open_id_metadata_url: str = None, + to_bot_from_emulator_open_id_metadata_url: str = None, + caller_id: str = None, + credential_factory: ServiceClientCredentialsFactory = PasswordServiceClientCredentialFactory(), + auth_configuration: AuthenticationConfiguration = AuthenticationConfiguration(), + http_client_factory: HttpClientFactory = None, + connector_client_configuration: BotFrameworkConnectorConfiguration = None, + logger: Logger = None + ) -> BotFrameworkAuthentication: + """ + Creates the appropriate BotFrameworkAuthentication instance. + + :param channel_service: The Channel Service. + :param validate_authority: The validate authority value to use. + :param to_channel_from_bot_login_url: The to Channel from bot login url. + :param to_channel_from_bot_oauth_scope: The to Channel from bot oauth scope. + :param to_bot_from_channel_token_issuer: The to bot from Channel Token Issuer. + :param oauth_url: The oAuth url. + :param to_bot_from_channel_open_id_metadata_url: The to bot from Channel Open Id Metadata url. + :param to_bot_from_emulator_open_id_metadata_url: The to bot from Emulator Open Id Metadata url. + :param caller_id: The Microsoft app password. + :param credential_factory: The ServiceClientCredentialsFactory to use to create credentials. + :param auth_configuration: The AuthenticationConfiguration to use. + :param http_client_factory: The HttpClientFactory to use for a skill BotFrameworkClient. + :param connector_client_configuration: Configuration to use custom http pipeline for the connector + :param logger: The Logger to use. + :return: A new BotFrameworkAuthentication instance. + """ + # pylint: disable=too-many-boolean-expressions + if ( + to_channel_from_bot_login_url + or to_channel_from_bot_oauth_scope + or to_bot_from_channel_token_issuer + or oauth_url + or to_bot_from_channel_open_id_metadata_url + or to_bot_from_emulator_open_id_metadata_url + or caller_id + ): + # if we have any of the 'parameterized' properties defined we'll assume this is the parameterized code + return _ParameterizedBotFrameworkAuthentication( + validate_authority, + to_channel_from_bot_login_url, + to_channel_from_bot_oauth_scope, + to_bot_from_channel_token_issuer, + oauth_url, + to_bot_from_channel_open_id_metadata_url, + to_bot_from_emulator_open_id_metadata_url, + caller_id, + credential_factory, + auth_configuration, + http_client_factory, + connector_client_configuration, + logger, + ) + # else apply the built in default behavior, which is either the public cloud or the gov cloud + # depending on whether we have a channelService value present + if not channel_service: + return _PublicCloudBotFrameworkAuthentication( + credential_factory, + auth_configuration, + http_client_factory, + connector_client_configuration, + logger, + ) + if channel_service == GovernmentConstants.CHANNEL_SERVICE: + return _GovernmentCloudBotFrameworkAuthentication( + credential_factory, + auth_configuration, + http_client_factory, + connector_client_configuration, + logger, + ) + + # The ChannelService value is used an indicator of which built in set of constants to use. + # If it is not recognized, a full configuration is expected. + raise ValueError("The provided channel_service value is not supported.") diff --git a/libraries/botframework-connector/botframework/connector/auth/certificate_app_credentials.py b/libraries/botframework-connector/botframework/connector/auth/certificate_app_credentials.py index 298bb581f..31e845eb6 100644 --- a/libraries/botframework-connector/botframework/connector/auth/certificate_app_credentials.py +++ b/libraries/botframework-connector/botframework/connector/auth/certificate_app_credentials.py @@ -23,7 +23,20 @@ def __init__( certificate_private_key: str, channel_auth_tenant: str = None, oauth_scope: str = None, + certificate_public: str = None, ): + """ + AppCredentials implementation using a certificate. + + :param app_id: + :param certificate_thumbprint: + :param certificate_private_key: + :param channel_auth_tenant: + :param oauth_scope: + :param certificate_public: public_certificate (optional) is public key certificate which will be sent + through ‘x5c’ JWT header only for subject name and issuer authentication to support cert auto rolls. + """ + # super will set proper scope and endpoint. super().__init__( app_id=app_id, @@ -31,10 +44,10 @@ def __init__( oauth_scope=oauth_scope, ) - self.scopes = [self.oauth_scope] self.app = None self.certificate_thumbprint = certificate_thumbprint self.certificate_private_key = certificate_private_key + self.certificate_public = certificate_public def get_access_token(self, force_refresh: bool = False) -> str: """ @@ -42,18 +55,29 @@ def get_access_token(self, force_refresh: bool = False) -> str: :return: The access token for the given certificate. """ + scope = self.oauth_scope + if not scope.endswith("/.default"): + scope += "/.default" + scopes = [scope] + # Firstly, looks up a token from cache # Since we are looking for token for the current app, NOT for an end user, # notice we give account parameter as None. - auth_token = self.__get_msal_app().acquire_token_silent( - self.scopes, account=None - ) + auth_token = self.__get_msal_app().acquire_token_silent(scopes, account=None) if not auth_token: # No suitable token exists in cache. Let's get a new one from AAD. - auth_token = self.__get_msal_app().acquire_token_for_client( - scopes=self.scopes - ) - return auth_token["access_token"] + auth_token = self.__get_msal_app().acquire_token_for_client(scopes=scopes) + if "access_token" in auth_token: + return auth_token["access_token"] + error = auth_token["error"] if "error" in auth_token else "Unknown error" + error_description = ( + auth_token["error_description"] + if "error_description" in auth_token + else "Unknown error description" + ) + raise PermissionError( + f"Failed to get access token with error: {error}, error_description: {error_description}" + ) def __get_msal_app(self): if not self.app: @@ -63,6 +87,9 @@ def __get_msal_app(self): client_credential={ "thumbprint": self.certificate_thumbprint, "private_key": self.certificate_private_key, + "public_certificate": ( + self.certificate_public if self.certificate_public else None + ), }, ) diff --git a/libraries/botframework-connector/botframework/connector/auth/certificate_government_app_credentials.py b/libraries/botframework-connector/botframework/connector/auth/certificate_government_app_credentials.py new file mode 100644 index 000000000..b2883cfa1 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/certificate_government_app_credentials.py @@ -0,0 +1,51 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .certificate_app_credentials import CertificateAppCredentials +from .government_constants import GovernmentConstants + + +class CertificateGovernmentAppCredentials(CertificateAppCredentials): + """ + GovernmentAppCredentials implementation using a certificate. + """ + + def __init__( + self, + app_id: str, + certificate_thumbprint: str, + certificate_private_key: str, + channel_auth_tenant: str = None, + oauth_scope: str = None, + certificate_public: str = None, + ): + """ + AppCredentials implementation using a certificate. + + :param app_id: + :param certificate_thumbprint: + :param certificate_private_key: + :param channel_auth_tenant: + :param oauth_scope: + :param certificate_public: public_certificate (optional) is public key certificate which will be sent + through ‘x5c’ JWT header only for subject name and issuer authentication to support cert auto rolls. + """ + + # super will set proper scope and endpoint. + super().__init__( + app_id=app_id, + channel_auth_tenant=channel_auth_tenant, + oauth_scope=oauth_scope, + certificate_thumbprint=certificate_thumbprint, + certificate_private_key=certificate_private_key, + certificate_public=certificate_public, + ) + + def _get_default_channelauth_tenant(self) -> str: + return GovernmentConstants.DEFAULT_CHANNEL_AUTH_TENANT + + def _get_to_channel_from_bot_loginurl_prefix(self) -> str: + return GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + + def _get_to_channel_from_bot_oauthscope(self) -> str: + return GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE diff --git a/libraries/botframework-connector/botframework/connector/auth/certificate_service_client_credential_factory.py b/libraries/botframework-connector/botframework/connector/auth/certificate_service_client_credential_factory.py new file mode 100644 index 000000000..7a71c28bd --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/certificate_service_client_credential_factory.py @@ -0,0 +1,129 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger + +from msrest.authentication import Authentication + +from .authentication_constants import AuthenticationConstants +from .government_constants import GovernmentConstants +from .certificate_app_credentials import CertificateAppCredentials +from .certificate_government_app_credentials import CertificateGovernmentAppCredentials +from .microsoft_app_credentials import MicrosoftAppCredentials +from .service_client_credentials_factory import ServiceClientCredentialsFactory + + +class CertificateServiceClientCredentialsFactory(ServiceClientCredentialsFactory): + def __init__( + self, + certificate_thumbprint: str, + certificate_private_key: str, + app_id: str, + tenant_id: str = None, + certificate_public: str = None, + *, + logger: Logger = None + ) -> None: + """ + CertificateServiceClientCredentialsFactory implementation using a certificate. + + :param certificate_thumbprint: + :param certificate_private_key: + :param app_id: + :param tenant_id: + :param certificate_public: public_certificate (optional) is public key certificate which will be sent + through ‘x5c’ JWT header only for subject name and issuer authentication to support cert auto rolls. + """ + + self.certificate_thumbprint = certificate_thumbprint + self.certificate_private_key = certificate_private_key + self.app_id = app_id + self.tenant_id = tenant_id + self.certificate_public = certificate_public + self._logger = logger + + async def is_valid_app_id(self, app_id: str) -> bool: + return app_id == self.app_id + + async def is_authentication_disabled(self) -> bool: + return not self.app_id + + async def create_credentials( + self, + app_id: str, + oauth_scope: str, + login_endpoint: str, + validate_authority: bool, + ) -> Authentication: + if await self.is_authentication_disabled(): + return MicrosoftAppCredentials.empty() + + if not await self.is_valid_app_id(app_id): + raise Exception("Invalid app_id") + + normalized_endpoint = login_endpoint.lower() if login_endpoint else "" + + if normalized_endpoint.startswith( + AuthenticationConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + ): + credentials = CertificateAppCredentials( + app_id, + self.certificate_thumbprint, + self.certificate_private_key, + self.tenant_id, + oauth_scope, + self.certificate_public, + ) + elif normalized_endpoint.startswith( + GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + ): + credentials = CertificateGovernmentAppCredentials( + app_id, + self.certificate_thumbprint, + self.certificate_private_key, + self.tenant_id, + oauth_scope, + self.certificate_public, + ) + else: + credentials = _CertificatePrivateCloudAppCredentials( + app_id, + self.certificate_thumbprint, + self.certificate_private_key, + self.tenant_id, + oauth_scope, + self.certificate_public, + login_endpoint, + validate_authority, + ) + + return credentials + + +class _CertificatePrivateCloudAppCredentials(CertificateAppCredentials): + def __init__( + self, + app_id: str, + certificate_thumbprint: str, + certificate_private_key: str, + channel_auth_tenant: str, + oauth_scope: str, + certificate_public: str, + oauth_endpoint: str, + validate_authority: bool, + ): + super().__init__( + app_id, + certificate_thumbprint, + certificate_private_key, + channel_auth_tenant, + oauth_scope, + certificate_public, + ) + + self.oauth_endpoint = oauth_endpoint + self._validate_authority = validate_authority + + @property + def validate_authority(self): + return self._validate_authority diff --git a/libraries/botframework-connector/botframework/connector/auth/channel_validation.py b/libraries/botframework-connector/botframework/connector/auth/channel_validation.py index fde7f1144..590e39862 100644 --- a/libraries/botframework-connector/botframework/connector/auth/channel_validation.py +++ b/libraries/botframework-connector/botframework/connector/auth/channel_validation.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import asyncio from .authentication_configuration import AuthenticationConfiguration @@ -33,7 +36,7 @@ async def authenticate_channel_token_with_service_url( channel_id: str, auth_configuration: AuthenticationConfiguration = None, ) -> ClaimsIdentity: - """ Validate the incoming Auth Header + """Validate the incoming Auth Header Validate the incoming Auth Header as a token sent from the Bot Framework Service. A token issued by the Bot Framework emulator will FAIL this check. @@ -68,7 +71,7 @@ async def authenticate_channel_token( channel_id: str, auth_configuration: AuthenticationConfiguration = None, ) -> ClaimsIdentity: - """ Validate the incoming Auth Header + """Validate the incoming Auth Header Validate the incoming Auth Header as a token sent from the Bot Framework Service. A token issued by the Bot Framework emulator will FAIL this check. @@ -85,7 +88,7 @@ async def authenticate_channel_token( metadata_endpoint = ( ChannelValidation.open_id_metadata_endpoint if ChannelValidation.open_id_metadata_endpoint - else AuthenticationConstants.TO_BOT_FROM_CHANNEL_OPEN_ID_METADATA_URL + else AuthenticationConstants.TO_BOT_FROM_CHANNEL_OPENID_METADATA_URL ) token_extractor = JwtTokenExtractor( diff --git a/libraries/botframework-connector/botframework/connector/auth/claims_identity.py b/libraries/botframework-connector/botframework/connector/auth/claims_identity.py index 9abdb6cb0..211f7b241 100644 --- a/libraries/botframework-connector/botframework/connector/auth/claims_identity.py +++ b/libraries/botframework-connector/botframework/connector/auth/claims_identity.py @@ -1,3 +1,7 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + class Claim: def __init__(self, claim_type: str, value): self.type = claim_type @@ -5,9 +9,12 @@ def __init__(self, claim_type: str, value): class ClaimsIdentity: - def __init__(self, claims: dict, is_authenticated: bool): + def __init__( + self, claims: dict, is_authenticated: bool, authentication_type: str = None + ): self.claims = claims self.is_authenticated = is_authenticated + self.authentication_type = authentication_type def get_claim_value(self, claim_type: str): return self.claims.get(claim_type) diff --git a/libraries/botframework-connector/botframework/connector/auth/connector_factory.py b/libraries/botframework-connector/botframework/connector/auth/connector_factory.py new file mode 100644 index 000000000..2cbadccf9 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/connector_factory.py @@ -0,0 +1,18 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod + +from botframework.connector.aio import ConnectorClient + + +class ConnectorFactory(ABC): + @abstractmethod + async def create(self, service_url: str, audience: str) -> ConnectorClient: + """ + A factory method used to create ConnectorClient instances. + :param service_url: The url for the client. + :param audience: The audience for the credentials the client will use. + :returns: A ConnectorClient for sending activities to the audience at the service_url. + """ + raise NotImplementedError() diff --git a/libraries/botframework-connector/botframework/connector/auth/credential_provider.py b/libraries/botframework-connector/botframework/connector/auth/credential_provider.py index b95cff120..b9a83a37f 100644 --- a/libraries/botframework-connector/botframework/connector/auth/credential_provider.py +++ b/libraries/botframework-connector/botframework/connector/auth/credential_provider.py @@ -1,6 +1,10 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + class CredentialProvider: """CredentialProvider. - This class allows Bots to provide their own implemention + This class allows Bots to provide their own implementation of what is, and what is not, a valid appId and password. This is useful in the case of multi-tenant bots, where the bot may need to call out to a service to determine if a particular @@ -16,7 +20,7 @@ async def is_valid_appid(self, app_id: str) -> bool: :param app_id: bot appid :return: true if it is a valid AppId """ - raise NotImplementedError + raise NotImplementedError() async def get_app_password(self, app_id: str) -> str: """Get the app password for a given bot appId, if it is not a valid appId, return Null @@ -27,7 +31,7 @@ async def get_app_password(self, app_id: str) -> str: :param app_id: bot appid :return: password or null for invalid appid """ - raise NotImplementedError + raise NotImplementedError() async def is_authentication_disabled(self) -> bool: """Checks if bot authentication is disabled. @@ -38,7 +42,7 @@ async def is_authentication_disabled(self) -> bool: :return: true if bot authentication is disabled. """ - raise NotImplementedError + raise NotImplementedError() class SimpleCredentialProvider(CredentialProvider): @@ -54,3 +58,17 @@ async def get_app_password(self, app_id: str) -> str: async def is_authentication_disabled(self) -> bool: return not self.app_id + + +class _DelegatingCredentialProvider(CredentialProvider): + def __init__(self, credentials_factory: "botframework.connector.auth"): + self._credentials_factory = credentials_factory + + async def is_valid_appid(self, app_id: str) -> bool: + return await self._credentials_factory.is_valid_app_id(app_id) + + async def get_app_password(self, app_id: str) -> str: + raise NotImplementedError() + + async def is_authentication_disabled(self) -> bool: + return await self._credentials_factory.is_authentication_disabled() diff --git a/libraries/botframework-connector/botframework/connector/auth/emulator_validation.py b/libraries/botframework-connector/botframework/connector/auth/emulator_validation.py index 0e2d7fcaa..4cd43ea9e 100644 --- a/libraries/botframework-connector/botframework/connector/auth/emulator_validation.py +++ b/libraries/botframework-connector/botframework/connector/auth/emulator_validation.py @@ -1,7 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -import asyncio from typing import Union import jwt @@ -35,6 +34,10 @@ class EmulatorValidation: "https://sts.windows.net/cab8a31a-1906-4287-a0d8-4eef66b95f6e/", # Auth for US Gov, 2.0 token "https://login.microsoftonline.us/cab8a31a-1906-4287-a0d8-4eef66b95f6e/v2.0", + # Auth for US Gov, 1.0 token + "https://login.microsoftonline.us/f8cdef31-a31e-4b4a-93e4-5f571e91255a/", + # Auth for US Gov, 2.0 token + "https://login.microsoftonline.us/f8cdef31-a31e-4b4a-93e4-5f571e91255a/v2.0", ], audience=None, clock_tolerance=5 * 60, @@ -43,7 +46,7 @@ class EmulatorValidation: @staticmethod def is_token_from_emulator(auth_header: str) -> bool: - """ Determines if a given Auth header is from the Bot Framework Emulator + """Determines if a given Auth header is from the Bot Framework Emulator :param auth_header: Bearer Token, in the 'Bearer [Long String]' Format. :type auth_header: str @@ -60,7 +63,7 @@ def is_token_from_emulator(auth_header: str) -> bool: bearer_token = auth_header.split(" ")[1] # Parse the Big Long String into an actual token. - token = jwt.decode(bearer_token, verify=False) + token = jwt.decode(bearer_token, options={"verify_signature": False}) if not token: return False @@ -88,7 +91,7 @@ async def authenticate_emulator_token( channel_service_or_provider: Union[str, ChannelProvider], channel_id: str, ) -> ClaimsIdentity: - """ Validate the incoming Auth Header + """Validate the incoming Auth Header Validate the incoming Auth Header as a token sent from the Bot Framework Service. A token issued by the Bot Framework emulator will FAIL this check. @@ -110,9 +113,9 @@ async def authenticate_emulator_token( is_gov = JwtTokenValidation.is_government(channel_service_or_provider) open_id_metadata = ( - GovernmentConstants.TO_BOT_FROM_EMULATOR_OPEN_ID_METADATA_URL + GovernmentConstants.TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL if is_gov - else AuthenticationConstants.TO_BOT_FROM_EMULATOR_OPEN_ID_METADATA_URL + else AuthenticationConstants.TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL ) token_extractor = JwtTokenExtractor( @@ -177,9 +180,8 @@ async def authenticate_emulator_token( "Unauthorized. Unknown Emulator Token version ", version_claim, "." ) - is_valid_app_id = await asyncio.ensure_future( - credentials.is_valid_appid(app_id) - ) + is_valid_app_id = await credentials.is_valid_appid(app_id) + if not is_valid_app_id: raise PermissionError( "Unauthorized. Invalid AppId passed on token: ", app_id diff --git a/libraries/botframework-connector/botframework/connector/auth/enterprise_channel_validation.py b/libraries/botframework-connector/botframework/connector/auth/enterprise_channel_validation.py index 48a93ba5d..0e6354e7c 100644 --- a/libraries/botframework-connector/botframework/connector/auth/enterprise_channel_validation.py +++ b/libraries/botframework-connector/botframework/connector/auth/enterprise_channel_validation.py @@ -15,7 +15,6 @@ class EnterpriseChannelValidation(ABC): - TO_BOT_FROM_ENTERPRISE_CHANNEL_TOKEN_VALIDATION_PARAMETERS = VerifyOptions( issuer=[AuthenticationConstants.TO_BOT_FROM_CHANNEL_TOKEN_ISSUER], audience=None, @@ -64,12 +63,14 @@ async def authenticate_channel_token_with_service_url( channel_service_or_provider: Union[str, ChannelProvider], auth_configuration: AuthenticationConfiguration = None, ) -> ClaimsIdentity: - identity: ClaimsIdentity = await EnterpriseChannelValidation.authenticate_channel_token( - auth_header, - credentials, - channel_id, - channel_service_or_provider, - auth_configuration, + identity: ClaimsIdentity = ( + await EnterpriseChannelValidation.authenticate_channel_token( + auth_header, + credentials, + channel_id, + channel_service_or_provider, + auth_configuration, + ) ) service_url_claim: str = identity.get_claim_value( diff --git a/libraries/botframework-connector/botframework/connector/auth/government_channel_validation.py b/libraries/botframework-connector/botframework/connector/auth/government_channel_validation.py index 3c2285393..d3ec16da1 100644 --- a/libraries/botframework-connector/botframework/connector/auth/government_channel_validation.py +++ b/libraries/botframework-connector/botframework/connector/auth/government_channel_validation.py @@ -13,7 +13,6 @@ class GovernmentChannelValidation(ABC): - OPEN_ID_METADATA_ENDPOINT = "" TO_BOT_FROM_GOVERNMENT_CHANNEL_TOKEN_VALIDATION_PARAMETERS = VerifyOptions( @@ -34,7 +33,7 @@ async def authenticate_channel_token( endpoint = ( GovernmentChannelValidation.OPEN_ID_METADATA_ENDPOINT if GovernmentChannelValidation.OPEN_ID_METADATA_ENDPOINT - else GovernmentConstants.TO_BOT_FROM_CHANNEL_OPEN_ID_METADATA_URL + else GovernmentConstants.TO_BOT_FROM_CHANNEL_OPENID_METADATA_URL ) token_extractor = JwtTokenExtractor( GovernmentChannelValidation.TO_BOT_FROM_GOVERNMENT_CHANNEL_TOKEN_VALIDATION_PARAMETERS, @@ -57,8 +56,10 @@ async def authenticate_channel_token_with_service_url( channel_id: str, auth_configuration: AuthenticationConfiguration = None, ) -> ClaimsIdentity: - identity: ClaimsIdentity = await GovernmentChannelValidation.authenticate_channel_token( - auth_header, credentials, channel_id, auth_configuration + identity: ClaimsIdentity = ( + await GovernmentChannelValidation.authenticate_channel_token( + auth_header, credentials, channel_id, auth_configuration + ) ) service_url_claim: str = identity.get_claim_value( diff --git a/libraries/botframework-connector/botframework/connector/auth/government_constants.py b/libraries/botframework-connector/botframework/connector/auth/government_constants.py index 8dcb19b34..3e109d3b6 100644 --- a/libraries/botframework-connector/botframework/connector/auth/government_constants.py +++ b/libraries/botframework-connector/botframework/connector/auth/government_constants.py @@ -1,10 +1,10 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + from abc import ABC class GovernmentConstants(ABC): - """ Government Channel Service property value """ @@ -13,34 +13,46 @@ class GovernmentConstants(ABC): """ TO CHANNEL FROM BOT: Login URL + + DEPRECATED: DO NOT USE """ TO_CHANNEL_FROM_BOT_LOGIN_URL = ( - "https://login.microsoftonline.us/" - "cab8a31a-1906-4287-a0d8-4eef66b95f6e/" - "oauth2/v2.0/token" + "https://login.microsoftonline.us/MicrosoftServices.onmicrosoft.us" ) + """ + TO CHANNEL FROM BOT: Login URL prefix + """ + TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX = "https://login.microsoftonline.us/" + + DEFAULT_CHANNEL_AUTH_TENANT = "MicrosoftServices.onmicrosoft.us" + """ TO CHANNEL FROM BOT: OAuth scope to request """ - TO_CHANNEL_FROM_BOT_OAUTH_SCOPE = "https://api.botframework.us/.default" + TO_CHANNEL_FROM_BOT_OAUTH_SCOPE = "https://api.botframework.us" """ TO BOT FROM CHANNEL: Token issuer """ TO_BOT_FROM_CHANNEL_TOKEN_ISSUER = "https://api.botframework.us" + """ + OAuth Url used to get a token from OAuthApiClient. + """ + OAUTH_URL_GOV = "https://api.botframework.azure.us" + """ TO BOT FROM CHANNEL: OpenID metadata document for tokens coming from MSA """ - TO_BOT_FROM_CHANNEL_OPEN_ID_METADATA_URL = ( + TO_BOT_FROM_CHANNEL_OPENID_METADATA_URL = ( "https://login.botframework.azure.us/v1/.well-known/openidconfiguration" ) """ TO BOT FROM GOV EMULATOR: OpenID metadata document for tokens coming from MSA """ - TO_BOT_FROM_EMULATOR_OPEN_ID_METADATA_URL = ( + TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL = ( "https://login.microsoftonline.us/" "cab8a31a-1906-4287-a0d8-4eef66b95f6e/v2.0/" ".well-known/openid-configuration" diff --git a/libraries/botframework-connector/botframework/connector/auth/jwt_token_extractor.py b/libraries/botframework-connector/botframework/connector/auth/jwt_token_extractor.py index 4a0850763..a030c3fb7 100644 --- a/libraries/botframework-connector/botframework/connector/auth/jwt_token_extractor.py +++ b/libraries/botframework-connector/botframework/connector/auth/jwt_token_extractor.py @@ -68,7 +68,7 @@ async def get_identity( raise error def _has_allowed_issuer(self, jwt_token: str) -> bool: - decoded = jwt.decode(jwt_token, verify=False) + decoded = jwt.decode(jwt_token, options={"verify_signature": False}) issuer = decoded.get("iss", None) if issuer in self.validation_parameters.issuer: return True @@ -111,6 +111,7 @@ async def _validate_token( metadata.public_key, leeway=self.validation_parameters.clock_tolerance, options=options, + algorithms=["RS256"], ) claims = ClaimsIdentity(decoded_payload, True) @@ -125,10 +126,16 @@ def __init__(self, url): self.last_updated = datetime.min async def get(self, key_id: str): - # If keys are more than 5 days old, refresh them - if self.last_updated < (datetime.now() + timedelta(days=5)): + # If keys are more than 1 day old, refresh them + if self.last_updated < (datetime.now() - timedelta(days=1)): await self._refresh() - return self._find(key_id) + + key = self._find(key_id) + if not key and self.last_updated < (datetime.now() - timedelta(hours=1)): + # Refresh the cache if a key is not found (max once per hour) + await self._refresh() + key = self._find(key_id) + return key async def _refresh(self): response = requests.get(self.url) diff --git a/libraries/botframework-connector/botframework/connector/auth/jwt_token_validation.py b/libraries/botframework-connector/botframework/connector/auth/jwt_token_validation.py index 2d21c4af1..a0e937156 100644 --- a/libraries/botframework-connector/botframework/connector/auth/jwt_token_validation.py +++ b/libraries/botframework-connector/botframework/connector/auth/jwt_token_validation.py @@ -1,15 +1,16 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + from typing import Dict, List, Union -from botbuilder.schema import Activity +from botbuilder.schema import Activity, RoleTypes +from ..channels import Channels from .authentication_configuration import AuthenticationConfiguration from .authentication_constants import AuthenticationConstants from .emulator_validation import EmulatorValidation from .enterprise_channel_validation import EnterpriseChannelValidation from .channel_validation import ChannelValidation -from .microsoft_app_credentials import MicrosoftAppCredentials from .credential_provider import CredentialProvider from .claims_identity import ClaimsIdentity from .government_constants import GovernmentConstants @@ -19,7 +20,6 @@ class JwtTokenValidation: - # TODO remove the default value on channel_service @staticmethod async def authenticate_request( @@ -43,14 +43,29 @@ async def authenticate_request( """ if not auth_header: # No auth header was sent. We might be on the anonymous code path. - is_auth_disabled = await credentials.is_authentication_disabled() - if is_auth_disabled: - # We are on the anonymous code path. - return ClaimsIdentity({}, True) - - # No Auth Header. Auth is required. Request is not authorized. - raise PermissionError("Unauthorized Access. Request is not authorized") - + auth_is_disabled = await credentials.is_authentication_disabled() + if not auth_is_disabled: + # No Auth Header. Auth is required. Request is not authorized. + raise PermissionError("Required Authorization token was not supplied") + + # Check if the activity is for a skill call and is coming from the Emulator. + try: + if ( + activity.channel_id == Channels.emulator + and activity.recipient.role == RoleTypes.skill + and activity.relates_to is not None + ): + # Return an anonymous claim with an anonymous skill AppId + return SkillValidation.create_anonymous_skill_claim() + except AttributeError: + pass + + # In the scenario where Auth is disabled, we still want to have the + # IsAuthenticated flag set in the ClaimsIdentity. To do this requires + # adding in an empty claim. + return ClaimsIdentity({}, True, AuthenticationConstants.ANONYMOUS_AUTH_TYPE) + + # Validate the header and extract claims. claims_identity = await JwtTokenValidation.validate_auth_header( auth_header, credentials, @@ -60,9 +75,6 @@ async def authenticate_request( auth_configuration, ) - # On the standard Auth path, we need to trust the URL that was incoming. - MicrosoftAppCredentials.trust_service_url(activity.service_url) - return claims_identity @staticmethod @@ -99,7 +111,7 @@ async def get_claims() -> ClaimsIdentity: ) is_gov = ( isinstance(channel_service_or_provider, ChannelProvider) - and channel_service_or_provider.is_public_azure() + and channel_service_or_provider.is_government() or isinstance(channel_service_or_provider, str) and JwtTokenValidation.is_government(channel_service_or_provider) ) @@ -165,6 +177,11 @@ async def validate_claims( ): if auth_config and auth_config.claims_validator: await auth_config.claims_validator(claims) + elif SkillValidation.is_skill_claim(claims): + # Skill claims must be validated using AuthenticationConfiguration claims_validator + raise PermissionError( + "Unauthorized Access. Request is not authorized. Skill Claims require validation." + ) @staticmethod def is_government(channel_service: str) -> bool: diff --git a/libraries/botframework-connector/botframework/connector/auth/managedidentity_app_credentials.py b/libraries/botframework-connector/botframework/connector/auth/managedidentity_app_credentials.py new file mode 100644 index 000000000..568eb19e2 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/managedidentity_app_credentials.py @@ -0,0 +1,56 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC + +import msal +import requests + +from .app_credentials import AppCredentials +from .microsoft_app_credentials import MicrosoftAppCredentials + + +class ManagedIdentityAppCredentials(AppCredentials, ABC): + """ + AppCredentials implementation using application ID and password. + """ + + global_token_cache = msal.TokenCache() + + def __init__(self, app_id: str, oauth_scope: str = None): + # super will set proper scope and endpoint. + super().__init__( + app_id=app_id, + oauth_scope=oauth_scope, + ) + + self._managed_identity = {"ManagedIdentityIdType": "ClientId", "Id": app_id} + + self.app = None + + @staticmethod + def empty(): + return MicrosoftAppCredentials("", "") + + def get_access_token(self, force_refresh: bool = False) -> str: + """ + Implementation of AppCredentials.get_token. + :return: The access token for the given app id and password. + """ + + # Firstly, looks up a token from cache + # Since we are looking for token for the current app, NOT for an end user, + # notice we give account parameter as None. + auth_token = self.__get_msal_app().acquire_token_for_client( + resource=self.oauth_scope + ) + return auth_token["access_token"] + + def __get_msal_app(self): + if not self.app: + self.app = msal.ManagedIdentityClient( + self._managed_identity, + http_client=requests.Session(), + token_cache=ManagedIdentityAppCredentials.global_token_cache, + ) + return self.app diff --git a/libraries/botframework-connector/botframework/connector/auth/managedidentity_service_client_credential_factory.py b/libraries/botframework-connector/botframework/connector/auth/managedidentity_service_client_credential_factory.py new file mode 100644 index 000000000..61bf2a12b --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/managedidentity_service_client_credential_factory.py @@ -0,0 +1,39 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger + +from msrest.authentication import Authentication + +from .managedidentity_app_credentials import ManagedIdentityAppCredentials +from .microsoft_app_credentials import MicrosoftAppCredentials +from .service_client_credentials_factory import ServiceClientCredentialsFactory + + +class ManagedIdentityServiceClientCredentialsFactory(ServiceClientCredentialsFactory): + def __init__(self, app_id: str = None, *, logger: Logger = None) -> None: + self.app_id = app_id + self._logger = logger + + async def is_valid_app_id(self, app_id: str) -> bool: + return app_id == self.app_id + + async def is_authentication_disabled(self) -> bool: + return not self.app_id + + async def create_credentials( + self, + app_id: str, + oauth_scope: str, + login_endpoint: str, + validate_authority: bool, + ) -> Authentication: + if await self.is_authentication_disabled(): + return MicrosoftAppCredentials.empty() + + if not await self.is_valid_app_id(app_id): + raise Exception("Invalid app_id") + + credentials = ManagedIdentityAppCredentials(app_id, oauth_scope) + + return credentials diff --git a/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py b/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py index d625d6ede..523977b08 100644 --- a/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py +++ b/libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py @@ -3,7 +3,6 @@ from abc import ABC -import requests from msal import ConfidentialClientApplication from .app_credentials import AppCredentials @@ -14,9 +13,6 @@ class MicrosoftAppCredentials(AppCredentials, ABC): AppCredentials implementation using application ID and password. """ - MICROSOFT_APP_ID = "MicrosoftAppId" - MICROSOFT_PASSWORD = "MicrosoftPassword" - def __init__( self, app_id: str, @@ -34,13 +30,6 @@ def __init__( self.microsoft_app_password = password self.app = None - # This check likely needs to be more nuanced than this. Assuming - # "/.default" precludes other valid suffixes - scope = self.oauth_scope - if oauth_scope and not scope.endswith("/.default"): - scope += "/.default" - self.scopes = [scope] - @staticmethod def empty(): return MicrosoftAppCredentials("", "") @@ -51,18 +40,29 @@ def get_access_token(self, force_refresh: bool = False) -> str: :return: The access token for the given app id and password. """ + scope = self.oauth_scope + if not scope.endswith("/.default"): + scope += "/.default" + scopes = [scope] + # Firstly, looks up a token from cache # Since we are looking for token for the current app, NOT for an end user, # notice we give account parameter as None. - auth_token = self.__get_msal_app().acquire_token_silent( - self.scopes, account=None - ) + auth_token = self.__get_msal_app().acquire_token_silent(scopes, account=None) if not auth_token: # No suitable token exists in cache. Let's get a new one from AAD. - auth_token = self.__get_msal_app().acquire_token_for_client( - scopes=self.scopes - ) - return auth_token["access_token"] + auth_token = self.__get_msal_app().acquire_token_for_client(scopes=scopes) + if "access_token" in auth_token: + return auth_token["access_token"] + error = auth_token["error"] if "error" in auth_token else "Unknown error" + error_description = ( + auth_token["error_description"] + if "error_description" in auth_token + else "Unknown error description" + ) + raise PermissionError( + f"Failed to get access token with error: {error}, error_description: {error_description}" + ) def __get_msal_app(self): if not self.app: @@ -73,11 +73,3 @@ def __get_msal_app(self): ) return self.app - - def _should_authorize(self, session: requests.Session) -> bool: - """ - Override of AppCredentials._should_authorize - :param session: - :return: - """ - return self.microsoft_app_id and self.microsoft_app_password diff --git a/libraries/botframework-connector/botframework/connector/auth/microsoft_government_app_credentials.py b/libraries/botframework-connector/botframework/connector/auth/microsoft_government_app_credentials.py index 17403b414..a2d9a6f1e 100644 --- a/libraries/botframework-connector/botframework/connector/auth/microsoft_government_app_credentials.py +++ b/libraries/botframework-connector/botframework/connector/auth/microsoft_government_app_credentials.py @@ -1,7 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from botframework.connector.auth import MicrosoftAppCredentials, GovernmentConstants +from .microsoft_app_credentials import MicrosoftAppCredentials +from .government_constants import GovernmentConstants class MicrosoftGovernmentAppCredentials(MicrosoftAppCredentials): @@ -14,11 +15,24 @@ def __init__( app_id: str, app_password: str, channel_auth_tenant: str = None, - scope: str = GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE, + scope: str = None, ): - super().__init__(app_id, app_password, channel_auth_tenant, scope) - self.oauth_endpoint = GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL + super().__init__( + app_id, + app_password, + channel_auth_tenant, + scope, + ) @staticmethod def empty(): return MicrosoftGovernmentAppCredentials("", "") + + def _get_default_channelauth_tenant(self) -> str: + return GovernmentConstants.DEFAULT_CHANNEL_AUTH_TENANT + + def _get_to_channel_from_bot_loginurl_prefix(self) -> str: + return GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + + def _get_to_channel_from_bot_oauthscope(self) -> str: + return GovernmentConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE diff --git a/libraries/botframework-connector/botframework/connector/auth/password_service_client_credential_factory.py b/libraries/botframework-connector/botframework/connector/auth/password_service_client_credential_factory.py new file mode 100644 index 000000000..a8ff069d2 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/password_service_client_credential_factory.py @@ -0,0 +1,98 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from logging import Logger + +from msrest.authentication import Authentication + +from .authentication_constants import AuthenticationConstants +from .government_constants import GovernmentConstants +from .microsoft_app_credentials import MicrosoftAppCredentials +from .microsoft_government_app_credentials import MicrosoftGovernmentAppCredentials +from .service_client_credentials_factory import ServiceClientCredentialsFactory + + +class PasswordServiceClientCredentialFactory(ServiceClientCredentialsFactory): + def __init__( + self, + app_id: str = None, + password: str = None, + tenant_id: str = None, + *, + logger: Logger = None + ) -> None: + self.app_id = app_id + self.password = password + self.tenant_id = tenant_id + self._logger = logger + + async def is_valid_app_id(self, app_id: str) -> bool: + return app_id == self.app_id + + async def is_authentication_disabled(self) -> bool: + return not self.app_id + + async def create_credentials( + self, + app_id: str, + oauth_scope: str, + login_endpoint: str, + validate_authority: bool, + ) -> Authentication: + if await self.is_authentication_disabled(): + return MicrosoftAppCredentials.empty() + + if not await self.is_valid_app_id(app_id): + raise Exception("Invalid app_id") + + credentials: MicrosoftAppCredentials + normalized_endpoint = login_endpoint.lower() if login_endpoint else "" + + if normalized_endpoint.startswith( + AuthenticationConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + ): + credentials = MicrosoftAppCredentials( + app_id, self.password, self.tenant_id, oauth_scope + ) + elif normalized_endpoint.startswith( + GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + ): + credentials = MicrosoftGovernmentAppCredentials( + app_id, + self.password, + self.tenant_id, + oauth_scope, + ) + else: + credentials = _PrivateCloudAppCredentials( + app_id, + self.password, + self.tenant_id, + oauth_scope, + login_endpoint, + validate_authority, + ) + + return credentials + + +class _PrivateCloudAppCredentials(MicrosoftAppCredentials): + def __init__( + self, + app_id: str, + password: str, + tenant_id: str, + oauth_scope: str, + oauth_endpoint: str, + validate_authority: bool, + ): + super().__init__( + app_id, password, channel_auth_tenant=tenant_id, oauth_scope=oauth_scope + ) + + self.oauth_endpoint = oauth_endpoint + self._validate_authority = validate_authority + + @property + def validate_authority(self): + return self._validate_authority diff --git a/libraries/botframework-connector/botframework/connector/auth/service_client_credentials_factory.py b/libraries/botframework-connector/botframework/connector/auth/service_client_credentials_factory.py new file mode 100644 index 000000000..cbd008beb --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/service_client_credentials_factory.py @@ -0,0 +1,46 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod + +from .app_credentials import AppCredentials + + +class ServiceClientCredentialsFactory(ABC): + @abstractmethod + async def is_valid_app_id(self, app_id: str) -> bool: + """ + Validates an app ID. + + :param app_id: The app ID to validate. + :returns: The result is true if `app_id` is valid for the controller; otherwise, false. + """ + raise NotImplementedError() + + @abstractmethod + async def is_authentication_disabled(self) -> bool: + """ + Checks whether bot authentication is disabled. + + :returns: If bot authentication is disabled, the result is true; otherwise, false. + """ + raise NotImplementedError() + + @abstractmethod + async def create_credentials( + self, + app_id: str, + oauth_scope: str, + login_endpoint: str, + validate_authority: bool, + ) -> AppCredentials: + """ + A factory method for creating AppCredentials. + + :param app_id: The appId. + :param audience: The audience. + :param login_endpoint: The login url. + :param validate_authority: The validate authority value to use. + :returns: An AppCredentials object. + """ + raise NotImplementedError() diff --git a/libraries/botframework-connector/botframework/connector/auth/skill_validation.py b/libraries/botframework-connector/botframework/connector/auth/skill_validation.py index a9028b34e..8c35f1b0a 100644 --- a/libraries/botframework-connector/botframework/connector/auth/skill_validation.py +++ b/libraries/botframework-connector/botframework/connector/auth/skill_validation.py @@ -24,20 +24,6 @@ class SkillValidation: Validates JWT tokens sent to and from a Skill. """ - _token_validation_parameters = VerifyOptions( - issuer=[ - "https://sts.windows.net/d6d49420-f39b-4df7-a1dc-d59a935871db/", # Auth v3.1, 1.0 token - "https://login.microsoftonline.com/d6d49420-f39b-4df7-a1dc-d59a935871db/v2.0", # Auth v3.1, 2.0 token - "https://sts.windows.net/f8cdef31-a31e-4b4a-93e4-5f571e91255a/", # Auth v3.2, 1.0 token - "https://login.microsoftonline.com/f8cdef31-a31e-4b4a-93e4-5f571e91255a/v2.0", # Auth v3.2, 2.0 token - "https://sts.windows.net/cab8a31a-1906-4287-a0d8-4eef66b95f6e/", # Auth for US Gov, 1.0 token - "https://login.microsoftonline.us/cab8a31a-1906-4287-a0d8-4eef66b95f6e/v2.0", # Auth for US Gov, 2.0 token - ], - audience=None, - clock_tolerance=timedelta(minutes=5), - ignore_expiration=False, - ) - @staticmethod def is_skill_token(auth_header: str) -> bool: """ @@ -53,7 +39,7 @@ def is_skill_token(auth_header: str) -> bool: bearer_token = auth_header.split(" ")[1] # Parse the Big Long String into an actual token. - token = jwt.decode(bearer_token, verify=False) + token = jwt.decode(bearer_token, options={"verify_signature": False}) return SkillValidation.is_skill_claim(token) @staticmethod @@ -63,6 +49,12 @@ def is_skill_claim(claims: Dict[str, object]) -> bool: :param claims: A dict of claims. :return bool: """ + if ( + claims.get(AuthenticationConstants.APP_ID_CLAIM, None) + == AuthenticationConstants.ANONYMOUS_SKILL_APP_ID + ): + return True + if AuthenticationConstants.VERSION_CLAIM not in claims: return False @@ -106,13 +98,34 @@ async def authenticate_channel_token( is_gov = JwtTokenValidation.is_government(channel_service_or_provider) open_id_metadata_url = ( - GovernmentConstants.TO_BOT_FROM_EMULATOR_OPEN_ID_METADATA_URL + GovernmentConstants.TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL if is_gov - else AuthenticationConstants.TO_BOT_FROM_EMULATOR_OPEN_ID_METADATA_URL + else AuthenticationConstants.TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL ) + token_validation_parameters = VerifyOptions( + issuer=[ + "https://sts.windows.net/d6d49420-f39b-4df7-a1dc-d59a935871db/", # v3.1, 1.0 token + "https://login.microsoftonline.com/d6d49420-f39b-4df7-a1dc-d59a935871db/v2.0", # v3.1, 2.0 token + "https://sts.windows.net/f8cdef31-a31e-4b4a-93e4-5f571e91255a/", # v3.2, 1.0 token + "https://login.microsoftonline.com/f8cdef31-a31e-4b4a-93e4-5f571e91255a/v2.0", # v3.2, 2.0 token + "https://sts.windows.net/cab8a31a-1906-4287-a0d8-4eef66b95f6e/", # US Gov, 1.0 token + "https://login.microsoftonline.us/cab8a31a-1906-4287-a0d8-4eef66b95f6e/v2.0", # US Gov, 2.0 token + "https://login.microsoftonline.us/f8cdef31-a31e-4b4a-93e4-5f571e91255a/", # US Gov, 1.0 token + "https://login.microsoftonline.us/f8cdef31-a31e-4b4a-93e4-5f571e91255a/v2.0", # US Gov, 2.0 token + ], + audience=None, + clock_tolerance=timedelta(minutes=5), + ignore_expiration=False, + ) + + if auth_configuration.valid_token_issuers: + token_validation_parameters.issuer.extend( + auth_configuration.valid_token_issuers + ) + token_extractor = JwtTokenExtractor( - SkillValidation._token_validation_parameters, + token_validation_parameters, open_id_metadata_url, AuthenticationConstants.ALLOWED_SIGNING_ALGORITHMS, ) @@ -124,6 +137,20 @@ async def authenticate_channel_token( return identity + @staticmethod + def create_anonymous_skill_claim(): + """ + Creates a ClaimsIdentity for an anonymous (unauthenticated) skill. + :return ClaimsIdentity: + """ + return ClaimsIdentity( + { + AuthenticationConstants.APP_ID_CLAIM: AuthenticationConstants.ANONYMOUS_SKILL_APP_ID + }, + True, + AuthenticationConstants.ANONYMOUS_AUTH_TYPE, + ) + @staticmethod async def _validate_identity( identity: ClaimsIdentity, credentials: CredentialProvider diff --git a/libraries/botframework-connector/botframework/connector/auth/user_token_client.py b/libraries/botframework-connector/botframework/connector/auth/user_token_client.py new file mode 100644 index 000000000..01911ef91 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/auth/user_token_client.py @@ -0,0 +1,143 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod +from base64 import b64encode +from json import dumps +from typing import Dict, List + +from botbuilder.schema import Activity, TokenResponse, TokenExchangeState + +from botframework.connector.token_api.models import ( + SignInUrlResponse, + TokenExchangeRequest, + TokenStatus, +) + + +class UserTokenClient(ABC): + @abstractmethod + async def get_user_token( + self, user_id: str, connection_name: str, channel_id: str, magic_code: str + ) -> TokenResponse: + """ + Attempts to retrieve the token for a user that's in a login flow. + + :param user_id: The user id that will be associated with the token. + :param connection_name: Name of the auth connection to use. + :param channel_id: The channel Id that will be associated with the token. + :param magic_code: (Optional) Optional user entered code to validate. + :return: A TokenResponse object. + """ + raise NotImplementedError() + + @abstractmethod + async def get_sign_in_resource( + self, connection_name: str, activity: Activity, final_redirect: str + ) -> SignInUrlResponse: + """ + Get the raw signin link to be sent to the user for signin for a connection name. + + :param connection_name: Name of the auth connection to use. + :param activity: The Activity from which to derive the token exchange state. + :param final_redirect: The final URL that the OAuth flow will redirect to. + :return: A SignInUrlResponse. + """ + raise NotImplementedError() + + @abstractmethod + async def sign_out_user(self, user_id: str, connection_name: str, channel_id: str): + """ + Signs the user out with the token server. + + :param user_id: The user id that will be associated with the token. + :param connection_name: Name of the auth connection to use. + :param channel_id: The channel Id that will be associated with the token. + """ + raise NotImplementedError() + + @abstractmethod + async def get_token_status( + self, user_id: str, channel_id: str, include_filter: str + ) -> List[TokenStatus]: + """ + Retrieves the token status for each configured connection for the given user. + + :param user_id: The user id that will be associated with the token. + :param channel_id: The channel Id that will be associated with the token. + :param include_filter: The include filter. + :return: A list of TokenStatus objects. + """ + raise NotImplementedError() + + @abstractmethod + async def get_aad_tokens( + self, + user_id: str, + connection_name: str, + resource_urls: List[str], + channel_id: str, + ) -> Dict[str, TokenResponse]: + """ + Retrieves Azure Active Directory tokens for particular resources on a configured connection. + + :param user_id: The user id that will be associated with the token. + :param connection_name: Name of the auth connection to use. + :param resource_urls: The list of resource URLs to retrieve tokens for. + :param channel_id: The channel Id that will be associated with the token. + :return: A Dictionary of resource_urls to the corresponding TokenResponse. + """ + raise NotImplementedError() + + @abstractmethod + async def exchange_token( + self, + user_id: str, + connection_name: str, + channel_id: str, + exchange_request: TokenExchangeRequest, + ) -> TokenResponse: + """ + Performs a token exchange operation such as for single sign-on. + + :param user_id The user id that will be associated with the token. + :param connection_name Name of the auth connection to use. + :param channel_id The channel Id that will be associated with the token. + :param exchange_request The exchange request details, either a token to exchange or a uri to exchange. + :return: A TokenResponse object. + """ + raise NotImplementedError() + + @staticmethod + def create_token_exchange_state( + app_id: str, connection_name: str, activity: Activity + ) -> str: + """ + Helper function to create the Base64 encoded token exchange state used in getSignInResource calls. + + :param app_id The app_id to include in the token exchange state. + :param connection_name The connection_name to include in the token exchange state. + :param activity The [Activity](xref:botframework-schema.Activity) from which to derive the token exchange state. + :return: Base64 encoded token exchange state. + """ + if app_id is None or not isinstance(app_id, str): + raise TypeError("app_id") + if connection_name is None or not isinstance(connection_name, str): + raise TypeError("connection_name") + if activity is None or not isinstance(activity, Activity): + raise TypeError("activity") + + token_exchange_state = TokenExchangeState( + connection_name=connection_name, + conversation=Activity.get_conversation_reference(activity), + relates_to=activity.relates_to, + ms_app_id=app_id, + ) + + tes_string = b64encode( + dumps(token_exchange_state.serialize()).encode( + encoding="UTF-8", errors="strict" + ) + ).decode() + + return tes_string diff --git a/libraries/botframework-connector/botframework/connector/bot_framework_sdk_client_async.py b/libraries/botframework-connector/botframework/connector/bot_framework_sdk_client_async.py new file mode 100644 index 000000000..9efb15b7d --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/bot_framework_sdk_client_async.py @@ -0,0 +1,40 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Optional, Type + +from msrest.async_client import SDKClientAsync +from msrest.universal_http.async_abc import AsyncHTTPSender as AsyncHttpDriver +from msrest.pipeline import AsyncPipeline +from msrest.pipeline.aiohttp import AsyncHTTPSender + + +from ._configuration import ConnectorClientConfiguration + + +class BotFrameworkConnectorConfiguration(ConnectorClientConfiguration): + def __init__( + self, + credentials, + base_url: str, + *, + pipeline_type: Optional[Type[AsyncPipeline]] = None, + sender: Optional[AsyncHTTPSender] = None, + driver: Optional[AsyncHttpDriver] = None + ): + super().__init__(credentials, base_url) + + # The overwrite hierarchy should be well documented + self.sender = sender + self.driver = driver + + self.custom_pipeline = pipeline_type(self) if pipeline_type else None + + +class BotFrameworkSDKClientAsync(SDKClientAsync): + def __init__(self, config: BotFrameworkConnectorConfiguration) -> None: + super().__init__(config) + + self._client.config.pipeline = ( + config.custom_pipeline or self._client.config.pipeline + ) diff --git a/libraries/botframework-connector/botframework/connector/channels.py b/libraries/botframework-connector/botframework/connector/channels.py index be59cc5f5..569596b9c 100644 --- a/libraries/botframework-connector/botframework/connector/channels.py +++ b/libraries/botframework-connector/botframework/connector/channels.py @@ -18,6 +18,9 @@ class Channels(str, Enum): direct_line = "directline" """Direct Line channel.""" + direct_line_speech = "directlinespeech" + """Direct Line Speech channel.""" + email = "email" """Email channel.""" @@ -54,5 +57,8 @@ class Channels(str, Enum): telegram = "telegram" """Telegram channel.""" + test = "test" + """Test channel.""" + webchat = "webchat" """WebChat channel.""" diff --git a/libraries/botframework-connector/botframework/connector/connector_client.py b/libraries/botframework-connector/botframework/connector/connector_client.py index ab88ac9ae..1a0c2947c 100644 --- a/libraries/botframework-connector/botframework/connector/connector_client.py +++ b/libraries/botframework-connector/botframework/connector/connector_client.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.service_client import SDKClient @@ -50,7 +46,6 @@ class ConnectorClient(SDKClient): """ def __init__(self, credentials, base_url=None): - self.config = ConnectorClientConfiguration(credentials, base_url) super(ConnectorClient, self).__init__(self.config.credentials, self.config) diff --git a/libraries/botframework-connector/botframework/connector/http_client_base.py b/libraries/botframework-connector/botframework/connector/http_client_base.py new file mode 100644 index 000000000..501352819 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/http_client_base.py @@ -0,0 +1,13 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod + +from .http_request import HttpRequest +from .http_response_base import HttpResponseBase + + +class HttpClientBase(ABC): + @abstractmethod + async def post(self, *, request: HttpRequest) -> HttpResponseBase: + raise NotImplementedError() diff --git a/libraries/botframework-connector/botframework/connector/http_client_factory.py b/libraries/botframework-connector/botframework/connector/http_client_factory.py new file mode 100644 index 000000000..a5311b424 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/http_client_factory.py @@ -0,0 +1,9 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .http_client_base import HttpClientBase + + +class HttpClientFactory: + def create_client(self) -> HttpClientBase: + pass diff --git a/libraries/botframework-connector/botframework/connector/http_request.py b/libraries/botframework-connector/botframework/connector/http_request.py new file mode 100644 index 000000000..de9f2db6b --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/http_request.py @@ -0,0 +1,17 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Any, Dict + + +class HttpRequest: + def __init__( + self, + *, + request_uri: str = None, + content: Any = None, + headers: Dict[str, str] = None + ) -> None: + self.request_uri = request_uri + self.content = content + self.headers = headers diff --git a/libraries/botframework-connector/botframework/connector/http_response_base.py b/libraries/botframework-connector/botframework/connector/http_response_base.py new file mode 100644 index 000000000..27db7e1f6 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/http_response_base.py @@ -0,0 +1,21 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod +from http import HTTPStatus +from typing import Union + + +class HttpResponseBase(ABC): + @property + @abstractmethod + def status_code(self) -> Union[HTTPStatus, int]: + raise NotImplementedError() + + @abstractmethod + async def is_succesful(self) -> bool: + raise NotImplementedError() + + @abstractmethod + async def read_content_str(self) -> str: + raise NotImplementedError() diff --git a/libraries/botframework-connector/botframework/connector/models/__init__.py b/libraries/botframework-connector/botframework/connector/models/__init__.py index c03adc0f5..54eea3e77 100644 --- a/libraries/botframework-connector/botframework/connector/models/__init__.py +++ b/libraries/botframework-connector/botframework/connector/models/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from botbuilder.schema import * diff --git a/libraries/botframework-connector/botframework/connector/operations/__init__.py b/libraries/botframework-connector/botframework/connector/operations/__init__.py index b2bc000ca..2476fcd20 100644 --- a/libraries/botframework-connector/botframework/connector/operations/__init__.py +++ b/libraries/botframework-connector/botframework/connector/operations/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from ._attachments_operations import AttachmentsOperations diff --git a/libraries/botframework-connector/botframework/connector/operations/_attachments_operations.py b/libraries/botframework-connector/botframework/connector/operations/_attachments_operations.py index 03cce075d..1f3b2f7c3 100644 --- a/libraries/botframework-connector/botframework/connector/operations/_attachments_operations.py +++ b/libraries/botframework-connector/botframework/connector/operations/_attachments_operations.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.pipeline import ClientRawResponse @@ -30,7 +26,6 @@ class AttachmentsOperations: models = models def __init__(self, client, config, serializer, deserializer): - self._client = client self._serialize = serializer self._deserialize = deserializer diff --git a/libraries/botframework-connector/botframework/connector/operations/_conversations_operations.py b/libraries/botframework-connector/botframework/connector/operations/_conversations_operations.py index 5fab0cc22..48d3c23fc 100644 --- a/libraries/botframework-connector/botframework/connector/operations/_conversations_operations.py +++ b/libraries/botframework-connector/botframework/connector/operations/_conversations_operations.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.pipeline import ClientRawResponse @@ -31,7 +27,6 @@ class ConversationsOperations: models = models def __init__(self, client, config, serializer, deserializer): - self._client = client self._serialize = serializer self._deserialize = deserializer @@ -500,6 +495,7 @@ def reply_to_activity( header_parameters = {} header_parameters["Accept"] = "application/json" header_parameters["Content-Type"] = "application/json; charset=utf-8" + header_parameters["x-ms-conversation-id"] = conversation_id if custom_headers: header_parameters.update(custom_headers) diff --git a/libraries/botframework-connector/botframework/connector/skills/__init__.py b/libraries/botframework-connector/botframework/connector/skills/__init__.py new file mode 100644 index 000000000..5afcccb28 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/skills/__init__.py @@ -0,0 +1,3 @@ +from .bot_framework_client import BotFrameworkClient + +__all__ = ["BotFrameworkClient"] diff --git a/libraries/botframework-connector/botframework/connector/skills/bot_framework_client.py b/libraries/botframework-connector/botframework/connector/skills/bot_framework_client.py new file mode 100644 index 000000000..6917f0109 --- /dev/null +++ b/libraries/botframework-connector/botframework/connector/skills/bot_framework_client.py @@ -0,0 +1,31 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC, abstractmethod + +# TODO: add InvokeResponse to botbuilder-schema or rethink dependencies +from botbuilder.schema import Activity + + +class BotFrameworkClient(ABC): + @abstractmethod + async def post_activity( + self, + from_bot_id: str, + to_bot_id: str, + to_url: str, + service_url: str, + conversation_id: str, + activity: Activity, + ) -> "botbuilder.core.InvokeResponse": + """ + Forwards an activity to a another bot. + + :param from_bot_id: The MicrosoftAppId of the bot sending the activity. + :param to_bot_id: The MicrosoftAppId of the bot receiving the activity. + :param to_url: The URL of the bot receiving the activity. + :param service_url: The callback Url for the skill host. + :param conversation_id: A conversation ID to use for the conversation with the skill. + :param activity: Activity to forward. + """ + raise NotImplementedError() diff --git a/libraries/botframework-connector/botframework/connector/teams/__init__.py b/libraries/botframework-connector/botframework/connector/teams/__init__.py index df0cf0a57..48125ad74 100644 --- a/libraries/botframework-connector/botframework/connector/teams/__init__.py +++ b/libraries/botframework-connector/botframework/connector/teams/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from .teams_connector_client import TeamsConnectorClient diff --git a/libraries/botframework-connector/botframework/connector/teams/operations/__init__.py b/libraries/botframework-connector/botframework/connector/teams/operations/__init__.py index 3e46b2dc2..326ddcf8d 100644 --- a/libraries/botframework-connector/botframework/connector/teams/operations/__init__.py +++ b/libraries/botframework-connector/botframework/connector/teams/operations/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from .teams_operations import TeamsOperations diff --git a/libraries/botframework-connector/botframework/connector/teams/operations/teams_operations.py b/libraries/botframework-connector/botframework/connector/teams/operations/teams_operations.py index e6a2d909d..6e453ae23 100644 --- a/libraries/botframework-connector/botframework/connector/teams/operations/teams_operations.py +++ b/libraries/botframework-connector/botframework/connector/teams/operations/teams_operations.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.pipeline import ClientRawResponse @@ -27,7 +23,6 @@ class TeamsOperations(object): models = models def __init__(self, client, config, serializer, deserializer): - self._client = client self._serialize = serializer self._deserialize = deserializer @@ -145,3 +140,202 @@ def get_team_details( return deserialized get_team_details.metadata = {"url": "/v3/teams/{teamId}"} + + def fetch_participant( + self, + meeting_id: str, + participant_id: str, + tenant_id: str, + custom_headers=None, + raw=False, + **operation_config + ): + """Fetches Teams meeting participant details. + + :param meeting_id: Teams meeting id + :type meeting_id: str + :param participant_id: Teams meeting participant id + :type participant_id: str + :param tenant_id: Teams meeting tenant id + :type tenant_id: str + :param dict custom_headers: headers that will be added to the request + :param bool raw: returns the direct response alongside the + deserialized response + :param operation_config: :ref:`Operation configuration + overrides`. + :return: TeamsMeetingParticipant or ClientRawResponse if raw=true + :rtype: ~botframework.connector.teams.models.TeamsParticipantChannelAccount or + ~msrest.pipeline.ClientRawResponse + :raises: + :class:`HttpOperationError` + """ + + # Construct URL + url = self.fetch_participant.metadata["url"] + path_format_arguments = { + "meetingId": self._serialize.url("meeting_id", meeting_id, "str"), + "participantId": self._serialize.url( + "participant_id", participant_id, "str" + ), + "tenantId": self._serialize.url("tenant_id", tenant_id, "str"), + } + url = self._client.format_url(url, **path_format_arguments) + + # Construct parameters + query_parameters = {} + + # Construct headers + header_parameters = {} + header_parameters["Accept"] = "application/json" + if custom_headers: + header_parameters.update(custom_headers) + + # Construct and send request + request = self._client.get(url, query_parameters, header_parameters) + response = self._client.send(request, stream=False, **operation_config) + + if response.status_code not in [200]: + raise HttpOperationError(self._deserialize, response) + + deserialized = None + + if response.status_code == 200: + deserialized = self._deserialize("TeamsMeetingParticipant", response) + + if raw: + client_raw_response = ClientRawResponse(deserialized, response) + return client_raw_response + + return deserialized + + fetch_participant.metadata = { + "url": "/v1/meetings/{meetingId}/participants/{participantId}?tenantId={tenantId}" + } + + def fetch_meeting( + self, meeting_id: str, custom_headers=None, raw=False, **operation_config + ): + """Fetch meeting information. + + :param meeting_id: Meeting Id, encoded as a BASE64 string. + :type meeting_id: str + :param dict custom_headers: headers that will be added to the request + :param bool raw: returns the direct response alongside the + deserialized response + :param operation_config: :ref:`Operation configuration + overrides`. + :return: MeetingInfo or ClientRawResponse if raw=true + :rtype: ~botframework.connector.teams.models.MeetingInfo or + ~msrest.pipeline.ClientRawResponse + :raises: + :class:`HttpOperationError` + """ + + # Construct URL + url = self.fetch_participant.metadata["url"] + path_format_arguments = { + "meetingId": self._serialize.url("meeting_id", meeting_id, "str") + } + url = self._client.format_url(url, **path_format_arguments) + + # Construct parameters + query_parameters = {} + + # Construct headers + header_parameters = {} + header_parameters["Accept"] = "application/json" + if custom_headers: + header_parameters.update(custom_headers) + + # Construct and send request + request = self._client.get(url, query_parameters, header_parameters) + response = self._client.send(request, stream=False, **operation_config) + + if response.status_code not in [200]: + raise HttpOperationError(self._deserialize, response) + + deserialized = None + + if response.status_code == 200: + deserialized = self._deserialize("MeetingInfo", response) + + if raw: + client_raw_response = ClientRawResponse(deserialized, response) + return client_raw_response + + return deserialized + + fetch_participant.metadata = {"url": "/v1/meetings/{meetingId}"} + + def send_meeting_notification( + self, + meeting_id: str, + notification: models.MeetingNotificationBase, + custom_headers=None, + raw=False, + **operation_config + ): + """Send a teams meeting notification. + + :param meeting_id: Meeting Id, encoded as a BASE64 string. + :type meeting_id: str + :param notification: The notification to send to Teams + :type notification: ~botframework.connector.teams.models.MeetingNotificationBase + :param dict custom_headers: headers that will be added to the request + :param bool raw: returns the direct response alongside the + deserialized response + :param operation_config: :ref:`Operation configuration + overrides`. + :return: MeetingNotificationResponse or ClientRawResponse if raw=true + :rtype: ~botframework.connector.teams.models.MeetingNotificationResponse or + ~msrest.pipeline.ClientRawResponse + :raises: + :class:`HttpOperationError` + """ + + # Construct URL + url = self.send_meeting_notification.metadata["url"] + path_format_arguments = { + "meetingId": self._serialize.url("meeting_id", meeting_id, "str"), + } + url = self._client.format_url(url, **path_format_arguments) + + # Construct parameters + query_parameters = {} + + # Construct headers + header_parameters = {} + header_parameters["Accept"] = "application/json" + header_parameters["Content-Type"] = "application/json; charset=utf-8" + if custom_headers: + header_parameters.update(custom_headers) + + # Construct body + body_content = self._serialize.body(notification, "notification") + + # Construct and send request + request = self._client.post( + url, query_parameters, header_parameters, body_content + ) + response = self._client.send(request, stream=False, **operation_config) + + if response.status_code not in [200, 201, 202]: + raise models.ErrorResponseException(self._deserialize, response) + + deserialized = None + if response.status_code == 200: + deserialized = self._deserialize("MeetingNotificationResponse", response) + if response.status_code == 201: + deserialized = self._deserialize("MeetingNotificationResponse", response) + if response.status_code == 202: + deserialized = self._deserialize("MeetingNotificationResponse", response) + + if raw: + client_raw_response = ClientRawResponse(deserialized, response) + return client_raw_response + + return deserialized + + send_meeting_notification.metadata = { + "url": "/v1/meetings/{meetingId}/notification" + } diff --git a/libraries/botframework-connector/botframework/connector/teams/teams_connector_client.py b/libraries/botframework-connector/botframework/connector/teams/teams_connector_client.py index ccf935032..5e071b091 100644 --- a/libraries/botframework-connector/botframework/connector/teams/teams_connector_client.py +++ b/libraries/botframework-connector/botframework/connector/teams/teams_connector_client.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.service_client import SDKClient @@ -28,7 +24,6 @@ class TeamsConnectorClientConfiguration(Configuration): """ def __init__(self, credentials, base_url=None): - if credentials is None: raise ValueError("Parameter 'credentials' must not be None.") if not base_url: @@ -67,7 +62,6 @@ class TeamsConnectorClient(SDKClient): """ def __init__(self, credentials, base_url=None): - self.config = TeamsConnectorClientConfiguration(credentials, base_url) super(TeamsConnectorClient, self).__init__(self.config.credentials, self.config) diff --git a/libraries/botframework-connector/botframework/connector/teams/version.py b/libraries/botframework-connector/botframework/connector/teams/version.py index e36069e74..059dc8b92 100644 --- a/libraries/botframework-connector/botframework/connector/teams/version.py +++ b/libraries/botframework-connector/botframework/connector/teams/version.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- VERSION = "v3" diff --git a/libraries/botframework-connector/botframework/connector/token_api/__init__.py b/libraries/botframework-connector/botframework/connector/token_api/__init__.py index e15b7c0d4..284737f97 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/__init__.py +++ b/libraries/botframework-connector/botframework/connector/token_api/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from ._configuration import TokenApiClientConfiguration diff --git a/libraries/botframework-connector/botframework/connector/token_api/_configuration.py b/libraries/botframework-connector/botframework/connector/token_api/_configuration.py index ff26db8d8..28550431e 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/_configuration.py +++ b/libraries/botframework-connector/botframework/connector/token_api/_configuration.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest import Configuration @@ -26,7 +22,6 @@ class TokenApiClientConfiguration(Configuration): """ def __init__(self, credentials, base_url=None): - if credentials is None: raise ValueError("Parameter 'credentials' must not be None.") if not base_url: diff --git a/libraries/botframework-connector/botframework/connector/token_api/_token_api_client.py b/libraries/botframework-connector/botframework/connector/token_api/_token_api_client.py index 863dcb2e5..3aafe6800 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/_token_api_client.py +++ b/libraries/botframework-connector/botframework/connector/token_api/_token_api_client.py @@ -3,18 +3,14 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.service_client import SDKClient from msrest import Serializer, Deserializer from ._configuration import TokenApiClientConfiguration -from .operations import BotSignInOperations -from .operations import UserTokenOperations +from .operations._bot_sign_in_operations import BotSignInOperations +from .operations._user_token_operations import UserTokenOperations from . import models @@ -36,7 +32,6 @@ class TokenApiClient(SDKClient): """ def __init__(self, credentials, base_url=None): - self.config = TokenApiClientConfiguration(credentials, base_url) super(TokenApiClient, self).__init__(self.config.credentials, self.config) diff --git a/libraries/botframework-connector/botframework/connector/token_api/aio/__init__.py b/libraries/botframework-connector/botframework/connector/token_api/aio/__init__.py index 967abe5f8..eb69ef863 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/aio/__init__.py +++ b/libraries/botframework-connector/botframework/connector/token_api/aio/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from ._token_api_client_async import TokenApiClient diff --git a/libraries/botframework-connector/botframework/connector/token_api/aio/_token_api_client_async.py b/libraries/botframework-connector/botframework/connector/token_api/aio/_token_api_client_async.py index a72fed429..bd6e70305 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/aio/_token_api_client_async.py +++ b/libraries/botframework-connector/botframework/connector/token_api/aio/_token_api_client_async.py @@ -3,18 +3,14 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.async_client import SDKClientAsync from msrest import Serializer, Deserializer from .._configuration import TokenApiClientConfiguration -from .operations_async import BotSignInOperations -from .operations_async import UserTokenOperations +from .operations_async._bot_sign_in_operations_async import BotSignInOperations +from .operations_async._user_token_operations_async import UserTokenOperations from .. import models @@ -36,7 +32,6 @@ class TokenApiClient(SDKClientAsync): """ def __init__(self, credentials, base_url=None): - self.config = TokenApiClientConfiguration(credentials, base_url) super(TokenApiClient, self).__init__(self.config) diff --git a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/__init__.py b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/__init__.py index 0c30a7ed3..8194c77fd 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/__init__.py +++ b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from ._bot_sign_in_operations_async import BotSignInOperations diff --git a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_bot_sign_in_operations_async.py b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_bot_sign_in_operations_async.py index 8798b13e1..bd5eb294b 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_bot_sign_in_operations_async.py +++ b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_bot_sign_in_operations_async.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.pipeline import ClientRawResponse @@ -31,7 +27,6 @@ class BotSignInOperations: models = models def __init__(self, client, config, serializer, deserializer) -> None: - self._client = client self._serialize = serializer self._deserialize = deserializer diff --git a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_user_token_operations_async.py b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_user_token_operations_async.py index b4fda1b37..f18b84d7f 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_user_token_operations_async.py +++ b/libraries/botframework-connector/botframework/connector/token_api/aio/operations_async/_user_token_operations_async.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.pipeline import ClientRawResponse @@ -30,7 +26,6 @@ class UserTokenOperations: models = models def __init__(self, client, config, serializer, deserializer) -> None: - self._client = client self._serialize = serializer self._deserialize = deserializer diff --git a/libraries/botframework-connector/botframework/connector/token_api/models/__init__.py b/libraries/botframework-connector/botframework/connector/token_api/models/__init__.py index be368b2f2..0f1f158da 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/models/__init__.py +++ b/libraries/botframework-connector/botframework/connector/token_api/models/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- try: @@ -17,6 +13,7 @@ from ._models_py3 import SignInUrlResponse from ._models_py3 import TokenExchangeRequest from ._models_py3 import TokenExchangeResource + from ._models_py3 import TokenPostResource from ._models_py3 import TokenResponse from ._models_py3 import TokenStatus except (SyntaxError, ImportError): @@ -27,6 +24,7 @@ from ._models import SignInUrlResponse from ._models import TokenExchangeRequest from ._models import TokenExchangeResource + from ._models import TokenPostResource from ._models import TokenResponse from ._models import TokenStatus @@ -39,6 +37,7 @@ "SignInUrlResponse", "TokenExchangeRequest", "TokenExchangeResource", + "TokenPostResource", "TokenResponse", "TokenStatus", ] diff --git a/libraries/botframework-connector/botframework/connector/token_api/models/_models.py b/libraries/botframework-connector/botframework/connector/token_api/models/_models.py index e100013a7..8b526324a 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/models/_models.py +++ b/libraries/botframework-connector/botframework/connector/token_api/models/_models.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.serialization import Model @@ -75,7 +71,6 @@ class ErrorResponseException(HttpOperationError): """ def __init__(self, deserialize, response, *args): - super(ErrorResponseException, self).__init__( deserialize, response, "ErrorResponse", *args ) @@ -109,6 +104,9 @@ class SignInUrlResponse(Model): :param token_exchange_resource: :type token_exchange_resource: ~botframework.tokenapi.models.TokenExchangeResource + :param token_post_resource: + :type token_post_resource: + ~botframework.tokenapi.models.TokenPostResource """ _attribute_map = { @@ -117,12 +115,17 @@ class SignInUrlResponse(Model): "key": "tokenExchangeResource", "type": "TokenExchangeResource", }, + "token_post_resource": { + "key": "tokenPostResource", + "type": "TokenPostResource", + }, } def __init__(self, **kwargs): super(SignInUrlResponse, self).__init__(**kwargs) self.sign_in_link = kwargs.get("sign_in_link", None) self.token_exchange_resource = kwargs.get("token_exchange_resource", None) + self.token_exchange_resource = kwargs.get("token_post_resource", None) class TokenExchangeRequest(Model): @@ -169,6 +172,22 @@ def __init__(self, **kwargs): self.provider_id = kwargs.get("provider_id", None) +class TokenPostResource(Model): + """TokenPostResource. + + :param sas_url: + :type id: str + """ + + _attribute_map = { + "sas_url": {"key": "sasUrl", "type": "str"}, + } + + def __init__(self, **kwargs): + super(TokenPostResource, self).__init__(**kwargs) + self.sas_url = kwargs.get("sas_url", None) + + class TokenResponse(Model): """TokenResponse. diff --git a/libraries/botframework-connector/botframework/connector/token_api/models/_models_py3.py b/libraries/botframework-connector/botframework/connector/token_api/models/_models_py3.py index bc2602eab..512e85356 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/models/_models_py3.py +++ b/libraries/botframework-connector/botframework/connector/token_api/models/_models_py3.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.serialization import Model @@ -77,7 +73,6 @@ class ErrorResponseException(HttpOperationError): """ def __init__(self, deserialize, response, *args): - super(ErrorResponseException, self).__init__( deserialize, response, "ErrorResponse", *args ) @@ -111,6 +106,9 @@ class SignInUrlResponse(Model): :param token_exchange_resource: :type token_exchange_resource: ~botframework.tokenapi.models.TokenExchangeResource + :param token_post_resource: + :type token_post_resource: + ~botframework.tokenapi.models.TokenPostResource """ _attribute_map = { @@ -119,14 +117,24 @@ class SignInUrlResponse(Model): "key": "tokenExchangeResource", "type": "TokenExchangeResource", }, + "token_post_resource": { + "key": "tokenPostResource", + "type": "TokenPostResource", + }, } def __init__( - self, *, sign_in_link: str = None, token_exchange_resource=None, **kwargs + self, + *, + sign_in_link: str = None, + token_exchange_resource=None, + token_post_resource=None, + **kwargs ) -> None: super(SignInUrlResponse, self).__init__(**kwargs) self.sign_in_link = sign_in_link self.token_exchange_resource = token_exchange_resource + self.token_post_resource = token_post_resource class TokenExchangeRequest(Model): @@ -175,6 +183,22 @@ def __init__( self.provider_id = provider_id +class TokenPostResource(Model): + """TokenPostResource. + + :param sas_url: + :type id: str + """ + + _attribute_map = { + "sas_url": {"key": "sasUrl", "type": "str"}, + } + + def __init__(self, *, sas_url: str = None, **kwargs) -> None: + super(TokenPostResource, self).__init__(**kwargs) + self.sas_url = sas_url + + class TokenResponse(Model): """TokenResponse. diff --git a/libraries/botframework-connector/botframework/connector/token_api/operations/__init__.py b/libraries/botframework-connector/botframework/connector/token_api/operations/__init__.py index d860b4524..76df7af4e 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/operations/__init__.py +++ b/libraries/botframework-connector/botframework/connector/token_api/operations/__init__.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from ._bot_sign_in_operations import BotSignInOperations diff --git a/libraries/botframework-connector/botframework/connector/token_api/operations/_bot_sign_in_operations.py b/libraries/botframework-connector/botframework/connector/token_api/operations/_bot_sign_in_operations.py index a768a3afc..7758e4067 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/operations/_bot_sign_in_operations.py +++ b/libraries/botframework-connector/botframework/connector/token_api/operations/_bot_sign_in_operations.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.pipeline import ClientRawResponse @@ -31,7 +27,6 @@ class BotSignInOperations: models = models def __init__(self, client, config, serializer, deserializer): - self._client = client self._serialize = serializer self._deserialize = deserializer diff --git a/libraries/botframework-connector/botframework/connector/token_api/operations/_user_token_operations.py b/libraries/botframework-connector/botframework/connector/token_api/operations/_user_token_operations.py index 0d0a66ad7..f8b43edb6 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/operations/_user_token_operations.py +++ b/libraries/botframework-connector/botframework/connector/token_api/operations/_user_token_operations.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- from msrest.pipeline import ClientRawResponse @@ -30,7 +26,6 @@ class UserTokenOperations: models = models def __init__(self, client, config, serializer, deserializer): - self._client = client self._serialize = serializer self._deserialize = deserializer diff --git a/libraries/botframework-connector/botframework/connector/token_api/version.py b/libraries/botframework-connector/botframework/connector/token_api/version.py index c184fa4a9..1ca57ef7f 100644 --- a/libraries/botframework-connector/botframework/connector/token_api/version.py +++ b/libraries/botframework-connector/botframework/connector/token_api/version.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- VERSION = "token" diff --git a/libraries/botframework-connector/botframework/connector/version.py b/libraries/botframework-connector/botframework/connector/version.py index e36069e74..059dc8b92 100644 --- a/libraries/botframework-connector/botframework/connector/version.py +++ b/libraries/botframework-connector/botframework/connector/version.py @@ -3,10 +3,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -# -# Code generated by Microsoft (R) AutoRest Code Generator. -# Changes may cause incorrect behavior and will be lost if the code is -# regenerated. # -------------------------------------------------------------------------- VERSION = "v3" diff --git a/libraries/botframework-connector/requirements.txt b/libraries/botframework-connector/requirements.txt index 7358f97e1..515030672 100644 --- a/libraries/botframework-connector/requirements.txt +++ b/libraries/botframework-connector/requirements.txt @@ -1,6 +1,6 @@ -msrest==0.6.10 -botbuilder-schema>=4.7.1 -requests==2.22.0 -PyJWT==1.5.3 -cryptography==2.8.0 -msal==1.1.0 +msrest==0.7.* +botbuilder-schema==4.17.0 +requests==2.32.0 +PyJWT==2.4.0 +cryptography==43.0.1 +msal>=1.31.1 diff --git a/libraries/botframework-connector/setup.py b/libraries/botframework-connector/setup.py index 537d6f60b..1bfc05d49 100644 --- a/libraries/botframework-connector/setup.py +++ b/libraries/botframework-connector/setup.py @@ -1,18 +1,17 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + import os from setuptools import setup NAME = "botframework-connector" -VERSION = os.environ["packageVersion"] if "packageVersion" in os.environ else "4.7.1" +VERSION = os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" REQUIRES = [ - "msrest==0.6.10", - "requests==2.22.0", - "cryptography==2.8.0", - "PyJWT==1.5.3", - "botbuilder-schema>=4.7.1", - "adal==1.2.1", - "msal==1.1.0", + "msrest==0.7.*", + # "requests>=2.23.0,<2.26", + "PyJWT>=2.4.0", + "botbuilder-schema==4.17.0", + "msal>=1.31.1", ] root = os.path.abspath(os.path.dirname(__file__)) @@ -36,10 +35,12 @@ "botframework.connector.models", "botframework.connector.aio", "botframework.connector.aio.operations_async", + "botframework.connector.skills", "botframework.connector.teams", "botframework.connector.teams.operations", "botframework.connector.token_api", "botframework.connector.token_api.aio", + "botframework.connector.token_api.aio.operations_async", "botframework.connector.token_api.models", "botframework.connector.token_api.operations", ], diff --git a/libraries/botframework-connector/tests/requirements.txt b/libraries/botframework-connector/tests/requirements.txt index 0c8169787..6facda892 100644 --- a/libraries/botframework-connector/tests/requirements.txt +++ b/libraries/botframework-connector/tests/requirements.txt @@ -1,5 +1,6 @@ -pytest-cov>=2.6.0 -pytest==5.2.2 -azure-devtools>=0.4.1 -pytest-asyncio==0.10.0 -ddt==1.2.1 \ No newline at end of file +pytest-cov>=5.0.0 +pytest~=8.3.3 +pyyaml==6.0.1 +pytest-asyncio==0.24.0 +ddt==1.2.1 +setuptools==72.1.0 \ No newline at end of file diff --git a/libraries/botframework-connector/tests/test_app_credentials.py b/libraries/botframework-connector/tests/test_app_credentials.py new file mode 100644 index 000000000..d56981e92 --- /dev/null +++ b/libraries/botframework-connector/tests/test_app_credentials.py @@ -0,0 +1,30 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botframework.connector.auth import AppCredentials, AuthenticationConstants + + +class AppCredentialsTests(aiounittest.AsyncTestCase): + @staticmethod + def test_should_not_send_token_for_anonymous(): + # AppID is None + app_creds_none = AppCredentials(app_id=None) + assert app_creds_none.signed_session().headers.get("Authorization") is None + + # AppID is anonymous skill + app_creds_anon = AppCredentials( + app_id=AuthenticationConstants.ANONYMOUS_SKILL_APP_ID + ) + assert app_creds_anon.signed_session().headers.get("Authorization") is None + + +def test_constructor(): + should_default_to_channel_scope = AppCredentials() + assert ( + should_default_to_channel_scope.oauth_scope + == AuthenticationConstants.TO_CHANNEL_FROM_BOT_OAUTH_SCOPE + ) + + should_default_to_custom_scope = AppCredentials(oauth_scope="customScope") + assert should_default_to_custom_scope.oauth_scope == "customScope" diff --git a/libraries/botframework-connector/tests/test_attachments.py b/libraries/botframework-connector/tests/test_attachments.py index d1706d2b3..a4b8b36b8 100644 --- a/libraries/botframework-connector/tests/test_attachments.py +++ b/libraries/botframework-connector/tests/test_attachments.py @@ -5,17 +5,16 @@ import base64 import asyncio import pytest -from azure_devtools.scenario_tests import ReplayableTest import msrest from botbuilder.schema import AttachmentData, ErrorResponseException -from botframework.connector import ConnectorClient +from botframework.connector import ConnectorClient, Channels from botframework.connector.auth import MicrosoftAppCredentials from authentication_stub import MicrosoftTokenAuthenticationStub SERVICE_URL = "https://slack.botframework.com" -CHANNEL_ID = "slack" +CHANNEL_ID = Channels.slack BOT_NAME = "botbuilder-pc-bot" BOT_ID = "B21UTEF8S:T03CWQ0QB" RECIPIENT_ID = "U19KH8EHJ:T03CWQ0QB" @@ -47,13 +46,21 @@ def read_base64(path_to_file): return encoded_string -LOOP = asyncio.get_event_loop() +# Ensure there's an event loop and get the auth token +# LOOP = asyncio.get_event_loop() +try: + LOOP = asyncio.get_running_loop() +except RuntimeError: + LOOP = asyncio.new_event_loop() + asyncio.set_event_loop(LOOP) + +# Run the async function to get the auth token AUTH_TOKEN = LOOP.run_until_complete(get_auth_token()) -class AttachmentsTest(ReplayableTest): - def __init__(self, method_name): # pylint: disable=useless-super-delegation - super(AttachmentsTest, self).__init__(method_name) +class AttachmentsTest: + def __init__(self): # pylint: disable=useless-super-delegation + super(AttachmentsTest, self).__init__() @property def credentials(self): diff --git a/libraries/botframework-connector/tests/test_attachments_async.py b/libraries/botframework-connector/tests/test_attachments_async.py index b60494146..fe0434184 100644 --- a/libraries/botframework-connector/tests/test_attachments_async.py +++ b/libraries/botframework-connector/tests/test_attachments_async.py @@ -5,7 +5,6 @@ import base64 import asyncio import pytest -from azure_devtools.scenario_tests import ReplayableTest import msrest from botbuilder.schema import AttachmentData, ErrorResponseException @@ -13,9 +12,10 @@ from botframework.connector.auth import MicrosoftAppCredentials from authentication_stub import MicrosoftTokenAuthenticationStub +from botframework.connector import Channels SERVICE_URL = "https://slack.botframework.com" -CHANNEL_ID = "slack" +CHANNEL_ID = Channels.slack BOT_NAME = "botbuilder-pc-bot" BOT_ID = "B21UTEF8S:T03CWQ0QB" RECIPIENT_ID = "U19KH8EHJ:T03CWQ0QB" @@ -58,9 +58,9 @@ async def return_sum(attachment_stream): AUTH_TOKEN = LOOP.run_until_complete(get_auth_token()) -class AttachmentsTest(ReplayableTest): - def __init__(self, method_name): - super(AttachmentsTest, self).__init__(method_name) +class AttachmentsTest: + def __init__(self): + super(AttachmentsTest, self).__init__() self.loop = asyncio.get_event_loop() @property diff --git a/libraries/botframework-connector/tests/test_auth.py b/libraries/botframework-connector/tests/test_auth.py index a05b88796..cc3abf66a 100644 --- a/libraries/botframework-connector/tests/test_auth.py +++ b/libraries/botframework-connector/tests/test_auth.py @@ -1,12 +1,14 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + import uuid from typing import Dict, List, Union from unittest.mock import Mock import pytest -from botbuilder.schema import Activity +from botbuilder.schema import Activity, ConversationReference, ChannelAccount, RoleTypes +from botframework.connector import Channels from botframework.connector.auth import ( AuthenticationConfiguration, AuthenticationConstants, @@ -17,10 +19,11 @@ ChannelValidation, ClaimsIdentity, MicrosoftAppCredentials, - GovernmentConstants, + # GovernmentConstants, GovernmentChannelValidation, SimpleChannelProvider, ChannelProvider, + # AppCredentials, ) @@ -56,13 +59,12 @@ class TestAuth: @pytest.mark.asyncio async def test_claims_validation(self): - claims: List[Dict] = [] + claims: List[Dict] = {} default_auth_config = AuthenticationConfiguration() # No validator should pass. await JwtTokenValidation.validate_claims(default_auth_config, claims) - # ClaimsValidator configured but no exception should pass. mock_validator = Mock() auth_with_validator = AuthenticationConfiguration( claims_validator=mock_validator @@ -75,84 +77,112 @@ async def test_claims_validation(self): assert "Invalid claims." in str(excinfo.value) - @pytest.mark.asyncio - async def test_connector_auth_header_correct_app_id_and_service_url_should_validate( - self, - ): - header = ( - "Bearer " - + MicrosoftAppCredentials( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ).get_access_token() - ) - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "" - ) - result = await JwtTokenValidation.validate_auth_header( - header, credentials, "", "https://webchat.botframework.com/" - ) - - result_with_provider = await JwtTokenValidation.validate_auth_header( - header, - credentials, - SimpleChannelProvider(), - "https://webchat.botframework.com/", - ) + # No validator with not skill cliams should pass. + default_auth_config.claims_validator = None + claims: List[Dict] = { + AuthenticationConstants.VERSION_CLAIM: "1.0", + AuthenticationConstants.AUDIENCE_CLAIM: "this_bot_id", + AuthenticationConstants.APP_ID_CLAIM: "this_bot_id", # Skill claims aud!=azp + } - assert result - assert result_with_provider - - @pytest.mark.asyncio - async def test_connector_auth_header_with_different_bot_app_id_should_not_validate( - self, - ): - header = ( - "Bearer " - + MicrosoftAppCredentials( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ).get_access_token() - ) - credentials = SimpleCredentialProvider( - "00000000-0000-0000-0000-000000000000", "" - ) - with pytest.raises(Exception) as excinfo: - await JwtTokenValidation.validate_auth_header( - header, credentials, "", "https://webchat.botframework.com/" - ) - assert "Unauthorized" in str(excinfo.value) + await JwtTokenValidation.validate_claims(default_auth_config, claims) - with pytest.raises(Exception) as excinfo2: - await JwtTokenValidation.validate_auth_header( - header, - credentials, - SimpleChannelProvider(), - "https://webchat.botframework.com/", - ) - assert "Unauthorized" in str(excinfo2.value) + # No validator with skill cliams should fail. + claims: List[Dict] = { + AuthenticationConstants.VERSION_CLAIM: "1.0", + AuthenticationConstants.AUDIENCE_CLAIM: "this_bot_id", + AuthenticationConstants.APP_ID_CLAIM: "not_this_bot_id", # Skill claims aud!=azp + } - @pytest.mark.asyncio - async def test_connector_auth_header_and_no_credential_should_not_validate(self): - header = ( - "Bearer " - + MicrosoftAppCredentials( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ).get_access_token() + mock_validator.side_effect = PermissionError( + "Unauthorized Access. Request is not authorized. Skill Claims require validation." ) - credentials = SimpleCredentialProvider("", "") - with pytest.raises(Exception) as excinfo: - await JwtTokenValidation.validate_auth_header( - header, credentials, "", "https://webchat.botframework.com/" - ) - assert "Unauthorized" in str(excinfo.value) + with pytest.raises(PermissionError) as excinfo_skill: + await JwtTokenValidation.validate_claims(auth_with_validator, claims) - with pytest.raises(Exception) as excinfo2: - await JwtTokenValidation.validate_auth_header( - header, - credentials, - SimpleChannelProvider(), - "https://webchat.botframework.com/", - ) - assert "Unauthorized" in str(excinfo2.value) + assert ( + "Unauthorized Access. Request is not authorized. Skill Claims require validation." + in str(excinfo_skill.value) + ) + + # @pytest.mark.asyncio + # async def test_connector_auth_header_correct_app_id_and_service_url_should_validate( + # self, + # ): + # header = ( + # "Bearer " + # MicrosoftAppCredentials( + # "", "" + # ).get_access_token() + # ) + # credentials = SimpleCredentialProvider( + # "", "" + # ) + # result = await JwtTokenValidation.validate_auth_header( + # header, credentials, "", "https://webchat.botframework.com/" + # ) + # + # result_with_provider = await JwtTokenValidation.validate_auth_header( + # header, + # credentials, + # SimpleChannelProvider(), + # "https://webchat.botframework.com/", + # ) + # + # assert result + # assert result_with_provider + + # @pytest.mark.asyncio + # async def test_connector_auth_header_with_different_bot_app_id_should_not_validate( + # self, + # ): + # header = ( + # "Bearer " + # MicrosoftAppCredentials( + # "", "" + # ).get_access_token() + # ) + # credentials = SimpleCredentialProvider( + # "00000000-0000-0000-0000-000000000000", "" + # ) + # with pytest.raises(Exception) as excinfo: + # await JwtTokenValidation.validate_auth_header( + # header, credentials, "", "https://webchat.botframework.com/" + # ) + # assert "Unauthorized" in str(excinfo.value) + # + # with pytest.raises(Exception) as excinfo2: + # await JwtTokenValidation.validate_auth_header( + # header, + # credentials, + # SimpleChannelProvider(), + # "https://webchat.botframework.com/", + # ) + # assert "Unauthorized" in str(excinfo2.value) + + # @pytest.mark.asyncio + # async def test_connector_auth_header_and_no_credential_should_not_validate(self): + # header = ( + # "Bearer " + # MicrosoftAppCredentials( + # "", "" + # ).get_access_token() + # ) + # credentials = SimpleCredentialProvider("", "") + # with pytest.raises(Exception) as excinfo: + # await JwtTokenValidation.validate_auth_header( + # header, credentials, "", "https://webchat.botframework.com/" + # ) + # assert "Unauthorized" in str(excinfo.value) + # + # with pytest.raises(Exception) as excinfo2: + # await JwtTokenValidation.validate_auth_header( + # header, + # credentials, + # SimpleChannelProvider(), + # "https://webchat.botframework.com/", + # ) + # assert "Unauthorized" in str(excinfo2.value) @pytest.mark.asyncio async def test_empty_header_and_no_credential_should_throw(self): @@ -168,122 +198,110 @@ async def test_empty_header_and_no_credential_should_throw(self): ) assert "auth_header" in str(excinfo2.value) - @pytest.mark.asyncio - async def test_emulator_msa_header_correct_app_id_and_service_url_should_validate( - self, - ): - header = ( - "Bearer " - + MicrosoftAppCredentials( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ).get_access_token() - ) - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "" - ) - result = await JwtTokenValidation.validate_auth_header( - header, credentials, "", "https://webchat.botframework.com/" - ) - - result_with_provider = await JwtTokenValidation.validate_auth_header( - header, - credentials, - SimpleChannelProvider(), - "https://webchat.botframework.com/", - ) - - assert result - assert result_with_provider - - @pytest.mark.asyncio - async def test_emulator_msa_header_and_no_credential_should_not_validate(self): - # pylint: disable=protected-access - header = ( - "Bearer " - + MicrosoftAppCredentials( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ).get_access_token() - ) - credentials = SimpleCredentialProvider( - "00000000-0000-0000-0000-000000000000", "" - ) - with pytest.raises(Exception) as excinfo: - await JwtTokenValidation.validate_auth_header(header, credentials, "", None) - assert "Unauthorized" in str(excinfo._excinfo) - - with pytest.raises(Exception) as excinfo2: - await JwtTokenValidation.validate_auth_header( - header, credentials, SimpleChannelProvider(), None - ) - assert "Unauthorized" in str(excinfo2._excinfo) + # @pytest.mark.asyncio + # async def test_emulator_msa_header_correct_app_id_and_service_url_should_validate( + # self, + # ): + # header = ( + # "Bearer " + # MicrosoftAppCredentials( + # "", "" + # ).get_access_token() + # ) + # credentials = SimpleCredentialProvider( + # "", "" + # ) + # result = await JwtTokenValidation.validate_auth_header( + # header, credentials, "", "https://webchat.botframework.com/" + # ) + # + # result_with_provider = await JwtTokenValidation.validate_auth_header( + # header, + # credentials, + # SimpleChannelProvider(), + # "https://webchat.botframework.com/", + # ) + # + # assert result + # assert result_with_provider + + # @pytest.mark.asyncio + # async def test_emulator_msa_header_and_no_credential_should_not_validate(self): + # # pylint: disable=protected-access + # header = ( + # "Bearer " + # MicrosoftAppCredentials( + # "", "" + # ).get_access_token() + # ) + # credentials = SimpleCredentialProvider( + # "00000000-0000-0000-0000-000000000000", "" + # ) + # with pytest.raises(Exception) as excinfo: + # await JwtTokenValidation.validate_auth_header(header, credentials, "", None) + # assert "Unauthorized" in str(excinfo._excinfo) + # + # with pytest.raises(Exception) as excinfo2: + # await JwtTokenValidation.validate_auth_header( + # header, credentials, SimpleChannelProvider(), None + # ) + # assert "Unauthorized" in str(excinfo2._excinfo) # Tests with a valid Token and service url; and ensures that Service url is added to Trusted service url list. - @pytest.mark.asyncio - async def test_channel_msa_header_valid_service_url_should_be_trusted(self): - activity = Activity( - service_url="https://smba.trafficmanager.net/amer-client-ss.msg/" - ) - header = ( - "Bearer " - + MicrosoftAppCredentials( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ).get_access_token() - ) - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "" - ) - - await JwtTokenValidation.authenticate_request(activity, header, credentials) - - assert MicrosoftAppCredentials.is_trusted_service( - "https://smba.trafficmanager.net/amer-client-ss.msg/" - ) + # @pytest.mark.asyncio + # async def test_channel_msa_header_valid_service_url_should_be_trusted(self): + # activity = Activity( + # service_url="https://smba.trafficmanager.net/amer-client-ss.msg/" + # ) + # header = ( + # "Bearer " + # MicrosoftAppCredentials( + # "", "" + # ).get_access_token() + # ) + # credentials = SimpleCredentialProvider( + # "", "" + # ) + # + # await JwtTokenValidation.authenticate_request(activity, header, credentials) + # + # assert AppCredentials.is_trusted_service( + # "https://smba.trafficmanager.net/amer-client-ss.msg/" + # ) + + # @pytest.mark.asyncio + # # Tests with a valid Token and invalid service url and ensures that Service url is NOT added to + # # Trusted service url list. + # async def test_channel_msa_header_invalid_service_url_should_not_be_trusted(self): + # activity = Activity(service_url="https://webchat.botframework.com/") + # header = ( + # "Bearer " + # MicrosoftAppCredentials( + # "", "" + # ).get_access_token() + # ) + # credentials = SimpleCredentialProvider( + # "7f74513e-6f96-4dbc-be9d-9a81fea22b88", "" + # ) + # + # with pytest.raises(Exception) as excinfo: + # await JwtTokenValidation.authenticate_request(activity, header, credentials) + # assert "Unauthorized" in str(excinfo.value) + # + # assert not MicrosoftAppCredentials.is_trusted_service( + # "https://webchat.botframework.com/" + # ) @pytest.mark.asyncio # Tests with a valid Token and invalid service url and ensures that Service url is NOT added to # Trusted service url list. - async def test_channel_msa_header_invalid_service_url_should_not_be_trusted(self): - activity = Activity(service_url="https://webchat.botframework.com/") - header = ( - "Bearer " - + MicrosoftAppCredentials( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ).get_access_token() - ) - credentials = SimpleCredentialProvider( - "7f74513e-6f96-4dbc-be9d-9a81fea22b88", "" - ) - - with pytest.raises(Exception) as excinfo: - await JwtTokenValidation.authenticate_request(activity, header, credentials) - assert "Unauthorized" in str(excinfo.value) - - assert not MicrosoftAppCredentials.is_trusted_service( - "https://webchat.botframework.com/" - ) - - @pytest.mark.asyncio - async def test_channel_msa_header_from_user_specified_tenant(self): + async def test_channel_authentication_disabled_and_skill_should_be_anonymous(self): activity = Activity( - service_url="https://smba.trafficmanager.net/amer-client-ss.msg/" - ) - header = "Bearer " + MicrosoftAppCredentials( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F", "microsoft.com" - ).get_access_token(True) - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "" - ) - - claims = await JwtTokenValidation.authenticate_request( - activity, header, credentials + channel_id=Channels.emulator, + service_url="https://webchat.botframework.com/", + relates_to=ConversationReference(), + recipient=ChannelAccount(role=RoleTypes.skill), ) - - assert claims.get_claim_value("tid") == "72f988bf-86f1-41af-91ab-2d7cd011db47" - - @pytest.mark.asyncio - # Tests with no authentication header and makes sure the service URL is not added to the trusted list. - async def test_channel_authentication_disabled_should_be_anonymous(self): - activity = Activity(service_url="https://webchat.botframework.com/") header = "" credentials = SimpleCredentialProvider("", "") @@ -291,61 +309,52 @@ async def test_channel_authentication_disabled_should_be_anonymous(self): activity, header, credentials ) - assert claims_principal.is_authenticated - assert not claims_principal.claims + assert ( + claims_principal.authentication_type + == AuthenticationConstants.ANONYMOUS_AUTH_TYPE + ) + assert ( + JwtTokenValidation.get_app_id_from_claims(claims_principal.claims) + == AuthenticationConstants.ANONYMOUS_SKILL_APP_ID + ) + + # @pytest.mark.asyncio + # async def test_channel_msa_header_from_user_specified_tenant(self): + # activity = Activity( + # service_url="https://smba.trafficmanager.net/amer-client-ss.msg/" + # ) + # header = "Bearer " MicrosoftAppCredentials( + # "", "", "microsoft.com" + # ).get_access_token(True) + # credentials = SimpleCredentialProvider( + # "", "" + # ) + # + # claims = await JwtTokenValidation.authenticate_request( + # activity, header, credentials + # ) + # + # assert claims.get_claim_value("tid") == "72f988bf-86f1-41af-91ab-2d7cd011db47" @pytest.mark.asyncio # Tests with no authentication header and makes sure the service URL is not added to the trusted list. - async def test_channel_authentication_disabled_service_url_should_not_be_trusted( - self, - ): + async def test_channel_authentication_disabled_should_be_anonymous(self): activity = Activity(service_url="https://webchat.botframework.com/") header = "" credentials = SimpleCredentialProvider("", "") - await JwtTokenValidation.authenticate_request(activity, header, credentials) - - assert not MicrosoftAppCredentials.is_trusted_service( - "https://webchat.botframework.com/" - ) - - @pytest.mark.asyncio - async def test_emulator_auth_header_correct_app_id_and_service_url_with_gov_channel_service_should_validate( - self, - ): - await jwt_token_validation_validate_auth_header_with_channel_service_succeeds( - "2cd87869-38a0-4182-9251-d056e8f0ac24", # emulator creds - "2.30Vs3VQLKt974F", - GovernmentConstants.CHANNEL_SERVICE, - ) - - await jwt_token_validation_validate_auth_header_with_channel_service_succeeds( - "2cd87869-38a0-4182-9251-d056e8f0ac24", # emulator creds - "2.30Vs3VQLKt974F", - SimpleChannelProvider(GovernmentConstants.CHANNEL_SERVICE), - ) - - @pytest.mark.asyncio - async def test_emulator_auth_header_correct_app_id_and_service_url_with_private_channel_service_should_validate( - self, - ): - await jwt_token_validation_validate_auth_header_with_channel_service_succeeds( - "2cd87869-38a0-4182-9251-d056e8f0ac24", # emulator creds - "2.30Vs3VQLKt974F", - "TheChannel", + claims_principal = await JwtTokenValidation.authenticate_request( + activity, header, credentials ) - await jwt_token_validation_validate_auth_header_with_channel_service_succeeds( - "2cd87869-38a0-4182-9251-d056e8f0ac24", # emulator creds - "2.30Vs3VQLKt974F", - SimpleChannelProvider("TheChannel"), + assert ( + claims_principal.authentication_type + == AuthenticationConstants.ANONYMOUS_AUTH_TYPE ) @pytest.mark.asyncio async def test_government_channel_validation_succeeds(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) + credentials = SimpleCredentialProvider("", "") await GovernmentChannelValidation.validate_identity( ClaimsIdentity( @@ -364,9 +373,7 @@ async def test_government_channel_validation_no_authentication_fails(self): @pytest.mark.asyncio async def test_government_channel_validation_no_issuer_fails(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) + credentials = SimpleCredentialProvider("", "") with pytest.raises(Exception) as excinfo: await GovernmentChannelValidation.validate_identity( ClaimsIdentity({"peanut": "peanut"}, True), credentials @@ -375,32 +382,28 @@ async def test_government_channel_validation_no_issuer_fails(self): @pytest.mark.asyncio async def test_government_channel_validation_wrong_issuer_fails(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) + credentials = SimpleCredentialProvider("", "") with pytest.raises(Exception) as excinfo: await GovernmentChannelValidation.validate_identity( ClaimsIdentity({"iss": "peanut"}, True), credentials ) assert "Unauthorized" in str(excinfo.value) - @pytest.mark.asyncio - async def test_government_channel_validation_no_audience_fails(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) - with pytest.raises(Exception) as excinfo: - await GovernmentChannelValidation.validate_identity( - ClaimsIdentity({"iss": "https://api.botframework.us"}, True), - credentials, - ) - assert "Unauthorized" in str(excinfo.value) + # @pytest.mark.asyncio + # async def test_government_channel_validation_no_audience_fails(self): + # credentials = SimpleCredentialProvider( + # "", "" + # ) + # with pytest.raises(Exception) as excinfo: + # await GovernmentChannelValidation.validate_identity( + # ClaimsIdentity({"iss": "https://api.botframework.us"}, True), + # credentials, + # ) + # assert "Unauthorized" in str(excinfo.value) @pytest.mark.asyncio async def test_government_channel_validation_wrong_audience_fails(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) + credentials = SimpleCredentialProvider("", "") with pytest.raises(Exception) as excinfo: await GovernmentChannelValidation.validate_identity( ClaimsIdentity( @@ -412,9 +415,7 @@ async def test_government_channel_validation_wrong_audience_fails(self): @pytest.mark.asyncio async def test_enterprise_channel_validation_succeeds(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) + credentials = SimpleCredentialProvider("", "") await EnterpriseChannelValidation.validate_identity( ClaimsIdentity( @@ -433,9 +434,7 @@ async def test_enterprise_channel_validation_no_authentication_fails(self): @pytest.mark.asyncio async def test_enterprise_channel_validation_no_issuer_fails(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) + credentials = SimpleCredentialProvider("", "") with pytest.raises(Exception) as excinfo: await EnterpriseChannelValidation.validate_identity( ClaimsIdentity({"peanut": "peanut"}, True), credentials @@ -444,9 +443,7 @@ async def test_enterprise_channel_validation_no_issuer_fails(self): @pytest.mark.asyncio async def test_enterprise_channel_validation_wrong_issuer_fails(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) + credentials = SimpleCredentialProvider("", "") with pytest.raises(Exception) as excinfo: await EnterpriseChannelValidation.validate_identity( ClaimsIdentity({"iss": "peanut"}, True), credentials @@ -455,9 +452,7 @@ async def test_enterprise_channel_validation_wrong_issuer_fails(self): @pytest.mark.asyncio async def test_enterprise_channel_validation_no_audience_fails(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) + credentials = SimpleCredentialProvider("", "") with pytest.raises(Exception) as excinfo: await GovernmentChannelValidation.validate_identity( ClaimsIdentity({"iss": "https://api.botframework.com"}, True), @@ -467,9 +462,7 @@ async def test_enterprise_channel_validation_no_audience_fails(self): @pytest.mark.asyncio async def test_enterprise_channel_validation_wrong_audience_fails(self): - credentials = SimpleCredentialProvider( - "2cd87869-38a0-4182-9251-d056e8f0ac24", "2.30Vs3VQLKt974F" - ) + credentials = SimpleCredentialProvider("", "") with pytest.raises(Exception) as excinfo: await GovernmentChannelValidation.validate_identity( ClaimsIdentity( diff --git a/libraries/botframework-connector/tests/test_certificate_service_client_credential_factory.py b/libraries/botframework-connector/tests/test_certificate_service_client_credential_factory.py new file mode 100644 index 000000000..558397c9f --- /dev/null +++ b/libraries/botframework-connector/tests/test_certificate_service_client_credential_factory.py @@ -0,0 +1,73 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import aiounittest +from botframework.connector.auth import ( + AppCredentials, + AuthenticationConstants, + GovernmentConstants, + CertificateServiceClientCredentialsFactory, + CertificateAppCredentials, + CertificateGovernmentAppCredentials, +) + + +class CertificateServiceClientCredentialsFactoryTests(aiounittest.AsyncTestCase): + test_appid = "test_appid" + test_tenant_id = "test_tenant_id" + test_audience = "test_audience" + login_endpoint = AuthenticationConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + gov_login_endpoint = GovernmentConstants.TO_CHANNEL_FROM_BOT_LOGIN_URL_PREFIX + private_login_endpoint = "https://login.privatecloud.com" + + async def test_can_create_public_credentials(self): + factory = CertificateServiceClientCredentialsFactory( + app_id=CertificateServiceClientCredentialsFactoryTests.test_appid, + certificate_thumbprint="thumbprint", + certificate_private_key="private_key", + ) + + credentials = await factory.create_credentials( + CertificateServiceClientCredentialsFactoryTests.test_appid, + CertificateServiceClientCredentialsFactoryTests.test_audience, + CertificateServiceClientCredentialsFactoryTests.login_endpoint, + True, + ) + + assert isinstance(credentials, CertificateAppCredentials) + + async def test_can_create_gov_credentials(self): + factory = CertificateServiceClientCredentialsFactory( + app_id=CertificateServiceClientCredentialsFactoryTests.test_appid, + certificate_thumbprint="thumbprint", + certificate_private_key="private_key", + ) + + credentials = await factory.create_credentials( + CertificateServiceClientCredentialsFactoryTests.test_appid, + CertificateServiceClientCredentialsFactoryTests.test_audience, + CertificateServiceClientCredentialsFactoryTests.gov_login_endpoint, + True, + ) + + assert isinstance(credentials, CertificateGovernmentAppCredentials) + + async def test_can_create_private_credentials(self): + factory = CertificateServiceClientCredentialsFactory( + app_id=CertificateServiceClientCredentialsFactoryTests.test_appid, + certificate_thumbprint="thumbprint", + certificate_private_key="private_key", + ) + + credentials = await factory.create_credentials( + CertificateServiceClientCredentialsFactoryTests.test_appid, + CertificateServiceClientCredentialsFactoryTests.test_audience, + CertificateServiceClientCredentialsFactoryTests.private_login_endpoint, + True, + ) + + assert isinstance(credentials, CertificateAppCredentials) + assert ( + credentials.oauth_endpoint + == CertificateServiceClientCredentialsFactoryTests.private_login_endpoint + ) diff --git a/libraries/botframework-connector/tests/test_conversations.py b/libraries/botframework-connector/tests/test_conversations.py index badd636d7..ea94a247b 100644 --- a/libraries/botframework-connector/tests/test_conversations.py +++ b/libraries/botframework-connector/tests/test_conversations.py @@ -3,7 +3,6 @@ import asyncio import pytest -from azure_devtools.scenario_tests import ReplayableTest from botbuilder.schema import ( Activity, @@ -16,13 +15,13 @@ ErrorResponseException, HeroCard, ) -from botframework.connector import ConnectorClient +from botframework.connector import ConnectorClient, Channels from botframework.connector.auth import MicrosoftAppCredentials from authentication_stub import MicrosoftTokenAuthenticationStub SERVICE_URL = "https://slack.botframework.com" -CHANNEL_ID = "slack" +CHANNEL_ID = Channels.slack BOT_NAME = "botbuilder-pc-bot" BOT_ID = "B21UTEF8S:T03CWQ0QB" RECIPIENT_ID = "U19KH8EHJ:T03CWQ0QB" @@ -48,9 +47,9 @@ async def get_auth_token(): AUTH_TOKEN = LOOP.run_until_complete(get_auth_token()) -class ConversationTest(ReplayableTest): - def __init__(self, method_name): # pylint: disable=useless-super-delegation - super(ConversationTest, self).__init__(method_name) +class ConversationTest: + def __init__(self): # pylint: disable=useless-super-delegation + super(ConversationTest, self).__init__() @property def credentials(self): diff --git a/libraries/botframework-connector/tests/test_conversations_async.py b/libraries/botframework-connector/tests/test_conversations_async.py index a6ad2242b..5e0c8fcc5 100644 --- a/libraries/botframework-connector/tests/test_conversations_async.py +++ b/libraries/botframework-connector/tests/test_conversations_async.py @@ -3,7 +3,6 @@ import asyncio import pytest -from azure_devtools.scenario_tests import ReplayableTest from botbuilder.schema import ( Activity, @@ -20,9 +19,10 @@ from botframework.connector.auth import MicrosoftAppCredentials from authentication_stub import MicrosoftTokenAuthenticationStub +from botframework.connector import Channels SERVICE_URL = "https://slack.botframework.com" -CHANNEL_ID = "slack" +CHANNEL_ID = Channels.slack BOT_NAME = "botbuilder-pc-bot" BOT_ID = "B21UTEF8S:T03CWQ0QB" RECIPIENT_ID = "U19KH8EHJ:T03CWQ0QB" @@ -48,9 +48,9 @@ async def get_auth_token(): AUTH_TOKEN = LOOP.run_until_complete(get_auth_token()) -class TestAsyncConversation(ReplayableTest): - def __init__(self, method_name): - super(TestAsyncConversation, self).__init__(method_name) +class TestAsyncConversation: + def __init__(self): + super(TestAsyncConversation, self).__init__() self.loop = asyncio.get_event_loop() self.credentials = MicrosoftTokenAuthenticationStub(AUTH_TOKEN) diff --git a/libraries/botframework-connector/tests/test_endorsements_validator.py b/libraries/botframework-connector/tests/test_endorsements_validator.py index 18dee4c31..9d4fad0fa 100644 --- a/libraries/botframework-connector/tests/test_endorsements_validator.py +++ b/libraries/botframework-connector/tests/test_endorsements_validator.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import pytest from botframework.connector.auth import EndorsementsValidator diff --git a/libraries/botframework-connector/tests/test_microsoft_app_credentials.py b/libraries/botframework-connector/tests/test_microsoft_app_credentials.py index f4cb2516d..e1beff8bf 100644 --- a/libraries/botframework-connector/tests/test_microsoft_app_credentials.py +++ b/libraries/botframework-connector/tests/test_microsoft_app_credentials.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import aiounittest from botframework.connector.auth import AuthenticationConstants, MicrosoftAppCredentials diff --git a/libraries/botframework-connector/tests/test_skill_validation.py b/libraries/botframework-connector/tests/test_skill_validation.py index a32625050..bfa4951ce 100644 --- a/libraries/botframework-connector/tests/test_skill_validation.py +++ b/libraries/botframework-connector/tests/test_skill_validation.py @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + import uuid from asyncio import Future from unittest.mock import Mock, DEFAULT @@ -9,6 +12,7 @@ ClaimsIdentity, CredentialProvider, SkillValidation, + JwtTokenValidation, ) @@ -33,9 +37,9 @@ def test_is_skill_claim_test(self): assert not SkillValidation.is_skill_claim(claims) # Emulator Audience claim - claims[ - AuthenticationConstants.AUDIENCE_CLAIM - ] = AuthenticationConstants.TO_BOT_FROM_CHANNEL_TOKEN_ISSUER + claims[AuthenticationConstants.AUDIENCE_CLAIM] = ( + AuthenticationConstants.TO_BOT_FROM_CHANNEL_TOKEN_ISSUER + ) assert not SkillValidation.is_skill_claim(claims) # No AppId claim @@ -47,6 +51,13 @@ def test_is_skill_claim_test(self): claims[AuthenticationConstants.APP_ID_CLAIM] = audience assert not SkillValidation.is_skill_claim(claims) + # Anonymous skill app id + del claims[AuthenticationConstants.APP_ID_CLAIM] + claims[AuthenticationConstants.APP_ID_CLAIM] = ( + AuthenticationConstants.ANONYMOUS_SKILL_APP_ID + ) + assert SkillValidation.is_skill_claim(claims) + # All checks pass, should be good now del claims[AuthenticationConstants.AUDIENCE_CLAIM] claims[AuthenticationConstants.AUDIENCE_CLAIM] = app_id @@ -157,3 +168,12 @@ def validate_appid(app_id: str): # All checks pass (no exception) claims[AuthenticationConstants.APP_ID_CLAIM] = app_id await SkillValidation._validate_identity(mock_identity, mock_credentials) + + @staticmethod + def test_create_anonymous_skill_claim(): + sut = SkillValidation.create_anonymous_skill_claim() + assert ( + JwtTokenValidation.get_app_id_from_claims(sut.claims) + == AuthenticationConstants.ANONYMOUS_SKILL_APP_ID + ) + assert sut.authentication_type == AuthenticationConstants.ANONYMOUS_AUTH_TYPE diff --git a/libraries/botframework-streaming/README.rst b/libraries/botframework-streaming/README.rst new file mode 100644 index 000000000..35838e289 --- /dev/null +++ b/libraries/botframework-streaming/README.rst @@ -0,0 +1,82 @@ + +================================= +BotFramework-Streaming for Python +================================= + +.. image:: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI?branchName=master + :target: https://fuselabs.visualstudio.com/SDK_v4/_apis/build/status/Python/SDK_v4-Python-CI + :align: right + :alt: Azure DevOps status for master branch +.. image:: https://badge.fury.io/py/botframework-streaming.svg + :target: https://badge.fury.io/py/botframework-streaming + :alt: Latest PyPI package version + +Streaming Extensions libraries for BotFramework. + +How to Install +============== + +.. code-block:: bash + + pip install botframework-streaming + +Documentation/Wiki +================== + +You can find more information on the botframework-python project by visiting our `Wiki`_. + +Requirements +============ + +* `Python >= 3.7.0`_ + + +Source Code +=========== +The latest developer version is available in a github repository: +https://github.com/Microsoft/botframework-python/ + + +Contributing +============ + +This project welcomes contributions and suggestions. Most contributions require you to agree to a +Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us +the rights to use your contribution. For details, visit https://cla.microsoft.com. + +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide +a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions +provided by the bot. You will only need to do this once across all repos using our CLA. + +This project has adopted the `Microsoft Open Source Code of Conduct`_. +For more information see the `Code of Conduct FAQ`_ or +contact `opencode@microsoft.com`_ with any additional questions or comments. + +Reporting Security Issues +========================= + +Security issues and bugs should be reported privately, via email, to the Microsoft Security +Response Center (MSRC) at `secure@microsoft.com`_. You should +receive a response within 24 hours. If for some reason you do not, please follow up via +email to ensure we received your original message. Further information, including the +`MSRC PGP`_ key, can be found in +the `Security TechCenter`_. + +License +======= + +Copyright (c) Microsoft Corporation. All rights reserved. + +Licensed under the MIT_ License. + +.. _Wiki: https://github.com/Microsoft/botframework-python/wiki +.. _Python >= 3.7.0: https://www.python.org/downloads/ +.. _MIT: https://github.com/Microsoft/vscode/blob/master/LICENSE.txt +.. _Microsoft Open Source Code of Conduct: https://opensource.microsoft.com/codeofconduct/ +.. _Code of Conduct FAQ: https://opensource.microsoft.com/codeofconduct/faq/ +.. _opencode@microsoft.com: mailto:opencode@microsoft.com +.. _secure@microsoft.com: mailto:secure@microsoft.com +.. _MSRC PGP: https://technet.microsoft.com/en-us/security/dn606155 +.. _Security TechCenter: https://github.com/Microsoft/vscode/blob/master/LICENSE.txt + +.. `_ \ No newline at end of file diff --git a/libraries/botframework-streaming/botframework/streaming/__init__.py b/libraries/botframework-streaming/botframework/streaming/__init__.py new file mode 100644 index 000000000..fac150fb5 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/__init__.py @@ -0,0 +1,27 @@ +# coding=utf-8 +# -------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +from .about import __version__, __title__ +from .receive_request import ReceiveRequest +from .payload_stream import PayloadStream +from .protocol_adapter import ProtocolAdapter +from .receive_response import ReceiveResponse +from .request_handler import RequestHandler +from .streaming_request import StreamingRequest +from .streaming_response import StreamingResponse + +__all__ = [ + "ReceiveRequest", + "ProtocolAdapter", + "ReceiveResponse", + "PayloadStream", + "RequestHandler", + "StreamingRequest", + "StreamingResponse", + "__title__", + "__version__", +] diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/about.py b/libraries/botframework-streaming/botframework/streaming/about.py similarity index 84% rename from samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/about.py rename to libraries/botframework-streaming/botframework/streaming/about.py index 340b35e3e..834a4a9a6 100644 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/about.py +++ b/libraries/botframework-streaming/botframework/streaming/about.py @@ -1,11 +1,11 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -"""Package information.""" + import os -__title__ = "model_corebot101" +__title__ = "botframework-streaming" __version__ = ( - os.environ["packageVersion"] if "packageVersion" in os.environ else "0.0.1" + os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" ) __uri__ = "https://www.github.com/Microsoft/botbuilder-python" __author__ = "Microsoft" diff --git a/libraries/botframework-streaming/botframework/streaming/payload_stream.py b/libraries/botframework-streaming/botframework/streaming/payload_stream.py new file mode 100644 index 000000000..4a9ec1463 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payload_stream.py @@ -0,0 +1,77 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from asyncio import Lock, Semaphore +from typing import List + +from botframework.streaming.payloads.assemblers import PayloadStreamAssembler + + +class PayloadStream: + def __init__(self, assembler: PayloadStreamAssembler): + self._assembler = assembler + self._buffer_queue: List[List[int]] = [] + self._lock = Lock() + self._data_available = Semaphore(0) + self._producer_length = 0 # total length + self._consumer_position = 0 # read position + self._active: List[int] = [] + self._active_offset = 0 + self._end = False + + def __len__(self): + return self._producer_length + + def give_buffer(self, buffer: List[int]): + self._buffer_queue.append(buffer) + self._producer_length += len(buffer) + + self._data_available.release() + + def done_producing(self): + self.give_buffer([]) + + def write(self, buffer: List[int], offset: int, count: int): + buffer_copy = buffer[offset : offset + count] + self.give_buffer(buffer_copy) + + async def read(self, buffer: List[int], offset: int, count: int): + if self._end: + return 0 + + if not self._active: + await self._data_available.acquire() + async with self._lock: + self._active = self._buffer_queue.pop(0) + + available_count = min(len(self._active) - self._active_offset, count) + + for index in range(available_count): + buffer[offset + index] = self._active[self._active_offset] + self._active_offset += 1 + + self._consumer_position += available_count + + if self._active_offset >= len(self._active): + self._active = [] + self._active_offset = 0 + + if ( + self._assembler + and self._consumer_position >= self._assembler.content_length + ): + self._end = True + + return available_count + + async def read_until_end(self): + result = [None] * self._assembler.content_length + current_size = 0 + + while not self._end: + count = await self.read( + result, current_size, self._assembler.content_length + ) + current_size += count + + return result diff --git a/libraries/botframework-streaming/botframework/streaming/payload_transport/__init__.py b/libraries/botframework-streaming/botframework/streaming/payload_transport/__init__.py new file mode 100644 index 000000000..6270c96f3 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payload_transport/__init__.py @@ -0,0 +1,10 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +from .payload_receiver import PayloadReceiver +from .payload_sender import PayloadSender +from .send_packet import SendPacket + + +__all__ = ["PayloadReceiver", "PayloadSender", "SendPacket"] diff --git a/libraries/botframework-streaming/botframework/streaming/payload_transport/payload_receiver.py b/libraries/botframework-streaming/botframework/streaming/payload_transport/payload_receiver.py new file mode 100644 index 000000000..b20df2050 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payload_transport/payload_receiver.py @@ -0,0 +1,165 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import traceback + +from asyncio import iscoroutinefunction, isfuture +from typing import Callable, List + +import botframework.streaming as streaming +from botframework.streaming.payloads import HeaderSerializer +from botframework.streaming.payloads.models import Header, PayloadTypes +from botframework.streaming.transport import ( + DisconnectedEventArgs, + TransportConstants, + TransportReceiverBase, +) + + +class PayloadReceiver: + def __init__(self): + self._get_stream: Callable[[Header], List[int]] = None + self._receive_action: Callable[[Header, List[int], int], None] = None + self._receiver: TransportReceiverBase = None + self._is_disconnecting = False + + self._receive_header_buffer: List[int] = [ + None + ] * TransportConstants.MAX_HEADER_LENGTH + self._receive_content_buffer: List[int] = [ + None + ] * TransportConstants.MAX_PAYLOAD_LENGTH + + self.disconnected: Callable[[object, DisconnectedEventArgs], None] = None + + @property + def is_connected(self) -> bool: + return self._receiver is not None + + async def connect(self, receiver: TransportReceiverBase): + if self._receiver: + raise RuntimeError(f"{self.__class__.__name__} instance already connected.") + + self._receiver = receiver + await self._run_receive() + + async def _run_receive(self): + await self._receive_packets() + + def subscribe( + self, + get_stream: Callable[[Header], List[int]], + receive_action: Callable[[Header, List[int]], int], + ): + self._get_stream = get_stream + self._receive_action = receive_action + + async def disconnect(self, event_args: DisconnectedEventArgs = None): + did_disconnect = False + + if not self._is_disconnecting: + self._is_disconnecting = True + try: + try: + if self._receiver: + await self._receiver.close() + # TODO: investigate if 'dispose' is necessary + did_disconnect = True + except Exception: + traceback.print_exc() + + self._receiver = None + + if did_disconnect: + if callable(self.disconnected): + # pylint: disable=not-callable + if iscoroutinefunction(self.disconnected) or isfuture( + self.disconnected + ): + await self.disconnected( + self, event_args or DisconnectedEventArgs.empty + ) + else: + self.disconnected( + self, event_args or DisconnectedEventArgs.empty + ) + finally: + self._is_disconnecting = False + + async def _receive_packets(self): + is_closed = False + disconnect_args = None + + while self._receiver and self._receiver.is_connected and not is_closed: + # receive a single packet + try: + # read the header + header_offset = 0 + # TODO: this while is probalby not necessary + while header_offset < TransportConstants.MAX_HEADER_LENGTH: + length = await self._receiver.receive( + self._receive_header_buffer, + header_offset, + TransportConstants.MAX_HEADER_LENGTH - header_offset, + ) + + if length == 0: + # TODO: make custom exception + raise Exception( + "TransportDisconnectedException: Stream closed while reading header bytes" + ) + + header_offset += length + + # deserialize the bytes into a header + header = HeaderSerializer.deserialize( + self._receive_header_buffer, 0, TransportConstants.MAX_HEADER_LENGTH + ) + + # read the payload + content_stream = self._get_stream(header) + + buffer = ( + [None] * header.payload_length + if PayloadTypes.is_stream(header) + else self._receive_content_buffer + ) + offset = 0 + + if header.payload_length: + while offset < header.payload_length: + count = min( + header.payload_length - offset, + TransportConstants.MAX_PAYLOAD_LENGTH, + ) + + # Send: Packet content + length = await self._receiver.receive(buffer, offset, count) + if length == 0: + # TODO: make custom exception + raise Exception( + "TransportDisconnectedException: Stream closed while reading header bytes" + ) + + if content_stream is not None: + # write chunks to the content_stream if it's not a stream type + # TODO: this has to be improved in custom buffer class (validate buffer ended) + if not PayloadTypes.is_stream(header): + for index in range(offset, offset + length): + content_stream[index] = buffer[index] + + offset += length + + # give the full payload buffer to the contentStream if it's a stream + if PayloadTypes.is_stream(header) and isinstance( + content_stream, streaming.PayloadStream + ): + content_stream.give_buffer(buffer) + + self._receive_action(header, content_stream, offset) + except Exception as exception: + traceback.print_exc() + is_closed = True + disconnect_args = DisconnectedEventArgs(reason=str(exception)) + + await self.disconnect(disconnect_args) diff --git a/libraries/botframework-streaming/botframework/streaming/payload_transport/payload_sender.py b/libraries/botframework-streaming/botframework/streaming/payload_transport/payload_sender.py new file mode 100644 index 000000000..817181846 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payload_transport/payload_sender.py @@ -0,0 +1,157 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from asyncio import Event, ensure_future, iscoroutinefunction, isfuture +from typing import Awaitable, Callable, List + +from botframework.streaming.transport import ( + DisconnectedEventArgs, + TransportSenderBase, + TransportConstants, +) +from botframework.streaming.payloads import HeaderSerializer +from botframework.streaming.payloads.models import Header + +from .send_queue import SendQueue +from .send_packet import SendPacket + + +# TODO: consider interface this class +class PayloadSender: + def __init__(self): + self._connected_event = Event() + self._sender: TransportSenderBase = None + self._is_disconnecting: bool = False + self._send_header_buffer: List[int] = [ + None + ] * TransportConstants.MAX_HEADER_LENGTH + self._send_content_buffer: List[int] = [ + None + ] * TransportConstants.MAX_PAYLOAD_LENGTH + + self._send_queue = SendQueue(action=self._write_packet) + + self.disconnected: Callable[[object, DisconnectedEventArgs], None] = None + + @property + def is_connected(self) -> bool: + return self._sender is not None + + def connect(self, sender: TransportSenderBase): + if self._sender: + raise RuntimeError(f"{self.__class__.__name__} instance already connected.") + + self._sender = sender + self._connected_event.set() + + # TODO: check 'stream' for payload + def send_payload( + self, + header: Header, + payload: object, + is_length_known: bool, + sent_callback: Callable[[Header], Awaitable], + ): + packet = SendPacket( + header=header, + payload=payload, + is_length_known=is_length_known, + sent_callback=sent_callback, + ) + + self._send_queue.post(packet) + + async def disconnect(self, event_args: DisconnectedEventArgs = None): + did_disconnect = False + + if not self._is_disconnecting: + self._is_disconnecting = True + try: + try: + if self._sender: + self._sender.close() + # TODO: investigate if 'dispose' is necessary + did_disconnect = True + except Exception: + pass + + self._sender = None + + if did_disconnect: + self._connected_event.clear() + if callable(self.disconnected): + # pylint: disable=not-callable + if iscoroutinefunction(self.disconnected) or isfuture( + self.disconnected + ): + await self.disconnected( + self, event_args or DisconnectedEventArgs.empty + ) + else: + self.disconnected( + self, event_args or DisconnectedEventArgs.empty + ) + finally: + self._is_disconnecting = False + + async def _write_packet(self, packet: SendPacket): + await self._connected_event.wait() + + try: + # determine if we know the payload length and end + if not packet.is_length_known: + count = packet.header.payload_length + packet.header.end = count == 0 + + header_length = HeaderSerializer.serialize( + packet.header, self._send_header_buffer, 0 + ) + + # Send: Packet Header + length = await self._sender.send(self._send_header_buffer, 0, header_length) + if not length: + # TODO: make custom exception + raise Exception("TransportDisconnectedException") + + offset = 0 + + # Send content in chunks + if packet.header.payload_length and packet.payload: + # If we already read the buffer, send that + # If we did not, read from the stream until we've sent that amount + if not packet.is_length_known: + # Send: Packet content + length = await self._sender.send( + self._send_content_buffer, 0, packet.header.payload_length + ) + if length == 0: + # TODO: make custom exception + raise Exception("TransportDisconnectedException") + else: + while offset < packet.header.payload_length: + count = min( + packet.header.payload_length - offset, + TransportConstants.MAX_PAYLOAD_LENGTH, + ) + + # copy the stream to the buffer + # TODO: this has to be improved in custom buffer class (validate buffer ended) + for index in range(count): + self._send_content_buffer[index] = packet.payload[index] + + # Send: Packet content + length = await self._sender.send( + self._send_content_buffer, 0, count + ) + if length == 0: + # TODO: make custom exception + raise Exception("TransportDisconnectedException") + + offset += count + + if packet.sent_callback: + # TODO: should this really run in the background? + ensure_future(packet.sent_callback(packet.header)) + except Exception as exception: + disconnected_args = DisconnectedEventArgs(reason=str(exception)) + await self.disconnect(disconnected_args) diff --git a/libraries/botframework-streaming/botframework/streaming/payload_transport/send_packet.py b/libraries/botframework-streaming/botframework/streaming/payload_transport/send_packet.py new file mode 100644 index 000000000..bf7164708 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payload_transport/send_packet.py @@ -0,0 +1,21 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Awaitable, Callable + +from botframework.streaming.payloads.models import Header + + +class SendPacket: + def __init__( + self, + *, + header: Header, + payload: object, + is_length_known: bool, + sent_callback: Callable[[Header], Awaitable] + ): + self.header = header + self.payload = payload + self.is_length_known = is_length_known + self.sent_callback = sent_callback diff --git a/libraries/botframework-streaming/botframework/streaming/payload_transport/send_queue.py b/libraries/botframework-streaming/botframework/streaming/payload_transport/send_queue.py new file mode 100644 index 000000000..1f52bee44 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payload_transport/send_queue.py @@ -0,0 +1,40 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import traceback + +from asyncio import Queue, ensure_future +from typing import Awaitable, Callable + + +class SendQueue: + def __init__(self, action: Callable[[object], Awaitable], timeout: int = 30): + self._action = action + + self._queue = Queue() + self._timeout_seconds = timeout + + # TODO: this have to be abstracted so can remove asyncio dependency + ensure_future(self._process()) + + def post(self, item: object): + self._post_internal(item) + + def _post_internal(self, item: object): + self._queue.put_nowait(item) + + async def _process(self): + while True: + try: + while True: + item = await self._queue.get() + try: + await self._action(item) + except Exception: + traceback.print_exc() + finally: + self._queue.task_done() + except Exception: + # AppInsights.TrackException(e) + traceback.print_exc() + return diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/__init__.py b/libraries/botframework-streaming/botframework/streaming/payloads/__init__.py new file mode 100644 index 000000000..06fd3ad21 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/__init__.py @@ -0,0 +1,20 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .content_stream import ContentStream +from .header_serializer import HeaderSerializer +from .payload_assembler_manager import PayloadAssemblerManager +from .request_manager import RequestManager +from .response_message_stream import ResponseMessageStream +from .send_operations import SendOperations +from .stream_manager import StreamManager + +__all__ = [ + "ContentStream", + "PayloadAssemblerManager", + "RequestManager", + "ResponseMessageStream", + "HeaderSerializer", + "SendOperations", + "StreamManager", +] diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/__init__.py b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/__init__.py new file mode 100644 index 000000000..0373292c4 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .assembler import Assembler +from .payload_stream_assembler import PayloadStreamAssembler +from .receive_request_assembler import ReceiveRequestAssembler +from .receive_response_assembler import ReceiveResponseAssembler + +__all__ = [ + "Assembler", + "PayloadStreamAssembler", + "ReceiveRequestAssembler", + "ReceiveResponseAssembler", +] diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/assembler.py b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/assembler.py new file mode 100644 index 000000000..5fdcfe49d --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/assembler.py @@ -0,0 +1,30 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +from abc import ABC +from uuid import UUID + +from typing import List + +from botframework.streaming.payloads.models import Header + + +class Assembler(ABC): + def __init__(self, end: bool, identifier: UUID): + self.end = end + self.identifier = identifier + + def close(self): + raise NotImplementedError() + + def create_stream_from_payload(self) -> List[int]: + raise NotImplementedError() + + def get_payload_as_stream(self) -> List[int]: + raise NotImplementedError() + + def on_receive( + self, header: Header, stream: List[int], content_length: int + ) -> List[int]: + raise NotImplementedError() diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/payload_stream_assembler.py b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/payload_stream_assembler.py new file mode 100644 index 000000000..c7aba13b6 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/payload_stream_assembler.py @@ -0,0 +1,46 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import UUID +from typing import List + +import botframework.streaming as streaming +import botframework.streaming.payloads as payloads +from botframework.streaming.payloads.models import Header + +from .assembler import Assembler + + +class PayloadStreamAssembler(Assembler): + # pylint: disable=super-init-not-called + def __init__( + self, + stream_manager: "payloads.StreamManager", + identifier: UUID, + type: str = None, + length: int = None, + ): + self._stream_manager = stream_manager or payloads.StreamManager() + self._stream: "streaming.PayloadStream" = None + # self._lock = Lock() + self.identifier = identifier + self.content_type = type + self.content_length = length + self.end: bool = None + + def create_stream_from_payload(self) -> "streaming.PayloadStream": + return streaming.PayloadStream(self) + + def get_payload_as_stream(self) -> "streaming.PayloadStream": + if self._stream is None: + self._stream = self.create_stream_from_payload() + + return self._stream + + def on_receive(self, header: Header, stream: List[int], content_length: int): + if header.end: + self.end = True + self._stream.done_producing() + + def close(self): + self._stream_manager.close_stream(self.identifier) diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/receive_request_assembler.py b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/receive_request_assembler.py new file mode 100644 index 000000000..f26f67c6a --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/receive_request_assembler.py @@ -0,0 +1,84 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import asyncio +from uuid import UUID +from typing import Awaitable, Callable, List + +import botframework.streaming as streaming +import botframework.streaming.payloads as payloads +from botframework.streaming.payloads.models import Header, RequestPayload + +from .assembler import Assembler + + +class ReceiveRequestAssembler(Assembler): + # pylint: disable=super-init-not-called + def __init__( + self, + header: Header, + stream_manager: "payloads.StreamManager", + on_completed: Callable[[UUID, "streaming.ReceiveRequest"], Awaitable], + ): + if not header: + raise TypeError( + f"'header: {header.__class__.__name__}' argument can't be None" + ) + if not on_completed: + raise TypeError(f"'on_completed' argument can't be None") + + self._stream_manager = stream_manager + self._on_completed = on_completed + self.identifier = header.id + self._length = header.payload_length if header.end else None + self._stream: List[int] = None + + def create_stream_from_payload(self) -> List[int]: + return [None] * (self._length or 0) + + def get_payload_as_stream(self) -> List[int]: + if self._stream is None: + self._stream = self.create_stream_from_payload() + + return self._stream + + def on_receive(self, header: Header, stream: List[int], content_length: int): + if header.end: + self.end = True + + # Execute the request in the background + asyncio.ensure_future(self.process_request(stream)) + + def close(self): + self._stream_manager.close_stream(self.identifier) + + async def process_request(self, stream: List[int]): + request_payload = RequestPayload().from_json(bytes(stream).decode("utf-8-sig")) + + request = streaming.ReceiveRequest( + verb=request_payload.verb, path=request_payload.path, streams=[] + ) + + if request_payload.streams: + for stream_description in request_payload.streams: + try: + identifier = UUID(stream_description.id) + except Exception: + raise ValueError( + f"Stream description id '{stream_description.id}' is not a Guid" + ) + + stream_assembler = self._stream_manager.get_payload_assembler( + identifier + ) + stream_assembler.content_type = stream_description.content_type + stream_assembler.content_length = stream_description.length + + content_stream = payloads.ContentStream( + identifier=identifier, assembler=stream_assembler + ) + content_stream.length = stream_description.length + content_stream.content_type = stream_description.content_type + request.streams.append(content_stream) + + await self._on_completed(self.identifier, request) diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/receive_response_assembler.py b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/receive_response_assembler.py new file mode 100644 index 000000000..9b6003021 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/assemblers/receive_response_assembler.py @@ -0,0 +1,86 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import asyncio +from uuid import UUID +from typing import Awaitable, Callable, List + +import botframework.streaming as streaming +import botframework.streaming.payloads as payloads +from botframework.streaming.payloads.models import Header, ResponsePayload + +from .assembler import Assembler + + +class ReceiveResponseAssembler(Assembler): + # pylint: disable=super-init-not-called + def __init__( + self, + header: Header, + stream_manager: "payloads.StreamManager", + on_completed: Callable[[UUID, "streaming.ReceiveResponse"], Awaitable], + ): + if not header: + raise TypeError( + f"'header: {header.__class__.__name__}' argument can't be None" + ) + if not on_completed: + raise TypeError(f"'on_completed' argument can't be None") + + self._stream_manager = stream_manager + self._on_completed = on_completed + self.identifier = header.id + self._length = header.payload_length if header.end else None + self._stream: List[int] = None + + def create_stream_from_payload(self) -> List[int]: + return [None] * (self._length or 0) + + def get_payload_as_stream(self) -> List[int]: + if self._stream is None: + self._stream = self.create_stream_from_payload() + + return self._stream + + def on_receive(self, header: Header, stream: List[int], content_length: int): + if header.end: + self.end = header.end + + # Execute the response on a separate Task + # Execute the request on a separate Thread in the background + # Execute the request on a separate in the background + asyncio.ensure_future(self.process_response(stream)) + + def close(self): + self._stream_manager.close_stream(self.identifier) + + async def process_response(self, stream: List[int]): + response_payload = ResponsePayload().from_json(bytes(stream).decode("utf8")) + + response = streaming.ReceiveResponse( + status_code=response_payload.status_code, streams=[] + ) + + if response_payload.streams: + for stream_description in response_payload.streams: + try: + identifier = UUID(int=int(stream_description.id)) + except Exception: + raise ValueError( + f"Stream description id '{stream_description.id}' is not a Guid" + ) + + stream_assembler = self._stream_manager.get_payload_assembler( + identifier + ) + stream_assembler.content_type = stream_description.content_type + stream_assembler.content_length = stream_description.length + + content_stream = payloads.ContentStream( + identifier=identifier, assembler=stream_assembler + ) + content_stream.length = stream_description.length + content_stream.content_type = stream_description.content_type + response.streams.append(content_stream) + + await self._on_completed(self.identifier, response) diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/content_stream.py b/libraries/botframework-streaming/botframework/streaming/payloads/content_stream.py new file mode 100644 index 000000000..c0c1ef67c --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/content_stream.py @@ -0,0 +1,23 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import UUID + +from botframework.streaming.payloads.assemblers import PayloadStreamAssembler + + +class ContentStream: + def __init__(self, identifier: UUID, assembler: PayloadStreamAssembler): + if not assembler: + raise TypeError( + f"'assembler: {assembler.__class__.__name__}' argument can't be None" + ) + + self.identifier = identifier + self._assembler = assembler + self.stream = self._assembler.get_payload_as_stream() + self.content_type: str = None + self.length: int = None + + def cancel(self): + self._assembler.close() diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/__init__.py b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/__init__.py new file mode 100644 index 000000000..bc4270be5 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/__init__.py @@ -0,0 +1,16 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .cancel_disassembler import CancelDisassembler +from .payload_disassembler import PayloadDisassembler +from .request_disassembler import RequestDisassembler +from .response_disassembler import ResponseDisassembler +from .response_message_stream_disassembler import ResponseMessageStreamDisassembler + +__all__ = [ + "CancelDisassembler", + "PayloadDisassembler", + "RequestDisassembler", + "ResponseDisassembler", + "ResponseMessageStreamDisassembler", +] diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/cancel_disassembler.py b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/cancel_disassembler.py new file mode 100644 index 000000000..c531cfe5d --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/cancel_disassembler.py @@ -0,0 +1,22 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import UUID + +from botframework.streaming.payload_transport import PayloadSender +from botframework.streaming.payloads.models import Header + + +class CancelDisassembler: + def __init__(self, *, sender: PayloadSender, identifier: UUID, type: str): + self._sender = sender + self._identifier = identifier + self._type = type + + async def disassemble(self): + header = Header(type=self._type, id=self._identifier, end=True) + + header.payload_length = 0 + + self._sender.send_payload(header, None, True, None) + return diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/payload_disassembler.py b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/payload_disassembler.py new file mode 100644 index 000000000..d60955d1f --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/payload_disassembler.py @@ -0,0 +1,110 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +from asyncio import Future +from abc import ABC, abstractmethod +from uuid import UUID +from typing import List + +from botframework.streaming.transport import TransportConstants +from botframework.streaming.payload_transport import PayloadSender +from botframework.streaming.payloads import ResponseMessageStream +from botframework.streaming.payloads.models import ( + Header, + Serializable, + StreamDescription, +) + + +class PayloadDisassembler(ABC): + def __init__(self, sender: PayloadSender, identifier: UUID): + self.sender = sender + self.identifier = identifier + self._task_completion_source = Future() + + self._stream: List[int] = None + self._stream_length: int = None + self._send_offset: int = None + self._is_end: bool = False + self._type: str = None + + @property + @abstractmethod + def type(self) -> str: + return self._type + + async def get_stream(self) -> List[int]: + raise NotImplementedError() + + async def disassemble(self): + self._stream = await self.get_stream() + self._stream_length = len(self._stream) + self._send_offset = 0 + + await self._send() + + @staticmethod + def get_stream_description(stream: ResponseMessageStream) -> StreamDescription: + description = StreamDescription(id=str(stream.id)) + + # TODO: This content type is hardcoded for POC, investigate how to proceed + content = bytes(stream.content).decode("utf8") + + try: + json.loads(content) + content_type = "application/json" + except ValueError: + content_type = "text/plain" + + description.content_type = content_type + description.length = len(content) + + # TODO: validate statement below, also make the string a constant + # content_length: int = stream.content.headers.get("Content-Length") + # if content_length: + # description.length = int(content_length) + # else: + # # TODO: check statement validity + # description.length = stream.content.headers.content_length + + return description + + @staticmethod + def serialize(item: Serializable, stream: List[int], length: List[int]): + encoded_json = item.to_json().encode() + stream.clear() + stream.extend(list(encoded_json)) + + length.clear() + length.append(len(stream)) + + async def _send(self): + # determine if we know the length we can send and whether we can tell if this is the end + is_length_known = self._is_end + + header = Header(type=self.type, id=self.identifier, end=self._is_end) + + header.payload_length = 0 + + if self._stream_length is not None: + # determine how many bytes we can send and if we are at the end + header.payload_length = min( + self._stream_length - self._send_offset, + TransportConstants.MAX_PAYLOAD_LENGTH, + ) + header.end = ( + self._send_offset + header.payload_length >= self._stream_length + ) + is_length_known = True + + self.sender.send_payload(header, self._stream, is_length_known, self._on_send) + + async def _on_send(self, header: Header): + self._send_offset += header.payload_length + self._is_end = header.end + + if self._is_end: + self._task_completion_source.set_result(True) + else: + await self._send() diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/request_disassembler.py b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/request_disassembler.py new file mode 100644 index 000000000..281dec376 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/request_disassembler.py @@ -0,0 +1,42 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import UUID +from typing import List + +from botframework.streaming.payload_transport import PayloadSender +from botframework.streaming.payloads.models import PayloadTypes, RequestPayload + +from .payload_disassembler import PayloadDisassembler + + +class RequestDisassembler(PayloadDisassembler): + def __init__( + self, + sender: PayloadSender, + identifier: UUID, + request: "streaming.StreamingRequest", + ): + super().__init__(sender, identifier) + + self.request = request + + @property + def type(self) -> str: + return PayloadTypes.REQUEST + + async def get_stream(self) -> List[int]: + payload = RequestPayload(verb=self.request.verb, path=self.request.path) + + if self.request.streams: + payload.streams = [ + self.get_stream_description(content_stream) + for content_stream in self.request.streams + ] + + memory_stream: List[int] = [] + stream_length: List[int] = [] + # TODO: high probability stream length is not necessary + self.serialize(payload, memory_stream, stream_length) + + return memory_stream diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/response_disassembler.py b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/response_disassembler.py new file mode 100644 index 000000000..7e480cac4 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/response_disassembler.py @@ -0,0 +1,42 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import UUID +from typing import List + +from botframework.streaming.payload_transport import PayloadSender +from botframework.streaming.payloads.models import PayloadTypes, ResponsePayload + +from .payload_disassembler import PayloadDisassembler + + +class ResponseDisassembler(PayloadDisassembler): + def __init__( + self, + sender: PayloadSender, + identifier: UUID, + response: "streaming.StreamingResponse", + ): + super().__init__(sender, identifier) + + self.response = response + + @property + def type(self) -> str: + return PayloadTypes.RESPONSE + + async def get_stream(self) -> List[int]: + payload = ResponsePayload(status_code=self.response.status_code) + + if self.response.streams: + payload.streams = [ + self.get_stream_description(content_stream) + for content_stream in self.response.streams + ] + + memory_stream: List[int] = [] + stream_length: List[int] = [] + # TODO: high probability stream length is not necessary + self.serialize(payload, memory_stream, stream_length) + + return memory_stream diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/response_message_stream_disassembler.py b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/response_message_stream_disassembler.py new file mode 100644 index 000000000..3f0f5d71c --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/disassemblers/response_message_stream_disassembler.py @@ -0,0 +1,27 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List + +from botframework.streaming.payload_transport import PayloadSender +from botframework.streaming.payloads import ResponseMessageStream +from botframework.streaming.payloads.models import PayloadTypes + +from .payload_disassembler import PayloadDisassembler + + +class ResponseMessageStreamDisassembler(PayloadDisassembler): + def __init__(self, sender: PayloadSender, content_stream: ResponseMessageStream): + super().__init__(sender, content_stream.id) + + self.content_stream = content_stream + + @property + def type(self) -> str: + return PayloadTypes.STREAM + + async def get_stream(self) -> List[int]: + # TODO: check if bypass is correct here or if serialization should take place. + # this is redundant -->stream: List[int] = list(str(self.content_stream.content).encode()) + + return self.content_stream.content diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/header_serializer.py b/libraries/botframework-streaming/botframework/streaming/payloads/header_serializer.py new file mode 100644 index 000000000..53a1d8fc3 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/header_serializer.py @@ -0,0 +1,169 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import UUID +from typing import List + +from botframework.streaming.transport import TransportConstants + +from .models import Header + +_CHAR_TO_BINARY_INT = {val.decode(): list(val)[0] for val in [b".", b"\n", b"1", b"0"]} + + +# TODO: consider abstracting the binary int list logic into a class for easier handling +class HeaderSerializer: + DELIMITER = _CHAR_TO_BINARY_INT["."] + TERMINATOR = _CHAR_TO_BINARY_INT["\n"] + END = _CHAR_TO_BINARY_INT["1"] + NOT_END = _CHAR_TO_BINARY_INT["0"] + TYPE_OFFSET = 0 + TYPE_DELIMITER_OFFSET = 1 + LENGTH_OFFSET = 2 + LENGTH_LENGTH = 6 + LENGTH_DELIMETER_OFFSET = 8 + ID_OFFSET = 9 + ID_LENGTH = 36 + ID_DELIMETER_OFFSET = 45 + END_OFFSET = 46 + TERMINATOR_OFFSET = 47 + + @staticmethod + def serialize( + header: Header, + buffer: List[int], + offset: int, # pylint: disable=unused-argument + ) -> int: + # write type + buffer[HeaderSerializer.TYPE_OFFSET] = HeaderSerializer._char_to_binary_int( + header.type + ) + buffer[HeaderSerializer.TYPE_DELIMITER_OFFSET] = HeaderSerializer.DELIMITER + + # write length + length_binary_array: List[int] = list( + HeaderSerializer._int_to_formatted_encoded_str( + header.payload_length, "{:06d}" + ) + ) + HeaderSerializer._write_in_buffer( + length_binary_array, buffer, HeaderSerializer.LENGTH_OFFSET + ) + buffer[HeaderSerializer.LENGTH_DELIMETER_OFFSET] = HeaderSerializer.DELIMITER + + # write id + id_binary_array: List[int] = list( + HeaderSerializer._uuid_to_numeric_encoded_str(header.id) + ) + HeaderSerializer._write_in_buffer( + id_binary_array, buffer, HeaderSerializer.ID_OFFSET + ) + buffer[HeaderSerializer.ID_DELIMETER_OFFSET] = HeaderSerializer.DELIMITER + + # write terminator + buffer[HeaderSerializer.END_OFFSET] = ( + HeaderSerializer.END if header.end else HeaderSerializer.NOT_END + ) + buffer[HeaderSerializer.TERMINATOR_OFFSET] = HeaderSerializer.TERMINATOR + + return TransportConstants.MAX_HEADER_LENGTH + + @staticmethod + def deserialize( + buffer: List[int], offset: int, count: int # pylint: disable=unused-argument + ) -> Header: + if count != TransportConstants.MAX_HEADER_LENGTH: + raise ValueError("Cannot deserialize header, incorrect length") + + header = Header( + type=HeaderSerializer._binary_int_to_char( + buffer[HeaderSerializer.TYPE_OFFSET] + ) + ) + + if buffer[HeaderSerializer.TYPE_DELIMITER_OFFSET] != HeaderSerializer.DELIMITER: + raise ValueError("Header type delimeter is malformed") + + length_str = HeaderSerializer._binary_array_to_str( + buffer[ + HeaderSerializer.LENGTH_OFFSET : HeaderSerializer.LENGTH_OFFSET + + HeaderSerializer.LENGTH_LENGTH + ] + ) + + try: + length = int(length_str) + except Exception: + raise ValueError("Header length is malformed") + + header.payload_length = length + + if ( + buffer[HeaderSerializer.LENGTH_DELIMETER_OFFSET] + != HeaderSerializer.DELIMITER + ): + raise ValueError("Header length delimeter is malformed") + + identifier_str = HeaderSerializer._binary_array_to_str( + buffer[ + HeaderSerializer.ID_OFFSET : HeaderSerializer.ID_OFFSET + + HeaderSerializer.ID_LENGTH + ] + ) + + try: + identifier = UUID(identifier_str) + except Exception: + raise ValueError("Header id is malformed") + + header.id = identifier + + if buffer[HeaderSerializer.ID_DELIMETER_OFFSET] != HeaderSerializer.DELIMITER: + raise ValueError("Header id delimeter is malformed") + + if buffer[HeaderSerializer.END_OFFSET] not in [ + HeaderSerializer.END, + HeaderSerializer.NOT_END, + ]: + raise ValueError("Header end is malformed") + + header.end = buffer[HeaderSerializer.END_OFFSET] == HeaderSerializer.END + + if buffer[HeaderSerializer.TERMINATOR_OFFSET] != HeaderSerializer.TERMINATOR: + raise ValueError("Header terminator is malformed") + + return header + + @staticmethod + def _char_to_binary_int(char: str) -> int: + if len(char) != 1: + raise ValueError("Char to cast should be a str of exactly length 1") + + unicode_list = list(char.encode()) + + if len(unicode_list) != 1: + raise ValueError("Char to cast should be in the ASCII domain") + + return unicode_list[0] + + @staticmethod + def _int_to_formatted_encoded_str(value: int, str_format: str) -> bytes: + return str_format.format(value).encode("ascii") + + @staticmethod + def _uuid_to_numeric_encoded_str(value: UUID) -> bytes: + return str(value).encode("ascii") + + @staticmethod + def _binary_int_to_char(binary_int: int) -> str: + return bytes([binary_int]).decode("ascii") + + @staticmethod + def _binary_array_to_str(binary_array: List[int]) -> str: + return bytes(binary_array).decode("ascii") + + @staticmethod + def _write_in_buffer(data: List[int], buffer: List[int], insert_index: int): + for byte_int in data: + buffer[insert_index] = byte_int + insert_index += 1 diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/models/__init__.py b/libraries/botframework-streaming/botframework/streaming/payloads/models/__init__.py new file mode 100644 index 000000000..f0d3e2024 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/models/__init__.py @@ -0,0 +1,19 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +from .header import Header +from .payload_types import PayloadTypes +from .request_payload import RequestPayload +from .response_payload import ResponsePayload +from .serializable import Serializable +from .stream_description import StreamDescription + +__all__ = [ + "Header", + "PayloadTypes", + "RequestPayload", + "ResponsePayload", + "Serializable", + "StreamDescription", +] diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/models/header.py b/libraries/botframework-streaming/botframework/streaming/payloads/models/header.py new file mode 100644 index 000000000..5eab7564e --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/models/header.py @@ -0,0 +1,33 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import UUID + +from botframework.streaming.transport import TransportConstants + + +class Header: + # pylint: disable=invalid-name + def __init__(self, *, type: str = None, id: UUID = None, end: bool = None): + self._internal_payload_length = None + self.type: str = type + self.id: UUID = id + self.end: bool = end + + @property + def payload_length(self) -> int: + return self._internal_payload_length + + @payload_length.setter + def payload_length(self, value: int): + self._validate_length( + value, TransportConstants.MAX_LENGTH, TransportConstants.MIN_LENGTH + ) + self._internal_payload_length = value + + def _validate_length(self, value: int, max_val: int, min_val: int): + if value > max_val: + raise ValueError(f"Length must be less or equal than {max_val}") + + if value < min_val: + raise ValueError(f"Length must be greater or equal than {min_val}") diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/models/payload_types.py b/libraries/botframework-streaming/botframework/streaming/payloads/models/payload_types.py new file mode 100644 index 000000000..ec9c01090 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/models/payload_types.py @@ -0,0 +1,16 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .header import Header + + +class PayloadTypes: + REQUEST = "A" + RESPONSE = "B" + STREAM = "S" + CANCEL_ALL = "X" + CANCEL_STREAM = "C" + + @staticmethod + def is_stream(header: Header) -> bool: + return header.type == PayloadTypes.STREAM diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/models/request_payload.py b/libraries/botframework-streaming/botframework/streaming/payloads/models/request_payload.py new file mode 100644 index 000000000..1003c292f --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/models/request_payload.py @@ -0,0 +1,44 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +from typing import List + +from .serializable import Serializable +from .stream_description import StreamDescription + + +class RequestPayload(Serializable): + def __init__( + self, + *, + verb: str = None, + path: str = None, + streams: List[StreamDescription] = None + ): + self.verb = verb + self.path = path + self.streams = streams + + def to_json(self) -> str: + obj = {"verb": self.verb, "path": self.path} + + if self.streams: + obj["streams"] = [stream.to_dict() for stream in self.streams] + + return json.dumps(obj) + + def from_json(self, json_str: str) -> "RequestPayload": + obj = json.loads(json_str) + + self.verb = obj.get("verb") + self.path = obj.get("path") + stream_list = obj.get("streams") + + if stream_list: + self.streams = [ + StreamDescription().from_dict(stream_dict) + for stream_dict in stream_list + ] + + return self diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/models/response_payload.py b/libraries/botframework-streaming/botframework/streaming/payloads/models/response_payload.py new file mode 100644 index 000000000..f1f41142c --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/models/response_payload.py @@ -0,0 +1,38 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +from typing import List + +from .serializable import Serializable +from .stream_description import StreamDescription + + +class ResponsePayload(Serializable): + def __init__( + self, *, status_code: int = None, streams: List[StreamDescription] = None + ): + self.status_code = status_code + self.streams = streams + + def to_json(self) -> str: + obj = {"statusCode": self.status_code} + + if self.streams: + obj["streams"] = [stream.to_dict() for stream in self.streams] + + return json.dumps(obj) + + def from_json(self, json_str: str) -> "ResponsePayload": + obj = json.loads(json_str) + + self.status_code = obj.get("statusCode") + stream_list = obj.get("streams") + + if stream_list: + self.streams = [ + StreamDescription().from_dict(stream_dict) + for stream_dict in stream_list + ] + + return self diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/models/serializable.py b/libraries/botframework-streaming/botframework/streaming/payloads/models/serializable.py new file mode 100644 index 000000000..8c01830be --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/models/serializable.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +from abc import ABC + + +# TODO: debate if this class is pertinent or should use msrest infrastructure +class Serializable(ABC): + def to_json(self) -> str: + raise NotImplementedError() + + def from_json(self, json_str: str): + raise NotImplementedError() diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/models/stream_description.py b/libraries/botframework-streaming/botframework/streaming/payloads/models/stream_description.py new file mode 100644 index 000000000..c426f5de1 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/models/stream_description.py @@ -0,0 +1,36 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json + +from .serializable import Serializable + + +class StreamDescription(Serializable): + # pylint: disable=invalid-name + def __init__(self, *, id: str = None, content_type: str = None, length: int = None): + self.id = id + self.content_type = content_type + self.length = length + + def to_dict(self) -> dict: + obj = {"id": self.id, "type": self.content_type} + + if self.length is not None: + obj["length"] = self.length + + return obj + + def from_dict(self, json_dict: dict) -> "StreamDescription": + self.id = json_dict.get("id") + self.content_type = json_dict.get("type") + self.length = json_dict.get("length") + + return self + + def to_json(self) -> str: + return json.dumps(self.to_dict) + + def from_json(self, json_str: str) -> "StreamDescription": + obj = json.loads(json_str) + return self.from_dict(obj) diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/payload_assembler_manager.py b/libraries/botframework-streaming/botframework/streaming/payloads/payload_assembler_manager.py new file mode 100644 index 000000000..276654b0b --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/payload_assembler_manager.py @@ -0,0 +1,73 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import UUID +from typing import Awaitable, Callable, Dict, List, Union + +from botframework.streaming.payloads.assemblers import ( + Assembler, + ReceiveRequestAssembler, + ReceiveResponseAssembler, +) +from botframework.streaming.payloads.models import Header, PayloadTypes + +from .stream_manager import StreamManager + + +class PayloadAssemblerManager: + def __init__( + self, + stream_manager: StreamManager, + on_receive_request: Callable[[UUID, "streaming.ReceiveRequest"], Awaitable], + on_receive_response: Callable[[UUID, "streaming.ReceiveResponse"], Awaitable], + ): + self._on_receive_request = on_receive_request + self._on_receive_response = on_receive_response + self._stream_manager = stream_manager + self._active_assemblers: Dict[UUID, Assembler] = {} + + def get_payload_stream( + self, header: Header + ) -> Union[List[int], "streaming.PayloadStream"]: + # TODO: The return value SHOULDN'T be a union, we should interface List[int] into a BFStream class + if self._is_stream_payload(header): + return self._stream_manager.get_payload_stream(header) + if not self._active_assemblers.get(header.id): + # a new requestId has come in, start a new task to process it as it is received + assembler = self._create_payload_assembler(header) + if assembler: + self._active_assemblers[header.id] = assembler + return assembler.get_payload_as_stream() + + return None + + def on_receive( + self, header: Header, content_stream: List[int], content_length: int + ): + if self._is_stream_payload(header): + self._stream_manager.on_receive(header, content_stream, content_length) + else: + assembler = self._active_assemblers.get(header.id) + if assembler: + assembler.on_receive(header, content_stream, content_length) + + # remove them when we are done + if header.end: + del self._active_assemblers[header.id] + + # ignore unknown header ids + + def _create_payload_assembler(self, header: Header) -> Assembler: + if header.type == PayloadTypes.REQUEST: + return ReceiveRequestAssembler( + header, self._stream_manager, self._on_receive_request + ) + if header.type == PayloadTypes.RESPONSE: + return ReceiveResponseAssembler( + header, self._stream_manager, self._on_receive_response + ) + + return None + + def _is_stream_payload(self, header: Header) -> bool: + return PayloadTypes.is_stream(header) diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/request_manager.py b/libraries/botframework-streaming/botframework/streaming/payloads/request_manager.py new file mode 100644 index 000000000..0ffdbeaad --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/request_manager.py @@ -0,0 +1,45 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from asyncio import Future, shield +from uuid import UUID +from typing import Dict + +import botframework.streaming as streaming + + +class RequestManager: + def __init__( + self, + *, + pending_requests: Dict[UUID, "Future[streaming.ReceiveResponse]"] = None + ): + self._pending_requests = pending_requests or {} + + async def signal_response( + self, request_id: UUID, response: "streaming.ReceiveResponse" + ) -> bool: + # TODO: dive more into this logic + signal: Future = self._pending_requests.get(request_id) + if signal: + signal.set_result(response) + # TODO: double check this + # del self._pending_requests[request_id] + + return True + + return False + + async def get_response(self, request_id: UUID) -> "streaming.ReceiveResponse": + if request_id in self._pending_requests: + return None + + pending_request = Future() + self._pending_requests[request_id] = pending_request + + try: + response: streaming.ReceiveResponse = await shield(pending_request) + return response + + finally: + del self._pending_requests[request_id] diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/response_message_stream.py b/libraries/botframework-streaming/botframework/streaming/payloads/response_message_stream.py new file mode 100644 index 000000000..04ae1dd77 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/response_message_stream.py @@ -0,0 +1,11 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import uuid4, UUID + + +class ResponseMessageStream: + # pylint: disable=invalid-name + def __init__(self, *, id: UUID = None, content: object = None): + self.id = id or uuid4() + self.content = content diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/send_operations.py b/libraries/botframework-streaming/botframework/streaming/payloads/send_operations.py new file mode 100644 index 000000000..82a7ecadc --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/send_operations.py @@ -0,0 +1,71 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import asyncio +from uuid import UUID + +from botframework.streaming.payload_transport import PayloadSender +from botframework.streaming.payloads.disassemblers import ( + CancelDisassembler, + RequestDisassembler, + ResponseDisassembler, + ResponseMessageStreamDisassembler, +) +from botframework.streaming.payloads.models import PayloadTypes + + +class SendOperations: + def __init__(self, payload_sender: PayloadSender): + self._payload_sender = payload_sender + + async def send_request( + self, identifier: UUID, request: "streaming.StreamingRequest" + ): + disassembler = RequestDisassembler(self._payload_sender, identifier, request) + + await disassembler.disassemble() + + if request.streams: + tasks = [ + ResponseMessageStreamDisassembler( + self._payload_sender, content_stream + ).disassemble() + for content_stream in request.streams + ] + + await asyncio.gather(*tasks) + + async def send_response( + self, identifier: UUID, response: "streaming.StreamingResponse" + ): + disassembler = ResponseDisassembler(self._payload_sender, identifier, response) + + await disassembler.disassemble() + + if response.streams: + tasks = [ + ResponseMessageStreamDisassembler( + self._payload_sender, content_stream + ).disassemble() + for content_stream in response.streams + ] + + await asyncio.gather(*tasks) + + async def send_cancel_all(self, identifier: UUID): + disassembler = CancelDisassembler( + sender=self._payload_sender, + identifier=identifier, + type=PayloadTypes.CANCEL_ALL, + ) + + await disassembler.disassemble() + + async def send_cancel_stream(self, identifier: UUID): + disassembler = CancelDisassembler( + sender=self._payload_sender, + identifier=identifier, + type=PayloadTypes.CANCEL_STREAM, + ) + + await disassembler.disassemble() diff --git a/libraries/botframework-streaming/botframework/streaming/payloads/stream_manager.py b/libraries/botframework-streaming/botframework/streaming/payloads/stream_manager.py new file mode 100644 index 000000000..84e4cf4f3 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/payloads/stream_manager.py @@ -0,0 +1,49 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import UUID +from typing import Callable, Dict, List + +from botframework.streaming.payloads.assemblers import PayloadStreamAssembler +from botframework.streaming.payloads.models import Header + + +class StreamManager: + def __init__( + self, on_cancel_stream: Callable[[PayloadStreamAssembler], None] = None + ): + self._on_cancel_stream = on_cancel_stream or (lambda ocs: None) + self._active_assemblers: Dict[UUID, PayloadStreamAssembler] = {} + + def get_payload_assembler(self, identifier: UUID) -> PayloadStreamAssembler: + self._active_assemblers[identifier] = self._active_assemblers.get( + identifier, PayloadStreamAssembler(self, identifier) + ) + + return self._active_assemblers[identifier] + + def get_payload_stream(self, header: Header) -> "streaming.PayloadStream": + assembler = self.get_payload_assembler(header.id) + + return assembler.get_payload_as_stream() + + def on_receive( + self, header: Header, content_stream: List[int], content_length: int + ): + assembler = self._active_assemblers.get(header.id) + + if assembler: + assembler.on_receive(header, content_stream, content_length) + + def close_stream(self, identifier: UUID): + assembler = self._active_assemblers.get(identifier) + + if assembler: + del self._active_assemblers[identifier] + stream = assembler.get_payload_as_stream() + if ( + assembler.content_length + and len(stream) < assembler.content_length + or not assembler.end + ): + self._on_cancel_stream(assembler) diff --git a/libraries/botframework-streaming/botframework/streaming/protocol_adapter.py b/libraries/botframework-streaming/botframework/streaming/protocol_adapter.py new file mode 100644 index 000000000..adee0c3a2 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/protocol_adapter.py @@ -0,0 +1,82 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +import asyncio +from uuid import UUID, uuid4 + +from botframework.streaming.payloads import ( + PayloadAssemblerManager, + RequestManager, + SendOperations, + StreamManager, +) +from botframework.streaming.payloads.assemblers import PayloadStreamAssembler +from botframework.streaming.payload_transport import PayloadSender, PayloadReceiver + +from .receive_request import ReceiveRequest +from .receive_response import ReceiveResponse +from .request_handler import RequestHandler +from .streaming_request import StreamingRequest + + +class ProtocolAdapter: + def __init__( + self, + request_handler: RequestHandler, + request_manager: RequestManager, + payload_sender: PayloadSender, + payload_receiver: PayloadReceiver, + handler_context: object = None, + ): + self._request_handler = request_handler + self._request_manager = request_manager + self._payload_sender = payload_sender + self._payload_receiver = payload_receiver + self._handler_context = handler_context + + self._send_operations = SendOperations(self._payload_sender) + # TODO: might be able to remove + self._stream_manager = StreamManager(self._on_cancel_stream) + self._assembler_manager = PayloadAssemblerManager( + self._stream_manager, self._on_receive_request, self._on_receive_response + ) + + self._payload_receiver.subscribe( + self._assembler_manager.get_payload_stream, + self._assembler_manager.on_receive, + ) + + async def send_request(self, request: StreamingRequest) -> ReceiveResponse: + if not request: + raise TypeError( + f"'request: {request.__class__.__name__}' argument can't be None" + ) + + request_id = uuid4() + response_task = self._request_manager.get_response(request_id) + request_task = self._send_operations.send_request(request_id, request) + + [_, response] = await asyncio.gather(request_task, response_task) + + return response + + async def _on_receive_request(self, identifier: UUID, request: ReceiveRequest): + # request is done, we can handle it + if self._request_handler: + response = await self._request_handler.process_request( + request, None, self._handler_context + ) + + if response: + await self._send_operations.send_response(identifier, response) + + async def _on_receive_response(self, identifier: UUID, response: ReceiveResponse): + # we received the response to something, signal it + await self._request_manager.signal_response(identifier, response) + + def _on_cancel_stream(self, content_stream_assembler: PayloadStreamAssembler): + # TODO: on original C# code content_stream_assembler is typed as IAssembler + task = asyncio.create_task( + self._send_operations.send_cancel_stream( + content_stream_assembler.identifier + ) + ) diff --git a/libraries/botframework-streaming/botframework/streaming/receive_request.py b/libraries/botframework-streaming/botframework/streaming/receive_request.py new file mode 100644 index 000000000..be9fed198 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/receive_request.py @@ -0,0 +1,29 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List + +from botframework.streaming.payloads import ContentStream + + +class ReceiveRequest: + def __init__( + self, *, verb: str = None, path: str = None, streams: List[ContentStream] = None + ): + self.verb = verb + self.path = path + self.streams: List[ContentStream] = streams or [] + + async def read_body_as_str(self) -> str: + try: + content_stream = self.streams[0] if self.streams else None + + if not content_stream: + # TODO: maybe raise an error + return "" + + # TODO: encoding double check + stream = await content_stream.stream.read_until_end() + return bytes(stream).decode("utf-8-sig") + except Exception as error: + raise error diff --git a/libraries/botframework-streaming/botframework/streaming/receive_response.py b/libraries/botframework-streaming/botframework/streaming/receive_response.py new file mode 100644 index 000000000..00ae529fd --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/receive_response.py @@ -0,0 +1,52 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List, Union, Type + +from msrest.serialization import Model +from botframework.streaming.payloads import ContentStream +from botframework.streaming.payloads.models import Serializable + + +class ReceiveResponse: + def __init__(self, status_code: int = 0, streams: List[ContentStream] = None): + self.status_code = status_code + self.streams = streams or [] + + def read_body_as_json( + self, cls: Union[Type[Model], Type[Serializable]] + ) -> Union[Model, Serializable]: + try: + body_str = self.read_body_as_str() + body = None + + if issubclass(cls, Serializable): + body = cls().from_json(body_str) + elif isinstance(cls, Model): + body = cls.deserialize(body_str) + return body + except Exception as error: + raise error + + def read_body_as_str(self) -> str: + try: + content_stream = self.read_body() + + if not content_stream: + return "" + + # TODO: encoding double check + return content_stream.decode("utf8") + except Exception as error: + raise error + + def read_body(self) -> bytes: + try: + content_stream = self.streams[0] if self.streams else None + + if not content_stream: + return None + + return bytes(content_stream.stream) + except Exception as error: + raise error diff --git a/libraries/botframework-streaming/botframework/streaming/request_handler.py b/libraries/botframework-streaming/botframework/streaming/request_handler.py new file mode 100644 index 000000000..3214eb7a1 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/request_handler.py @@ -0,0 +1,15 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC +from logging import Logger + +from .receive_request import ReceiveRequest +from .streaming_response import StreamingResponse + + +class RequestHandler(ABC): + async def process_request( + self, request: ReceiveRequest, logger: Logger, context: object + ) -> StreamingResponse: + raise NotImplementedError() diff --git a/libraries/botframework-streaming/botframework/streaming/streaming_request.py b/libraries/botframework-streaming/botframework/streaming/streaming_request.py new file mode 100644 index 000000000..232446cb4 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/streaming_request.py @@ -0,0 +1,90 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +from uuid import UUID, uuid4 +from typing import List, Union + +from msrest.serialization import Model +from botframework.streaming.payloads import ResponseMessageStream +from botframework.streaming.payloads.models import Serializable + + +class StreamingRequest: + GET = "GET" + POST = "POST" + PUT = "PUT" + DELETE = "DELETE" + + def __init__( + self, + *, + verb: str = None, + path: str = None, + streams: List[ResponseMessageStream] = None, + ): + self.verb = verb + self.path = path + self.streams = streams + + @staticmethod + def create_request( + method: str, path: str = None, body: object = None + ) -> "StreamingRequest": + if not method: + return None + + request = StreamingRequest( + verb=method, + path=path, + ) + + if body: + request.add_stream(body) + + return request + + @staticmethod + def create_get(path: str = None, body: object = None) -> "StreamingRequest": + return StreamingRequest.create_request("GET", path, body) + + @staticmethod + def create_post(path: str = None, body: object = None) -> "StreamingRequest": + return StreamingRequest.create_request("POST", path, body) + + @staticmethod + def create_put(path: str = None, body: object = None) -> "StreamingRequest": + return StreamingRequest.create_request("PUT", path, body) + + @staticmethod + def create_delete(path: str = None, body: object = None) -> "StreamingRequest": + return StreamingRequest.create_request("DELETE", path, body) + + def set_body(self, body: Union[str, Serializable, Model, bytes]): + # TODO: verify if msrest.serialization.Model is necessary + if not body: + return + + if isinstance(body, bytes): + pass + else: + if isinstance(body, Serializable): + body = body.to_json() + elif isinstance(body, Model): + body = json.dumps(body.as_dict()) + + body = body.encode("ascii") + + self.add_stream(list(body)) + + def add_stream(self, content: object, stream_id: UUID = None): + if not content: + raise TypeError( + f"'content: {content.__class__.__name__}' argument can't be None" + ) + if not self.streams: + self.streams = [] + + self.streams.append( + ResponseMessageStream(id=stream_id or uuid4(), content=content) + ) diff --git a/libraries/botframework-streaming/botframework/streaming/streaming_response.py b/libraries/botframework-streaming/botframework/streaming/streaming_response.py new file mode 100644 index 000000000..9dbf3ad98 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/streaming_response.py @@ -0,0 +1,68 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +from http import HTTPStatus +from uuid import UUID, uuid4 +from typing import List, Union + +from msrest.serialization import Model +from botframework.streaming.payloads import ResponseMessageStream +from botframework.streaming.payloads.models import Serializable + + +class StreamingResponse: + def __init__( + self, *, status_code: int = 0, streams: List[ResponseMessageStream] = None + ): + self.status_code = status_code + self.streams = streams + + def add_stream(self, content: object, identifier: UUID = None): + if not content: + raise TypeError("content can't be None") + + if self.streams is None: + self.streams: List[ResponseMessageStream] = [] + + self.streams.append( + ResponseMessageStream(id=identifier or uuid4(), content=content) + ) + + def set_body(self, body: Union[str, Serializable, Model]): + # TODO: verify if msrest.serialization.Model is necessary + if not body: + return + + if isinstance(body, Serializable): + body = body.to_json() + elif isinstance(body, Model): + body = json.dumps(body.as_dict()) + + self.add_stream(list(body.encode())) + + @staticmethod + def create_response(status_code: int, body: object) -> "StreamingResponse": + response = StreamingResponse(status_code=status_code) + + if body: + response.add_stream(body) + + return response + + @staticmethod + def not_found(body: object = None) -> "StreamingResponse": + return StreamingResponse.create_response(HTTPStatus.NOT_FOUND, body) + + @staticmethod + def forbidden(body: object = None) -> "StreamingResponse": + return StreamingResponse.create_response(HTTPStatus.FORBIDDEN, body) + + # pylint: disable=invalid-name + @staticmethod + def ok(body: object = None) -> "StreamingResponse": + return StreamingResponse.create_response(HTTPStatus.OK, body) + + @staticmethod + def internal_server_error(body: object = None) -> "StreamingResponse": + return StreamingResponse.create_response(HTTPStatus.INTERNAL_SERVER_ERROR, body) diff --git a/libraries/botframework-streaming/botframework/streaming/transport/__init__.py b/libraries/botframework-streaming/botframework/streaming/transport/__init__.py new file mode 100644 index 000000000..3939e47a5 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/__init__.py @@ -0,0 +1,18 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .disconnected_event_args import DisconnectedEventArgs +from .streaming_transport_service import StreamingTransportService +from .transport_base import TransportBase +from .transport_constants import TransportConstants +from .transport_receiver_base import TransportReceiverBase +from .transport_sender_base import TransportSenderBase + +__all__ = [ + "DisconnectedEventArgs", + "StreamingTransportService", + "TransportBase", + "TransportConstants", + "TransportReceiverBase", + "TransportSenderBase", +] diff --git a/libraries/botframework-streaming/botframework/streaming/transport/disconnected_event_args.py b/libraries/botframework-streaming/botframework/streaming/transport/disconnected_event_args.py new file mode 100644 index 000000000..9db882219 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/disconnected_event_args.py @@ -0,0 +1,10 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +class DisconnectedEventArgs: + def __init__(self, *, reason: str = None): + self.reason = reason + + +DisconnectedEventArgs.empty = DisconnectedEventArgs() diff --git a/libraries/botframework-streaming/botframework/streaming/transport/streaming_transport_service.py b/libraries/botframework-streaming/botframework/streaming/transport/streaming_transport_service.py new file mode 100644 index 000000000..498f7198c --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/streaming_transport_service.py @@ -0,0 +1,12 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC + + +class StreamingTransportService(ABC): + async def start(self): + raise NotImplementedError() + + async def send(self, request): + raise NotImplementedError() diff --git a/libraries/botframework-streaming/botframework/streaming/transport/transport_base.py b/libraries/botframework-streaming/botframework/streaming/transport/transport_base.py new file mode 100644 index 000000000..4955f96e8 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/transport_base.py @@ -0,0 +1,10 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + + +class TransportBase: + def __init__(self): + self.is_connected: bool = None + + def close(self): + return diff --git a/libraries/botframework-streaming/botframework/streaming/transport/transport_constants.py b/libraries/botframework-streaming/botframework/streaming/transport/transport_constants.py new file mode 100644 index 000000000..139099512 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/transport_constants.py @@ -0,0 +1,11 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC + + +class TransportConstants(ABC): + MAX_PAYLOAD_LENGTH = 4096 + MAX_HEADER_LENGTH = 48 + MAX_LENGTH = 999999 + MIN_LENGTH = 0 diff --git a/libraries/botframework-streaming/botframework/streaming/transport/transport_receiver_base.py b/libraries/botframework-streaming/botframework/streaming/transport/transport_receiver_base.py new file mode 100644 index 000000000..e7e849a49 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/transport_receiver_base.py @@ -0,0 +1,11 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC + +from .transport_base import TransportBase + + +class TransportReceiverBase(ABC, TransportBase): + async def receive(self, buffer: object, offset: int, count: int) -> int: + raise NotImplementedError() diff --git a/libraries/botframework-streaming/botframework/streaming/transport/transport_sender_base.py b/libraries/botframework-streaming/botframework/streaming/transport/transport_sender_base.py new file mode 100644 index 000000000..33d647159 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/transport_sender_base.py @@ -0,0 +1,11 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC + +from .transport_base import TransportBase + + +class TransportSenderBase(ABC, TransportBase): + async def send(self, buffer: object, offset: int, count: int) -> int: + raise NotImplementedError() diff --git a/libraries/botframework-streaming/botframework/streaming/transport/web_socket/__init__.py b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/__init__.py new file mode 100644 index 000000000..ef5847cbf --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/__init__.py @@ -0,0 +1,20 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .web_socket import WebSocketMessage +from .web_socket import WebSocket +from .web_socket_close_status import WebSocketCloseStatus +from .web_socket_server import WebSocketServer +from .web_socket_message_type import WebSocketMessageType +from .web_socket_transport import WebSocketTransport +from .web_socket_state import WebSocketState + +__all__ = [ + "WebSocketMessage", + "WebSocket", + "WebSocketCloseStatus", + "WebSocketMessageType", + "WebSocketServer", + "WebSocketTransport", + "WebSocketState", +] diff --git a/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket.py b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket.py new file mode 100644 index 000000000..c50cc1181 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket.py @@ -0,0 +1,35 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from abc import ABC +from typing import List, Any + +from .web_socket_close_status import WebSocketCloseStatus +from .web_socket_state import WebSocketState +from .web_socket_message_type import WebSocketMessageType + + +class WebSocketMessage: + def __init__(self, *, message_type: WebSocketMessageType, data: List[int]): + self.message_type = message_type + self.data = data + + +class WebSocket(ABC): + def dispose(self): + raise NotImplementedError() + + async def close(self, close_status: WebSocketCloseStatus, status_description: str): + raise NotImplementedError() + + async def receive(self) -> WebSocketMessage: + raise NotImplementedError() + + async def send( + self, buffer: Any, message_type: WebSocketMessageType, end_of_message: bool + ): + raise NotImplementedError() + + @property + def status(self) -> WebSocketState: + raise NotImplementedError() diff --git a/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_close_status.py b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_close_status.py new file mode 100644 index 000000000..417c6588c --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_close_status.py @@ -0,0 +1,17 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from enum import IntEnum + + +class WebSocketCloseStatus(IntEnum): + NORMAL_CLOSURE = 1000 + ENDPOINT_UNAVAILABLE = 1001 + PROTOCOL_ERROR = 1002 + INVALID_MESSAGE_TYPE = 1003 + EMPTY = 1005 + INVALID_PAYLOAD_DATA = 1007 + POLICY_VIOLATION = 1008 + MESSAGE_TOO_BIG = 1009 + MANDATORY_EXTENSION = 1010 + INTERNAL_SERVER_ERROR = 1011 diff --git a/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_message_type.py b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_message_type.py new file mode 100644 index 000000000..658b7e073 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_message_type.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from enum import IntEnum + + +class WebSocketMessageType(IntEnum): + # websocket spec types + CONTINUATION = 0 + TEXT = 1 + BINARY = 2 + PING = 9 + PONG = 10 + CLOSE = 8 diff --git a/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_server.py b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_server.py new file mode 100644 index 000000000..67d0d8336 --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_server.py @@ -0,0 +1,97 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from asyncio import Future, iscoroutinefunction, isfuture +from typing import Callable + +from botframework.streaming import ( + ProtocolAdapter, + ReceiveResponse, + RequestHandler, + StreamingRequest, +) +from botframework.streaming.payloads import RequestManager +from botframework.streaming.payload_transport import PayloadSender, PayloadReceiver +from botframework.streaming.transport import DisconnectedEventArgs + +from .web_socket import WebSocket +from .web_socket_transport import WebSocketTransport + + +class WebSocketServer: + def __init__(self, socket: WebSocket, request_handler: RequestHandler): + if socket is None: + raise TypeError( + f"'socket: {socket.__class__.__name__}' argument can't be None" + ) + if not request_handler: + raise TypeError( + f"'request_handler: {request_handler.__class__.__name__}' argument can't be None" + ) + + self.disconnected_event_handler: Callable[ + [object, DisconnectedEventArgs], None + ] = None + + self._web_socket_transport = WebSocketTransport(socket) + self._request_handler = request_handler + self._request_manager = RequestManager() + self._sender = PayloadSender() + self._sender.disconnected = self._on_connection_disconnected + self._receiver = PayloadReceiver() + self._receiver.disconnected = self._on_connection_disconnected + self._protocol_adapter = ProtocolAdapter( + self._request_handler, self._request_manager, self._sender, self._receiver + ) + self._closed_signal: Future = None + self._is_disconnecting: bool = False + + @property + def is_connected(self) -> bool: + return self._sender.is_connected and self._receiver.is_connected + + async def start(self): + self._closed_signal = Future() + self._sender.connect(self._web_socket_transport) + await self._receiver.connect(self._web_socket_transport) + + return self._closed_signal + + async def send(self, request: StreamingRequest) -> ReceiveResponse: + if not request: + raise TypeError( + f"'request: {request.__class__.__name__}' argument can't be None" + ) + + if not self._sender.is_connected or not self._sender.is_connected: + raise RuntimeError("The server is not connected") + + return await self._protocol_adapter.send_request(request) + + async def disconnect(self): + await self._sender.disconnect() + await self._receiver.disconnect() + + async def _on_connection_disconnected( + self, sender: object, event_args: object # pylint: disable=unused-argument + ): + if not self._is_disconnecting: + self._is_disconnecting = True + + if self._closed_signal: + self._closed_signal.set_result("close") + self._closed_signal = None + + if sender in [self._sender, self._receiver]: + if iscoroutinefunction(sender.disconnect) or isfuture( + sender.disconnect + ): + await sender.disconnect() + else: + sender.disconnect() + + if self.disconnected_event_handler: + # pylint: disable=not-callable + self.disconnected_event_handler(self, DisconnectedEventArgs.empty) + + self._is_disconnecting = False diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/model_runtime/__init__.py b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_state.py similarity index 53% rename from samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/model_runtime/__init__.py rename to libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_state.py index 8c5946fe4..fddd42ec2 100644 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/model_runtime/__init__.py +++ b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_state.py @@ -1,6 +1,9 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -from .bert_model_runtime import BertModelRuntime +from enum import IntEnum -__all__ = ["BertModelRuntime"] + +class WebSocketState(IntEnum): + OPEN = 2 + CLOSED = 5 diff --git a/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_transport.py b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_transport.py new file mode 100644 index 000000000..bd327affa --- /dev/null +++ b/libraries/botframework-streaming/botframework/streaming/transport/web_socket/web_socket_transport.py @@ -0,0 +1,89 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import traceback +from typing import List + +from botframework.streaming.transport import TransportReceiverBase, TransportSenderBase + +from .web_socket import WebSocket +from .web_socket_message_type import WebSocketMessageType +from .web_socket_close_status import WebSocketCloseStatus +from .web_socket_state import WebSocketState + + +class WebSocketTransport(TransportReceiverBase, TransportSenderBase): + def __init__(self, web_socket: WebSocket): + self._socket = web_socket + + @property + def is_connected(self): + # TODO: mock logic + return self._socket.status == WebSocketState.OPEN + + async def close(self): + # TODO: mock logic + if self._socket.status == WebSocketState.OPEN: + try: + await self._socket.close( + WebSocketCloseStatus.NORMAL_CLOSURE, + "Closed by the WebSocketTransport", + ) + except Exception: + # pylint: disable=pointless-string-statement + """ + Any exception thrown here will be caused by the socket already being closed, + which is the state we want to put it in by calling this method, which + means we don't care if it was already closed and threw an exception + when we tried to close it again. + """ + traceback.print_exc() + + # TODO: might need to remove offset and count if no segmentation possible + # TODO: considering to create a BFTransportBuffer class to abstract the logic of binary buffers adapting to + # current interfaces + async def receive( + self, buffer: List[int], offset: int = 0, count: int = None + ) -> int: + try: + if self._socket: + result = await self._socket.receive() + buffer_index = offset + result_length = count if count is not None else len(result.data) + for result_index in range(result_length): + buffer[buffer_index] = result.data[result_index] + buffer_index += 1 + if result.message_type == WebSocketMessageType.CLOSE: + await self._socket.close( + WebSocketCloseStatus.NORMAL_CLOSURE, "Socket closed" + ) + + # Depending on ws implementation library next line might not be necessary + if self._socket.status == WebSocketState.CLOSED: + self._socket.dispose() + + return result_length + except Exception as error: + # Exceptions of the three types below will also have set the socket's state to closed, which fires an + # event consumers of this class are subscribed to and have handling around. Any other exception needs to + # be thrown to cause a non-transport-connectivity failure. + raise error + + # TODO: might need to remove offset and count if no segmentation possible (or put them in BFTransportBuffer) + async def send(self, buffer: List[int], offset: int = 0, count: int = None) -> int: + try: + if self._socket: + await self._socket.send( + buffer[offset:count] if count is not None else buffer, + WebSocketMessageType.BINARY, + True, + ) + return count or len(buffer) + except Exception as error: + # Exceptions of the three types below will also have set the socket's state to closed, which fires an + # event consumers of this class are subscribed to and have handling around. Any other exception needs to + # be thrown to cause a non-transport-connectivity failure. + traceback.print_exc() + raise error + + return 0 diff --git a/libraries/botframework-streaming/requirements.txt b/libraries/botframework-streaming/requirements.txt new file mode 100644 index 000000000..d951e779a --- /dev/null +++ b/libraries/botframework-streaming/requirements.txt @@ -0,0 +1,3 @@ +msrest==0.7.* +botframework-connector>=4.17.0 +botbuilder-schema>=4.17.0 \ No newline at end of file diff --git a/libraries/botframework-streaming/setup.py b/libraries/botframework-streaming/setup.py new file mode 100644 index 000000000..76c1e9549 --- /dev/null +++ b/libraries/botframework-streaming/setup.py @@ -0,0 +1,58 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os +from setuptools import setup + +VERSION = os.environ["packageVersion"] if "packageVersion" in os.environ else "4.17.0" +REQUIRES = [ + "botbuilder-schema==4.17.0", + "botframework-connector==4.17.0", +] + +root = os.path.abspath(os.path.dirname(__file__)) + +with open(os.path.join(root, "botframework", "streaming", "about.py")) as f: + package_info = {} + info = f.read() + exec(info, package_info) + +with open(os.path.join(root, "README.rst"), encoding="utf-8") as f: + long_description = f.read() + +setup( + name=package_info["__title__"], + version=package_info["__version__"], + url=package_info["__uri__"], + author=package_info["__author__"], + description=package_info["__description__"], + keywords=[ + "BotFrameworkStreaming", + "bots", + "ai", + "botframework", + "botframework", + ], + long_description=long_description, + long_description_content_type="text/x-rst", + license=package_info["__license__"], + packages=[ + "botframework.streaming", + "botframework.streaming.payloads", + "botframework.streaming.payloads.assemblers", + "botframework.streaming.payloads.disassemblers", + "botframework.streaming.payloads.models", + "botframework.streaming.payload_transport", + "botframework.streaming.transport", + "botframework.streaming.transport.web_socket", + ], + install_requires=REQUIRES, + classifiers=[ + "Programming Language :: Python :: 3.7", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Development Status :: 5 - Production/Stable", + "Topic :: Scientific/Engineering :: Artificial Intelligence", + ], +) diff --git a/libraries/botframework-streaming/tests/test_content_stream.py b/libraries/botframework-streaming/tests/test_content_stream.py new file mode 100644 index 000000000..4415f0b72 --- /dev/null +++ b/libraries/botframework-streaming/tests/test_content_stream.py @@ -0,0 +1,34 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import uuid4 + +import aiounittest + +from botframework.streaming.payloads import ContentStream +from botframework.streaming.payloads.assemblers import PayloadStreamAssembler + + +class TestResponses(aiounittest.AsyncTestCase): + async def test_content_stream_ctor_none_assembler_throws(self): + with self.assertRaises(TypeError): + ContentStream(uuid4(), None) + + async def test_content_stream_id(self): + test_id = uuid4() + test_assembler = PayloadStreamAssembler(None, test_id) + sut = ContentStream(test_id, test_assembler) + + self.assertEqual(test_id, sut.identifier) + + async def test_content_stream_type(self): + test_id = uuid4() + test_assembler = PayloadStreamAssembler(None, test_id) + sut = ContentStream(test_id, test_assembler) + test_type = "foo/bar" + + sut.content_type = test_type + + self.assertEqual(test_type, sut.content_type) + + sut.cancel() diff --git a/libraries/botframework-streaming/tests/test_header_serializer.py b/libraries/botframework-streaming/tests/test_header_serializer.py new file mode 100644 index 000000000..ac0be0c6b --- /dev/null +++ b/libraries/botframework-streaming/tests/test_header_serializer.py @@ -0,0 +1,136 @@ +from typing import List +from unittest import TestCase +from uuid import uuid4, UUID + +import pytest +from botframework.streaming.payloads import HeaderSerializer +from botframework.streaming.payloads.models import Header, PayloadTypes +from botframework.streaming.transport import TransportConstants + + +class TestHeaderSerializer(TestCase): + def test_can_round_trip(self): + header = Header() + header.type = PayloadTypes.REQUEST + header.payload_length = 168 + header.id = uuid4() + header.end = True + + buffer: List[int] = [None] * TransportConstants.MAX_PAYLOAD_LENGTH + offset: int = 0 + + length = HeaderSerializer.serialize(header, buffer, offset) + result = HeaderSerializer.deserialize(buffer, 0, length) + + self.assertEqual(header.type, result.type) + self.assertEqual(header.payload_length, result.payload_length) + self.assertEqual(header.id, result.id) + self.assertEqual(header.end, result.end) + + def test_serializes_to_ascii(self): + header = Header() + header.type = PayloadTypes.REQUEST + header.payload_length = 168 + header.id = uuid4() + header.end = True + + buffer: List[int] = [None] * TransportConstants.MAX_PAYLOAD_LENGTH + offset: int = 0 + + length = HeaderSerializer.serialize(header, buffer, offset) + decoded = bytes(buffer[offset:length]).decode("ascii") + + self.assertEqual(f"A.000168.{str(header.id)}.1\n", decoded) + + def test_deserializes_from_ascii(self): + header_id: UUID = uuid4() + header: str = f"A.000168.{str(header_id)}.1\n" + buffer: List[int] = list(bytes(header, "ascii")) + + result = HeaderSerializer.deserialize(buffer, 0, len(buffer)) + + self.assertEqual("A", result.type) + self.assertEqual(168, result.payload_length) + self.assertEqual(header_id, result.id) + self.assertTrue(result.end) + + def test_deserialize_unknown_type(self): + header_id: UUID = uuid4() + header: str = f"Z.000168.{str(header_id)}.1\n" + buffer: List[int] = list(bytes(header, "ascii")) + + result = HeaderSerializer.deserialize(buffer, 0, len(buffer)) + + self.assertEqual("Z", result.type) + self.assertEqual(168, result.payload_length) + + def test_deserialize_length_too_short_throws(self): + header_id: UUID = uuid4() + header: str = f"A.000168.{str(header_id)}.1\n" + buffer: List[int] = list(bytes(header, "ascii")) + + with pytest.raises(ValueError): + HeaderSerializer.deserialize(buffer, 0, 5) + + def test_deserialize_length_too_long_throws(self): + header_id: UUID = uuid4() + header: str = f"A.000168.{str(header_id)}.1\n" + buffer: List[int] = list(bytes(header, "ascii")) + + with pytest.raises(ValueError): + HeaderSerializer.deserialize(buffer, 0, 55) + + def test_deserialize_bad_type_delimiter_throws(self): + header_id: UUID = uuid4() + header: str = f"Ax000168.{str(header_id)}.1\n" + buffer: List[int] = list(bytes(header, "ascii")) + + with pytest.raises(ValueError): + HeaderSerializer.deserialize(buffer, 0, len(buffer)) + + def test_deserialize_bad_length_delimiter_throws(self): + header_id: UUID = uuid4() + header: str = f"A.000168x{str(header_id)}.1\n" + buffer: List[int] = list(bytes(header, "ascii")) + + with pytest.raises(ValueError): + HeaderSerializer.deserialize(buffer, 0, len(buffer)) + + def test_deserialize_bad_id_delimiter_throws(self): + header_id: UUID = uuid4() + header: str = f"A.000168.{str(header_id)}x1\n" + buffer: List[int] = list(bytes(header, "ascii")) + + with pytest.raises(ValueError): + HeaderSerializer.deserialize(buffer, 0, len(buffer)) + + def test_deserialize_bad_terminator_throws(self): + header_id: UUID = uuid4() + header: str = f"A.000168.{str(header_id)}.1c" + buffer: List[int] = list(bytes(header, "ascii")) + + with pytest.raises(ValueError): + HeaderSerializer.deserialize(buffer, 0, len(buffer)) + + def test_deserialize_bad_length_throws(self): + header_id: UUID = uuid4() + header: str = f"A.00p168.{str(header_id)}.1\n" + buffer: List[int] = list(bytes(header, "ascii")) + + with pytest.raises(ValueError): + HeaderSerializer.deserialize(buffer, 0, len(buffer)) + + def test_deserialize_bad_id_throws(self): + header: str = "A.000168.68e9p9ca-a651-40f4-ad8f-3aaf781862b4.1\n" + buffer: List[int] = list(bytes(header, "ascii")) + + with pytest.raises(ValueError): + HeaderSerializer.deserialize(buffer, 0, len(buffer)) + + def test_deserialize_bad_end_throws(self): + header_id: UUID = uuid4() + header: str = f"A.000168.{str(header_id)}.z\n" + buffer: List[int] = list(bytes(header, "ascii")) + + with pytest.raises(ValueError): + HeaderSerializer.deserialize(buffer, 0, len(buffer)) diff --git a/libraries/botframework-streaming/tests/test_payload_assembler.py b/libraries/botframework-streaming/tests/test_payload_assembler.py new file mode 100644 index 000000000..a8cea4580 --- /dev/null +++ b/libraries/botframework-streaming/tests/test_payload_assembler.py @@ -0,0 +1,57 @@ +from unittest import TestCase +from uuid import UUID, uuid4 + +from botframework.streaming.payloads import StreamManager +from botframework.streaming.payloads.assemblers import PayloadStreamAssembler +from botframework.streaming.payloads.models import Header + + +class TestPayloadAssembler(TestCase): + def test_ctor_id(self): + identifier: UUID = uuid4() + stream_manager = StreamManager() + assembler = PayloadStreamAssembler(stream_manager, identifier) + self.assertEqual(identifier, assembler.identifier) + + def test_ctor_end_false(self): + identifier: UUID = uuid4() + stream_manager = StreamManager() + assembler = PayloadStreamAssembler(stream_manager, identifier) + self.assertFalse(assembler.end) + + def test_get_stream(self): + identifier: UUID = uuid4() + stream_manager = StreamManager() + assembler = PayloadStreamAssembler(stream_manager, identifier) + stream = assembler.get_payload_as_stream() + self.assertIsNotNone(stream) + + def test_get_stream_does_not_make_new_each_time(self): + identifier: UUID = uuid4() + stream_manager = StreamManager() + assembler = PayloadStreamAssembler(stream_manager, identifier) + stream1 = assembler.get_payload_as_stream() + stream2 = assembler.get_payload_as_stream() + self.assertEqual(stream1, stream2) + + def test_on_receive_sets_end(self): + identifier: UUID = uuid4() + stream_manager = StreamManager() + assembler = PayloadStreamAssembler(stream_manager, identifier) + + header = Header() + header.end = True + + assembler.get_payload_as_stream() + assembler.on_receive(header, [], 100) + + self.assertTrue(assembler.end) + + def test_close_does_not_set_end(self): + identifier: UUID = uuid4() + stream_manager = StreamManager() + assembler = PayloadStreamAssembler(stream_manager, identifier) + + assembler.close() + + self.assertFalse(assembler.end) diff --git a/libraries/botframework-streaming/tests/test_payload_processor.py b/libraries/botframework-streaming/tests/test_payload_processor.py new file mode 100644 index 000000000..456775d9e --- /dev/null +++ b/libraries/botframework-streaming/tests/test_payload_processor.py @@ -0,0 +1,57 @@ +from typing import List +from uuid import UUID, uuid4 + +import aiounittest +from botframework.streaming import ReceiveRequest +from botframework.streaming.payloads import StreamManager +from botframework.streaming.payloads.assemblers import ( + ReceiveRequestAssembler, + PayloadStreamAssembler, +) +from botframework.streaming.payloads.models import ( + Header, + RequestPayload, + StreamDescription, +) + + +class MockStreamManager(StreamManager): + def get_payload_assembler(self, identifier: UUID) -> PayloadStreamAssembler: + return PayloadStreamAssembler(self, identifier) + + +class TestPayloadProcessor(aiounittest.AsyncTestCase): + async def test_process_request(self): + # Arrange + header_id: UUID = uuid4() + header = Header(type="A", id=header_id, end=True) + header.payload_length = 3 + stream_manager = MockStreamManager() + + on_completed_called = False + + async def mock_on_completed(identifier: UUID, request: ReceiveRequest): + nonlocal on_completed_called + assert identifier == header_id + assert request.verb == "POST" + assert request.path == "/api/messages" + assert len(request.streams) == 1 + on_completed_called = True + + sut = ReceiveRequestAssembler( + header, stream_manager, on_completed=mock_on_completed + ) + + # Act + stream_id: UUID = uuid4() + streams: List[StreamDescription] = [ + StreamDescription(id=str(stream_id), content_type="json", length=100) + ] + payload = RequestPayload( + verb="POST", path="/api/messages", streams=streams + ).to_json() + payload_stream: List[int] = list(bytes(payload, "utf-8")) + await sut.process_request(payload_stream) + + # Assert + assert on_completed_called diff --git a/libraries/botframework-streaming/tests/test_payload_receiver.py b/libraries/botframework-streaming/tests/test_payload_receiver.py new file mode 100644 index 000000000..00c1253c2 --- /dev/null +++ b/libraries/botframework-streaming/tests/test_payload_receiver.py @@ -0,0 +1,71 @@ +from typing import List + +import aiounittest + +from botframework.streaming import PayloadStream +from botframework.streaming.payload_transport import PayloadReceiver +from botframework.streaming.transport import TransportReceiverBase + + +class MockTransportReceiver(TransportReceiverBase): + # pylint: disable=unused-argument + def __init__(self, mock_header: bytes, mock_payload: bytes): + self._is_connected = True + self._mock_gen = self._mock_receive(mock_header, mock_payload) + + def _mock_receive(self, mock_header: bytes, mock_payload: bytes): + yield mock_header + yield mock_payload + + @property + def is_connected(self): + if self._is_connected: + self._is_connected = False + return True + return False + + async def close(self): + return + + async def receive(self, buffer: object, offset: int, count: int) -> int: + resp_buffer = list(next(self._mock_gen)) + for index, val in enumerate(resp_buffer): + buffer[index] = val + return len(resp_buffer) + + +class MockStream(PayloadStream): + # pylint: disable=super-init-not-called + def __init__(self): + self.buffer = None + self._producer_length = 0 # total length + + def give_buffer(self, buffer: List[int]): + self.buffer = buffer + + +class TestBotFrameworkHttpClient(aiounittest.AsyncTestCase): + async def test_connect(self): + mock_header = b"S.000004.e35ed534-0808-4acf-af1e-24aa81d2b31d.1\n" + mock_payload = b"test" + + mock_receiver = MockTransportReceiver(mock_header, mock_payload) + mock_stream = MockStream() + + receive_action_called = False + + def mock_get_stream(header): # pylint: disable=unused-argument + return mock_stream + + def mock_receive_action(header, stream, offset): + nonlocal receive_action_called + assert header.type == "S" + assert len(stream.buffer) == offset + receive_action_called = True + + sut = PayloadReceiver() + sut.subscribe(mock_get_stream, mock_receive_action) + await sut.connect(mock_receiver) + + assert bytes(mock_stream.buffer) == mock_payload + assert receive_action_called diff --git a/libraries/botframework-streaming/tests/test_payload_sender.py b/libraries/botframework-streaming/tests/test_payload_sender.py new file mode 100644 index 000000000..242e0de45 --- /dev/null +++ b/libraries/botframework-streaming/tests/test_payload_sender.py @@ -0,0 +1,60 @@ +from asyncio import Semaphore +from typing import List +from uuid import UUID, uuid4 + +import aiounittest +from botframework.streaming.payload_transport import PayloadSender +from botframework.streaming.payloads import HeaderSerializer +from botframework.streaming.payloads.models import Header +from botframework.streaming.transport import TransportSenderBase + + +class MockTransportSender(TransportSenderBase): + def __init__(self): + super().__init__() + self.send_called = Semaphore(0) + + async def send(self, buffer: List[int], offset: int, count: int) -> int: + # Assert + if count == 48: # Header + print("Validating Header...") + header = HeaderSerializer.deserialize(buffer, offset, count) + assert header.type == "A" + assert header.payload_length == 3 + assert header.end + else: # Payload + print("Validating Payload...") + assert count == 3 + self.send_called.release() + + return count + + def close(self): + pass + + +class TestPayloadSender(aiounittest.AsyncTestCase): + async def test_send(self): + # Arrange + sut = PayloadSender() + sender = MockTransportSender() + sut.connect(sender) + + header_id: UUID = uuid4() + header = Header(type="A", id=header_id, end=True) + header.payload_length = 3 + payload = [1, 2, 3] + + async def mock_sent_callback(callback_header: Header): + print( + f"{callback_header.type}.{callback_header.payload_length}.{callback_header.id}.{callback_header.end}" + ) + + # Act + sut.send_payload( + header, payload, is_length_known=True, sent_callback=mock_sent_callback + ) + + # Assert + await sender.send_called.acquire() + await sut.disconnect() diff --git a/libraries/botframework-streaming/tests/test_request_manager.py b/libraries/botframework-streaming/tests/test_request_manager.py new file mode 100644 index 000000000..20358d3c5 --- /dev/null +++ b/libraries/botframework-streaming/tests/test_request_manager.py @@ -0,0 +1,119 @@ +import asyncio +from asyncio import Future, ensure_future +from typing import Dict +from uuid import UUID, uuid4 + +import aiounittest +from botframework.streaming import ReceiveResponse +from botframework.streaming.payloads import RequestManager + + +class TestRequestManager(aiounittest.AsyncTestCase): + def test_ctor_empty_dictionary(self): + pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} + _ = RequestManager(pending_requests=pending_requests) + + self.assertEqual(0, len(pending_requests)) + + async def test_signal_response_returns_false_when_no_uuid(self): + pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} + manager = RequestManager(pending_requests=pending_requests) + request_id: UUID = uuid4() + response = ReceiveResponse() + signal = await manager.signal_response(request_id=request_id, response=response) + + self.assertFalse(signal) + + async def test_signal_response_returns_true_when_uuid(self): + pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} + request_id: UUID = uuid4() + pending_requests[request_id] = Future() + + manager = RequestManager(pending_requests=pending_requests) + + response = ReceiveResponse() + signal = await manager.signal_response(request_id=request_id, response=response) + + self.assertTrue(signal) + + async def test_signal_response_null_response_is_ok(self): + pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} + request_id: UUID = uuid4() + pending_requests[request_id] = Future() + + manager = RequestManager(pending_requests=pending_requests) + + # noinspection PyTypeChecker + _ = await manager.signal_response(request_id=request_id, response=None) + + self.assertIsNone(pending_requests[request_id].result()) + + async def test_signal_response_response(self): + pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} + request_id: UUID = uuid4() + pending_requests[request_id] = Future() + + manager = RequestManager(pending_requests=pending_requests) + response = ReceiveResponse() + + _ = await manager.signal_response(request_id=request_id, response=response) + + self.assertEqual(response, pending_requests[request_id].result()) + + async def test_get_response_returns_null_on_duplicate_call(self): + pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} + request_id: UUID = uuid4() + pending_requests[request_id] = Future() + + manager = RequestManager(pending_requests=pending_requests) + + response = await manager.get_response(request_id) + + self.assertIsNone(response) + + async def test_get_response_returns_response(self): + pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} + request_id: UUID = uuid4() + + manager = RequestManager(pending_requests=pending_requests) + test_response = ReceiveResponse() + + async def set_response(): + nonlocal manager + nonlocal request_id + nonlocal test_response + + while True: + signal = await manager.signal_response( + request_id, response=test_response + ) + if signal: + break + await asyncio.sleep(2) + + ensure_future(set_response()) + response = await manager.get_response(request_id) + + self.assertEqual(test_response, response) + + async def test_get_response_returns_null_response(self): + pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} + request_id: UUID = uuid4() + + manager = RequestManager(pending_requests=pending_requests) + + async def set_response(): + nonlocal manager + nonlocal request_id + + while True: + # noinspection PyTypeChecker + signal = await manager.signal_response(request_id, response=None) + if signal: + break + await asyncio.sleep(2) + + ensure_future(set_response()) + response = await manager.get_response(request_id) + + self.assertIsNone(response) diff --git a/libraries/botframework-streaming/tests/test_requests.py b/libraries/botframework-streaming/tests/test_requests.py new file mode 100644 index 000000000..3c35258fb --- /dev/null +++ b/libraries/botframework-streaming/tests/test_requests.py @@ -0,0 +1,129 @@ +import json + +import aiounittest + +from botbuilder.schema import Activity +from botframework.streaming import ReceiveRequest, StreamingRequest +from botframework.streaming.payloads import ResponseMessageStream + + +class TestRequests(aiounittest.AsyncTestCase): + async def test_receive_request_empty_streams(self): + sut = ReceiveRequest() + + self.assertIsNotNone(sut.streams) + self.assertEqual(0, len(sut.streams)) + + async def test_receive_request_null_properties(self): + sut = ReceiveRequest() + + self.assertIsNone(sut.verb) + self.assertIsNone(sut.path) + + async def test_streaming_request_null_properties(self): + sut = StreamingRequest() + + self.assertIsNone(sut.verb) + self.assertIsNone(sut.path) + + async def test_streaming_request_add_stream_null_throws(self): + sut = StreamingRequest() + + with self.assertRaises(TypeError): + sut.add_stream(None) + + async def test_streaming_request_add_stream_success(self): + sut = StreamingRequest() + content = "hi" + + sut.add_stream(content) + + self.assertIsNotNone(sut.streams) + self.assertEqual(1, len(sut.streams)) + self.assertEqual(content, sut.streams[0].content) + + async def test_streaming_request_add_stream_existing_list_success(self): + sut = StreamingRequest() + content = "hi" + content_2 = "hello" + + sut.streams = [ResponseMessageStream(content=content_2)] + + sut.add_stream(content) + + self.assertIsNotNone(sut.streams) + self.assertEqual(2, len(sut.streams)) + self.assertEqual(content_2, sut.streams[0].content) + self.assertEqual(content, sut.streams[1].content) + + async def test_streaming_request_create_get_success(self): + sut = StreamingRequest.create_get() + + self.assertEqual(StreamingRequest.GET, sut.verb) + self.assertIsNone(sut.path) + self.assertIsNone(sut.streams) + + async def test_streaming_request_create_post_success(self): + sut = StreamingRequest.create_post() + + self.assertEqual(StreamingRequest.POST, sut.verb) + self.assertIsNone(sut.path) + self.assertIsNone(sut.streams) + + async def test_streaming_request_create_delete_success(self): + sut = StreamingRequest.create_delete() + + self.assertEqual(StreamingRequest.DELETE, sut.verb) + self.assertIsNone(sut.path) + self.assertIsNone(sut.streams) + + async def test_streaming_request_create_put_success(self): + sut = StreamingRequest.create_put() + + self.assertEqual(StreamingRequest.PUT, sut.verb) + self.assertIsNone(sut.path) + self.assertIsNone(sut.streams) + + async def test_streaming_request_create_with_body_success(self): + content = "hi" + sut = StreamingRequest.create_request(StreamingRequest.POST, "123", content) + + self.assertEqual(StreamingRequest.POST, sut.verb) + self.assertEqual("123", sut.path) + self.assertIsNotNone(sut.streams) + self.assertEqual(1, len(sut.streams)) + self.assertEqual(content, sut.streams[0].content) + + async def test_streaming_request_set_body_string_success(self): + sut = StreamingRequest() + + sut.set_body("123") + + self.assertIsNotNone(sut.streams) + self.assertEqual(1, len(sut.streams)) + self.assertIsInstance(sut.streams[0].content, list) + self.assertIsInstance(sut.streams[0].content[0], int) + self.assertEqual("123", bytes(sut.streams[0].content).decode("utf-8-sig")) + + async def test_streaming_request_set_body_none_does_not_throw(self): + sut = StreamingRequest() + + sut.set_body(None) + + async def test_streaming_request_set_body_success(self): + sut = StreamingRequest() + activity = Activity(text="hi", type="message") + + sut.set_body(activity) + + self.assertIsNotNone(sut.streams) + self.assertEqual(1, len(sut.streams)) + self.assertIsInstance(sut.streams[0].content, list) + self.assertIsInstance(sut.streams[0].content[0], int) + + assert_activity = Activity.deserialize( + json.loads(bytes(sut.streams[0].content).decode("utf-8-sig")) + ) + + self.assertEqual(activity.text, assert_activity.text) + self.assertEqual(activity.type, assert_activity.type) diff --git a/libraries/botframework-streaming/tests/test_responses.py b/libraries/botframework-streaming/tests/test_responses.py new file mode 100644 index 000000000..e99d25e80 --- /dev/null +++ b/libraries/botframework-streaming/tests/test_responses.py @@ -0,0 +1,132 @@ +import json +from http import HTTPStatus + +import aiounittest + +from botbuilder.schema import Activity +from botframework.streaming import ReceiveResponse, StreamingResponse +from botframework.streaming.payloads import ResponseMessageStream + + +class TestResponses(aiounittest.AsyncTestCase): + async def test_receive_response_empty_streams(self): + sut = ReceiveResponse() + + self.assertIsNotNone(sut.streams) + self.assertEqual(0, len(sut.streams)) + + async def test_receive_response_none_properties(self): + sut = ReceiveResponse() + + self.assertEqual(0, sut.status_code) + + async def test_streaming_response_null_properties(self): + sut = StreamingResponse() + + self.assertEqual(0, sut.status_code) + self.assertIsNone(sut.streams) + + async def test_streaming_response_add_stream_none_throws(self): + sut = StreamingResponse() + + with self.assertRaises(TypeError): + sut.add_stream(None) + + async def test_streaming_response_add_stream_success(self): + sut = StreamingResponse() + content = "hi" + + sut.add_stream(content) + + self.assertIsNotNone(sut.streams) + self.assertEqual(1, len(sut.streams)) + self.assertEqual(content, sut.streams[0].content) + + async def test_streaming_response_add_stream_existing_list_success(self): + sut = StreamingResponse() + content = "hi" + content_2 = "hello" + + sut.streams = [ResponseMessageStream(content=content_2)] + + sut.add_stream(content) + + self.assertIsNotNone(sut.streams) + self.assertEqual(2, len(sut.streams)) + self.assertEqual(content_2, sut.streams[0].content) + self.assertEqual(content, sut.streams[1].content) + + async def test_streaming_response_not_found_success(self): + sut = StreamingResponse.not_found() + + self.assertEqual(HTTPStatus.NOT_FOUND, sut.status_code) + self.assertIsNone(sut.streams) + + async def test_streaming_response_forbidden_success(self): + sut = StreamingResponse.forbidden() + + self.assertEqual(HTTPStatus.FORBIDDEN, sut.status_code) + self.assertIsNone(sut.streams) + + async def test_streaming_response_ok_success(self): + sut = StreamingResponse.ok() + + self.assertEqual(HTTPStatus.OK, sut.status_code) + self.assertIsNone(sut.streams) + + async def test_streaming_response_internal_server_error_success(self): + sut = StreamingResponse.internal_server_error() + + self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR, sut.status_code) + self.assertIsNone(sut.streams) + + async def test_streaming_response_create_with_body_success(self): + content = "hi" + sut = StreamingResponse.create_response(HTTPStatus.OK, content) + + self.assertEqual(HTTPStatus.OK, sut.status_code) + self.assertIsNotNone(sut.streams) + self.assertEqual(1, len(sut.streams)) + self.assertEqual(content, sut.streams[0].content) + + async def test_streaming_response_set_body_string_success(self): + sut = StreamingResponse() + + sut.set_body("123") + + self.assertIsNotNone(sut.streams) + self.assertEqual(1, len(sut.streams)) + self.assertIsInstance(sut.streams[0].content, list) + self.assertIsInstance(sut.streams[0].content[0], int) + self.assertEqual("123", bytes(sut.streams[0].content).decode("utf-8-sig")) + + async def test_streaming_response_set_body_none_does_not_throw(self): + sut = StreamingResponse() + + sut.set_body(None) + + async def test_streaming_response_set_body_success(self): + sut = StreamingResponse() + activity = Activity(text="hi", type="message") + + sut.set_body(activity) + + self.assertIsNotNone(sut.streams) + self.assertEqual(1, len(sut.streams)) + self.assertIsInstance(sut.streams[0].content, list) + self.assertIsInstance(sut.streams[0].content[0], int) + + assert_activity = Activity.deserialize( + json.loads(bytes(sut.streams[0].content).decode("utf-8-sig")) + ) + + self.assertEqual(activity.text, assert_activity.text) + self.assertEqual(activity.type, assert_activity.type) + + async def test_receive_base_read_body_as_string_no_content_empty_string(self): + sut = ReceiveResponse() + sut.streams = [] + + result = sut.read_body_as_str() + + self.assertEqual("", result) diff --git a/libraries/botframework-streaming/tests/test_send_operations.py b/libraries/botframework-streaming/tests/test_send_operations.py new file mode 100644 index 000000000..926124304 --- /dev/null +++ b/libraries/botframework-streaming/tests/test_send_operations.py @@ -0,0 +1,56 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List +from uuid import uuid4 + +import aiounittest + +from botframework.streaming import PayloadStream, StreamingRequest +from botframework.streaming.payloads import SendOperations +from botframework.streaming.payloads.assemblers import PayloadStreamAssembler +from botframework.streaming.payload_transport import PayloadSender +from botframework.streaming.transport import TransportSenderBase + + +class MockTransportSender(TransportSenderBase): + # pylint: disable=unused-argument + def __init__(self): + super().__init__() + self.is_connected = True + self.buffers = [] + + async def send(self, buffer: List[int], offset: int, count: int) -> int: + self.buffers.append(buffer.copy()) + + return count + + +class TestSendOperations(aiounittest.AsyncTestCase): + async def test_request_dissasembler_with_variable_stream_send(self): + sender = PayloadSender() + transport = MockTransportSender() + sender.connect(transport) + + sut = SendOperations(sender) + + request = StreamingRequest.create_post("/a/b") + stream = PayloadStream(PayloadStreamAssembler(None, uuid4(), "blah", 100)) + stream.write([0] * 100, 0, 100) + request.add_stream(await stream.read_until_end()) + + await sut.send_request(uuid4(), request) + self.assertEqual(4, len(transport.buffers)) + + async def test_request_dissasembler_with_json_stream_send(self): + sender = PayloadSender() + transport = MockTransportSender() + sender.connect(transport) + + sut = SendOperations(sender) + + request = StreamingRequest.create_post("/a/b") + request.add_stream(bytes("abc", "ascii")) + + await sut.send_request(uuid4(), request) + self.assertEqual(4, len(transport.buffers)) diff --git a/libraries/botframework-streaming/tests/test_stream_manager.py b/libraries/botframework-streaming/tests/test_stream_manager.py new file mode 100644 index 000000000..622a4a7a2 --- /dev/null +++ b/libraries/botframework-streaming/tests/test_stream_manager.py @@ -0,0 +1,112 @@ +from unittest import TestCase +from uuid import UUID, uuid4 + +from botframework.streaming.payloads import StreamManager +from botframework.streaming.payloads.assemblers import PayloadStreamAssembler +from botframework.streaming.payloads.models import Header + + +class TestStreamManager(TestCase): + def test_ctor_null_cancel_ok(self): + manager = StreamManager(None) + self.assertIsNotNone(manager) + + def test_get_payload_assembler_not_exists_ok(self): + manager = StreamManager(None) + identifier: UUID = uuid4() + + assembler = manager.get_payload_assembler(identifier) + + self.assertIsNotNone(assembler) + self.assertEqual(identifier, assembler.identifier) + + def test_get_payload_assembler_exists_ok(self): + manager = StreamManager(None) + identifier: UUID = uuid4() + + assembler1 = manager.get_payload_assembler(identifier) + assembler2 = manager.get_payload_assembler(identifier) + + self.assertEqual(assembler1, assembler2) + + def test_get_payload_stream_not_exists_ok(self): + manager = StreamManager(None) + identifier: UUID = uuid4() + + stream = manager.get_payload_stream(Header(id=identifier)) + + self.assertIsNotNone(stream) + + def test_get_payload_stream_exists_ok(self): + manager = StreamManager(None) + identifier: UUID = uuid4() + + stream1 = manager.get_payload_stream(Header(id=identifier)) + stream2 = manager.get_payload_stream(Header(id=identifier)) + + self.assertEqual(stream1, stream2) + + def test_get_payload_stream_streams_match(self): + manager = StreamManager(None) + identifier: UUID = uuid4() + + assembler = manager.get_payload_assembler(identifier) + stream = manager.get_payload_stream(Header(id=identifier)) + + self.assertEqual(assembler.get_payload_as_stream(), stream) + + def test_on_receive_not_exists_no_op(self): + manager = StreamManager(None) + identifier: UUID = uuid4() + + manager.on_receive(Header(id=identifier), [], 100) + + def test_on_receive_exists(self): + manager = StreamManager(None) + identifier: UUID = uuid4() + + assembler = manager.get_payload_assembler(identifier) + assembler.get_payload_as_stream() + + manager.on_receive(Header(id=identifier, end=True), [], 100) + + self.assertTrue(assembler.end) + + def test_close_stream_not_exists_no_op(self): + manager = StreamManager(None) + identifier: UUID = uuid4() + + manager.close_stream(identifier) + + def test_close_stream_not_end_closed(self): + closed = False + + def mock_cancel_stream(_: PayloadStreamAssembler): + nonlocal closed + closed = True + + manager = StreamManager(on_cancel_stream=mock_cancel_stream) + identifier: UUID = uuid4() + assembler = manager.get_payload_assembler(identifier) + assembler.get_payload_as_stream() + + manager.close_stream(identifier) + + self.assertTrue(closed) + + def test_close_stream_end_no_op(self): + closed = False + + def mock_cancel_stream(_: PayloadStreamAssembler): + nonlocal closed + closed = True + + manager = StreamManager(on_cancel_stream=mock_cancel_stream) + identifier: UUID = uuid4() + assembler = manager.get_payload_assembler(identifier) + assembler.get_payload_as_stream() + assembler.on_receive(Header(end=True), [], 1) # Set it as ended + + manager.close_stream(identifier) + + self.assertFalse(closed) diff --git a/libraries/swagger/package-lock.json b/libraries/swagger/package-lock.json deleted file mode 100644 index 6b2ba00be..000000000 --- a/libraries/swagger/package-lock.json +++ /dev/null @@ -1,521 +0,0 @@ -{ - "name": "generateclient", - "version": "1.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "@microsoft.azure/autorest.python": { - "version": "4.0.68", - "resolved": "https://registry.npmjs.org/@microsoft.azure/autorest.python/-/autorest.python-4.0.68.tgz", - "integrity": "sha512-+hSGll3lCRoy1Q6puvyCYvgLyfDYNzAZkWmJRJp6eEVVk3/QbaGvNOZUChwhnI3fkvCCdhO1jZV4LOiXw5dmZw==", - "dev": true, - "requires": { - "dotnet-2.0.0": "^1.4.4" - } - }, - "ansi-regex": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", - "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=", - "dev": true - }, - "autorest": { - "version": "3.0.5165", - "resolved": "https://registry.npmjs.org/autorest/-/autorest-3.0.5165.tgz", - "integrity": "sha512-EarkJLUHiVLQrSrxYa2oHIrCoagyaxNj0RgEB9K+n4n0XnZdYlneIYhdc/V1RDQyiN7eB2CgaV/8LjKMfN9CdA==", - "dev": true - }, - "balanced-match": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true - }, - "brace-expansion": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", - "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, - "requires": { - "balanced-match": "^1.0.0", - "concat-map": "0.0.1" - } - }, - "camelcase": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.2.0.tgz", - "integrity": "sha512-IXFsBS2pC+X0j0N/GE7Dm7j3bsEBp+oTpb7F50dwEVX7rf3IgwO9XatnegTsDtniKCUtEJH4fSU6Asw7uoVLfQ==", - "dev": true - }, - "cliui": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/cliui/-/cliui-4.1.0.tgz", - "integrity": "sha512-4FG+RSG9DL7uEwRUZXZn3SS34DiDPfzP0VOiEwtUWlE+AR2EIg+hSyvrIgUUfhdgR/UkAeW2QHgeP+hWrXs7jQ==", - "dev": true, - "requires": { - "string-width": "^2.1.1", - "strip-ansi": "^4.0.0", - "wrap-ansi": "^2.0.0" - } - }, - "code-point-at": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", - "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=", - "dev": true - }, - "colors": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/colors/-/colors-1.2.4.tgz", - "integrity": "sha512-6Y+iBnWmXL+AWtlOp2Vr6R2w5MUlNJRwR0ShVFaAb1CqWzhPOpQg4L0jxD+xpw/Nc8QJwaq3KM79QUCriY8CWQ==", - "dev": true - }, - "concat-map": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true - }, - "cross-spawn": { - "version": "6.0.5", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", - "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", - "dev": true, - "requires": { - "nice-try": "^1.0.4", - "path-key": "^2.0.1", - "semver": "^5.5.0", - "shebang-command": "^1.2.0", - "which": "^1.2.9" - } - }, - "decamelize": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", - "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=", - "dev": true - }, - "dotnet-2.0.0": { - "version": "1.4.4", - "resolved": "https://registry.npmjs.org/dotnet-2.0.0/-/dotnet-2.0.0-1.4.4.tgz", - "integrity": "sha512-KDbUncVUhwkJH2wjL9gbUWQ5NcZIe+PFEI0CGTMtX5TImFG6Nlt9CABVGBBG+oWf13zLARaBVenkD20moz1NPw==", - "dev": true - }, - "end-of-stream": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.1.tgz", - "integrity": "sha512-1MkrZNvWTKCaigbn+W15elq2BB/L22nqrSY5DKlo3X6+vclJm8Bb5djXJBmEX6fS3+zCh/F4VBK5Z2KxJt4s2Q==", - "dev": true, - "requires": { - "once": "^1.4.0" - } - }, - "execa": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz", - "integrity": "sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==", - "dev": true, - "requires": { - "cross-spawn": "^6.0.0", - "get-stream": "^4.0.0", - "is-stream": "^1.1.0", - "npm-run-path": "^2.0.0", - "p-finally": "^1.0.0", - "signal-exit": "^3.0.0", - "strip-eof": "^1.0.0" - } - }, - "find-up": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", - "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", - "dev": true, - "requires": { - "locate-path": "^3.0.0" - } - }, - "get-caller-file": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz", - "integrity": "sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==", - "dev": true - }, - "get-stream": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", - "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", - "dev": true, - "requires": { - "pump": "^3.0.0" - } - }, - "invert-kv": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", - "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==", - "dev": true - }, - "is-fullwidth-code-point": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", - "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", - "dev": true - }, - "is-stream": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", - "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=", - "dev": true - }, - "isexe": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", - "dev": true - }, - "lcid": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", - "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", - "dev": true, - "requires": { - "invert-kv": "^2.0.0" - } - }, - "locate-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", - "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", - "dev": true, - "requires": { - "p-locate": "^3.0.0", - "path-exists": "^3.0.0" - } - }, - "map-age-cleaner": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz", - "integrity": "sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w==", - "dev": true, - "requires": { - "p-defer": "^1.0.0" - } - }, - "mem": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/mem/-/mem-4.2.0.tgz", - "integrity": "sha512-5fJxa68urlY0Ir8ijatKa3eRz5lwXnRCTvo9+TbTGAuTFJOwpGcY0X05moBd0nW45965Njt4CDI2GFQoG8DvqA==", - "dev": true, - "requires": { - "map-age-cleaner": "^0.1.1", - "mimic-fn": "^2.0.0", - "p-is-promise": "^2.0.0" - } - }, - "mimic-fn": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.0.0.tgz", - "integrity": "sha512-jbex9Yd/3lmICXwYT6gA/j2mNQGU48wCh/VzRd+/Y/PjYQtlg1gLMdZqvu9s/xH7qKvngxRObl56XZR609IMbA==", - "dev": true - }, - "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, - "requires": { - "brace-expansion": "^1.1.7" - } - }, - "nice-try": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", - "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", - "dev": true - }, - "npm-run-path": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz", - "integrity": "sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=", - "dev": true, - "requires": { - "path-key": "^2.0.0" - } - }, - "number-is-nan": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", - "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=", - "dev": true - }, - "once": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, - "requires": { - "wrappy": "1" - } - }, - "os-locale": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", - "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", - "dev": true, - "requires": { - "execa": "^1.0.0", - "lcid": "^2.0.0", - "mem": "^4.0.0" - } - }, - "p-defer": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz", - "integrity": "sha1-n26xgvbJqozXQwBKfU+WsZaw+ww=", - "dev": true - }, - "p-finally": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", - "integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=", - "dev": true - }, - "p-is-promise": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.0.0.tgz", - "integrity": "sha512-pzQPhYMCAgLAKPWD2jC3Se9fEfrD9npNos0y150EeqZll7akhEgGhTW/slB6lHku8AvYGiJ+YJ5hfHKePPgFWg==", - "dev": true - }, - "p-limit": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.0.tgz", - "integrity": "sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==", - "dev": true, - "requires": { - "p-try": "^2.0.0" - } - }, - "p-locate": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", - "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", - "dev": true, - "requires": { - "p-limit": "^2.0.0" - } - }, - "p-try": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.1.0.tgz", - "integrity": "sha512-H2RyIJ7+A3rjkwKC2l5GGtU4H1vkxKCAGsWasNVd0Set+6i4znxbWy6/j16YDPJDWxhsgZiKAstMEP8wCdSpjA==", - "dev": true - }, - "path-exists": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", - "dev": true - }, - "path-key": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", - "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=", - "dev": true - }, - "pump": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", - "integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==", - "dev": true, - "requires": { - "end-of-stream": "^1.1.0", - "once": "^1.3.1" - } - }, - "replace": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/replace/-/replace-1.1.0.tgz", - "integrity": "sha512-0k9rtPG0MUDfJj77XtMCSJKOPdzSwVwM79ZQ6lZuFjqqXrQAMKIMp0g7/8GDAzeERxdktV/LzqbMtJ3yxB23lg==", - "dev": true, - "requires": { - "colors": "1.2.4", - "minimatch": "3.0.4", - "yargs": "12.0.5" - } - }, - "require-directory": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", - "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=", - "dev": true - }, - "require-main-filename": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-1.0.1.tgz", - "integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE=", - "dev": true - }, - "semver": { - "version": "5.6.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz", - "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==", - "dev": true - }, - "set-blocking": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", - "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=", - "dev": true - }, - "shebang-command": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", - "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", - "dev": true, - "requires": { - "shebang-regex": "^1.0.0" - } - }, - "shebang-regex": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", - "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=", - "dev": true - }, - "signal-exit": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", - "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=", - "dev": true - }, - "string-width": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", - "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", - "dev": true, - "requires": { - "is-fullwidth-code-point": "^2.0.0", - "strip-ansi": "^4.0.0" - } - }, - "strip-ansi": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", - "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", - "dev": true, - "requires": { - "ansi-regex": "^3.0.0" - } - }, - "strip-eof": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", - "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=", - "dev": true - }, - "which": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", - "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", - "dev": true, - "requires": { - "isexe": "^2.0.0" - } - }, - "which-module": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz", - "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=", - "dev": true - }, - "wrap-ansi": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", - "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", - "dev": true, - "requires": { - "string-width": "^1.0.1", - "strip-ansi": "^3.0.1" - }, - "dependencies": { - "ansi-regex": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", - "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", - "dev": true - }, - "is-fullwidth-code-point": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", - "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", - "dev": true, - "requires": { - "number-is-nan": "^1.0.0" - } - }, - "string-width": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", - "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", - "dev": true, - "requires": { - "code-point-at": "^1.0.0", - "is-fullwidth-code-point": "^1.0.0", - "strip-ansi": "^3.0.0" - } - }, - "strip-ansi": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", - "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", - "dev": true, - "requires": { - "ansi-regex": "^2.0.0" - } - } - } - }, - "wrappy": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true - }, - "y18n": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", - "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==", - "dev": true - }, - "yargs": { - "version": "12.0.5", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.5.tgz", - "integrity": "sha512-Lhz8TLaYnxq/2ObqHDql8dX8CJi97oHxrjUcYtzKbbykPtVW9WB+poxI+NM2UIzsMgNCZTIf0AQwsjK5yMAqZw==", - "dev": true, - "requires": { - "cliui": "^4.0.0", - "decamelize": "^1.2.0", - "find-up": "^3.0.0", - "get-caller-file": "^1.0.1", - "os-locale": "^3.0.0", - "require-directory": "^2.1.1", - "require-main-filename": "^1.0.1", - "set-blocking": "^2.0.0", - "string-width": "^2.0.0", - "which-module": "^2.0.0", - "y18n": "^3.2.1 || ^4.0.0", - "yargs-parser": "^11.1.1" - } - }, - "yargs-parser": { - "version": "11.1.1", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-11.1.1.tgz", - "integrity": "sha512-C6kB/WJDiaxONLJQnF8ccx9SEeoTTLek8RVbaOIsrAUS8VrBEXfmeSnCZxygc+XC2sNMBIwOOnfcxiynjHsVSQ==", - "dev": true, - "requires": { - "camelcase": "^5.0.0", - "decamelize": "^1.2.0" - } - } - } -} diff --git a/old.pylintrc b/old.pylintrc new file mode 100644 index 000000000..955005f07 --- /dev/null +++ b/old.pylintrc @@ -0,0 +1,593 @@ +[MASTER] + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-whitelist= + +# Add files or directories to the blacklist. They should be base names, not +# paths. +ignore=CVS,build,botbuilder-schema,samples,django_tests,Generator,operations,operations_async,schema,tests + +# Add files or directories matching the regex patterns to the blacklist. The +# regex matches against base names, not paths. +ignore-patterns=setup.py,azure_bdist_wheel.py + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use. +jobs=1 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=100 + +# List of plugins (as comma separated values of python modules names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Specify a configuration file. +#rcfile= + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. +confidence= + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=print-statement, + parameter-unpacking, + unpacking-in-except, + old-raise-syntax, + backtick, + long-suffix, + old-ne-operator, + old-octal-literal, + import-star-module-level, + non-ascii-bytes-literal, + raw-checker-failed, + bad-inline-option, + locally-disabled, + file-ignored, + suppressed-message, + useless-suppression, + deprecated-pragma, + use-symbolic-message-instead, + apply-builtin, + basestring-builtin, + buffer-builtin, + cmp-builtin, + coerce-builtin, + execfile-builtin, + file-builtin, + long-builtin, + raw_input-builtin, + reduce-builtin, + standarderror-builtin, + unicode-builtin, + xrange-builtin, + coerce-method, + delslice-method, + getslice-method, + setslice-method, + no-absolute-import, + old-division, + dict-iter-method, + dict-view-method, + next-method-called, + metaclass-assignment, + indexing-exception, + raising-string, + reload-builtin, + oct-method, + hex-method, + nonzero-method, + cmp-method, + input-builtin, + round-builtin, + intern-builtin, + unichr-builtin, + map-builtin-not-iterating, + zip-builtin-not-iterating, + range-builtin-not-iterating, + filter-builtin-not-iterating, + using-cmp-argument, + eq-without-hash, + div-method, + idiv-method, + rdiv-method, + exception-message-attribute, + invalid-str-codec, + sys-max-int, + bad-python3-import, + deprecated-string-function, + deprecated-str-translate-call, + deprecated-itertools-function, + deprecated-types-field, + next-method-defined, + dict-items-not-iterating, + dict-keys-not-iterating, + dict-values-not-iterating, + deprecated-operator-function, + deprecated-urllib-function, + xreadlines-attribute, + deprecated-sys-function, + exception-escape, + comprehension-escape, + bad-continuation, + duplicate-code, + redefined-outer-name, + missing-docstring, + too-many-instance-attributes, + too-few-public-methods, + redefined-builtin, + too-many-arguments, + no-self-use, + fixme, + broad-except, + bare-except, + too-many-public-methods, + cyclic-import, + too-many-locals, + too-many-function-args, + too-many-return-statements, + import-error, + no-name-in-module, + too-many-branches, + too-many-ancestors, + too-many-nested-blocks, + attribute-defined-outside-init + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[REPORTS] + +# Python expression which should return a note less than 10 (10 is the highest +# note). You have access to the variables errors warning, statement which +# respectively contain the number of errors / warnings messages and the total +# number of statements analyzed. This is used by the global evaluation report +# (RP0004). +evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +#msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +output-format=text + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit + + +[LOGGING] + +# Format style used to check logging format string. `old` means using % +# formatting, while `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. Available dictionaries: none. To make it working +# install python-enchant package.. +spelling-dict= + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to indicated private dictionary in +# --spelling-private-dict-file option instead of raising a message. +spelling-store-unknown-words=no + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether missing members accessed in mixin class should be ignored. A +# mixin class is detected if its name ends with "mixin" (case insensitive). +ignore-mixin-members=yes + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis. It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=120 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# List of optional constructs for which whitespace checking is disabled. `dict- +# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. +# `trailing-comma` allows a space between comma and closing bracket: (a, ). +# `empty-line` allows space-only lines. +no-space-check=trailing-comma, + dict-separator + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[SIMILARITIES] + +# Ignore comments when computing similarities. +ignore-comments=yes + +# Ignore docstrings when computing similarities. +ignore-docstrings=yes + +# Ignore imports when computing similarities. +ignore-imports=no + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. +#class-attribute-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _ + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. +#variable-rgx= + + +[STRING] + +# This flag controls whether the implicit-str-concat-in-sequence should +# generate a warning on implicit string concatenation in sequences defined over +# several lines. +check-str-concat-over-line-jumps=no + + +[IMPORTS] + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules=optparse,tkinter.tix + +# Create a graph of external dependencies in the given file (report RP0402 must +# not be disabled). +ext-import-graph= + +# Create a graph of every (i.e. internal and external) dependencies in the +# given file (report RP0402 must not be disabled). +import-graph= + +# Create a graph of internal dependencies in the given file (report RP0402 must +# not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + + +[CLASSES] + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict, + _fields, + _replace, + _source, + _make + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=cls + + +[DESIGN] + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement. +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=15 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. Defaults to +# "BaseException, Exception". +overgeneral-exceptions=BaseException, + Exception diff --git a/pipelines/botbuilder-python-ci-slack-test.yml b/pipelines/botbuilder-python-ci-slack-test.yml new file mode 100644 index 000000000..7e540187b --- /dev/null +++ b/pipelines/botbuilder-python-ci-slack-test.yml @@ -0,0 +1,105 @@ +# +# Runs functional tests against the Slack channel. +# + +# "name" here defines the build number format. Build number is accessed via $(Build.BuildNumber) +name: $(Build.BuildId) + +pool: + vmImage: $[ coalesce( variables['VMImage'], 'windows-2019' ) ] # or 'windows-latest' or 'vs2017-win2016' + +trigger: # ci trigger + batch: true + branches: + include: + - main + paths: + include: + - '*' + exclude: + - doc/ + - specs/ + - LICENSE + - README.md + - UsingTestPyPI.md + +pr: # pr trigger + branches: + include: + - main + paths: + include: + - pipelines/botbuilder-python-ci-slack-test.yml + +variables: + AppId: $(SlackTestBotAppId) + AppSecret: $(SlackTestBotAppSecret) + BotGroup: $(SlackTestBotBotGroup) + BotName: $(SlackTestBotBotName) + SlackBotToken: $(SlackTestBotSlackBotToken) + SlackClientSigningSecret: $(SlackTestBotSlackClientSigningSecret) + SlackVerificationToken: $(SlackTestBotSlackVerificationToken) +# AzureSubscription: define this in Azure +# SlackTestBotAppId: define this in Azure +# SlackTestBotAppSecret: define this in Azure +# SlackTestBotBotGroup: define this in Azure +# SlackTestBotBotName: define this in Azure +# SlackTestBotSlackBotToken: define this in Azure +# SlackTestBotSlackChannel: define this in Azure +# SlackTestBotSlackClientSigningSecret: define this in Azure +# SlackTestBotSlackVerificationToken: define this in Azure +# DeleteResourceGroup: (optional) define in Azure + +steps: +- powershell: 'gci env:* | sort-object name | Format-Table -AutoSize -Wrap' + displayName: 'Display env vars' + +- task: AzureCLI@2 + displayName: 'Create Azure resources' + inputs: + azureSubscription: $(AzureSubscription) + scriptType: pscore + scriptLocation: inlineScript + inlineScript: | + Set-PSDebug -Trace 1; + # set up resource group, bot channels registration, app service, app service plan + az deployment sub create --name "$(BotName)" --template-file "$(System.DefaultWorkingDirectory)/libraries/functional-tests/slacktestbot/deploymentTemplates/template-with-new-rg.json" --location "westus" --parameters groupName="$(BotGroup)" appId="$(AppId)" appSecret="$(AppSecret)" botId="$(BotName)" botSku="F0" newAppServicePlanName="$(BotName)" newWebAppName="$(BotName)" slackVerificationToken="$(SlackVerificationToken)" slackBotToken="$(SlackBotToken)" slackClientSigningSecret="$(SlackClientSigningSecret)" groupLocation="westus" newAppServicePlanLocation="westus"; + Set-PSDebug -Trace 0; + +- powershell: | + 7z a -tzip "$(System.DefaultWorkingDirectory)/libraries/functional-tests/slacktestbot/bot.zip" "$(System.DefaultWorkingDirectory)/libraries/functional-tests/slacktestbot/*" -aoa + displayName: 'Zip Bot' + +- task: AzureCLI@1 + displayName: 'Deploy bot' + inputs: + azureSubscription: $(AzureSubscription) + scriptType: ps + scriptLocation: inlineScript + inlineScript: | + az webapp deployment source config-zip --resource-group "$(BotGroup)" --name "$(BotName)" --src "$(System.DefaultWorkingDirectory)/libraries/functional-tests/slacktestbot/bot.zip" --timeout 300 + +- script: | + python -m pip install --upgrade pip + pip install -r ./libraries/functional-tests/requirements.txt + pip install pytest + displayName: 'Install test dependencies' + +- script: | + pytest test_slack_client.py + workingDirectory: '$(System.DefaultWorkingDirectory)/libraries/functional-tests/tests/' + displayName: Run test + env: + BotName: $(SlackTestBotBotName) + SlackBotToken: $(SlackTestBotSlackBotToken) + SlackChannel: $(SlackTestBotSlackChannel) + SlackClientSigningSecret: $(SlackTestBotSlackClientSigningSecret) + SlackVerificationToken: $(SlackTestBotSlackVerificationToken) + +- task: AzureCLI@1 + displayName: 'Delete resources' + inputs: + azureSubscription: $(AzureSubscription) + scriptLocation: inlineScript + inlineScript: 'call az group delete -n "$(BotGroup)" --yes' + condition: and(always(), ne(variables['DeleteResourceGroup'], 'false')) diff --git a/pipelines/botbuilder-python-ci.yml b/pipelines/botbuilder-python-ci.yml index 22f4edbb8..b622bab3f 100644 --- a/pipelines/botbuilder-python-ci.yml +++ b/pipelines/botbuilder-python-ci.yml @@ -6,9 +6,10 @@ variables: COVERALLS_GIT_COMMIT: $(Build.SourceVersion) COVERALLS_SERVICE_JOB_ID: $(Build.BuildId) COVERALLS_SERVICE_NAME: python-ci - python.36: 3.6.10 - python.37: 3.7.6 - python.38: 3.8.2 + python.38: 3.8 + python.39: 3.9 + python.310: 3.10 + python.311: 3.11 # PythonCoverallsToken: get this from Azure jobs: @@ -16,16 +17,18 @@ jobs: - job: Build #Multi-configuration and multi-agent job options are not exported to YAML. Configure these options using documentation guidance: https://docs.microsoft.com/vsts/pipelines/process/phases pool: - name: Hosted Ubuntu 1604 + vmImage: 'ubuntu-latest' strategy: matrix: - Python36: - PYTHON_VERSION: '$(python.36)' - Python37: - PYTHON_VERSION: '$(python.37)' Python38: PYTHON_VERSION: '$(python.38)' + Python39: + PYTHON_VERSION: '$(python.39)' + Python310: + PYTHON_VERSION: '$(python.310)' + Python311: + PYTHON_VERSION: '$(python.311)' maxParallel: 3 steps: @@ -38,13 +41,11 @@ jobs: inputs: versionSpec: '$(PYTHON_VERSION)' - - script: 'sudo ln -s /opt/hostedtoolcache/Python/3.6.9/x64/lib/libpython3.6m.so.1.0 /usr/lib/libpython3.6m.so' - displayName: libpython3.6m - - script: | python -m pip install --upgrade pip pip install -e ./libraries/botbuilder-schema pip install -e ./libraries/botframework-connector + pip install -e ./libraries/botframework-streaming pip install -e ./libraries/botbuilder-core pip install -e ./libraries/botbuilder-ai pip install -e ./libraries/botbuilder-applicationinsights @@ -56,33 +57,42 @@ jobs: pip install -e ./libraries/botbuilder-integration-aiohttp pip install -r ./libraries/botframework-connector/tests/requirements.txt pip install -r ./libraries/botbuilder-core/tests/requirements.txt + pip install -r ./libraries/botbuilder-ai/tests/requirements.txt pip install coveralls - pip install pylint - pip install black + pip install pylint==3.2.6 + pip install black==24.4.2 displayName: 'Install dependencies' + - script: 'black --check libraries' + displayName: 'Check Black compliant' + + - script: 'pylint --rcfile=.pylintrc libraries' + displayName: Pylint + - script: | pip install pytest pip install pytest-cov pip install coveralls - pytest --junitxml=junit/test-results.$(PYTHON_VERSION).xml --cov-config=.coveragerc --cov --cov-report=xml --cov-report=html + pytest --junitxml=junit/test-results.$(PYTHON_VERSION).xml --cov-config=.coveragerc --cov --cov-report=xml --cov-report=html --ignore=libraries/functional-tests/tests/test_slack_client.py displayName: Pytest + - task: PublishCodeCoverageResults@1 + displayName: 'Publish Test Coverage' + inputs: + codeCoverageTool: Cobertura + summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml' + reportDirectory: '$(System.DefaultWorkingDirectory)/**/htmlcov' + - task: PublishTestResults@2 displayName: 'Publish Test Results **/test-results.$(PYTHON_VERSION).xml' inputs: testResultsFiles: '**/test-results.$(PYTHON_VERSION).xml' testRunTitle: 'Python $(PYTHON_VERSION)' - - script: 'black --check libraries' - displayName: 'Check Black compliant' - - - script: 'pylint --rcfile=.pylintrc libraries' - displayName: Pylint - - script: 'COVERALLS_REPO_TOKEN=$(PythonCoverallsToken) coveralls' displayName: 'Push test results to coveralls https://coveralls.io/github/microsoft/botbuilder-python' continueOnError: true + condition: and(succeeded(), eq(variables['System.PullRequest.IsFork'], 'false')) - powershell: | Set-Location .. @@ -100,3 +110,6 @@ jobs: Get-ChildItem -Path "$(Build.SourcesDirectory)" -Include "*coverage*" | Copy-Item -Destination "$(Build.ArtifactStagingDirectory)/CodeCoverage" displayName: 'Copy .coverage Files to CodeCoverage folder' continueOnError: true + + - task: ms.vss-governance-buildtask.governance-build-task-component-detection.ComponentGovernanceComponentDetection@0 + displayName: 'Component Detection' diff --git a/pipelines/experimental-create-azure-container-registry.yml b/pipelines/experimental-create-azure-container-registry.yml new file mode 100644 index 000000000..9bf7d4f20 --- /dev/null +++ b/pipelines/experimental-create-azure-container-registry.yml @@ -0,0 +1,36 @@ +trigger: none # no ci trigger + +pr: none # no pr trigger + +pool: + vmImage: 'ubuntu-latest' + +steps: +- task: AzurePowerShell@5 + displayName: 'Create container' + inputs: + azureSubscription: 'FUSE Temporary (174c5021-8109-4087-a3e2-a1de20420569)' + ScriptType: 'InlineScript' + Inline: | + Set-PSDebug -Trace 1; + Write-Host 'blah'; + Write-Host 'az group create --name NightlyPythonFunctionalTestContainerRegistryRG --location eastus' + az group create --name NightlyPythonFunctionalTestContainerRegistryRG --location eastus + Write-Host 'az acr create --resource-group NightlyPythonFunctionalTestContainerRegistryRG --name NightlyPythonFunctionalTestContainerRegistry --sku Basic' + az acr create --resource-group NightlyPythonFunctionalTestContainerRegistryRG --name NightlyPythonFunctionalTestContainerRegistry --sku Basic + az acr login --name NightlyPythonFunctionalTestContainerRegistry + docker pull hello-world + docker tag hello-world nightlypythonfunctionaltestcontainerregistry.azurecr.io/hello-world:v1 + docker push nightlypythonfunctionaltestcontainerregistry.azurecr.io/hello-world:v1 + docker rmi nightlypythonfunctionaltestcontainerregistry.azurecr.io/hello-world:v1 + az acr repository list --name NightlyPythonFunctionalTestContainerRegistry --output table + az acr repository show-tags --name NightlyPythonFunctionalTestContainerRegistry --repository hello-world --output table + azurePowerShellVersion: 'LatestVersion' + +- script: echo Hello, world! + displayName: 'Run a one-line script' + +- script: | + echo Add other tasks to build, test, and deploy your project. + echo See https://aka.ms/yaml + displayName: 'Run a multi-line script' diff --git a/samples/experimental/101.corebot-bert-bidaf/Dockerfile_bot b/samples/experimental/101.corebot-bert-bidaf/Dockerfile_bot deleted file mode 100644 index 322372e67..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/Dockerfile_bot +++ /dev/null @@ -1,35 +0,0 @@ -FROM tiangolo/uwsgi-nginx-flask:python3.6 - -# Setup for nginx -RUN mkdir -p /home/LogFiles \ - && apt update \ - && apt install -y --no-install-recommends vim - -EXPOSE 3978 - -COPY /model /model - -# Pytorch very large. Install from wheel. -RUN wget https://files.pythonhosted.org/packages/69/60/f685fb2cfb3088736bafbc9bdbb455327bdc8906b606da9c9a81bae1c81e/torch-1.1.0-cp36-cp36m-manylinux1_x86_64.whl -RUN pip3 install torch-1.1.0-cp36-cp36m-manylinux1_x86_64.whl - -RUN pip3 install -e /model/ - - -COPY ./bot /bot - -RUN pip3 install -r /bot/requirements.txt - -ENV FLASK_APP=/bot/main.py -ENV LANG=C.UTF-8 -ENV LC_ALL=C.UTF-8 -ENV PATH ${PATH}:/home/site/wwwroot - -WORKDIR bot -# Initialize models - - -# For Debugging, uncomment the following: -#ENTRYPOINT ["python3.6", "-c", "import time ; time.sleep(500000)"] -ENTRYPOINT [ "flask" ] -CMD [ "run", "--port", "3978", "--host", "0.0.0.0" ] diff --git a/samples/experimental/101.corebot-bert-bidaf/Dockerfile_model_runtime b/samples/experimental/101.corebot-bert-bidaf/Dockerfile_model_runtime deleted file mode 100644 index c1a22217f..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/Dockerfile_model_runtime +++ /dev/null @@ -1,29 +0,0 @@ -# https://github.com/tornadoweb/tornado/blob/master/demos/blog/Dockerfile -FROM python:3.6 - -# Port the model runtime service will listen on. -EXPOSE 8880 - -# Make structure where the models will live. -RUN mkdir -p /cognitiveModels/bert -RUN mkdir -p /cognitiveModels/bidaf - -# Copy and install models. -COPY model /model/ -#RUN pip3 install --upgrade pip -#RUN pip3 install --upgrade nltk -RUN wget https://files.pythonhosted.org/packages/69/60/f685fb2cfb3088736bafbc9bdbb455327bdc8906b606da9c9a81bae1c81e/torch-1.1.0-cp36-cp36m-manylinux1_x86_64.whl -RUN pip3 install torch-1.1.0-cp36-cp36m-manylinux1_x86_64.whl -RUN pip3 install -e /model - -# Copy and install model runtime service api. -COPY model_runtime_svc /model_runtime_svc/ -RUN pip3 install -e /model_runtime_svc - -# One time initialization of the models. -RUN python3 /model_runtime_svc/model_runtime_svc_corebot101/docker_init.py -RUN rm /model_runtime_svc/model_runtime_svc_corebot101/docker_init.py - -WORKDIR /model_runtime_svc - -ENTRYPOINT ["python3", "./model_runtime_svc_corebot101/main.py"] \ No newline at end of file diff --git a/samples/experimental/101.corebot-bert-bidaf/NOTICE.md b/samples/experimental/101.corebot-bert-bidaf/NOTICE.md deleted file mode 100644 index 984258e65..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/NOTICE.md +++ /dev/null @@ -1,8 +0,0 @@ -## NOTICE - -Please note that while the 101.corebot-bert-bidaf sample is licensed under the MIT license, the sample has dependencies that use other types of licenses. - -Since Microsoft does not modify nor distribute these dependencies, it is the sole responsibility of the user to determine the correct/compliant usage of these dependencies. Please refer to the -[bot requirements](./bot/requirements.txt), [model requirements](./model/setup.py) and [model runtime requirements](./model_runtime_svc/setup.py) for a list of the **direct** dependencies. - -Please also note that the sample depends on the `requests` package, which has a dependency `chardet` that uses LGPL license. \ No newline at end of file diff --git a/samples/experimental/101.corebot-bert-bidaf/README.md b/samples/experimental/101.corebot-bert-bidaf/README.md deleted file mode 100644 index 4d66d258b..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/README.md +++ /dev/null @@ -1,349 +0,0 @@ -# CoreBot-bert-bidaf - -Bot Framework v4 core bot sample demonstrating using open source language models employing the BERT and BiDAF. This is for demonstration purposes only. - -## Table of Contents -- [Overview](#overview) -- [Terminology](#terminology) -- [Setup](#setup) -- [Model Development](#model-development) -- [Model Runtime Options](#model-runtime-options) - - [In-Process](#in-process) - - [Out-of-process to local service](#out-of-process-to-local-service) - - [Using Docker Containers](#using-docker-containers) - - -## Overview -This bot has been created using [Bot Framework](https://dev.botframework.com). It demonstrates the following: -- Train a one layer logistic regression classifier on top of the pretrained BERT model to infer intents. -- Use locally the ONNX BiDAF pre-trained model to infer entities for the scenario, in this case simple flight booking. -- Run the bot with the model runtime in-process to the bot. -- Run the bot with the model runtime external to the bot. - -## Terminology -This document uses the following terminology. -**Model Development**: Model Development broadly covers gathering data, data processing, training/validation/evaluation and testing. This can also be thought of as model preparation or authoring time. -**Model Runtime**: The built model which can be used to perform inferences against bot utterances. The model runtime refers to the model and the associated code to perform inferences. The model runtime is used when the bot is running to infer intents and entities. -**Inference**: Applying the bot utterance to a model yields intents and entities. The intents and entities are the inferences used by the bot. - -## Setup - -This sample uses the Anaconda environment (which provides Jupyter Lab and other machine learning tools) in order to run. - -The following instructions assume using the [Anaconda]() environment (v4.6.11+). - -Note: Be sure to install the **64-bit** version of Anaconda for the purposes of this tutorial. - -### Create and activate virtual environment - -In your local folder, open an **Anaconda prompt** and run the following commands: - -```bash -cd 101.corebot-bert-bidaf -conda create -n botsample python=3.6 anaconda -y -conda activate botsample # source conda - -# Add extension to handle Jupyter kernel based on the new environemnt. -pip install ipykernel -ipython kernel install --user --name=botsample - -# Add extension for visual controls to display correctly -conda install -c conda-forge nodejs -y -jupyter labextension install @jupyter-widgets/jupyterlab-manager -``` - -From here on out, all CLI interactions should occur within the `botsample` Anaconda virtual environment. - -### Install models package -The `models` package contains source to perform model development support and runtime inferencing using the tuned BERT and BiDAF models. - - -```bash -# Install Pytorch -conda install -c pytorch pytorch -y - -# Install models package using code in sample -# This will create the python package that contains all the -# models used in the Jupyter Notebooks and the Bot code. -cd model -pip install -e . # Note the '.' after -e - -# Verify packages installed - # On Windows: - conda list | findstr "corebot pytorch onnx" - - # On Linux/etc: - conda list | grep -e corebot -e pytorch -e onnx -``` - -You should see something like: -```bash -model-corebot101 0.0.1 dev_0 -onnx 1.5.0 pypi_0 pypi -onnxruntime 0.4.0 pypi_0 pypi -pytorch 1.1.0 py3.6_cuda100_cudnn7_1 pytorch -pytorch-pretrained-bert 0.6.2 pypi_0 pypi -``` - -## Model Development -Model development in this sample involves building a BERT classifier model and is performed in the Juypter Lab environment. - -### Training in Jupyter Lab -Training the model can be performed in Jupyter Lab. -Within the Anaconda shell, launch Jupyter Lab from the sample directory. - -```bash -# Start JupyterLab from the root of the sample directory -(botsample) 101.corebot-bert-bidaf> jupyter lab - -``` -#### Click on `notebooks` folder in the left hand navigation of JupyterLab - -
- Click for screen shot. - Selecting notebooks folder in Jupyter - Selecting notebooks folder in Jupyter -
- -#### Click on `bert_train.ipynb` notebook -If running the first time, you should select the `botsample` environment. -
- Click for screen shot. - Selecting Anaconda `botsample` environment for Jupyter Kernel - Selecting Jupyter Kernel -
- -#### Train the model - -To build the BERT classifier model, run the Jupyterlab Notebok (Run->**Run All Cells**). - -
- Click for screen shot. - Selecting Model to build folder - Selecting Bert model -
- -This process may take several minutes. The built model is placed into a directory that will get packaged with the bot during deployment. The sample demonstrates using this package in-process to the bot, our of process in a separate host that performs inferences, and within Jupyter Notebooks. - -After running the Jupyter Notebook, the output should resemble something like the following: -
- Click for screen shot. - Showing Completed Model Build - Completed Model Build -
- - - -#### Test the BERT runtime model classification -Once the model has been built, you can test the model with a separately provided Jupyter Notebook (Run->Run All Cells). -- Within the `notebooks` folder, select the `bert_model_runtime.ipynb` file. -- Run the notebook. - -[![Sample output of the bert model](./media/jupyter_lab_bert_runtime.PNG)] - -- The output shows intents (`Book flight`, `Cancel`) that will be used by the bot. - -- Add additional test cases to see how phrases will be inferenced. - -- To modify the training data, update the file below. The format of this is compatible with the LUIS schema. When done modifying the training data [re-train the model](#train-the-model) before testing. - `101.corebot-bert-bidaf/model/model_corebot101/bert/training_data/FlightBooking.json` - - [Click to edit Training Data.](./model/model_corebot101/bert/training_data/FlightBooking.json) - -> **NOTE**: The default file output location for the tuned BERT model is `/models/bert`. - -### Test the BiDAF runtime model classification -Similarly, you can test the BiDAF model. Note there is no explicit data processing for the bidaf model. The entities detected are configured at runtime in the notebook. - -[![Sample output of the bert model](./media/jupyter_lab_bidaf_runtime.PNG)] - -> **NOTE**: The default file output location for the BiDAF model is `/models/bidaf`. - - - -## Model Runtime Options - -The sample can host the model runtime within the bot process or out-of-process in a REST API service. The following sections demonstrate how to do this on the local machine. In addition, the sample provides Dockerfiles to run this sample in containers. - -### In-process -Within an Anaconda environment (bring up a new Anaconda shell and activate your virtual environment if you would like to continue having JupyterLab running in the original shell), install dependencies for the bot: -```bash -# Install requirements required for the bot -(botsample) 101.corebot-bert-bidaf> pip install -r requirements.txt -``` -> **NOTE**: If `requirements.txt` doesn't install, you may have to stop JupyterLab if it's running. - -```bash -# Run the bot -(botsample) 101.corebot-bert-bidaf> cd bot -(botsample) 101.corebot-bert-bidaf\bot> python main.py -``` - - -> **NOTE**: If executing `main.py` with Python above doesn't work, try running Flask directly: -> -> ```bash -> # Set FLASK_APP with full path to main.py in the sample directory -> # On linux, use export instead of set. -> (botsample) 101.corebot-bert-bidaf> set FLASK_APP=main.py -> -> # Turn on development -> (botsample) 101.corebot-bert-bidaf> set FLASK_ENV=development -> -> # Run flask -> (botsample) 101.corebot-bert-bidaf> flask run -> ``` - -At this point, you can [test the bot](#testing-the-bot-using-bot-framework-emulator). - -### Out-of-process to local service -Sometimes it's helpful to host the model outside the bot's process space and serve inferences from a separate process. - -This section builds on the previous section of [In-process](#in-process). - -#### Stop any running bot/model runtime processes -Ensure there are no running bot or model runtimes executing. Hit ^C on any Anaconda shells running flask/bot or the model service runtime (`python main.py`). - -#### Modify bot configuration for localhost -To call the out-of-process REST API, the bot configuration is modified. Edit the following file: -`101.corebot-bert-bidaf/bot/config.py` - -Edit the settings for `USE_MODEL_RUNTIME_SERVICE` and set to `True`. - -```python -class DefaultConfig(object): - """Bot configuration parameters.""" - # TCP port that the bot listens on (default:3978) - PORT = 3978 - - # Azure Application ID (not required if running locally) - APP_ID = "" - # Azure Application Password (not required if running locally) - APP_PASSWORD = "" - - # Determines if the bot calls the models in-proc to the bot or call out of process - # to the service api using a REST API. - USE_MODEL_RUNTIME_SERVICE = True - # Host serving the out-of-process model runtime service api. - MODEL_RUNTIME_SERVICE_HOST = "localhost" - # TCP serving the out-of-process model runtime service api. - MODEL_RUNTIME_SERVICE_PORT = 8880 -``` -#### Set up model runtime service -Inside a separate Anaconda shell, activate the `botsample` environment, and install the model runtime service. - -```bash -# Install requirements required for model runtime service -(botsample) 101.corebot-bert-bidaf> cd model_runtime_svc -(botsample) 101.corebot-bert-bidaf\model_runtime_svc> pip install -e . # Note the dot after the -e switch -``` - -#### Run model runtime service -To run the model runtime service, execute the following: -```bash -# Navigate into the model_runtime_svc_corebot101 folder -cd model_runtime_svc_corebot101 - -# From 101.corebot-bert-bidaf\model_runtime_svc\model_runtime_svc_corebot101 -python main.py -``` -If not already running, create a separate Anaconda shell set to the `botsample` environment and [run the local bot](#local-in-process) as described above. If it was already running, ensure [the configuration changes made above](#modify-bot-configuration) are running. - -At this point, you can [test the bot](#testing-the-bot-using-bot-framework-emulator). - -### Using Docker Containers -This sample also demonstrates using Docker and Docker Compose to run a bot and model runtime service on the local host, that communicate together. - -> **NOTE**: For Windows: https://hub.docker.com/editions/community/docker-ce-desktop-windows In the configuration dialog make sure the use Linux containers is checked. - - -#### Modify bot configuration for Docker -To call the out-of-process REST API inside a Docker container, the bot configuration is modified. Edit the following file: -`101.corebot-bert-bidaf/bot/config.py` - -Ensure that the bot configuration is set to serve model predictions remotely by setting `USE_MODEL_RUNTIME_SERVICE` to `True`. - -In addition, modify the `MODEL_RUNTIME_SERVICE_HOST` to `api` (previously `localhost`). This will allow the `bot` container to properly address the model `model runtime api` service container. - -The resulting `config.py`should look like the following: -```python -class DefaultConfig(object): - """Bot configuration parameters.""" - # TCP port that the bot listens on (default:3978) - PORT = 3978 - - # Azure Application ID (not required if running locally) - APP_ID = "" - # Azure Application Password (not required if running locally) - APP_PASSWORD = "" - - # Determines if the bot calls the models in-proc to the bot or call out of process - # to the service api using a REST API. - USE_MODEL_RUNTIME_SERVICE = True - # Host serving the out-of-process model runtime service api. - MODEL_RUNTIME_SERVICE_HOST = "api" - # TCP serving the out-of-process model runtime service api. - MODEL_RUNTIME_SERVICE_PORT = 8880 -``` -#### Build the containers - -The following command builds both the bot and the model runtime service containers. -```bash -# From 101.corebot-bert-bidaf directory -docker-compose --project-directory . --file docker/docker-compose.yml build -``` -> **NOTE**: If you get error code 137, you may need to increase the amount of memory supplied to Docker. - -#### Run the containers locally -```bash -# From 101.corebot-bert-bidaf directory -docker-compose --project-directory . --file docker/docker-compose.yml up -d -``` -#### Verify -```bash -# From 101.corebot-bert-bidaf directory -docker-compose --project-directory . --file docker/docker-compose.yml logs -docker ps -``` -Look at the logs and docker to ensure the containers are running. - -> **NOTE**: When testing the bot inside containers, use your local IP address instead of `localhost` (`http://:3978/api/messages`). -> To find your IP address: -> -> - On **Windows**, `ipconfig` at a command prompt. -> - On **Linux**, `ip addr` at a command prompt. - - -## Testing the bot using Bot Framework Emulator - -[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to the bot using Bot Framework Emulator - -- Launch Bot Framework Emulator -- File -> Open Bot -- Enter a Bot URL of `http://localhost:3978/api/messages` - -### Test the bot -In the emulator, type phrases such as`hello`, `book flight from seattle to miami`, etc - - - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Dialogs](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0) -- [Gathering Input Using Prompts](https://docs.microsoft.com/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=csharp) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) -- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) -- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) -- [.NET Core CLI tools](https://docs.microsoft.com/dotnet/core/tools/?tabs=netcore2x) -- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) -- [Azure Portal](https://portal.azure.com) -- [Language Understanding using LUIS](https://docs.microsoft.com/azure/cognitive-services/luis/) -- [Channels and Bot Connector Service](https://docs.microsoft.com/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) -- [Google BERT](https://github.com/google-research/bert) -- [ONNX BiDAF](https://github.com/onnx/models/tree/master/bidaf) \ No newline at end of file diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/config.py b/samples/experimental/101.corebot-bert-bidaf/bot/config.py deleted file mode 100644 index 4e8bfd007..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/bot/config.py +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Bot/Flask Configuration parameters. -Configuration parameters for the bot. -""" - - -class DefaultConfig(object): - """Bot configuration parameters.""" - - # TCP port that the bot listens on (default:3978) - PORT = 3978 - - # Azure Application ID (not required if running locally) - APP_ID = "" - # Azure Application Password (not required if running locally) - APP_PASSWORD = "" - - # Determines if the bot calls the models in-proc to the bot or call out of process - # to the service api. - USE_MODEL_RUNTIME_SERVICE = False - # Host serving the out-of-process model runtime service api. - MODEL_RUNTIME_SERVICE_HOST = "localhost" - # TCP serving the out-of-process model runtime service api. - MODEL_RUNTIME_SERVICE_PORT = 8880 diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/main_dialog.py b/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/main_dialog.py deleted file mode 100644 index 8e501a596..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/bot/dialogs/main_dialog.py +++ /dev/null @@ -1,100 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Main dialog. """ -from typing import Dict -from botbuilder.dialogs import ( - ComponentDialog, - WaterfallDialog, - WaterfallStepContext, - DialogTurnResult, -) -from botbuilder.dialogs.prompts import TextPrompt, PromptOptions -from botbuilder.core import MessageFactory -from model_corebot101.booking_details import BookingDetails -from model_corebot101.language_helper import LanguageHelper -from .booking_dialog import BookingDialog - -# pylint:disable=line-too-long -class MainDialog(ComponentDialog): - """Main dialog.""" - - def __init__( - self, - configuration: Dict[str, object], - dialog_id: str = None, - language_helper: LanguageHelper = None, - ): - super(MainDialog, self).__init__(dialog_id or MainDialog.__name__) - - self._configuration = configuration - self._language_helper = language_helper - - self.add_dialog(TextPrompt(TextPrompt.__name__)) - self.add_dialog(BookingDialog()) - self.add_dialog( - WaterfallDialog( - "WFDialog", [self.intro_step, self.act_step, self.final_step] - ) - ) - - self.initial_dialog_id = "WFDialog" - - # Initialize language models if we're not using service - self._language_helper = LanguageHelper() - if not self._configuration["USE_MODEL_RUNTIME_SERVICE"]: - self._language_helper.initialize_models() - - async def intro_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: - """Initial prompt.""" - return await step_context.prompt( - TextPrompt.__name__, - PromptOptions( - prompt=MessageFactory.text("What can I help you with today?") - ), - ) - - async def act_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: - """Use language understanding to gather details about booking. - In this sample we only have a single Intent we are concerned with. - However, typically a scenario will have multiple different Intents - each corresponding to starting a different child Dialog.""" - - if step_context.result is not None: - utterance = step_context.context.activity.text - if not self._configuration["USE_MODEL_RUNTIME_SERVICE"]: - booking_details = await self._language_helper.excecute_query_inproc( - utterance - ) - else: - booking_details = await self._language_helper.excecute_query_service( - self._configuration, utterance - ) - else: - booking_details = BookingDetails() - - # Run the BookingDialog giving it whatever details we have from the - # model. The dialog will prompt to find out the remaining details. - return await step_context.begin_dialog(BookingDialog.__name__, booking_details) - - async def final_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: - """Complete dialog. - At this step, with details from the user, display the completed - flight booking to the user. - """ - # If the child dialog ("BookingDialog") was cancelled or the user failed - # to confirm, the Result here will be null. - if step_context.result is not None: - result = step_context.result - - # Now we have all the booking details call the booking service. - # If the call to the booking service was successful tell the user. - # time_property = Timex(result.travel_date) - # travel_date_msg = time_property.to_natural_language(datetime.now()) - msg = ( - f"I have you booked to {result.destination} from" - f" {result.origin} on {result.travel_date}." - ) - await step_context.context.send_activity(MessageFactory.text(msg)) - else: - await step_context.context.send_activity(MessageFactory.text("Thank you.")) - return await step_context.end_dialog() diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/helpers/__init__.py b/samples/experimental/101.corebot-bert-bidaf/bot/helpers/__init__.py deleted file mode 100644 index b2860c928..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/bot/helpers/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Helpers module.""" - -from . import activity_helper, dialog_helper - -__all__ = ["activity_helper", "dialog_helper"] diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/helpers/activity_helper.py b/samples/experimental/101.corebot-bert-bidaf/bot/helpers/activity_helper.py deleted file mode 100644 index bbd0ac848..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/bot/helpers/activity_helper.py +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Helper to create reply object.""" - -from datetime import datetime -from botbuilder.schema import ( - Activity, - ActivityTypes, - ChannelAccount, - ConversationAccount, -) - - -def create_activity_reply(activity: Activity, text: str = None, locale: str = None): - """Helper to create reply object.""" - return Activity( - type=ActivityTypes.message, - timestamp=datetime.utcnow(), - from_property=ChannelAccount( - id=getattr(activity.recipient, "id", None), - name=getattr(activity.recipient, "name", None), - ), - recipient=ChannelAccount( - id=activity.from_property.id, name=activity.from_property.name - ), - reply_to_id=activity.id, - service_url=activity.service_url, - channel_id=activity.channel_id, - conversation=ConversationAccount( - is_group=activity.conversation.is_group, - id=activity.conversation.id, - name=activity.conversation.name, - ), - text=text or "", - locale=locale or "", - attachments=[], - entities=[], - ) diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/main.py b/samples/experimental/101.corebot-bert-bidaf/bot/main.py deleted file mode 100644 index 3dcfce782..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/bot/main.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -""" -This sample shows how to create a simple EchoBot with state. -""" -import asyncio - -from flask import Flask, request, Response -from botbuilder.core import ( - BotFrameworkAdapter, - BotFrameworkAdapterSettings, - ConversationState, - MemoryStorage, - UserState, -) -from botbuilder.schema import Activity -from dialogs import MainDialog -from bots import DialogAndWelcomeBot - -LOOP = asyncio.get_event_loop() -APP = Flask(__name__, instance_relative_config=True) - -# Retrieve Flask/Bot configuration -APP.config.from_object("config.DefaultConfig") - -SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) -ADAPTER = BotFrameworkAdapter(SETTINGS) - -# Create MemoryStorage, UserState and ConversationState -MEMORY = MemoryStorage() -USER_STATE = UserState(MEMORY) -CONVERSATION_STATE = ConversationState(MEMORY) -DIALOG = MainDialog(APP.config) -BOT = DialogAndWelcomeBot(CONVERSATION_STATE, USER_STATE, DIALOG) - - -@APP.route("/api/messages", methods=["POST"]) -def messages(): - """Main bot message handler.""" - if "application/json" in request.headers["Content-Type"]: - body = request.json - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = ( - request.headers["Authorization"] if "Authorization" in request.headers else "" - ) - - async def aux_func(turn_context): - await BOT.on_turn(turn_context) - - try: - task = LOOP.create_task( - ADAPTER.process_activity(activity, auth_header, aux_func) - ) - LOOP.run_until_complete(task) - return Response(status=201) - - except Exception as exception: - raise exception - - -if __name__ == "__main__": - try: - APP.run(debug=True, port=APP.config["PORT"]) # nosec debug - except Exception as exception: - raise exception diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/requirements.txt b/samples/experimental/101.corebot-bert-bidaf/bot/requirements.txt deleted file mode 100644 index 4b99b18d5..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/bot/requirements.txt +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -# Note: The model must be built first! -# cd model -# - -# The following are installed outside of requirements.txt -# conda install -c pytorch pytorch -y -# pip install onnxruntime -# Install python package dependencies with the following: -# `pip install -r requirements.txt` - -# Bot -Flask>=1.0.2 -asyncio>=3.4.3 -requests>=2.18.1 - -# Bot Framework -botframework-connector>=4.4.0.b1 -botbuilder-schema>=4.4.0.b1 -botbuilder-core>=4.4.0.b1 -botbuilder-dialogs>=4.4.0.b1 -botbuilder-ai>=4.4.0.b1 -datatypes-date-time>=1.0.0.a1 -azure-cognitiveservices-language-luis>=0.2.0 -msrest>=0.6.6 - -# Internal library - must be built first! -model_corebot101>=0.0.1 - -torch -onnx -onnxruntime -tqdm>=4.32.1 -pytorch-pretrained-bert>=0.6.2 -nltk>=3.4.1 -numpy>=1.16.3 -scipy>=1.3.0 -scikit-learn>=0.21.2 - diff --git a/samples/experimental/101.corebot-bert-bidaf/docker/docker-compose.yml b/samples/experimental/101.corebot-bert-bidaf/docker/docker-compose.yml deleted file mode 100644 index 29c6de853..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/docker/docker-compose.yml +++ /dev/null @@ -1,20 +0,0 @@ -version: '3.7' -services: - bot: - build: - context: . - dockerfile: Dockerfile_bot - ports: - - "3978:3978" - links: - - api - environment: - MODEL_RUNTIME_API_HOST : api - - api: - build: - context: . - dockerfile: Dockerfile_model_runtime - ports: - - "8880:8880" - diff --git a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bert_complete.PNG b/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bert_complete.PNG deleted file mode 100644 index 0254191fe..000000000 Binary files a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bert_complete.PNG and /dev/null differ diff --git a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bert_runtime.PNG b/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bert_runtime.PNG deleted file mode 100644 index cbf7f5f76..000000000 Binary files a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bert_runtime.PNG and /dev/null differ diff --git a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bert_train.PNG b/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bert_train.PNG deleted file mode 100644 index 231ae766b..000000000 Binary files a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bert_train.PNG and /dev/null differ diff --git a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bidaf_runtime.PNG b/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bidaf_runtime.PNG deleted file mode 100644 index 80e7bb79f..000000000 Binary files a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_bidaf_runtime.PNG and /dev/null differ diff --git a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_model_nav.PNG b/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_model_nav.PNG deleted file mode 100644 index 55181da4b..000000000 Binary files a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_model_nav.PNG and /dev/null differ diff --git a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_run_all_cells.PNG b/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_run_all_cells.PNG deleted file mode 100644 index eb151981f..000000000 Binary files a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_run_all_cells.PNG and /dev/null differ diff --git a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_select_kernel.PNG b/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_select_kernel.PNG deleted file mode 100644 index b4c2f48d8..000000000 Binary files a/samples/experimental/101.corebot-bert-bidaf/media/jupyter_lab_select_kernel.PNG and /dev/null differ diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/__init__.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/__init__.py deleted file mode 100644 index b339b691f..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .bert_util import BertUtil -from .input_example import InputExample -from .input_features import InputFeatures - -__all__ = ["BertUtil", "InputExample", "InputFeatures"] diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/bert_util.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/bert_util.py deleted file mode 100644 index ee9ab630e..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/bert_util.py +++ /dev/null @@ -1,156 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import logging -from typing import List - -from .input_features import InputFeatures -from scipy.stats import pearsonr, spearmanr -from sklearn.metrics import f1_score - - -class BertUtil: - logger = logging.getLogger(__name__) - - @classmethod - def convert_examples_to_features( - cls, examples, label_list, max_seq_length, tokenizer, output_mode - ) -> List: - """Loads a data file into a list of `InputBatch`s.""" - - label_map = {label: i for i, label in enumerate(label_list)} - - features = [] - for (ex_index, example) in enumerate(examples): - if ex_index % 10000 == 0: - cls.logger.info("Writing example %d of %d" % (ex_index, len(examples))) - - tokens_a = tokenizer.tokenize(example.text_a) - - tokens_b = None - if example.text_b: - tokens_b = tokenizer.tokenize(example.text_b) - # Modifies `tokens_a` and `tokens_b` in place so that the total - # length is less than the specified length. - # Account for [CLS], [SEP], [SEP] with "- 3" - BertUtil._truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3) - else: - # Account for [CLS] and [SEP] with "- 2" - if len(tokens_a) > max_seq_length - 2: - tokens_a = tokens_a[: (max_seq_length - 2)] - - # The convention in BERT is: - # (a) For sequence pairs: - # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] - # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 - # (b) For single sequences: - # tokens: [CLS] the dog is hairy . [SEP] - # type_ids: 0 0 0 0 0 0 0 - # - # Where "type_ids" are used to indicate whether this is the first - # sequence or the second sequence. The embedding vectors for `type=0` and - # `type=1` were learned during pre-training and are added to the wordpiece - # embedding vector (and position vector). This is not *strictly* necessary - # since the [SEP] token unambiguously separates the sequences, but it makes - # it easier for the model to learn the concept of sequences. - # - # For classification tasks, the first vector (corresponding to [CLS]) is - # used as as the "sentence vector". Note that this only makes sense because - # the entire model is fine-tuned. - tokens = ["[CLS]"] + tokens_a + ["[SEP]"] - segment_ids = [0] * len(tokens) - - if tokens_b: - tokens += tokens_b + ["[SEP]"] - segment_ids += [1] * (len(tokens_b) + 1) - - input_ids = tokenizer.convert_tokens_to_ids(tokens) - - # The mask has 1 for real tokens and 0 for padding tokens. Only real - # tokens are attended to. - input_mask = [1] * len(input_ids) - - # Zero-pad up to the sequence length. - padding = [0] * (max_seq_length - len(input_ids)) - input_ids += padding - input_mask += padding - segment_ids += padding - - assert len(input_ids) == max_seq_length - assert len(input_mask) == max_seq_length - assert len(segment_ids) == max_seq_length - - if output_mode == "classification": - label_id = label_map[example.label] - elif output_mode == "regression": - label_id = float(example.label) - else: - raise KeyError(output_mode) - - if ex_index < 5: - cls.logger.info("*** Example ***") - cls.logger.info("guid: %s" % (example.guid)) - cls.logger.info("tokens: %s" % " ".join([str(x) for x in tokens])) - cls.logger.info("input_ids: %s" % " ".join([str(x) for x in input_ids])) - cls.logger.info( - "input_mask: %s" % " ".join([str(x) for x in input_mask]) - ) - cls.logger.info( - "segment_ids: %s" % " ".join([str(x) for x in segment_ids]) - ) - cls.logger.info("label: %s (id = %d)" % (example.label, label_id)) - - features.append( - InputFeatures( - input_ids=input_ids, - input_mask=input_mask, - segment_ids=segment_ids, - label_id=label_id, - ) - ) - return features - - @staticmethod - def _truncate_seq_pair(tokens_a, tokens_b, max_length): - """Truncates a sequence pair in place to the maximum length.""" - - # This is a simple heuristic which will always truncate the longer sequence - # one token at a time. This makes more sense than truncating an equal percent - # of tokens from each, since if one sequence is very short then each token - # that's truncated likely contains more information than a longer sequence. - while True: - total_length = len(tokens_a) + len(tokens_b) - if total_length <= max_length: - break - if len(tokens_a) > len(tokens_b): - tokens_a.pop() - else: - tokens_b.pop() - - @staticmethod - def simple_accuracy(preds, labels): - return (preds == labels).mean() - - @staticmethod - def acc_and_f1(preds, labels): - acc = BertUtil.simple_accuracy(preds, labels) - f1 = f1_score(y_true=labels, y_pred=preds) - return {"acc": acc, "f1": f1, "acc_and_f1": (acc + f1) / 2} - - @staticmethod - def pearson_and_spearman(preds, labels): - pearson_corr = pearsonr(preds, labels)[0] - spearman_corr = spearmanr(preds, labels)[0] - return { - "pearson": pearson_corr, - "spearmanr": spearman_corr, - "corr": (pearson_corr + spearman_corr) / 2, - } - - @staticmethod - def compute_metrics(task_name, preds, labels): - assert len(preds) == len(labels) - if task_name == "flight_booking": - return BertUtil.acc_and_f1(preds, labels) - else: - raise KeyError(task_name) diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/input_example.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/input_example.py deleted file mode 100644 index b674642f3..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/input_example.py +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - - -class InputExample(object): - """A single training/test example for sequence classification.""" - - def __init__(self, guid, text_a, text_b=None, label=None): - """Constructs a InputExample. - - Args: - guid: Unique id for the example. - text_a: string. The untokenized text of the first sequence. For single - sequence tasks, only this sequence must be specified. - text_b: (Optional) string. The untokenized text of the second sequence. - Only must be specified for sequence pair tasks. - label: (Optional) string. The label of the example. This should be - specified for train and dev examples, but not for test examples. - """ - self.guid = guid - self.text_a = text_a - self.text_b = text_b - self.label = label diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/input_features.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/input_features.py deleted file mode 100644 index 97be63ecf..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/common/input_features.py +++ /dev/null @@ -1,12 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - - -class InputFeatures(object): - """A single set of features of data.""" - - def __init__(self, input_ids, input_mask, segment_ids, label_id): - self.input_ids = input_ids - self.input_mask = input_mask - self.segment_ids = segment_ids - self.label_id = label_id diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/model_runtime/bert_model_runtime.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/model_runtime/bert_model_runtime.py deleted file mode 100644 index 112c1167b..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/model_runtime/bert_model_runtime.py +++ /dev/null @@ -1,122 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Bert model runtime.""" - -import os -import sys -from typing import List -import numpy as np -import torch -from torch.utils.data import DataLoader, SequentialSampler, TensorDataset -from pytorch_pretrained_bert import BertForSequenceClassification, BertTokenizer -from model_corebot101.bert.common.bert_util import BertUtil -from model_corebot101.bert.common.input_example import InputExample - - -class BertModelRuntime: - """Model runtime for the Bert model.""" - - def __init__( - self, - model_dir: str, - label_list: List[str], - max_seq_length: int = 128, - output_mode: str = "classification", - no_cuda: bool = False, - do_lower_case: bool = True, - ): - self.model_dir = model_dir - self.label_list = label_list - self.num_labels = len(self.label_list) - self.max_seq_length = max_seq_length - self.output_mode = output_mode - self.no_cuda = no_cuda - self.do_lower_case = do_lower_case - self._load_model() - - # pylint:disable=unused-argument - @staticmethod - def init_bert(bert_model_dir: str) -> bool: - """ Handle any one-time initlization """ - if os.path.isdir(bert_model_dir): - print("bert model directory already present..", file=sys.stderr) - else: - print("Creating bert model directory..", file=sys.stderr) - os.makedirs(bert_model_dir, exist_ok=True) - return True - - def _load_model(self) -> None: - self.device = torch.device( - "cuda" if torch.cuda.is_available() and not self.no_cuda else "cpu" - ) - self.n_gpu = torch.cuda.device_count() - - # Load a trained model and vocabulary that you have fine-tuned - self.model = BertForSequenceClassification.from_pretrained( - self.model_dir, num_labels=self.num_labels - ) - self.tokenizer = BertTokenizer.from_pretrained( - self.model_dir, do_lower_case=self.do_lower_case - ) - self.model.to(self.device) - - def serve(self, query: str) -> str: - example = InputExample( - guid="", text_a=query, text_b=None, label=self.label_list[0] - ) - examples = [example] - - eval_features = BertUtil.convert_examples_to_features( - examples, - self.label_list, - self.max_seq_length, - self.tokenizer, - self.output_mode, - ) - all_input_ids = torch.tensor( - [f.input_ids for f in eval_features], dtype=torch.long - ) - all_input_mask = torch.tensor( - [f.input_mask for f in eval_features], dtype=torch.long - ) - all_segment_ids = torch.tensor( - [f.segment_ids for f in eval_features], dtype=torch.long - ) - - if self.output_mode == "classification": - all_label_ids = torch.tensor( - [f.label_id for f in eval_features], dtype=torch.long - ) - - eval_data = TensorDataset( - all_input_ids, all_input_mask, all_segment_ids, all_label_ids - ) - # Run prediction for full data - eval_sampler = SequentialSampler(eval_data) - eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=1) - - self.model.eval() - nb_eval_steps = 0 - preds = [] - - for input_ids, input_mask, segment_ids, label_ids in eval_dataloader: - input_ids = input_ids.to(self.device) - input_mask = input_mask.to(self.device) - segment_ids = segment_ids.to(self.device) - - with torch.no_grad(): - logits = self.model(input_ids, segment_ids, input_mask, labels=None) - - nb_eval_steps += 1 - if len(preds) == 0: - preds.append(logits.detach().cpu().numpy()) - else: - preds[0] = np.append(preds[0], logits.detach().cpu().numpy(), axis=0) - - preds = preds[0] - if self.output_mode == "classification": - preds = np.argmax(preds, axis=1) - - label_id = preds[0] - pred_label = self.label_list[label_id] - return pred_label diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/requirements.txt b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/requirements.txt deleted file mode 100644 index 10f898f8b..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -torch -tqdm -pytorch-pretrained-bert diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/__init__.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/__init__.py deleted file mode 100644 index 277890a19..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Bert tuning training.""" - -from .args import Args -from .bert_train_eval import BertTrainEval -from .flight_booking_processor import FlightBookingProcessor - -__all__ = ["Args", "BertTrainEval", "FlightBookingProcessor"] diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/args.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/args.py deleted file mode 100644 index c49036572..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/args.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Arguments for the model. """ - -import os -import sys -from pathlib import Path - -# pylint:disable=line-too-long -class Args: - """Arguments for the model.""" - - training_data_dir: str = None - bert_model: str = None - task_name: str = None - model_dir: str = None - cleanup_output_dir: bool = False - cache_dir: str = "" - max_seq_length: int = 128 - do_train: bool = None - do_eval: bool = None - do_lower_case: bool = None - train_batch_size: int = 4 - eval_batch_size: int = 8 - learning_rate: float = 5e-5 - num_train_epochs: float = 3.0 - warmup_proportion: float = 0.1 - no_cuda: bool = None - local_rank: int = -1 - seed: int = 42 - gradient_accumulation_steps: int = 1 - fp16: bool = None - loss_scale: float = 0 - - @classmethod - def for_flight_booking( - cls, - training_data_dir: str = os.path.abspath( - os.path.join(os.path.dirname(os.path.abspath(__file__)), "../training_data") - ), - task_name: str = "flight_booking", - ): - """Return the flight booking args.""" - args = cls() - - args.training_data_dir = training_data_dir - args.task_name = task_name - home_dir = str(Path.home()) - args.model_dir = os.path.abspath(os.path.join(home_dir, "models/bert")) - args.bert_model = "bert-base-uncased" - args.do_lower_case = True - - print( - f"Bert Model training_data_dir is set to {args.training_data_dir}", - file=sys.stderr, - ) - print(f"Bert Model model_dir is set to {args.model_dir}", file=sys.stderr) - return args diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/bert_train_eval.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/bert_train_eval.py deleted file mode 100644 index fde3fce80..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/bert_train_eval.py +++ /dev/null @@ -1,375 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import logging -import os -import random -import shutil -import numpy as np -import torch -from .args import Args - -from model_corebot101.bert.common.bert_util import BertUtil -from model_corebot101.bert.train.flight_booking_processor import FlightBookingProcessor -from pytorch_pretrained_bert.file_utils import ( - CONFIG_NAME, - PYTORCH_PRETRAINED_BERT_CACHE, - WEIGHTS_NAME, -) -from pytorch_pretrained_bert.modeling import ( - BertForSequenceClassification, - BertPreTrainedModel, -) -from pytorch_pretrained_bert.optimization import BertAdam -from pytorch_pretrained_bert.tokenization import BertTokenizer -from torch.nn import CrossEntropyLoss -from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset -from torch.utils.data.distributed import DistributedSampler - -from tqdm import tqdm, trange - - -class BertTrainEval: - logger = logging.getLogger(__name__) - - def __init__(self, args: Args): - self.processor = FlightBookingProcessor() - self.output_mode = "classification" - self.args = args - self._prepare() - self.model = self._prepare_model() - - @classmethod - def train_eval(cls, cleanup_output_dir: bool = False) -> None: - # uncomment the following line for debugging. - # import pdb; pdb.set_trace() - args = Args.for_flight_booking() - args.do_train = True - args.do_eval = True - args.cleanup_output_dir = cleanup_output_dir - bert = cls(args) - bert.train() - bert.eval() - - def train(self) -> None: - # Prepare optimizer - param_optimizer = list(self.model.named_parameters()) - no_decay = ["bias", "LayerNorm.bias", "LayerNorm.weight"] - optimizer_grouped_parameters = [ - { - "params": [ - p for n, p in param_optimizer if not any(nd in n for nd in no_decay) - ], - "weight_decay": 0.01, - }, - { - "params": [ - p for n, p in param_optimizer if any(nd in n for nd in no_decay) - ], - "weight_decay": 0.0, - }, - ] - optimizer = BertAdam( - optimizer_grouped_parameters, - lr=self.args.learning_rate, - warmup=self.args.warmup_proportion, - t_total=self.num_train_optimization_steps, - ) - - global_step: int = 0 - nb_tr_steps = 0 - tr_loss: float = 0 - train_features = BertUtil.convert_examples_to_features( - self.train_examples, - self.label_list, - self.args.max_seq_length, - self.tokenizer, - self.output_mode, - ) - self.logger.info("***** Running training *****") - self.logger.info(" Num examples = %d", len(self.train_examples)) - self.logger.info(" Batch size = %d", self.args.train_batch_size) - self.logger.info(" Num steps = %d", self.num_train_optimization_steps) - all_input_ids = torch.tensor( - [f.input_ids for f in train_features], dtype=torch.long - ) - all_input_mask = torch.tensor( - [f.input_mask for f in train_features], dtype=torch.long - ) - all_segment_ids = torch.tensor( - [f.segment_ids for f in train_features], dtype=torch.long - ) - - if self.output_mode == "classification": - all_label_ids = torch.tensor( - [f.label_id for f in train_features], dtype=torch.long - ) - - train_data = TensorDataset( - all_input_ids, all_input_mask, all_segment_ids, all_label_ids - ) - if self.args.local_rank == -1: - train_sampler = RandomSampler(train_data) - else: - train_sampler = DistributedSampler(train_data) - train_dataloader = DataLoader( - train_data, sampler=train_sampler, batch_size=self.args.train_batch_size - ) - - self.model.train() - for _ in trange(int(self.args.num_train_epochs), desc="Epoch"): - tr_loss = 0 - nb_tr_examples, nb_tr_steps = 0, 0 - for step, batch in enumerate(tqdm(train_dataloader, desc="Iteration")): - batch = tuple(t.to(self.device) for t in batch) - input_ids, input_mask, segment_ids, label_ids = batch - - # define a new function to compute loss values for both output_modes - logits = self.model(input_ids, segment_ids, input_mask, labels=None) - - if self.output_mode == "classification": - loss_fct = CrossEntropyLoss() - loss = loss_fct( - logits.view(-1, self.num_labels), label_ids.view(-1) - ) - - if self.args.gradient_accumulation_steps > 1: - loss = loss / self.args.gradient_accumulation_steps - - loss.backward() - - tr_loss += loss.item() - nb_tr_examples += input_ids.size(0) - nb_tr_steps += 1 - if (step + 1) % self.args.gradient_accumulation_steps == 0: - optimizer.step() - optimizer.zero_grad() - global_step += 1 - - if self.args.local_rank == -1 or torch.distributed.get_rank() == 0: - # Save a trained model, configuration and tokenizer - model_to_save = ( - self.model.module if hasattr(self.model, "module") else self.model - ) # Only save the model it-self - - # If we save using the predefined names, we can load using `from_pretrained` - output_model_file = os.path.join(self.args.model_dir, WEIGHTS_NAME) - output_config_file = os.path.join(self.args.model_dir, CONFIG_NAME) - - torch.save(model_to_save.state_dict(), output_model_file) - model_to_save.config.to_json_file(output_config_file) - self.tokenizer.save_vocabulary(self.args.model_dir) - - # Load a trained model and vocabulary that you have fine-tuned - self.model = BertForSequenceClassification.from_pretrained( - self.args.model_dir, num_labels=self.num_labels - ) - self.tokenizer = BertTokenizer.from_pretrained( - self.args.model_dir, do_lower_case=self.args.do_lower_case - ) - else: - self.model = BertForSequenceClassification.from_pretrained( - self.args.bert_model, num_labels=self.num_labels - ) - self.model.to(self.device) - - self.tr_loss, self.global_step = tr_loss, global_step - - self.logger.info("DONE TRAINING."), - - def eval(self) -> None: - if not (self.args.local_rank == -1 or torch.distributed.get_rank() == 0): - return - - eval_examples = self.processor.get_dev_examples(self.args.training_data_dir) - eval_features = BertUtil.convert_examples_to_features( - eval_examples, - self.label_list, - self.args.max_seq_length, - self.tokenizer, - self.output_mode, - ) - self.logger.info("***** Running evaluation *****") - self.logger.info(" Num examples = %d", len(eval_examples)) - self.logger.info(" Batch size = %d", self.args.eval_batch_size) - all_input_ids = torch.tensor( - [f.input_ids for f in eval_features], dtype=torch.long - ) - all_input_mask = torch.tensor( - [f.input_mask for f in eval_features], dtype=torch.long - ) - all_segment_ids = torch.tensor( - [f.segment_ids for f in eval_features], dtype=torch.long - ) - - if self.output_mode == "classification": - all_label_ids = torch.tensor( - [f.label_id for f in eval_features], dtype=torch.long - ) - - eval_data = TensorDataset( - all_input_ids, all_input_mask, all_segment_ids, all_label_ids - ) - # Run prediction for full data - eval_sampler = SequentialSampler(eval_data) - eval_dataloader = DataLoader( - eval_data, sampler=eval_sampler, batch_size=self.args.eval_batch_size - ) - - self.model.eval() - eval_loss = 0 - nb_eval_steps = 0 - preds = [] - - for input_ids, input_mask, segment_ids, label_ids in tqdm( - eval_dataloader, desc="Evaluating" - ): - input_ids = input_ids.to(self.device) - input_mask = input_mask.to(self.device) - segment_ids = segment_ids.to(self.device) - label_ids = label_ids.to(self.device) - - with torch.no_grad(): - logits = self.model(input_ids, segment_ids, input_mask, labels=None) - - # create eval loss and other metric required by the task - if self.output_mode == "classification": - loss_fct = CrossEntropyLoss() - tmp_eval_loss = loss_fct( - logits.view(-1, self.num_labels), label_ids.view(-1) - ) - - eval_loss += tmp_eval_loss.mean().item() - nb_eval_steps += 1 - if len(preds) == 0: - preds.append(logits.detach().cpu().numpy()) - else: - preds[0] = np.append(preds[0], logits.detach().cpu().numpy(), axis=0) - - eval_loss = eval_loss / nb_eval_steps - preds = preds[0] - if self.output_mode == "classification": - preds = np.argmax(preds, axis=1) - result = BertUtil.compute_metrics(self.task_name, preds, all_label_ids.numpy()) - loss = self.tr_loss / self.global_step if self.args.do_train else None - - result["eval_loss"] = eval_loss - result["global_step"] = self.global_step - result["loss"] = loss - - output_eval_file = os.path.join(self.args.model_dir, "eval_results.txt") - with open(output_eval_file, "w") as writer: - self.logger.info("***** Eval results *****") - for key in sorted(result.keys()): - self.logger.info(" %s = %s", key, str(result[key])) - writer.write("%s = %s\n" % (key, str(result[key]))) - - self.logger.info("DONE EVALUATING.") - - def _prepare(self, cleanup_output_dir: bool = False) -> None: - if self.args.local_rank == -1 or self.args.no_cuda: - self.device = torch.device( - "cuda" if torch.cuda.is_available() and not self.args.no_cuda else "cpu" - ) - self.n_gpu = torch.cuda.device_count() - else: - torch.cuda.set_device(self.args.local_rank) - self.device = torch.device("cuda", self.args.local_rank) - self.n_gpu = 1 - # Initializes the distributed backend which will take care of sychronizing nodes/GPUs - torch.distributed.init_process_group(backend="nccl") - - logging.basicConfig( - format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", - datefmt="%m/%d/%Y %H:%M:%S", - level=logging.INFO if self.args.local_rank in [-1, 0] else logging.WARN, - ) - - self.logger.info( - "device: {} n_gpu: {}, distributed training: {}, 16-bits training: {}".format( - self.device, - self.n_gpu, - bool(self.args.local_rank != -1), - self.args.fp16, - ) - ) - - if self.args.gradient_accumulation_steps < 1: - raise ValueError( - "Invalid gradient_accumulation_steps parameter: {}, should be >= 1".format( - self.args.gradient_accumulation_steps - ) - ) - - self.args.train_batch_size = ( - self.args.train_batch_size // self.args.gradient_accumulation_steps - ) - - random.seed(self.args.seed) - np.random.seed(self.args.seed) - torch.manual_seed(self.args.seed) - if self.n_gpu > 0: - torch.cuda.manual_seed_all(self.args.seed) - - if not self.args.do_train and not self.args.do_eval: - raise ValueError("At least one of `do_train` or `do_eval` must be True.") - - if self.args.cleanup_output_dir: - if os.path.exists(self.args.model_dir): - shutil.rmtree(self.args.model_dir) - - if ( - os.path.exists(self.args.model_dir) - and os.listdir(self.args.model_dir) - and self.args.do_train - ): - raise ValueError( - "Output directory ({}) already exists and is not empty.".format( - self.args.model_dir - ) - ) - if not os.path.exists(self.args.model_dir): - os.makedirs(self.args.model_dir) - - self.task_name = self.args.task_name.lower() - - self.label_list = self.processor.get_labels() - self.num_labels = len(self.label_list) - - self.tokenizer = BertTokenizer.from_pretrained( - self.args.bert_model, do_lower_case=self.args.do_lower_case - ) - - self.train_examples = None - self.num_train_optimization_steps = None - if self.args.do_train: - self.train_examples = self.processor.get_train_examples( - self.args.training_data_dir - ) - self.num_train_optimization_steps = ( - int( - len(self.train_examples) - / self.args.train_batch_size - / self.args.gradient_accumulation_steps - ) - * self.args.num_train_epochs - ) - if self.args.local_rank != -1: - self.num_train_optimization_steps = ( - self.num_train_optimization_steps - // torch.distributed.get_world_size() - ) - - def _prepare_model(self) -> BertPreTrainedModel: - if self.args.cache_dir: - cache_dir = self.args.cache_dir - else: - cache_dir = os.path.join( - str(PYTORCH_PRETRAINED_BERT_CACHE), - f"distributed_{self.args.local_rank}", - ) - model = BertForSequenceClassification.from_pretrained( - self.args.bert_model, cache_dir=cache_dir, num_labels=self.num_labels - ) - model.to(self.device) - return model diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/flight_booking_processor.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/flight_booking_processor.py deleted file mode 100644 index f59759d53..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bert/train/flight_booking_processor.py +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import json -import os -from typing import List, Tuple - -from model_corebot101.bert.common.input_example import InputExample - - -class FlightBookingProcessor: - """Processor for the flight booking data set.""" - - def get_train_examples(self, data_dir): - """See base class.""" - return self._create_examples( - self._read_json(os.path.join(data_dir, "FlightBooking.json")), "train" - ) - - def get_dev_examples(self, data_dir): - """See base class.""" - return self._create_examples( - self._read_json(os.path.join(data_dir, "FlightBooking.json")), "dev" - ) - - def get_labels(self): - """See base class.""" - return ["Book flight", "Cancel"] - - def _create_examples(self, lines, set_type): - """Creates examples for the training and dev sets.""" - examples = [] - for (i, line) in enumerate(lines): - guid = "%s-%s" % (set_type, i) - text_a = line[1] - label = line[0] - examples.append( - InputExample(guid=guid, text_a=text_a, text_b=None, label=label) - ) - return examples - - @classmethod - def _read_json(cls, input_file): - with open(input_file, "r", encoding="utf-8") as f: - obj = json.load(f) - examples = obj["utterances"] - lines: List[Tuple[str, str]] = [] - for example in examples: - lines.append((example["intent"], example["text"])) - - return lines diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bidaf/model_runtime/__init__.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bidaf/model_runtime/__init__.py deleted file mode 100644 index a7780a076..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bidaf/model_runtime/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .bidaf_model_runtime import BidafModelRuntime - -__all__ = ["BidafModelRuntime"] diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bidaf/model_runtime/bidaf_model_runtime.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bidaf/model_runtime/bidaf_model_runtime.py deleted file mode 100644 index 982e31054..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bidaf/model_runtime/bidaf_model_runtime.py +++ /dev/null @@ -1,101 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -import os -import sys -import requests -import shutil -from typing import Dict, List, Tuple -import nltk -import numpy as np -from nltk import word_tokenize -from onnxruntime import InferenceSession - -# pylint:disable=line-too-long -class BidafModelRuntime: - def __init__(self, targets: List[str], queries: Dict[str, str], model_dir: str): - self.queries = queries - self.targets = targets - bidaf_model = os.path.abspath(os.path.join(model_dir, "bidaf.onnx")) - print(f"Loading Inference session from {bidaf_model}..", file=sys.stderr) - self.session = InferenceSession(bidaf_model) - print(f"Inference session loaded..", file=sys.stderr) - self.processed_queries = self._process_queries() - print(f"Processed queries..", file=sys.stderr) - - @staticmethod - def init_bidaf(bidaf_model_dir: str, download_ntlk_punkt: bool = False) -> bool: - if os.path.isdir(bidaf_model_dir): - print("bidaf model directory already present..", file=sys.stderr) - else: - print("Creating bidaf model directory..", file=sys.stderr) - os.makedirs(bidaf_model_dir, exist_ok=True) - - # Download Punkt Sentence Tokenizer - if download_ntlk_punkt: - nltk.download("punkt", download_dir=bidaf_model_dir) - nltk.download("punkt") - - # Download bidaf onnx model - onnx_model_file = os.path.abspath(os.path.join(bidaf_model_dir, "bidaf.onnx")) - - print(f"Checking file {onnx_model_file}..", file=sys.stderr) - if os.path.isfile(onnx_model_file): - print("bidaf.onnx downloaded already!", file=sys.stderr) - else: - print("Downloading bidaf.onnx...", file=sys.stderr) - response = requests.get( - "https://onnxzoo.blob.core.windows.net/models/opset_9/bidaf/bidaf.onnx", - stream=True, - ) - with open(onnx_model_file, "wb") as f: - response.raw.decode_content = True - shutil.copyfileobj(response.raw, f) - return True - - def serve(self, context: str) -> Dict[str, str]: - result = {} - cw, cc = BidafModelRuntime._preprocess(context) - for target in self.targets: - qw, qc = self.processed_queries[target] - answer = self.session.run( - ["start_pos", "end_pos"], - { - "context_word": cw, - "context_char": cc, - "query_word": qw, - "query_char": qc, - }, - ) - start = answer[0].item() - end = answer[1].item() - result_item = cw[start : end + 1] - result[target] = BidafModelRuntime._convert_result(result_item) - - return result - - def _process_queries(self) -> Dict[str, Tuple[np.ndarray, np.ndarray]]: - result = {} - for target in self.targets: - question = self.queries[target] - result[target] = BidafModelRuntime._preprocess(question) - - return result - - @staticmethod - def _convert_result(result_item: np.ndarray) -> str: - result = [] - for item in result_item: - result.append(item[0]) - - return " ".join(result) - - @staticmethod - def _preprocess(text: str) -> Tuple[np.ndarray, np.ndarray]: - tokens = word_tokenize(text) - # split into lower-case word tokens, in numpy array with shape of (seq, 1) - words = np.asarray([w.lower() for w in tokens]).reshape(-1, 1) - # split words into chars, in numpy array with shape of (seq, 1, 1, 16) - chars = [[c for c in t][:16] for t in tokens] - chars = [cs + [""] * (16 - len(cs)) for cs in chars] - chars = np.asarray(chars).reshape(-1, 1, 1, 16) - return words, chars diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bidaf/requirements.txt b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bidaf/requirements.txt deleted file mode 100644 index a2eea036e..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/bidaf/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -nltk -numpy -onnxruntime diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/booking_details.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/booking_details.py deleted file mode 100644 index 81f3ee5b8..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/booking_details.py +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Booking Details. -The lu model will detect the properties of a flight booking. -""" - - -class BookingDetails: - """Booking properties from lu model.""" - - def __init__( - self, destination: str = None, origin: str = None, travel_date: str = None - ): - self.destination = destination - self.origin = origin - self.travel_date = travel_date diff --git a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/language_helper.py b/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/language_helper.py deleted file mode 100644 index bc75260f9..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/model_corebot101/language_helper.py +++ /dev/null @@ -1,212 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Language helper that invokes the language model. -This is used from the Bot and Model Runtime to load and invoke the language models. -""" - -import os -import sys -from typing import Dict -from pathlib import Path -import requests -from datatypes_date_time.timex import Timex -from model_corebot101.booking_details import BookingDetails -from model_corebot101.bidaf.model_runtime import BidafModelRuntime -from model_corebot101.bert.model_runtime import BertModelRuntime -from model_corebot101.bert.train import BertTrainEval - -# pylint:disable=line-too-long -class LanguageHelper: - """Language helper that invokes the language model.""" - - home_dir = str(Path.home()) - bert_model_dir_default = os.path.abspath(os.path.join(home_dir, "models/bert")) - bidaf_model_dir_default = os.path.abspath(os.path.join(home_dir, "models/bidaf")) - - # pylint:disable=bad-continuation - def __init__(self): - """Create Language Helper. - Note: Creating the Bert/Bidaf Model Runtime is only necessary for in-proc usage. - """ - self._bidaf_entities = None - self._bert_intents = None - - @property - def entities(self) -> BidafModelRuntime: - """Model used to detect entities.""" - return self._bidaf_entities - - @property - def intents(self) -> BertModelRuntime: - """Model used to detect intents.""" - return self._bert_intents - - def initialize_models( - self, - bert_model_dir: str = bert_model_dir_default, - bidaf_model_dir: str = bidaf_model_dir_default, - ) -> bool: - """ Initialize models. - Perform initialization of the models. - """ - if not BidafModelRuntime.init_bidaf(bidaf_model_dir, download_ntlk_punkt=True): - print( - f"bidaf model creation failed at model directory {bidaf_model_dir}..", - file=sys.stderr, - ) - return False - - if not BertModelRuntime.init_bert(bert_model_dir): - print( - "bert model creation failed at model directory {bert_model_dir}..", - file=sys.stderr, - ) - return False - - print(f"Loading BERT model from {bert_model_dir}...", file=sys.stderr) - if not os.listdir(bert_model_dir): - print(f"No BERT model present, building model..", file=sys.stderr) - BertTrainEval.train_eval(cleanup_output_dir=True) - - self._bert_intents = BertModelRuntime( - model_dir=bert_model_dir, label_list=["Book flight", "Cancel"] - ) - print(f"Loaded BERT model. Loading BiDaf model..", file=sys.stderr) - - self._bidaf_entities = BidafModelRuntime( - targets=["from", "to", "date"], - queries={ - "from": "which city will you travel from?", - "to": "which city will you travel to?", - "date": "which date will you travel?", - }, - model_dir=bidaf_model_dir, - ) - print(f"Loaded BiDAF model from {bidaf_model_dir}.", file=sys.stderr) - - return True - - async def excecute_query_inproc(self, utterance: str) -> BookingDetails: - """Exeecute a query against language model.""" - booking_details = BookingDetails() - intent = self.intents.serve(utterance) - print(f'Recognized intent "{intent}" from "{utterance}".', file=sys.stderr) - if intent == "Book flight": - # Bert gave us the intent. - # Now look for entities with BiDAF.. - entities = self.entities.serve(utterance) - - if "to" in entities: - print(f' Recognized "to" entitiy: {entities["to"]}.', file=sys.stderr) - booking_details.destination = entities["to"] - if "from" in entities: - print( - f' Recognized "from" entitiy: {entities["from"]}.', - file=sys.stderr, - ) - booking_details.origin = entities["from"] - if "date" in entities: - # This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part. - # TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year. - print( - f' Recognized "date" entitiy: {entities["date"]}.', - file=sys.stderr, - ) - travel_date = entities["date"] - if await LanguageHelper.validate_timex(travel_date): - booking_details.travel_date = travel_date - - return booking_details - - @staticmethod - async def excecute_query_service( - configuration: dict, utterance: str - ) -> BookingDetails: - """Invoke lu service to perform prediction/evaluation of utterance.""" - booking_details = BookingDetails() - lu_response = await LanguageHelper.call_model_runtime(configuration, utterance) - if lu_response.status_code == 200: - - response_json = lu_response.json() - intent = response_json["intent"] if "intent" in response_json else None - entities = await LanguageHelper.validate_entities( - response_json["entities"] if "entities" in response_json else None - ) - if intent: - if "to" in entities: - print( - f' Recognized "to" entity: {entities["to"]}.', file=sys.stderr - ) - booking_details.destination = entities["to"] - if "from" in entities: - print( - f' Recognized "from" entity: {entities["from"]}.', - file=sys.stderr, - ) - booking_details.origin = entities["from"] - if "date" in entities: - # This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part. - # TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year. - print( - f' Recognized "date" entity: {entities["date"]}.', - file=sys.stderr, - ) - travel_date = entities["date"] - if await LanguageHelper.validate_timex(travel_date): - booking_details.travel_date = travel_date - return booking_details - - @staticmethod - async def call_model_runtime( - configuration: Dict[str, object], text: str - ) -> requests.Request: - """ Makes a call to the model runtime api - - The model runtime api signature is: - http://:/v1.0/model?q= - - where: - - model_runtime_host - The host running the model runtime api. To resolve - the host running the model runtime api (in the following order): - - MODEL_RUNTIME_API environment variable. Used in docker. - - config.py (which contains the DefaultConfig class). Used running - locally. - - port - http port number (ie, 8880) - - q - A query string to process (ie, the text utterance from user) - - For more details: (See TBD swagger file) - """ - port = os.environ.get("MODEL_RUNTIME_SERVICE_PORT") - host = os.environ.get("MODEL_RUNTIME_SERVICE_HOST") - if host is None: - host = configuration["MODEL_RUNTIME_SERVICE_HOST"] - if port is None: - port = configuration["MODEL_RUNTIME_SERVICE_PORT"] - - api_url = f"http://{host}:{port}/v1.0/model" - qstrings = {"q": text} - return requests.get(api_url, params=qstrings) - - @staticmethod - async def validate_entities(entities: Dict[str, str]) -> bool: - """Validate the entities. - The to and from cities can't be the same. If this is detected, - remove the ambiguous results. """ - if "to" in entities and "from" in entities: - if entities["to"] == entities["from"]: - del entities["to"] - del entities["from"] - return entities - - @staticmethod - async def validate_timex(travel_date: str) -> bool: - """Validate the time. - Make sure time given in the right format. """ - # uncomment the following line for debugging. - # import pdb; pdb.set_trace() - timex_property = Timex(travel_date) - - return len(timex_property.types) > 0 and "definite" not in timex_property.types diff --git a/samples/experimental/101.corebot-bert-bidaf/model/setup.py b/samples/experimental/101.corebot-bert-bidaf/model/setup.py deleted file mode 100644 index e10cc4872..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model/setup.py +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os -from setuptools import setup - -REQUIRES = [ - "torch", - "tqdm", - "pytorch-pretrained-bert", - "onnxruntime>=0.4.0", - "onnx>=1.5.0", - "datatypes-date-time>=1.0.0.a1", - "nltk>=3.4.1", -] - - -root = os.path.abspath(os.path.dirname(__file__)) - -with open(os.path.join(root, "model_corebot101", "about.py")) as f: - package_info = {} - info = f.read() - exec(info, package_info) - -setup( - name=package_info["__title__"], - version=package_info["__version__"], - url=package_info["__uri__"], - author=package_info["__author__"], - description=package_info["__description__"], - keywords="botframework azure botbuilder", - long_description=package_info["__summary__"], - license=package_info["__license__"], - packages=[ - "model_corebot101.bert.train", - "model_corebot101.bert.common", - "model_corebot101.bert.model_runtime", - "model_corebot101.bidaf.model_runtime", - ], - install_requires=REQUIRES, - dependency_links=["https://github.com/pytorch/pytorch"], - include_package_data=True, - classifiers=[ - "Programming Language :: Python :: 3.6", - "Intended Audience :: Developers", - "License :: OSI Approved :: MIT License", - "Operating System :: OS Independent", - "Development Status :: 3 - Alpha", - "Topic :: Scientific/Engineering :: Artificial Intelligence", - ], -) diff --git a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/__init__.py b/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/__init__.py deleted file mode 100644 index d3a549063..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Model Runtime.""" -from .model_cache import ModelCache - -__all__ = ["ModelCache"] diff --git a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/docker_init.py b/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/docker_init.py deleted file mode 100644 index 1e285cc3e..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/docker_init.py +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Docker initialization. -This is called from the Dockerfile when creating the model runtime service API -container. -""" -import os -from pathlib import Path -from model_corebot101.language_helper import LanguageHelper - -# Initialize the models -LH = LanguageHelper() -HOME_DIR = str(Path.home()) -BERT_MODEL_DIR_DEFAULT = os.path.abspath(os.path.join(HOME_DIR, "models/bert")) -BIDAF_MODEL_DIR_DEFAULT = os.path.abspath(os.path.join(HOME_DIR, "models/bidaf")) - -LH.initialize_models( - bert_model_dir=BERT_MODEL_DIR_DEFAULT, bidaf_model_dir=BIDAF_MODEL_DIR_DEFAULT -) diff --git a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/handlers/model_handler.py b/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/handlers/model_handler.py deleted file mode 100644 index 32a3ff1bb..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/handlers/model_handler.py +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Tornado handler to access the model runtime. - -To invoke: - /v1.0/model?q= -""" - -import logging -import json -from tornado.web import RequestHandler -from model_corebot101.language_helper import LanguageHelper - -# pylint:disable=abstract-method -class ModelHandler(RequestHandler): - """Model Handler implementation to access the model runtime.""" - - _handler_routes = ["/v1.0/model/$", "/v1.0/model$"] - - @classmethod - def build_config(cls, ref_obj: dict): - """Build the Tornado configuration for this handler.""" - return [(route, ModelHandler, ref_obj) for route in cls._handler_routes] - - def set_default_headers(self): - """Set the default HTTP headers.""" - RequestHandler.set_default_headers(self) - self.set_header("Content-Type", "application/json") - self.set_header("Access-Control-Allow-Origin", "*") - self.set_header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept") - self.set_header("Access-Control-Allow-Methods", "OPTIONS, GET") - - # pylint:disable=attribute-defined-outside-init - def initialize(self, language_helper: LanguageHelper): - """Initialize the handler.""" - RequestHandler.initialize(self) - self._language_helper = language_helper - self._logger = logging.getLogger("MODEL_HANDLER") - - async def get(self): - """Handle HTTP GET request.""" - text = self.get_argument("q", None, True) - if not text: - return (404, "Missing the q query string with the text") - - response = {} - intent = self._language_helper.intents.serve(text) - response["intent"] = intent if intent else "None" - entities = self._language_helper.entities.serve(text) - response["entities"] = entities if entities else "None" - self.write(json.dumps(response)) - return (200, "Complete") diff --git a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/main.py b/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/main.py deleted file mode 100644 index 6d622f182..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/main.py +++ /dev/null @@ -1,109 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Model Runtime. -Entry point for the model runtime. -""" -import os -import signal -import logging -from logging.handlers import RotatingFileHandler -import tornado -from tornado.options import define, options -from pathlib import Path -from model_corebot101.language_helper import LanguageHelper -from handlers.model_handler import ModelHandler - -HOME_DIR = str(Path.home()) - -# Define Tornado options -define("port", default=8880, help="HTTP port for model runtime to listen on", type=int) -define( - "bidaf_model_dir", - default=os.path.abspath(os.path.join(HOME_DIR, "models/bidaf")), - help="bidaf model directory", -) -define( - "bert_model_dir", - default=os.path.abspath(os.path.join(HOME_DIR, "models/bert")), - help="bert model directory", -) - - -def setup_logging(): - """Set up logging.""" - logging.info("Setting up logging infrastructure") - - # Create the rotating log handler - if not os.path.exists("logs"): - os.mkdir("logs") - handler = RotatingFileHandler( - os.path.join("./logs", "model-runtime.log"), - maxBytes=5 * 1024 ** 2, # 5 MB chunks, - backupCount=5, # limit to 25 MB logs max - ) - - # Set the formatter - handler.setFormatter( - logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s") - ) - - # Setup the root logging with the necessary handlers - log = logging.getLogger() - log.addHandler(handler) - - # Set to info for normal processing - log.setLevel(logging.INFO) - - -# pylint:disable=unused-argument -def signal_handler(sig_num, frame): - """Stop activity on signal.""" - tornado.ioloop.IOLoop.instance().stop() - - -def run(): - """Main entry point for model runtime api.""" - - # Register signal handlers. - logging.info("Preparing signal handlers..") - signal.signal(signal.SIGINT, signal_handler) - signal.signal(signal.SIGTERM, signal_handler) - - # Set up model cache. - # If containerizing, suggest initializing the directories (and associated - # file downloads) be performed during container build time. - logging.info("Initializing model directories:") - logging.info(" bert : %s", options.bert_model_dir) - logging.info(" bidaf : %s", options.bidaf_model_dir) - - language_helper = LanguageHelper() - if ( - language_helper.initialize_models( - options.bert_model_dir, options.bidaf_model_dir - ) - is False - ): - logging.error("Could not initilize model directories. Exiting..") - return - - # Build the configuration - logging.info("Building config..") - ref_obj = {"language_helper": language_helper} - app_config = ModelHandler.build_config(ref_obj) - - logging.info("Starting Tornado model runtime service..") - application = tornado.web.Application(app_config) - application.listen(options.port) - - # Protect the loop with a try/catch - try: - # Start the app and wait for a close - tornado.ioloop.IOLoop.instance().start() - finally: - # handle error with shutting down loop - tornado.ioloop.IOLoop.instance().stop() - - -if __name__ == "__main__": - setup_logging() - run() diff --git a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/model_cache.py b/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/model_cache.py deleted file mode 100644 index 2c9d61c87..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/model_runtime_svc_corebot101/model_cache.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -"""Model Cache. -Simple container for bidaf/bert models. -""" -import os -import logging - -from model_corebot101.bidaf.model_runtime import BidafModelRuntime -from model_corebot101.bert.model_runtime import BertModelRuntime - -# pylint:disable=line-too-long,bad-continuation -class DeprecateModelCache(object): - """Model Cache implementation.""" - - def __init__(self): - self._logger = logging.getLogger("ModelCache") - self._bert_model_dir = None - self._bidaf_model_dir = None - self._bert_intents = None - self._bidaf_entities = None - - def init_model_dir(self, bidaf_model_dir: str, bert_model_dir: str) -> bool: - """ Initialize models """ - if not os.path.exists(bidaf_model_dir): - # BiDAF needs no training, just download - if not BidafModelRuntime.init_bidaf(bidaf_model_dir, True): - self._logger.error( - "bidaf model creation failed at model directory %s..", - bidaf_model_dir, - ) - return False - - if not os.path.exists(bert_model_dir): - self._logger.error( - 'BERT model directory does not exist "%s"', bert_model_dir - ) - return False - - self._bert_model_dir = os.path.normpath(bert_model_dir) - self._bidaf_model_dir = os.path.normpath(bidaf_model_dir) - - self._bert_intents = BertModelRuntime( - model_dir=self._bert_model_dir, label_list=["Book flight", "Cancel"] - ) - self._bidaf_entities = BidafModelRuntime( - targets=["from", "to", "date"], - queries={ - "from": "which city will you travel from?", - "to": "which city will you travel to?", - "date": "which date will you travel?", - }, - model_dir=self._bidaf_model_dir, - ) - self._logger.info("bidaf entities model created : %s..", self._bidaf_model_dir) - - return True - - @property - def entities(self): - """Get the model that detect entities: bidaf.""" - return self._bidaf_entities - - @property - def intents(self): - """Get the model that detect intents: bert.""" - return self._bert_intents diff --git a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/setup.py b/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/setup.py deleted file mode 100644 index 35494dacc..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/model_runtime_svc/setup.py +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os -from setuptools import setup - -REQUIRES = [ - "scikit-learn>=0.21.2", - "scipy>=1.3.0", - "tornado>=6.0.2", - "model_corebot101>=0.0.1", -] - -root = os.path.abspath(os.path.dirname(__file__)) - -with open(os.path.join(root, "model_runtime_svc_corebot101", "about.py")) as f: - package_info = {} - info = f.read() - exec(info, package_info) - -setup( - name=package_info["__title__"], - version=package_info["__version__"], - url=package_info["__uri__"], - author=package_info["__author__"], - description=package_info["__description__"], - keywords="botframework azure botbuilder", - long_description=package_info["__summary__"], - license=package_info["__license__"], - packages=["model_runtime_svc_corebot101", "model_runtime_svc_corebot101.handlers"], - install_requires=REQUIRES, - dependency_links=["https://github.com/pytorch/pytorch"], - include_package_data=True, - classifiers=[ - "Programming Language :: Python :: 3.6", - "Intended Audience :: Developers", - "License :: OSI Approved :: MIT License", - "Operating System :: OS Independent", - "Development Status :: 3 - Alpha", - "Topic :: Scientific/Engineering :: Artificial Intelligence", - ], -) diff --git a/samples/experimental/101.corebot-bert-bidaf/notebooks/bert_model_runtime.ipynb b/samples/experimental/101.corebot-bert-bidaf/notebooks/bert_model_runtime.ipynb deleted file mode 100644 index 4dd87e70d..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/notebooks/bert_model_runtime.ipynb +++ /dev/null @@ -1,323 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Test the intent classifier\n", - "This notebook uses the model trained in [bert_train.ipynb notebook](bert_train.ipynb). See the [README.md](../README.md) for instructions on how to use that notebook.\n", - "\n", - "## `model_corebot101` package\n", - "This sample creates a separate python package (`model_corebot101`) which contains code to train (tune), evaluate and infer intent classifiers for this sample.\n", - "\n", - "\n", - "## See also:\n", - "- [bert_train.ipynb](bert_train.ipynb) to train the intent classifier model.\n", - "- [bidaf_model_runtime.ipynb](bidaf_model_runtime.ipynb) to test the associated BiDAF model to test the entity classifier model.\n", - "- [The model runtime](model_runtime.ipynb) to test the both the BERT and BiDAF model together." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from model_corebot101.bert.model_runtime.bert_model_runtime import BertModelRuntime" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `BertModelRuntime` class\n", - "The `BertModelRuntime` class is used to perform the inferences against the bot utterances.\n", - "\n", - "The model is placed (during training) in the `/models/bert` directory which is packaged with the bot.\n", - "\n", - "The `label_list` is an array of intents." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "from pathlib import Path\n", - "bert_model_dir = os.path.abspath(os.path.join(Path.home(), \"models/bert\"))\n", - "s = BertModelRuntime(model_dir=bert_model_dir, label_list=[\"Book flight\", \"Cancel\"])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `BertModelRuntime.serve` method\n", - "The `BertModelRuntime.serve` method is used to perform the classification against the bot utterances." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Book flight'" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve('i want to travel from new york to berlin')" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Book flight'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"please book a flight for me\")" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Book flight'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"from seattle to san\")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Cancel'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"random random random 42\")" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Cancel'" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"any\")" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Book flight'" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"take me to New York\")" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Book flight'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"we'd like to go to seattle\")" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Cancel'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"not this one\")" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Cancel'" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"I don't care about this one\")" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Cancel'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"I don't want to see that\")" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Cancel'" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"boring\")" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Book flight'" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"you have no clue how to book a flight\")" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "botsample", - "language": "python", - "name": "botsample" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.8" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/samples/experimental/101.corebot-bert-bidaf/notebooks/bert_train.ipynb b/samples/experimental/101.corebot-bert-bidaf/notebooks/bert_train.ipynb deleted file mode 100644 index a9296240e..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/notebooks/bert_train.ipynb +++ /dev/null @@ -1,281 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Train the intent classifier using pretrained BERT model as featurizer\n", - "This notebook creates the BERT language classifier model. See the [README.md](../README.md) for instructions on how to run this sample.\n", - "The resulting model is placed in the `/models/bert` directory which is packaged with the bot.\n", - "\n", - "## `model_corebot101` package\n", - "This sample creates a separate python package (`model_corebot101`) which contains all the code to train, evaluate and infer intent classifiers for this sample.\n", - "\n", - "## See also:\n", - "- [The BERT runtime model](bert_model_runtime.ipynb) to test the resulting intent classifier model.\n", - "- [The BiDAF runtime model](bidaf_model_runtime.ipynb) to test the associated BiDAF model to test the entity classifier model.\n", - "- [The model runtime](model_runtime.ipynb) to test the both the BERT and BiDAF model together.\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from model_corebot101.bert.train import BertTrainEval" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `BertTrainEvan.train_eval` method\n", - "This method performs all the training and performs evaluation that's listed at the bottom of the output. Training may take several minutes to complete.\n", - "\n", - "The evaluation output should look something like the following:\n", - "```bash\n", - "06/02/2019 19:46:52 - INFO - model_corebot101.bert.train.bert_train_eval - ***** Eval results *****\n", - "06/02/2019 19:46:52 - INFO - model_corebot101.bert.train.bert_train_eval - acc = 1.0\n", - "06/02/2019 19:46:52 - INFO - model_corebot101.bert.train.bert_train_eval - acc_and_f1 = 1.0\n", - "06/02/2019 19:46:52 - INFO - model_corebot101.bert.train.bert_train_eval - eval_loss = 0.06498947739601135\n", - "06/02/2019 19:46:52 - INFO - model_corebot101.bert.train.bert_train_eval - f1 = 1.0\n", - "06/02/2019 19:46:52 - INFO - model_corebot101.bert.train.bert_train_eval - global_step = 12\n", - "06/02/2019 19:46:52 - INFO - model_corebot101.bert.train.bert_train_eval - loss = 0.02480666587750117\n", - "```" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Bert Model training_data_dir is set to d:\\python\\daveta-docker-wizard\\apub\\samples\\flask\\101.corebot-bert-bidaf\\model\\model_corebot101\\bert\\training_data\n", - "Bert Model model_dir is set to C:\\Users\\daveta\\models\\bert\n", - "07/02/2019 07:16:09 - INFO - model_corebot101.bert.train.bert_train_eval - device: cpu n_gpu: 0, distributed training: False, 16-bits training: None\n", - "07/02/2019 07:16:09 - INFO - pytorch_pretrained_bert.tokenization - loading vocabulary file https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased-vocab.txt from cache at C:\\Users\\daveta\\.pytorch_pretrained_bert\\26bc1ad6c0ac742e9b52263248f6d0f00068293b33709fae12320c0e35ccfbbb.542ce4285a40d23a559526243235df47c5f75c197f04f37d1a0c124c32c9a084\n", - "07/02/2019 07:16:10 - INFO - pytorch_pretrained_bert.modeling - loading archive file https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased.tar.gz from cache at C:\\Users\\daveta\\.pytorch_pretrained_bert\\distributed_-1\\9c41111e2de84547a463fd39217199738d1e3deb72d4fec4399e6e241983c6f0.ae3cef932725ca7a30cdcb93fc6e09150a55e2a130ec7af63975a16c153ae2ba\n", - "07/02/2019 07:16:10 - INFO - pytorch_pretrained_bert.modeling - extracting archive file C:\\Users\\daveta\\.pytorch_pretrained_bert\\distributed_-1\\9c41111e2de84547a463fd39217199738d1e3deb72d4fec4399e6e241983c6f0.ae3cef932725ca7a30cdcb93fc6e09150a55e2a130ec7af63975a16c153ae2ba to temp dir C:\\Users\\daveta\\AppData\\Local\\Temp\\tmp9hepebcl\n", - "07/02/2019 07:16:13 - INFO - pytorch_pretrained_bert.modeling - Model config {\n", - " \"attention_probs_dropout_prob\": 0.1,\n", - " \"hidden_act\": \"gelu\",\n", - " \"hidden_dropout_prob\": 0.1,\n", - " \"hidden_size\": 768,\n", - " \"initializer_range\": 0.02,\n", - " \"intermediate_size\": 3072,\n", - " \"max_position_embeddings\": 512,\n", - " \"num_attention_heads\": 12,\n", - " \"num_hidden_layers\": 12,\n", - " \"type_vocab_size\": 2,\n", - " \"vocab_size\": 30522\n", - "}\n", - "\n", - "07/02/2019 07:16:16 - INFO - pytorch_pretrained_bert.modeling - Weights of BertForSequenceClassification not initialized from pretrained model: ['classifier.weight', 'classifier.bias']\n", - "07/02/2019 07:16:16 - INFO - pytorch_pretrained_bert.modeling - Weights from pretrained model not used in BertForSequenceClassification: ['cls.predictions.bias', 'cls.predictions.transform.dense.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.decoder.weight', 'cls.seq_relationship.weight', 'cls.seq_relationship.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.LayerNorm.bias']\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - Writing example 0 of 16\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - *** Example ***\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - guid: train-0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - tokens: [CLS] book flight from london to paris on feb 14th [SEP]\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_ids: 101 2338 3462 2013 2414 2000 3000 2006 13114 6400 102 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_mask: 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - segment_ids: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - label: Book flight (id = 0)\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - *** Example ***\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - guid: train-1\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - tokens: [CLS] book flight to berlin on feb 14th [SEP]\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_ids: 101 2338 3462 2000 4068 2006 13114 6400 102 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_mask: 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - segment_ids: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - label: Book flight (id = 0)\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - *** Example ***\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - guid: train-2\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - tokens: [CLS] book me a flight from london to paris [SEP]\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_ids: 101 2338 2033 1037 3462 2013 2414 2000 3000 102 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_mask: 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - segment_ids: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - label: Book flight (id = 0)\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - *** Example ***\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - guid: train-3\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - tokens: [CLS] bye [SEP]\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_ids: 101 9061 102 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_mask: 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - segment_ids: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - label: Cancel (id = 1)\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - *** Example ***\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - guid: train-4\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - tokens: [CLS] cancel booking [SEP]\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_ids: 101 17542 21725 102 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - input_mask: 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - segment_ids: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.common.bert_util - label: Cancel (id = 1)\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.train.bert_train_eval - ***** Running training *****\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.train.bert_train_eval - Num examples = 16\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.train.bert_train_eval - Batch size = 4\n", - "07/02/2019 07:16:16 - INFO - model_corebot101.bert.train.bert_train_eval - Num steps = 12\n", - "Epoch: 0%| | 0/3 [00:00 float(.30):\n", - " raise Exception(f'Size of output file {f} is out of range of expected.')\n", - " else:\n", - " raise Exception(f'Expected file {f} missing from output.')" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "botsample", - "language": "python", - "name": "botsample" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.8" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/samples/experimental/101.corebot-bert-bidaf/notebooks/bidaf_model_runtime.ipynb b/samples/experimental/101.corebot-bert-bidaf/notebooks/bidaf_model_runtime.ipynb deleted file mode 100644 index 682df02c8..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/notebooks/bidaf_model_runtime.ipynb +++ /dev/null @@ -1,228 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Test the BiDAF runtime model\n", - "This notebook uses the BiDAF language entitiy recognizer model. See the [README.md](../README.md) for instructions on how to run this sample.\n", - "\n", - "## `model_corebot101` package\n", - "This sample creates a python package (`model_corebot101`) which contains all the code to train, evaluate and infer intent classifier for this sample.\n", - "\n", - "## See also:\n", - "- [The BERT training model](bert_train.ipynb) to train the intent classifier model.\n", - "- [The BERT runtime model](bert_model_runtime.ipynb) to test the BERT model to test the intent classifier model.\n", - "- [The model runtime](model_runtime.ipynb) to test the both the BERT and BiDAF model together." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from model_corebot101.bidaf.model_runtime.bidaf_model_runtime import BidafModelRuntime\n", - "import os\n", - "from pathlib import Path\n", - "from IPython.display import display\n", - "\n", - "bidaf_model_dir = os.path.abspath(os.path.join(Path.home(), \"models/bidaf\"))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `BidafModelRuntime` class\n", - "The `BidafModelRuntime` class is used to perform the classification for entities on the bot utterances.\n", - "\n", - "The model is completely is downloaded and placed in the `/models/bidaf` directory.\n", - "\n", - "## `BidafModelRuntime.init_bidaf` method\n", - "The `BidafModelRuntime.init_bidaf` method downloads the necessary ONNX model.\n", - "\n", - "Output should look like the following: \n", - "\n", - "```bash\n", - "Creating bidaf model directory..\n", - "Checking file ../../bot/cognitiveModels/bidaf\\bidaf.onnx..\n", - "Downloading bidaf.onnx...\n", - "```" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "bidaf model directory already present..\n", - "[nltk_data] Downloading package punkt to\n", - "[nltk_data] C:\\Users\\daveta\\models\\bidaf...\n", - "[nltk_data] Package punkt is already up-to-date!\n", - "[nltk_data] Downloading package punkt to\n", - "[nltk_data] C:\\Users\\daveta\\AppData\\Roaming\\nltk_data...\n", - "[nltk_data] Package punkt is already up-to-date!\n", - "Checking file C:\\Users\\daveta\\models\\bidaf\\bidaf.onnx..\n", - "bidaf.onnx downloaded already!\n" - ] - }, - { - "data": { - "text/plain": [ - "'The BiDAF model successfully downloaded.'" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "if not BidafModelRuntime.init_bidaf(bidaf_model_dir, True):\n", - " display('The BiDAF model was not downloaded successfully. See output below for more details.')\n", - "else:\n", - " display('The BiDAF model successfully downloaded.')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `BidafModelRuntime` class\n", - "The `BidafModelRuntime` class is used to perform the classification against the bot utterances.\n", - "\n", - "- `targets` : an array of entities to classify.\n", - "- `queries` : examples passed to assist the classifier\n", - "- `model_dir` : path to the model\n", - "\n", - "The output should resemble the following:\n", - "\n", - "```bash\n", - "Loading Inference session from C:\\Users\\<>\\models\\bidaf\\bidaf.onnx..\n", - "Inference session loaded..\n", - "Processed queries..\n", - "```\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Loading Inference session from C:\\Users\\daveta\\models\\bidaf\\bidaf.onnx..\n", - "Inference session loaded..\n", - "Processed queries..\n" - ] - } - ], - "source": [ - "s = BidafModelRuntime(\n", - " targets=[\"from\", \"to\", \"date\"],\n", - " queries={\n", - " \"from\": \"which city will you travel from?\",\n", - " \"to\": \"which city will you travel to?\",\n", - " \"date\": \"which date will you travel?\",\n", - " },\n", - " model_dir = bidaf_model_dir\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'from': 'london', 'to': 'paris', 'date': 'feb 14th'}" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"flight to paris from london on feb 14th\")" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'from': 'london', 'to': 'paris', 'date': 'feb 14th'}" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"book flight from london to paris on feb 14th\")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'from': 'berlin', 'to': 'berlin', 'date': '5th'}" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.serve(\"fly from berlin to paris on may 5th\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "botsample", - "language": "python", - "name": "botsample" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.8" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/samples/experimental/101.corebot-bert-bidaf/notebooks/model_runtime.ipynb b/samples/experimental/101.corebot-bert-bidaf/notebooks/model_runtime.ipynb deleted file mode 100644 index 8adca2b30..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/notebooks/model_runtime.ipynb +++ /dev/null @@ -1,206 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Test the intent classifier and entity extractor\n", - "This notebook uses the pretrained BiDAF model and BERT model tuned in [bert_train.ipynb notebook](bert_train.ipynb). See the [README.md](../README.md) for instructions on how to use that notebook.\n", - "\n", - "## See also:\n", - "- [bert_train.ipynb](bert_train.ipynb) to train the intent classifier model.\n", - "- [bert_model_runtime.ipynb](bert_model_runtime.ipynb) to test the BERT intent classifier model.\n", - "- [bidaf_model_runtime.ipynb](bidaf_model_runtime.ipynb) to test the associated BiDAF model to test the entity extractor model." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from model_corebot101.bert.model_runtime.bert_model_runtime import BertModelRuntime\n", - "from model_corebot101.bidaf.model_runtime.bidaf_model_runtime import BidafModelRuntime\n", - "import os\n", - "from pathlib import Path\n", - "bidaf_model_dir = os.path.abspath(os.path.join(Path.home(), \"models/bidaf\"))\n", - "bert_model_dir = os.path.abspath(os.path.join(Path.home(), \"models/bert\"))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "bidaf model directory already present..\n" - ] - } - ], - "source": [ - "BidafModelRuntime.init_bidaf(bidaf_model_dir, True)\n", - "bidaf = BidafModelRuntime(\n", - " targets=[\"from\", \"to\", \"date\"],\n", - " queries={\n", - " \"from\": \"which city will you travel from?\",\n", - " \"to\": \"which city will you travel to?\",\n", - " \"date\": \"which date will you travel?\",\n", - " },\n", - " model_dir = bidaf_model_dir\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bert = BertModelRuntime(model_dir=bert_model_dir, label_list=[\"Book flight\", \"Cancel\"])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def serve(utterance):\n", - " intent = bert.serve(utterance)\n", - " entities = bidaf.serve(utterance)\n", - " return intent, entities" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `BertModelRuntime.serve` method\n", - "The `BertModelRuntime.serve` method is used to perform the classification against the bot utterances." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"flight to paris from london on feb 14th\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"from seattle to san\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"random random random 42\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"any\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"take me to New York\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"we'd like to go to seattle\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"not this one\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"I don't care about this one\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"I don't want to see that\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"boring\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "serve(\"you have no clue how to book a flight\")" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.8" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/samples/experimental/101.corebot-bert-bidaf/requirements.txt b/samples/experimental/101.corebot-bert-bidaf/requirements.txt deleted file mode 100644 index 496696f2c..000000000 --- a/samples/experimental/101.corebot-bert-bidaf/requirements.txt +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -# Note: The model must be built first! -# cd model -# - -# The following are installed outside of requirements.txt -# conda install -c pytorch pytorch -y -# pip install onnxruntime -# Install python package dependencies with the following: -# `pip install -r requirements.txt` - -# Bot -Flask>=1.0.2 -asyncio>=3.4.3 -requests>=2.18.1 - -# Bot Framework -botframework-connector>=4.4.0.b1 -botbuilder-schema>=4.4.0.b1 -botbuilder-core>=4.4.0.b1 -botbuilder-dialogs>=4.4.0.b1 -botbuilder-ai>=4.4.0.b1 -datatypes-date-time>=1.0.0.a1 -azure-cognitiveservices-language-luis>=0.2.0 -msrest>=0.6.6 - -# Internal library - must be built first! -model_corebot101>=0.0.1 - -torch -onnx -onnxruntime -tqdm>=4.32.1 -pytorch-pretrained-bert>=0.6.2 -nltk>=3.4.1 -numpy>=1.16.3 -scipy>=1.3.0 -scikit-learn>=0.21.2 - diff --git a/scenarios/action-based-messaging-extension-fetch-task/app.py b/scenarios/action-based-messaging-extension-fetch-task/app.py deleted file mode 100644 index 103c5f31a..000000000 --- a/scenarios/action-based-messaging-extension-fetch-task/app.py +++ /dev/null @@ -1,93 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import json -import sys -from datetime import datetime - -from aiohttp import web -from aiohttp.web import Request, Response, json_response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes -from bots import ActionBasedMessagingExtensionFetchTaskBot -from config import DefaultConfig - -CONFIG = DefaultConfig() - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error(context: TurnContext, error: Exception): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = on_error - -# Create the Bot -BOT = ActionBasedMessagingExtensionFetchTaskBot() - - -# Listen for incoming requests on /api/messages -async def messages(req: Request) -> Response: - # Main bot message handler. - if "application/json" in req.headers["Content-Type"]: - body = await req.json() - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - - try: - invoke_response = await ADAPTER.process_activity( - activity, auth_header, BOT.on_turn - ) - if invoke_response: - return json_response( - data=invoke_response.body, status=invoke_response.status - ) - return Response(status=201) - except PermissionError: - return Response(status=401) - except Exception: - return Response(status=500) - - -APP = web.Application() -APP.router.add_post("/api/messages", messages) - -if __name__ == "__main__": - try: - web.run_app(APP, host="localhost", port=CONFIG.PORT) - except Exception as error: - raise error diff --git a/scenarios/action-based-messaging-extension-fetch-task/bots/__init__.py b/scenarios/action-based-messaging-extension-fetch-task/bots/__init__.py deleted file mode 100644 index fe9caf948..000000000 --- a/scenarios/action-based-messaging-extension-fetch-task/bots/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .action_based_messaging_extension_fetch_task_bot import ( - ActionBasedMessagingExtensionFetchTaskBot, -) - -__all__ = ["ActionBasedMessagingExtensionFetchTaskBot"] diff --git a/scenarios/action-based-messaging-extension-fetch-task/bots/action_based_messaging_extension_fetch_task_bot.py b/scenarios/action-based-messaging-extension-fetch-task/bots/action_based_messaging_extension_fetch_task_bot.py deleted file mode 100644 index 9e9c13fa9..000000000 --- a/scenarios/action-based-messaging-extension-fetch-task/bots/action_based_messaging_extension_fetch_task_bot.py +++ /dev/null @@ -1,229 +0,0 @@ -# Copyright (c) Microsoft Corp. All rights reserved. -# Licensed under the MIT License. - -from typing import List -import random -from botbuilder.core import ( - CardFactory, - MessageFactory, - TurnContext, -) -from botbuilder.schema import Attachment -from botbuilder.schema.teams import ( - MessagingExtensionAction, - MessagingExtensionActionResponse, - TaskModuleContinueResponse, - MessagingExtensionResult, - TaskModuleTaskInfo, -) -from botbuilder.core.teams import TeamsActivityHandler -from example_data import ExampleData - - -class ActionBasedMessagingExtensionFetchTaskBot(TeamsActivityHandler): - async def on_message_activity(self, turn_context: TurnContext): - value = turn_context.activity.value - if value is not None: - # This was a message from the card. - answer = value["Answer"] - choices = value["Choices"] - reply = MessageFactory.text( - f"{turn_context.activity.from_property.name} answered '{answer}' and chose '{choices}'." - ) - await turn_context.send_activity(reply) - else: - # This is a regular text message. - reply = MessageFactory.text( - "Hello from ActionBasedMessagingExtensionFetchTaskBot." - ) - await turn_context.send_activity(reply) - - async def on_teams_messaging_extension_fetch_task( - self, turn_context: TurnContext, action: MessagingExtensionAction - ) -> MessagingExtensionActionResponse: - card = self._create_adaptive_card_editor() - task_info = TaskModuleTaskInfo( - card=card, height=450, title="Task Module Fetch Example", width=500 - ) - continue_response = TaskModuleContinueResponse(type="continue", value=task_info) - return MessagingExtensionActionResponse(task=continue_response) - - async def on_teams_messaging_extension_submit_action( # pylint: disable=unused-argument - self, turn_context: TurnContext, action: MessagingExtensionAction - ) -> MessagingExtensionActionResponse: - question = action.data["Question"] - multi_select = action.data["MultiSelect"] - option1 = action.data["Option1"] - option2 = action.data["Option2"] - option3 = action.data["Option3"] - preview_card = self._create_adaptive_card_preview( - user_text=question, - is_multi_select=multi_select, - option1=option1, - option2=option2, - option3=option3, - ) - - extension_result = MessagingExtensionResult( - type="botMessagePreview", - activity_preview=MessageFactory.attachment(preview_card), - ) - return MessagingExtensionActionResponse(compose_extension=extension_result) - - async def on_teams_messaging_extension_bot_message_preview_edit( # pylint: disable=unused-argument - self, turn_context: TurnContext, action: MessagingExtensionAction - ) -> MessagingExtensionActionResponse: - activity_preview = action.bot_activity_preview[0] - content = activity_preview.attachments[0].content - data = self._get_example_data(content) - card = self._create_adaptive_card_editor( - data.question, - data.is_multi_select, - data.option1, - data.option2, - data.option3, - ) - task_info = TaskModuleTaskInfo( - card=card, height=450, title="Task Module Fetch Example", width=500 - ) - continue_response = TaskModuleContinueResponse(type="continue", value=task_info) - return MessagingExtensionActionResponse(task=continue_response) - - async def on_teams_messaging_extension_bot_message_preview_send( # pylint: disable=unused-argument - self, turn_context: TurnContext, action: MessagingExtensionAction - ) -> MessagingExtensionActionResponse: - activity_preview = action.bot_activity_preview[0] - content = activity_preview.attachments[0].content - data = self._get_example_data(content) - card = self._create_adaptive_card_preview( - data.question, - data.is_multi_select, - data.option1, - data.option2, - data.option3, - ) - message = MessageFactory.attachment(card) - await turn_context.send_activity(message) - - def _get_example_data(self, content: dict) -> ExampleData: - body = content["body"] - question = body[1]["text"] - choice_set = body[3] - multi_select = "isMultiSelect" in choice_set - option1 = choice_set["choices"][0]["value"] - option2 = choice_set["choices"][1]["value"] - option3 = choice_set["choices"][2]["value"] - return ExampleData(question, multi_select, option1, option2, option3) - - def _create_adaptive_card_editor( - self, - user_text: str = None, - is_multi_select: bool = False, - option1: str = None, - option2: str = None, - option3: str = None, - ) -> Attachment: - return CardFactory.adaptive_card( - { - "actions": [ - { - "data": {"submitLocation": "messagingExtensionFetchTask"}, - "title": "Submit", - "type": "Action.Submit", - } - ], - "body": [ - { - "text": "This is an Adaptive Card within a Task Module", - "type": "TextBlock", - "weight": "bolder", - }, - {"type": "TextBlock", "text": "Enter text for Question:"}, - { - "id": "Question", - "placeholder": "Question text here", - "type": "Input.Text", - "value": user_text, - }, - {"type": "TextBlock", "text": "Options for Question:"}, - {"type": "TextBlock", "text": "Is Multi-Select:"}, - { - "choices": [ - {"title": "True", "value": "true"}, - {"title": "False", "value": "false"}, - ], - "id": "MultiSelect", - "isMultiSelect": "false", - "style": "expanded", - "type": "Input.ChoiceSet", - "value": "true" if is_multi_select else "false", - }, - { - "id": "Option1", - "placeholder": "Option 1 here", - "type": "Input.Text", - "value": option1, - }, - { - "id": "Option2", - "placeholder": "Option 2 here", - "type": "Input.Text", - "value": option2, - }, - { - "id": "Option3", - "placeholder": "Option 3 here", - "type": "Input.Text", - "value": option3, - }, - ], - "type": "AdaptiveCard", - "version": "1.0", - } - ) - - def _create_adaptive_card_preview( - self, - user_text: str = None, - is_multi_select: bool = False, - option1: str = None, - option2: str = None, - option3: str = None, - ) -> Attachment: - return CardFactory.adaptive_card( - { - "actions": [ - { - "type": "Action.Submit", - "title": "Submit", - "data": {"submitLocation": "messagingExtensionSubmit"}, - } - ], - "body": [ - { - "text": "Adaptive Card from Task Module", - "type": "TextBlock", - "weight": "bolder", - }, - {"text": user_text, "type": "TextBlock", "id": "Question"}, - { - "id": "Answer", - "placeholder": "Answer here...", - "type": "Input.Text", - }, - { - "choices": [ - {"title": option1, "value": option1}, - {"title": option2, "value": option2}, - {"title": option3, "value": option3}, - ], - "id": "Choices", - "isMultiSelect": is_multi_select, - "style": "expanded", - "type": "Input.ChoiceSet", - }, - ], - "type": "AdaptiveCard", - "version": "1.0", - } - ) diff --git a/scenarios/action-based-messaging-extension-fetch-task/example_data.py b/scenarios/action-based-messaging-extension-fetch-task/example_data.py deleted file mode 100644 index 79dede038..000000000 --- a/scenarios/action-based-messaging-extension-fetch-task/example_data.py +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - - -class ExampleData(object): - def __init__( - self, - question: str = None, - is_multi_select: bool = False, - option1: str = None, - option2: str = None, - option3: str = None, - ): - self.question = question - self.is_multi_select = is_multi_select - self.option1 = option1 - self.option2 = option2 - self.option3 = option3 diff --git a/scenarios/action-based-messaging-extension-fetch-task/requirements.txt b/scenarios/action-based-messaging-extension-fetch-task/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/action-based-messaging-extension-fetch-task/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/action-based-messaging-extension-fetch-task/teams_app_manifest/icon-color.png b/scenarios/action-based-messaging-extension-fetch-task/teams_app_manifest/icon-color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/action-based-messaging-extension-fetch-task/teams_app_manifest/icon-color.png and /dev/null differ diff --git a/scenarios/action-based-messaging-extension-fetch-task/teams_app_manifest/icon-outline.png b/scenarios/action-based-messaging-extension-fetch-task/teams_app_manifest/icon-outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/action-based-messaging-extension-fetch-task/teams_app_manifest/icon-outline.png and /dev/null differ diff --git a/scenarios/action-based-messaging-extension-fetch-task/teams_app_manifest/manifest.json b/scenarios/action-based-messaging-extension-fetch-task/teams_app_manifest/manifest.json deleted file mode 100644 index 8c87f9f40..000000000 --- a/scenarios/action-based-messaging-extension-fetch-task/teams_app_manifest/manifest.json +++ /dev/null @@ -1,67 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0", - "id": "<>", - "packageName": "com.microsoft.teams.samples", - "developer": { - "name": "Microsoft", - "websiteUrl": "https://dev.botframework.com", - "privacyUrl": "https://privacy.microsoft.com", - "termsOfUseUrl": "https://www.microsoft.com/en-us/legal/intellectualproperty/copyright/default.aspx" - }, - "icons": { - "color": "icon-color.png", - "outline": "icon-outline.png" - }, - "name": { - "short": "Preview Messaging Extension", - "full": "Microsoft Teams Action Based Messaging Extension with Preview" - }, - "description": { - "short": "Sample demonstrating an Action Based Messaging Extension with Preview", - "full": "Sample Action Messaging Extension built with the Bot Builder SDK demonstrating Preview" - }, - "accentColor": "#FFFFFF", - "bots": [ - { - "botId": "<>", - "scopes": [ - "team" - ] - } - ], - "composeExtensions": [ - { - "botId": "<>", - "canUpdateConfiguration": false, - "commands": [ - { - "id": "createWithPreview", - "type": "action", - "title": "Create Card", - "description": "Example of creating a Card", - "initialRun": false, - "fetchTask": true, - "context": [ - "commandBox", - "compose", - "message" - ], - "parameters": [ - { - "name": "param", - "title": "param", - "description": "" - } - ] - } - ] - } - ], - "permissions": [ - "identity", - "messageTeamMembers" - ], - "validDomains": [] -} \ No newline at end of file diff --git a/scenarios/action-based-messaging-extension/bots/__init__.py b/scenarios/action-based-messaging-extension/bots/__init__.py deleted file mode 100644 index daea6bcda..000000000 --- a/scenarios/action-based-messaging-extension/bots/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .teams_messaging_extensions_action_bot import TeamsMessagingExtensionsActionBot - -__all__ = ["TeamsMessagingExtensionsActionBot"] diff --git a/scenarios/action-based-messaging-extension/bots/teams_messaging_extensions_action_bot.py b/scenarios/action-based-messaging-extension/bots/teams_messaging_extensions_action_bot.py deleted file mode 100644 index aea850e2a..000000000 --- a/scenarios/action-based-messaging-extension/bots/teams_messaging_extensions_action_bot.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright (c) Microsoft Corp. All rights reserved. -# Licensed under the MIT License. - -from typing import List -import random -from botbuilder.core import ( - CardFactory, - MessageFactory, - TurnContext, - UserState, - ConversationState, - PrivateConversationState, -) -from botbuilder.schema import ChannelAccount, HeroCard, CardAction, CardImage -from botbuilder.schema.teams import ( - MessagingExtensionAction, - MessagingExtensionActionResponse, - MessagingExtensionAttachment, - MessagingExtensionResult, -) -from botbuilder.core.teams import TeamsActivityHandler, TeamsInfo -from botbuilder.azure import CosmosDbPartitionedStorage - - -class TeamsMessagingExtensionsActionBot(TeamsActivityHandler): - async def on_teams_messaging_extension_submit_action_dispatch( - self, turn_context: TurnContext, action: MessagingExtensionAction - ) -> MessagingExtensionActionResponse: - if action.command_id == "createCard": - return await self.create_card_command(turn_context, action) - elif action.command_id == "shareMessage": - return await self.share_message_command(turn_context, action) - - async def create_card_command( - self, turn_context: TurnContext, action: MessagingExtensionAction - ) -> MessagingExtensionActionResponse: - title = action.data["title"] - subTitle = action.data["subTitle"] - text = action.data["text"] - - card = HeroCard(title=title, subtitle=subTitle, text=text) - cardAttachment = CardFactory.hero_card(card) - attachment = MessagingExtensionAttachment( - content=card, - content_type=CardFactory.content_types.hero_card, - preview=cardAttachment, - ) - attachments = [attachment] - - extension_result = MessagingExtensionResult( - attachment_layout="list", type="result", attachments=attachments - ) - return MessagingExtensionActionResponse(compose_extension=extension_result) - - async def share_message_command( - self, turn_context: TurnContext, action: MessagingExtensionAction - ) -> MessagingExtensionActionResponse: - # The user has chosen to share a message by choosing the 'Share Message' context menu command. - - # TODO: .user is None - title = "Shared Message" # f'{action.message_payload.from_property.user.display_name} orignally sent this message:' - text = action.message_payload.body.content - card = HeroCard(title=title, text=text) - - if not action.message_payload.attachments is None: - # This sample does not add the MessagePayload Attachments. This is left as an - # exercise for the user. - card.subtitle = ( - f"({len(action.message_payload.attachments)} Attachments not included)" - ) - - # This Messaging Extension example allows the user to check a box to include an image with the - # shared message. This demonstrates sending custom parameters along with the message payload. - include_image = action.data["includeImage"] - if include_image == "true": - image = CardImage( - url="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU" - ) - card.images = [image] - - cardAttachment = CardFactory.hero_card(card) - attachment = MessagingExtensionAttachment( - content=card, - content_type=CardFactory.content_types.hero_card, - preview=cardAttachment, - ) - attachments = [attachment] - - extension_result = MessagingExtensionResult( - attachment_layout="list", type="result", attachments=attachments - ) - return MessagingExtensionActionResponse(compose_extension=extension_result) diff --git a/scenarios/action-based-messaging-extension/requirements.txt b/scenarios/action-based-messaging-extension/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/action-based-messaging-extension/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/action-based-messaging-extension/teams_app_manifest/icon-color.png b/scenarios/action-based-messaging-extension/teams_app_manifest/icon-color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/action-based-messaging-extension/teams_app_manifest/icon-color.png and /dev/null differ diff --git a/scenarios/action-based-messaging-extension/teams_app_manifest/icon-outline.png b/scenarios/action-based-messaging-extension/teams_app_manifest/icon-outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/action-based-messaging-extension/teams_app_manifest/icon-outline.png and /dev/null differ diff --git a/scenarios/action-based-messaging-extension/teams_app_manifest/manifest.json b/scenarios/action-based-messaging-extension/teams_app_manifest/manifest.json deleted file mode 100644 index 282c8f99a..000000000 --- a/scenarios/action-based-messaging-extension/teams_app_manifest/manifest.json +++ /dev/null @@ -1,78 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0", - "id": "00000000-0000-0000-0000-000000000000", - "packageName": "com.microsoft.teams.samples", - "developer": { - "name": "Microsoft", - "websiteUrl": "https://dev.botframework.com", - "privacyUrl": "https://privacy.microsoft.com", - "termsOfUseUrl": "https://www.microsoft.com/en-us/legal/intellectualproperty/copyright/default.aspx" - }, - "name": { - "short": "Action Messaging Extension", - "full": "Microsoft Teams Action Based Messaging Extension" - }, - "description": { - "short": "Sample demonstrating an Action Based Messaging Extension", - "full": "Sample Action Messaging Extension built with the Bot Builder SDK" - }, - "icons": { - "outline": "icon-outline.png", - "color": "icon-color.png" - }, - "accentColor": "#FFFFFF", - "composeExtensions": [ - { - "botId": "00000000-0000-0000-0000-000000000000", - "commands": [ - { - "id": "createCard", - "type": "action", - "context": [ "compose" ], - "description": "Command to run action to create a Card from Compose Box", - "title": "Create Card", - "parameters": [ - { - "name": "title", - "title": "Card title", - "description": "Title for the card", - "inputType": "text" - }, - { - "name": "subTitle", - "title": "Subtitle", - "description": "Subtitle for the card", - "inputType": "text" - }, - { - "name": "text", - "title": "Text", - "description": "Text for the card", - "inputType": "textarea" - } - ] - }, - { - "id": "shareMessage", - "type": "action", - "context": [ "message" ], - "description": "Test command to run action on message context (message sharing)", - "title": "Share Message", - "parameters": [ - { - "name": "includeImage", - "title": "Include Image", - "description": "Include image in Hero Card", - "inputType": "toggle" - } - ] - } - ] - } - ], - "permissions": [ - "identity" - ] -} diff --git a/scenarios/activity-update-and-delete/README.md b/scenarios/activity-update-and-delete/README.md deleted file mode 100644 index 40e84f525..000000000 --- a/scenarios/activity-update-and-delete/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# EchoBot - -Bot Framework v4 echo bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/scenarios/activity-update-and-delete/app.py b/scenarios/activity-update-and-delete/app.py deleted file mode 100644 index 166cee39d..000000000 --- a/scenarios/activity-update-and-delete/app.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import asyncio -import sys -from datetime import datetime -from types import MethodType - -from flask import Flask, request, Response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes - -from bots import ActivitiyUpdateAndDeleteBot - -# Create the loop and Flask app -LOOP = asyncio.get_event_loop() -APP = Flask(__name__, instance_relative_config=True) -APP.config.from_object("config.DefaultConfig") - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error( # pylint: disable=unused-argument - self, context: TurnContext, error: Exception -): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = MethodType(on_error, ADAPTER) -ACTIVITY_IDS = [] -# Create the Bot -BOT = ActivitiyUpdateAndDeleteBot(ACTIVITY_IDS) - -# Listen for incoming requests on /api/messages.s -@APP.route("/api/messages", methods=["POST"]) -def messages(): - # Main bot message handler. - if "application/json" in request.headers["Content-Type"]: - body = request.json - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = ( - request.headers["Authorization"] if "Authorization" in request.headers else "" - ) - - try: - task = LOOP.create_task( - ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - ) - LOOP.run_until_complete(task) - return Response(status=201) - except Exception as exception: - raise exception - - -if __name__ == "__main__": - try: - APP.run(debug=False, port=APP.config["PORT"]) # nosec debug - except Exception as exception: - raise exception diff --git a/scenarios/activity-update-and-delete/bots/__init__.py b/scenarios/activity-update-and-delete/bots/__init__.py deleted file mode 100644 index e6c728a12..000000000 --- a/scenarios/activity-update-and-delete/bots/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .activity_update_and_delete_bot import ActivitiyUpdateAndDeleteBot - -__all__ = ["ActivitiyUpdateAndDeleteBot"] diff --git a/scenarios/activity-update-and-delete/bots/activity_update_and_delete_bot.py b/scenarios/activity-update-and-delete/bots/activity_update_and_delete_bot.py deleted file mode 100644 index 350cec8c2..000000000 --- a/scenarios/activity-update-and-delete/bots/activity_update_and_delete_bot.py +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import MessageFactory, TurnContext, ActivityHandler - - -class ActivitiyUpdateAndDeleteBot(ActivityHandler): - def __init__(self, activity_ids): - self.activity_ids = activity_ids - - async def on_message_activity(self, turn_context: TurnContext): - TurnContext.remove_recipient_mention(turn_context.activity) - if turn_context.activity.text == "delete": - for activity in self.activity_ids: - await turn_context.delete_activity(activity) - - self.activity_ids = [] - else: - await self._send_message_and_log_activity_id( - turn_context, turn_context.activity.text - ) - - for activity_id in self.activity_ids: - new_activity = MessageFactory.text(turn_context.activity.text) - new_activity.id = activity_id - await turn_context.update_activity(new_activity) - - async def _send_message_and_log_activity_id( - self, turn_context: TurnContext, text: str - ): - reply_activity = MessageFactory.text(text) - resource_response = await turn_context.send_activity(reply_activity) - self.activity_ids.append(resource_response.id) diff --git a/scenarios/activity-update-and-delete/requirements.txt b/scenarios/activity-update-and-delete/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/activity-update-and-delete/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/activity-update-and-delete/teams_app_manifest/color.png b/scenarios/activity-update-and-delete/teams_app_manifest/color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/activity-update-and-delete/teams_app_manifest/color.png and /dev/null differ diff --git a/scenarios/activity-update-and-delete/teams_app_manifest/manifest.json b/scenarios/activity-update-and-delete/teams_app_manifest/manifest.json deleted file mode 100644 index 844969c04..000000000 --- a/scenarios/activity-update-and-delete/teams_app_manifest/manifest.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0.0", - "id": "", - "packageName": "com.teams.sample.conversationUpdate", - "developer": { - "name": "ConversationUpdatesBot", - "websiteUrl": "https://www.microsoft.com", - "privacyUrl": "https://www.teams.com/privacy", - "termsOfUseUrl": "https://www.teams.com/termsofuser" - }, - "icons": { - "color": "color.png", - "outline": "outline.png" - }, - "name": { - "short": "ConversationUpdatesBot", - "full": "ConversationUpdatesBot" - }, - "description": { - "short": "ConversationUpdatesBot", - "full": "ConversationUpdatesBot" - }, - "accentColor": "#FFFFFF", - "bots": [ - { - "botId": "", - "scopes": [ - "groupchat", - "team", - "personal" - ], - "supportsFiles": false, - "isNotificationOnly": false - } - ], - "permissions": [ - "identity", - "messageTeamMembers" - ], - "validDomains": [] -} \ No newline at end of file diff --git a/scenarios/activity-update-and-delete/teams_app_manifest/outline.png b/scenarios/activity-update-and-delete/teams_app_manifest/outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/activity-update-and-delete/teams_app_manifest/outline.png and /dev/null differ diff --git a/scenarios/conversation-update/README.md b/scenarios/conversation-update/README.md deleted file mode 100644 index 40e84f525..000000000 --- a/scenarios/conversation-update/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# EchoBot - -Bot Framework v4 echo bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/scenarios/conversation-update/app.py b/scenarios/conversation-update/app.py deleted file mode 100644 index 8d1bc4ac0..000000000 --- a/scenarios/conversation-update/app.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import asyncio -import sys -from datetime import datetime -from types import MethodType - -from flask import Flask, request, Response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes - -from bots import ConversationUpdateBot - -# Create the loop and Flask app -LOOP = asyncio.get_event_loop() -APP = Flask(__name__, instance_relative_config=True) -APP.config.from_object("config.DefaultConfig") - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error( # pylint: disable=unused-argument - self, context: TurnContext, error: Exception -): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = MethodType(on_error, ADAPTER) - -# Create the Bot -BOT = ConversationUpdateBot() - -# Listen for incoming requests on /api/messages.s -@APP.route("/api/messages", methods=["POST"]) -def messages(): - # Main bot message handler. - if "application/json" in request.headers["Content-Type"]: - body = request.json - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = ( - request.headers["Authorization"] if "Authorization" in request.headers else "" - ) - - try: - task = LOOP.create_task( - ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - ) - LOOP.run_until_complete(task) - return Response(status=201) - except Exception as exception: - raise exception - - -if __name__ == "__main__": - try: - APP.run(debug=False, port=APP.config["PORT"]) # nosec debug - except Exception as exception: - raise exception diff --git a/scenarios/conversation-update/bots/__init__.py b/scenarios/conversation-update/bots/__init__.py deleted file mode 100644 index f9e91a398..000000000 --- a/scenarios/conversation-update/bots/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .conversation_update_bot import ConversationUpdateBot - -__all__ = ["ConversationUpdateBot"] diff --git a/scenarios/conversation-update/bots/conversation_update_bot.py b/scenarios/conversation-update/bots/conversation_update_bot.py deleted file mode 100644 index ec34da0f0..000000000 --- a/scenarios/conversation-update/bots/conversation_update_bot.py +++ /dev/null @@ -1,56 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import MessageFactory, TurnContext -from botbuilder.core.teams import TeamsActivityHandler -from botbuilder.schema.teams import ChannelInfo, TeamInfo, TeamsChannelAccount - - -class ConversationUpdateBot(TeamsActivityHandler): - async def on_teams_channel_created_activity( - self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext - ): - return await turn_context.send_activity( - MessageFactory.text( - f"The new channel is {channel_info.name}. The channel id is {channel_info.id}" - ) - ) - - async def on_teams_channel_deleted_activity( - self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext - ): - return await turn_context.send_activity( - MessageFactory.text(f"The deleted channel is {channel_info.name}") - ) - - async def on_teams_channel_renamed_activity( - self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext - ): - return await turn_context.send_activity( - MessageFactory.text(f"The new channel name is {channel_info.name}") - ) - - async def on_teams_team_renamed_activity( - self, team_info: TeamInfo, turn_context: TurnContext - ): - return await turn_context.send_activity( - MessageFactory.text(f"The new team name is {team_info.name}") - ) - - async def on_teams_members_added_activity( - self, teams_members_added: [TeamsChannelAccount], turn_context: TurnContext - ): - for member in teams_members_added: - await turn_context.send_activity( - MessageFactory.text(f"Welcome your new team member {member.id}") - ) - return - - async def on_teams_members_removed_activity( - self, teams_members_removed: [TeamsChannelAccount], turn_context: TurnContext - ): - for member in teams_members_removed: - await turn_context.send_activity( - MessageFactory.text(f"Say goodbye to your team member {member.id}") - ) - return diff --git a/scenarios/conversation-update/config.py b/scenarios/conversation-update/config.py deleted file mode 100644 index 6b5116fba..000000000 --- a/scenarios/conversation-update/config.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") diff --git a/scenarios/conversation-update/requirements.txt b/scenarios/conversation-update/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/conversation-update/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/conversation-update/teams_app_manifest/color.png b/scenarios/conversation-update/teams_app_manifest/color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/conversation-update/teams_app_manifest/color.png and /dev/null differ diff --git a/scenarios/conversation-update/teams_app_manifest/manifest.json b/scenarios/conversation-update/teams_app_manifest/manifest.json deleted file mode 100644 index 844969c04..000000000 --- a/scenarios/conversation-update/teams_app_manifest/manifest.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0.0", - "id": "", - "packageName": "com.teams.sample.conversationUpdate", - "developer": { - "name": "ConversationUpdatesBot", - "websiteUrl": "https://www.microsoft.com", - "privacyUrl": "https://www.teams.com/privacy", - "termsOfUseUrl": "https://www.teams.com/termsofuser" - }, - "icons": { - "color": "color.png", - "outline": "outline.png" - }, - "name": { - "short": "ConversationUpdatesBot", - "full": "ConversationUpdatesBot" - }, - "description": { - "short": "ConversationUpdatesBot", - "full": "ConversationUpdatesBot" - }, - "accentColor": "#FFFFFF", - "bots": [ - { - "botId": "", - "scopes": [ - "groupchat", - "team", - "personal" - ], - "supportsFiles": false, - "isNotificationOnly": false - } - ], - "permissions": [ - "identity", - "messageTeamMembers" - ], - "validDomains": [] -} \ No newline at end of file diff --git a/scenarios/conversation-update/teams_app_manifest/outline.png b/scenarios/conversation-update/teams_app_manifest/outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/conversation-update/teams_app_manifest/outline.png and /dev/null differ diff --git a/scenarios/create-thread-in-channel/README.md b/scenarios/create-thread-in-channel/README.md deleted file mode 100644 index 40e84f525..000000000 --- a/scenarios/create-thread-in-channel/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# EchoBot - -Bot Framework v4 echo bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/scenarios/create-thread-in-channel/app.py b/scenarios/create-thread-in-channel/app.py deleted file mode 100644 index 3c55decbe..000000000 --- a/scenarios/create-thread-in-channel/app.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import asyncio -import sys -from datetime import datetime -from types import MethodType - -from flask import Flask, request, Response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes - -from bots import CreateThreadInTeamsBot - -# Create the loop and Flask app -LOOP = asyncio.get_event_loop() -APP = Flask(__name__, instance_relative_config=True) -APP.config.from_object("config.DefaultConfig") - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error( # pylint: disable=unused-argument - self, context: TurnContext, error: Exception -): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = MethodType(on_error, ADAPTER) - -# Create the Bot -BOT = CreateThreadInTeamsBot(APP.config["APP_ID"]) - -# Listen for incoming requests on /api/messages.s -@APP.route("/api/messages", methods=["POST"]) -def messages(): - # Main bot message handler. - if "application/json" in request.headers["Content-Type"]: - body = request.json - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = ( - request.headers["Authorization"] if "Authorization" in request.headers else "" - ) - - try: - task = LOOP.create_task( - ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - ) - LOOP.run_until_complete(task) - return Response(status=201) - except Exception as exception: - raise exception - - -if __name__ == "__main__": - try: - APP.run(debug=False, port=APP.config["PORT"]) # nosec debug - except Exception as exception: - raise exception diff --git a/scenarios/create-thread-in-channel/bots/__init__.py b/scenarios/create-thread-in-channel/bots/__init__.py deleted file mode 100644 index f5e8a121c..000000000 --- a/scenarios/create-thread-in-channel/bots/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .create_thread_in_teams_bot import CreateThreadInTeamsBot - -__all__ = ["CreateThreadInTeamsBot"] diff --git a/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py b/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py deleted file mode 100644 index 6feca9af4..000000000 --- a/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import MessageFactory, TurnContext -from botbuilder.core.teams import ( - teams_get_channel_id, - TeamsActivityHandler, - TeamsInfo -) - - -class CreateThreadInTeamsBot(TeamsActivityHandler): - def __init__(self, id): - self.id = id - - async def on_message_activity(self, turn_context: TurnContext): - message = MessageFactory.text("first message") - channel_id = teams_get_channel_id(turn_context.activity) - result = await TeamsInfo.send_message_to_teams_channel(turn_context, message, channel_id) - - await turn_context.adapter.continue_conversation(result[0], self._continue_conversation_callback, self.id) - - async def _continue_conversation_callback(self, turn_context): - await turn_context.send_activity(MessageFactory.text("second message")) diff --git a/scenarios/create-thread-in-channel/config.py b/scenarios/create-thread-in-channel/config.py deleted file mode 100644 index 6b5116fba..000000000 --- a/scenarios/create-thread-in-channel/config.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") diff --git a/scenarios/create-thread-in-channel/requirements.txt b/scenarios/create-thread-in-channel/requirements.txt deleted file mode 100644 index 7e54b62ec..000000000 --- a/scenarios/create-thread-in-channel/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.4.0b1 -flask>=1.0.3 diff --git a/scenarios/create-thread-in-channel/teams_app_manifest/color.png b/scenarios/create-thread-in-channel/teams_app_manifest/color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/create-thread-in-channel/teams_app_manifest/color.png and /dev/null differ diff --git a/scenarios/create-thread-in-channel/teams_app_manifest/manifest.json b/scenarios/create-thread-in-channel/teams_app_manifest/manifest.json deleted file mode 100644 index b9d5b596f..000000000 --- a/scenarios/create-thread-in-channel/teams_app_manifest/manifest.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0.0", - "id": "<>", - "packageName": "com.teams.sample.conversationUpdate", - "developer": { - "name": "MentionBot", - "websiteUrl": "https://www.microsoft.com", - "privacyUrl": "https://www.teams.com/privacy", - "termsOfUseUrl": "https://www.teams.com/termsofuser" - }, - "icons": { - "color": "color.png", - "outline": "outline.png" - }, - "name": { - "short": "MentionBot", - "full": "MentionBot" - }, - "description": { - "short": "MentionBot", - "full": "MentionBot" - }, - "accentColor": "#FFFFFF", - "bots": [ - { - "botId": "<>", - "scopes": [ - "groupchat", - "team", - "personal" - ], - "supportsFiles": false, - "isNotificationOnly": false - } - ], - "permissions": [ - "identity", - "messageTeamMembers" - ], - "validDomains": [] -} \ No newline at end of file diff --git a/scenarios/create-thread-in-channel/teams_app_manifest/outline.png b/scenarios/create-thread-in-channel/teams_app_manifest/outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/create-thread-in-channel/teams_app_manifest/outline.png and /dev/null differ diff --git a/scenarios/file-upload/README.md b/scenarios/file-upload/README.md deleted file mode 100644 index dbbb975fb..000000000 --- a/scenarios/file-upload/README.md +++ /dev/null @@ -1,119 +0,0 @@ -# FileUpload - -Bot Framework v4 echo bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Prerequisites -- Open Notepad (or another text editor) to save some values as you complete the setup. - -- Ngrok setup -1. Download and install [Ngrok](https://ngrok.com/download) -2. In terminal navigate to the directory where Ngrok is installed -3. Run this command: ```ngrok http -host-header=rewrite 3978 ``` -4. Copy the https://xxxxxxxx.ngrok.io address and put it into notepad. **NOTE** You want the https address. - -- Azure setup -1. Login to the [Azure Portal]((https://portal.azure.com) -2. (optional) create a new resource group if you don't currently have one -3. Go to your resource group -4. Click "Create a new resource" -5. Search for "Bot Channel Registration" -6. Click Create -7. Enter bot name, subscription -8. In the "Messaging endpoint url" enter the ngrok address from earlier. -8a. Finish the url with "/api/messages. It should look like ```https://xxxxxxxxx.ngrok.io/api/messages``` -9. Click the "Microsoft App Id and password" box -10. Click on "Create New" -11. Click on "Create App ID in the App Registration Portal" -12. Click "New registration" -13. Enter a name -14. Under "Supported account types" select "Accounts in any organizational directory and personal Microsoft accounts" -15. Click register -16. Copy the application (client) ID and put it in Notepad. Label it "Microsoft App ID" -17. Go to "Certificates & Secrets" -18. Click "+ New client secret" -19. Enter a description -20. Click "Add" -21. Copy the value and put it into Notepad. Label it "Password" -22. (back in the channel registration view) Copy/Paste the Microsoft App ID and Password into their respective fields -23. Click Create -24. Go to "Resource groups" on the left -25. Select the resource group that the bot channel reg was created in -26. Select the bot channel registration -27. Go to Channels -28. Select the "Teams" icon under "Add a featured channel -29. Click Save - -- Updating Sample Project Settings -1. Open the project -2. Open config.py -3. Enter the app id under the ```MicrosoftAppId``` and the password under the ```MicrosoftAppPassword``` -4. Save the close the file -5. Under the teams_app_manifest folder open the manifest.json file -6. Update the ```botId``` with the Microsoft App ID from before -7. Update the ```id``` with the Microsoft App ID from before -8. Save the close the file - -- Uploading the bot to Teams -1. In file explorer navigate to the TeamsAppManifest folder in the project -2. Select the 3 files and zip them -3. Open Teams -4. Click on "Apps" -5. Select "Upload a custom app" on the left at the bottom -6. Select the zip -7. Select for you -8. (optionally) click install if prompted -9. Click open - -## To try this sample - -- Clone the repository - - ```bash - git clone https://github.com/Microsoft/botbuilder-python.git - ``` - -- In a terminal, navigate to `samples/python/scenarios/file-upload` - - - From a terminal - - ```bash - pip install -r requirements.txt - python app.py - ``` - -- Interacting with the bot -1. Send a message to your bot in Teams -2. Confirm you are getting a 200 back in Ngrok -3. Click Accept on the card that is shown -4. Confirm you see a 2nd 200 in Ngrok -5. In Teams go to Files -> OneDrive -> Applications - -## Testing the bot using Bot Framework Emulator - -[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to the bot using Bot Framework Emulator - -- Launch Bot Framework Emulator -- File -> Open Bot -- Enter a Bot URL of `http://localhost:3978/api/messages` - -## Deploy the bot to Azure - -To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete list of deployment instructions. - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) -- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) -- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) -- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) -- [Azure Portal](https://portal.azure.com) -- [Language Understanding using LUIS](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/) -- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) diff --git a/scenarios/file-upload/app.py b/scenarios/file-upload/app.py deleted file mode 100644 index 048afd4c2..000000000 --- a/scenarios/file-upload/app.py +++ /dev/null @@ -1,91 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import asyncio -import sys -import traceback -from datetime import datetime - -from flask import Flask, request, Response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes - -from bots import TeamsFileBot - -# Create the loop and Flask app -LOOP = asyncio.get_event_loop() -APP = Flask(__name__, instance_relative_config=True) -APP.config.from_object("config.DefaultConfig") - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error(context: TurnContext, error: Exception): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - print(traceback.format_exc()) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = on_error - -# Create the Bot -BOT = TeamsFileBot() - -# Listen for incoming requests on /api/messages.s -@APP.route("/api/messages", methods=["POST"]) -def messages(): - # Main bot message handler. - if "application/json" in request.headers["Content-Type"]: - body = request.json - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = ( - request.headers["Authorization"] if "Authorization" in request.headers else "" - ) - - try: - task = LOOP.create_task( - ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - ) - LOOP.run_until_complete(task) - return Response(status=201) - except Exception as exception: - raise exception - - -if __name__ == "__main__": - try: - APP.run(debug=False, port=APP.config["PORT"]) # nosec debug - except Exception as exception: - raise exception diff --git a/scenarios/file-upload/bots/teams_file_bot.py b/scenarios/file-upload/bots/teams_file_bot.py deleted file mode 100644 index 93401d5df..000000000 --- a/scenarios/file-upload/bots/teams_file_bot.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from datetime import datetime -import os - -import requests -from botbuilder.core import TurnContext -from botbuilder.core.teams import TeamsActivityHandler -from botbuilder.schema import ( - Activity, - ChannelAccount, - ActivityTypes, - ConversationAccount, - Attachment, -) -from botbuilder.schema.teams import ( - FileDownloadInfo, - FileConsentCard, - FileConsentCardResponse, - FileInfoCard, -) -from botbuilder.schema.teams.additional_properties import ContentType - - -class TeamsFileBot(TeamsActivityHandler): - async def on_message_activity(self, turn_context: TurnContext): - message_with_file_download = ( - False - if not turn_context.activity.attachments - else turn_context.activity.attachments[0].content_type == ContentType.FILE_DOWNLOAD_INFO - ) - - if message_with_file_download: - # Save an uploaded file locally - file = turn_context.activity.attachments[0] - file_download = FileDownloadInfo.deserialize(file.content) - file_path = "files/" + file.name - - response = requests.get(file_download.download_url, allow_redirects=True) - open(file_path, "wb").write(response.content) - - reply = self._create_reply( - turn_context.activity, f"Complete downloading {file.name}", "xml" - ) - await turn_context.send_activity(reply) - else: - # Attempt to upload a file to Teams. This will display a confirmation to - # the user (Accept/Decline card). If they accept, on_teams_file_consent_accept - # will be called, otherwise on_teams_file_consent_decline. - filename = "teams-logo.png" - file_path = "files/" + filename - file_size = os.path.getsize(file_path) - await self._send_file_card(turn_context, filename, file_size) - - async def _send_file_card( - self, turn_context: TurnContext, filename: str, file_size: int - ): - """ - Send a FileConsentCard to get permission from the user to upload a file. - """ - - consent_context = {"filename": filename} - - file_card = FileConsentCard( - description="This is the file I want to send you", - size_in_bytes=file_size, - accept_context=consent_context, - decline_context=consent_context - ) - - as_attachment = Attachment( - content=file_card.serialize(), content_type=ContentType.FILE_CONSENT_CARD, name=filename - ) - - reply_activity = self._create_reply(turn_context.activity) - reply_activity.attachments = [as_attachment] - await turn_context.send_activity(reply_activity) - - async def on_teams_file_consent_accept( - self, - turn_context: TurnContext, - file_consent_card_response: FileConsentCardResponse - ): - """ - The user accepted the file upload request. Do the actual upload now. - """ - - file_path = "files/" + file_consent_card_response.context["filename"] - file_size = os.path.getsize(file_path) - - headers = { - "Content-Length": f"\"{file_size}\"", - "Content-Range": f"bytes 0-{file_size-1}/{file_size}" - } - response = requests.put( - file_consent_card_response.upload_info.upload_url, open(file_path, "rb"), headers=headers - ) - - if response.status_code != 200: - await self._file_upload_failed(turn_context, "Unable to upload file.") - else: - await self._file_upload_complete(turn_context, file_consent_card_response) - - async def on_teams_file_consent_decline( - self, - turn_context: TurnContext, - file_consent_card_response: FileConsentCardResponse - ): - """ - The user declined the file upload. - """ - - context = file_consent_card_response.context - - reply = self._create_reply( - turn_context.activity, - f"Declined. We won't upload file {context['filename']}.", - "xml" - ) - await turn_context.send_activity(reply) - - async def _file_upload_complete( - self, - turn_context: TurnContext, - file_consent_card_response: FileConsentCardResponse - ): - """ - The file was uploaded, so display a FileInfoCard so the user can view the - file in Teams. - """ - - name = file_consent_card_response.upload_info.name - - download_card = FileInfoCard( - unique_id=file_consent_card_response.upload_info.unique_id, - file_type=file_consent_card_response.upload_info.file_type - ) - - as_attachment = Attachment( - content=download_card.serialize(), - content_type=ContentType.FILE_INFO_CARD, - name=name, - content_url=file_consent_card_response.upload_info.content_url - ) - - reply = self._create_reply( - turn_context.activity, - f"File uploaded. Your file {name} is ready to download", - "xml" - ) - reply.attachments = [as_attachment] - - await turn_context.send_activity(reply) - - async def _file_upload_failed(self, turn_context: TurnContext, error: str): - reply = self._create_reply( - turn_context.activity, - f"File upload failed. Error:
{error}
", - "xml" - ) - await turn_context.send_activity(reply) - - def _create_reply(self, activity, text=None, text_format=None): - return Activity( - type=ActivityTypes.message, - timestamp=datetime.utcnow(), - from_property=ChannelAccount( - id=activity.recipient.id, name=activity.recipient.name - ), - recipient=ChannelAccount( - id=activity.from_property.id, name=activity.from_property.name - ), - reply_to_id=activity.id, - service_url=activity.service_url, - channel_id=activity.channel_id, - conversation=ConversationAccount( - is_group=activity.conversation.is_group, - id=activity.conversation.id, - name=activity.conversation.name, - ), - text=text or "", - text_format=text_format or None, - locale=activity.locale, - ) diff --git a/scenarios/file-upload/config.py b/scenarios/file-upload/config.py deleted file mode 100644 index 6b5116fba..000000000 --- a/scenarios/file-upload/config.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") diff --git a/scenarios/file-upload/files/teams-logo.png b/scenarios/file-upload/files/teams-logo.png deleted file mode 100644 index 78b0a0c30..000000000 Binary files a/scenarios/file-upload/files/teams-logo.png and /dev/null differ diff --git a/scenarios/file-upload/requirements.txt b/scenarios/file-upload/requirements.txt deleted file mode 100644 index 8ee86105f..000000000 --- a/scenarios/file-upload/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -requests -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/file-upload/teams_app_manifest/color.png b/scenarios/file-upload/teams_app_manifest/color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/file-upload/teams_app_manifest/color.png and /dev/null differ diff --git a/scenarios/file-upload/teams_app_manifest/manifest.json b/scenarios/file-upload/teams_app_manifest/manifest.json deleted file mode 100644 index 8a1f2365a..000000000 --- a/scenarios/file-upload/teams_app_manifest/manifest.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0", - "id": "<>", - "packageName": "com.microsoft.teams.samples.fileUpload", - "developer": { - "name": "Microsoft Corp", - "websiteUrl": "https://example.azurewebsites.net", - "privacyUrl": "https://example.azurewebsites.net/privacy", - "termsOfUseUrl": "https://example.azurewebsites.net/termsofuse" - }, - "name": { - "short": "V4 File Sample", - "full": "Microsoft Teams V4 File Sample Bot" - }, - "description": { - "short": "Sample bot using V4 SDK to demo bot file features", - "full": "Sample bot using V4 Bot Builder SDK and V4 Microsoft Teams Extension SDK to demo bot file features" - }, - "icons": { - "outline": "outline.png", - "color": "color.png" - }, - "accentColor": "#abcdef", - "bots": [ - { - "botId": "<>", - "scopes": [ - "personal" - ], - "supportsFiles": true - } - ], - "validDomains": [ - "*.azurewebsites.net" - ] -} \ No newline at end of file diff --git a/scenarios/file-upload/teams_app_manifest/outline.png b/scenarios/file-upload/teams_app_manifest/outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/file-upload/teams_app_manifest/outline.png and /dev/null differ diff --git a/scenarios/link-unfurling/README.md b/scenarios/link-unfurling/README.md deleted file mode 100644 index 39f77916c..000000000 --- a/scenarios/link-unfurling/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# RosterBot - -Bot Framework v4 teams roster bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\roster` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/scenarios/link-unfurling/bots/__init__.py b/scenarios/link-unfurling/bots/__init__.py deleted file mode 100644 index 7dc2c44a9..000000000 --- a/scenarios/link-unfurling/bots/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .link_unfurling_bot import LinkUnfurlingBot - -__all__ = ["LinkUnfurlingBot"] diff --git a/scenarios/link-unfurling/bots/link_unfurling_bot.py b/scenarios/link-unfurling/bots/link_unfurling_bot.py deleted file mode 100644 index 1c1888375..000000000 --- a/scenarios/link-unfurling/bots/link_unfurling_bot.py +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import CardFactory, MessageFactory, TurnContext -from botbuilder.schema import ChannelAccount, ThumbnailCard, CardImage, HeroCard, Attachment -from botbuilder.schema.teams import AppBasedLinkQuery, MessagingExtensionAttachment, MessagingExtensionQuery, MessagingExtensionResult, MessagingExtensionResponse -from botbuilder.core.teams import TeamsActivityHandler, TeamsInfo - -class LinkUnfurlingBot(TeamsActivityHandler): - async def on_teams_app_based_link_query(self, turn_context: TurnContext, query: AppBasedLinkQuery): - hero_card = ThumbnailCard( - title="Thumnnail card", - text=query.url, - images=[ - CardImage( - url="https://raw.githubusercontent.com/microsoft/botframework-sdk/master/icon.png" - ) - ] - ) - attachments = MessagingExtensionAttachment( - content_type=CardFactory.content_types.hero_card, - content=hero_card) - result = MessagingExtensionResult( - attachment_layout="list", - type="result", - attachments=[attachments] - ) - return MessagingExtensionResponse(compose_extension=result) - - async def on_teams_messaging_extension_query(self, turn_context: TurnContext, query: MessagingExtensionQuery): - if query.command_id == "searchQuery": - card = HeroCard( - title="This is a Link Unfurling Sample", - subtitle="It will unfurl links from *.botframework.com", - text="This sample demonstrates how to handle link unfurling in Teams. Please review the readme for more information." - ) - attachment = Attachment( - content_type=CardFactory.content_types.hero_card, - content=card - ) - msg_ext_atc = MessagingExtensionAttachment( - content=card, - content_type=CardFactory.content_types.hero_card, - preview=attachment - ) - msg_ext_res = MessagingExtensionResult( - attachment_layout="list", - type="result", - attachments=[msg_ext_atc] - ) - response = MessagingExtensionResponse( - compose_extension=msg_ext_res - ) - - return response - - raise NotImplementedError(f"Invalid command: {query.command_id}") \ No newline at end of file diff --git a/scenarios/link-unfurling/config.py b/scenarios/link-unfurling/config.py deleted file mode 100644 index 6b5116fba..000000000 --- a/scenarios/link-unfurling/config.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") diff --git a/scenarios/link-unfurling/requirements.txt b/scenarios/link-unfurling/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/link-unfurling/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/link-unfurling/teams_app_manifest/color.png b/scenarios/link-unfurling/teams_app_manifest/color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/link-unfurling/teams_app_manifest/color.png and /dev/null differ diff --git a/scenarios/link-unfurling/teams_app_manifest/manifest.json b/scenarios/link-unfurling/teams_app_manifest/manifest.json deleted file mode 100644 index ad1d3c3a6..000000000 --- a/scenarios/link-unfurling/teams_app_manifest/manifest.json +++ /dev/null @@ -1,67 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0", - "id": "<>", - "packageName": "com.teams.sample.linkunfurling", - "developer": { - "name": "Link Unfurling", - "websiteUrl": "https://www.microsoft.com", - "privacyUrl": "https://www.teams.com/privacy", - "termsOfUseUrl": "https://www.teams.com/termsofuser" - }, - "icons": { - "color": "color.png", - "outline": "outline.png" - }, - "name": { - "short": "Link Unfurling", - "full": "Link Unfurling" - }, - "description": { - "short": "Link Unfurling", - "full": "Link Unfurling" - }, - "accentColor": "#FFFFFF", - "bots": [ - { - "botId": "<>", - "scopes": [ "personal", "team" ] - } - ], - "composeExtensions": [ - { - "botId": "<>", - "commands": [ - { - "id": "searchQuery", - "context": [ "compose", "commandBox" ], - "description": "Test command to run query", - "title": "Search", - "type": "query", - "parameters": [ - { - "name": "searchQuery", - "title": "Search Query", - "description": "Your search query", - "inputType": "text" - } - ] - } - ], - "messageHandlers": [ - { - "type": "link", - "value": { - "domains": [ - "microsoft.com", - "github.com", - "linkedin.com", - "bing.com" - ] - } - } - ] - } - ] -} diff --git a/scenarios/link-unfurling/teams_app_manifest/manifest.zip b/scenarios/link-unfurling/teams_app_manifest/manifest.zip deleted file mode 100644 index aaedf42c4..000000000 Binary files a/scenarios/link-unfurling/teams_app_manifest/manifest.zip and /dev/null differ diff --git a/scenarios/link-unfurling/teams_app_manifest/outline.png b/scenarios/link-unfurling/teams_app_manifest/outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/link-unfurling/teams_app_manifest/outline.png and /dev/null differ diff --git a/scenarios/mentions/README.md b/scenarios/mentions/README.md deleted file mode 100644 index 40e84f525..000000000 --- a/scenarios/mentions/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# EchoBot - -Bot Framework v4 echo bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/scenarios/mentions/app.py b/scenarios/mentions/app.py deleted file mode 100644 index 1db89f6ec..000000000 --- a/scenarios/mentions/app.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import asyncio -import sys -from datetime import datetime -from types import MethodType - -from flask import Flask, request, Response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes - -from bots import MentionBot - -# Create the loop and Flask app -LOOP = asyncio.get_event_loop() -APP = Flask(__name__, instance_relative_config=True) -APP.config.from_object("config.DefaultConfig") - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error( # pylint: disable=unused-argument - self, context: TurnContext, error: Exception -): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = MethodType(on_error, ADAPTER) - -# Create the Bot -BOT = MentionBot() - -# Listen for incoming requests on /api/messages.s -@APP.route("/api/messages", methods=["POST"]) -def messages(): - # Main bot message handler. - if "application/json" in request.headers["Content-Type"]: - body = request.json - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = ( - request.headers["Authorization"] if "Authorization" in request.headers else "" - ) - - try: - task = LOOP.create_task( - ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - ) - LOOP.run_until_complete(task) - return Response(status=201) - except Exception as exception: - raise exception - - -if __name__ == "__main__": - try: - APP.run(debug=False, port=APP.config["PORT"]) # nosec debug - except Exception as exception: - raise exception diff --git a/scenarios/mentions/bots/__init__.py b/scenarios/mentions/bots/__init__.py deleted file mode 100644 index 82e97adab..000000000 --- a/scenarios/mentions/bots/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .mention_bot import MentionBot - -__all__ = ["MentionBot"] diff --git a/scenarios/mentions/bots/mention_bot.py b/scenarios/mentions/bots/mention_bot.py deleted file mode 100644 index f343c4584..000000000 --- a/scenarios/mentions/bots/mention_bot.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import MessageFactory, TurnContext -from botbuilder.core.teams import TeamsActivityHandler -from botbuilder.schema import Mention - - -class MentionBot(TeamsActivityHandler): - async def on_message_activity(self, turn_context: TurnContext): - mention_data = { - "mentioned": turn_context.activity.from_property, - "text": f"{turn_context.activity.from_property.name}", - "type": "mention", - } - - mention_object = Mention(**mention_data) - - reply_activity = MessageFactory.text(f"Hello {mention_object.text}") - reply_activity.entities = [mention_object] - await turn_context.send_activity(reply_activity) diff --git a/scenarios/mentions/config.py b/scenarios/mentions/config.py deleted file mode 100644 index 6b5116fba..000000000 --- a/scenarios/mentions/config.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") diff --git a/scenarios/mentions/requirements.txt b/scenarios/mentions/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/mentions/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/mentions/teams_app_manifest/color.png b/scenarios/mentions/teams_app_manifest/color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/mentions/teams_app_manifest/color.png and /dev/null differ diff --git a/scenarios/mentions/teams_app_manifest/manifest.json b/scenarios/mentions/teams_app_manifest/manifest.json deleted file mode 100644 index b9d5b596f..000000000 --- a/scenarios/mentions/teams_app_manifest/manifest.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0.0", - "id": "<>", - "packageName": "com.teams.sample.conversationUpdate", - "developer": { - "name": "MentionBot", - "websiteUrl": "https://www.microsoft.com", - "privacyUrl": "https://www.teams.com/privacy", - "termsOfUseUrl": "https://www.teams.com/termsofuser" - }, - "icons": { - "color": "color.png", - "outline": "outline.png" - }, - "name": { - "short": "MentionBot", - "full": "MentionBot" - }, - "description": { - "short": "MentionBot", - "full": "MentionBot" - }, - "accentColor": "#FFFFFF", - "bots": [ - { - "botId": "<>", - "scopes": [ - "groupchat", - "team", - "personal" - ], - "supportsFiles": false, - "isNotificationOnly": false - } - ], - "permissions": [ - "identity", - "messageTeamMembers" - ], - "validDomains": [] -} \ No newline at end of file diff --git a/scenarios/mentions/teams_app_manifest/outline.png b/scenarios/mentions/teams_app_manifest/outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/mentions/teams_app_manifest/outline.png and /dev/null differ diff --git a/scenarios/message-reactions/README.md b/scenarios/message-reactions/README.md deleted file mode 100644 index 40e84f525..000000000 --- a/scenarios/message-reactions/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# EchoBot - -Bot Framework v4 echo bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/scenarios/message-reactions/activity_log.py b/scenarios/message-reactions/activity_log.py deleted file mode 100644 index 0ef5a837d..000000000 --- a/scenarios/message-reactions/activity_log.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import MemoryStorage -from botbuilder.schema import Activity - - -class ActivityLog: - def __init__(self, storage: MemoryStorage): - self._storage = storage - - async def append(self, activity_id: str, activity: Activity): - if not activity_id: - raise TypeError("activity_id is required for ActivityLog.append") - - if not activity: - raise TypeError("activity is required for ActivityLog.append") - - obj = {} - obj[activity_id] = activity - - await self._storage.write(obj) - return - - async def find(self, activity_id: str) -> Activity: - if not activity_id: - raise TypeError("activity_id is required for ActivityLog.find") - - items = await self._storage.read([activity_id]) - return items[activity_id] if len(items) >= 1 else None diff --git a/scenarios/message-reactions/app.py b/scenarios/message-reactions/app.py deleted file mode 100644 index f92c64c3c..000000000 --- a/scenarios/message-reactions/app.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import sys -from datetime import datetime -from types import MethodType - -from flask import Flask, request, Response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, - MemoryStorage, -) -from botbuilder.schema import Activity, ActivityTypes -from activity_log import ActivityLog -from bots import MessageReactionBot -from threading_helper import run_coroutine - -# Create the Flask app -APP = Flask(__name__, instance_relative_config=True) -APP.config.from_object("config.DefaultConfig") - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error( # pylint: disable=unused-argument - self, context: TurnContext, error: Exception -): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = MethodType(on_error, ADAPTER) - -MEMORY = MemoryStorage() -ACTIVITY_LOG = ActivityLog(MEMORY) -# Create the Bot -BOT = MessageReactionBot(ACTIVITY_LOG) - -# Listen for incoming requests on /api/messages.s -@APP.route("/api/messages", methods=["POST"]) -def messages(): - # Main bot message handler. - if "application/json" in request.headers["Content-Type"]: - body = request.json - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = ( - request.headers["Authorization"] if "Authorization" in request.headers else "" - ) - - try: - print("about to create task") - print("about to run until complete") - run_coroutine(ADAPTER.process_activity(activity, auth_header, BOT.on_turn)) - print("is now complete") - return Response(status=201) - except Exception as exception: - raise exception - - -if __name__ == "__main__": - try: - APP.run(debug=False, port=APP.config["PORT"]) # nosec debug - except Exception as exception: - raise exception diff --git a/scenarios/message-reactions/bots/__init__.py b/scenarios/message-reactions/bots/__init__.py deleted file mode 100644 index 4c417f70c..000000000 --- a/scenarios/message-reactions/bots/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .message_reaction_bot import MessageReactionBot - -__all__ = ["MessageReactionBot"] diff --git a/scenarios/message-reactions/bots/message_reaction_bot.py b/scenarios/message-reactions/bots/message_reaction_bot.py deleted file mode 100644 index ce8c34cea..000000000 --- a/scenarios/message-reactions/bots/message_reaction_bot.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from typing import List -from botbuilder.core import MessageFactory, TurnContext, ActivityHandler -from botbuilder.schema import MessageReaction -from activity_log import ActivityLog - - -class MessageReactionBot(ActivityHandler): - def __init__(self, activity_log: ActivityLog): - self._log = activity_log - - async def on_reactions_added( - self, message_reactions: List[MessageReaction], turn_context: TurnContext - ): - for reaction in message_reactions: - activity = await self._log.find(turn_context.activity.reply_to_id) - if not activity: - await self._send_message_and_log_activity_id( - turn_context, - f"Activity {turn_context.activity.reply_to_id} not found in log", - ) - else: - await self._send_message_and_log_activity_id( - turn_context, - f"You added '{reaction.type}' regarding '{activity.text}'", - ) - return - - async def on_reactions_removed( - self, message_reactions: List[MessageReaction], turn_context: TurnContext - ): - for reaction in message_reactions: - activity = await self._log.find(turn_context.activity.reply_to_id) - if not activity: - await self._send_message_and_log_activity_id( - turn_context, - f"Activity {turn_context.activity.reply_to_id} not found in log", - ) - else: - await self._send_message_and_log_activity_id( - turn_context, - f"You removed '{reaction.type}' regarding '{activity.text}'", - ) - return - - async def on_message_activity(self, turn_context: TurnContext): - await self._send_message_and_log_activity_id( - turn_context, f"echo: {turn_context.activity.text}" - ) - - async def _send_message_and_log_activity_id( - self, turn_context: TurnContext, text: str - ): - reply_activity = MessageFactory.text(text) - resource_response = await turn_context.send_activity(reply_activity) - - await self._log.append(resource_response.id, reply_activity) - return diff --git a/scenarios/message-reactions/config.py b/scenarios/message-reactions/config.py deleted file mode 100644 index 480b0647b..000000000 --- a/scenarios/message-reactions/config.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "e4c570ca-189d-4fee-a81b-5466be24a557") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "bghqYKJV3709;creKFP8$@@") diff --git a/scenarios/message-reactions/requirements.txt b/scenarios/message-reactions/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/message-reactions/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/message-reactions/teams_app_manifest/color.png b/scenarios/message-reactions/teams_app_manifest/color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/message-reactions/teams_app_manifest/color.png and /dev/null differ diff --git a/scenarios/message-reactions/teams_app_manifest/manifest.json b/scenarios/message-reactions/teams_app_manifest/manifest.json deleted file mode 100644 index a3ec0ae45..000000000 --- a/scenarios/message-reactions/teams_app_manifest/manifest.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0.0", - "id": "<>", - "packageName": "com.teams.sample.conversationUpdate", - "developer": { - "name": "MessageReactions", - "websiteUrl": "https://www.microsoft.com", - "privacyUrl": "https://www.teams.com/privacy", - "termsOfUseUrl": "https://www.teams.com/termsofuser" - }, - "icons": { - "color": "color.png", - "outline": "outline.png" - }, - "name": { - "short": "MessageReactions", - "full": "MessageReactions" - }, - "description": { - "short": "MessageReactions", - "full": "MessageReactions" - }, - "accentColor": "#FFFFFF", - "bots": [ - { - "botId": "<>", - "scopes": [ - "groupchat", - "team", - "personal" - ], - "supportsFiles": false, - "isNotificationOnly": false - } - ], - "permissions": [ - "identity", - "messageTeamMembers" - ], - "validDomains": [] -} \ No newline at end of file diff --git a/scenarios/message-reactions/teams_app_manifest/outline.png b/scenarios/message-reactions/teams_app_manifest/outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/message-reactions/teams_app_manifest/outline.png and /dev/null differ diff --git a/scenarios/message-reactions/threading_helper.py b/scenarios/message-reactions/threading_helper.py deleted file mode 100644 index ab3316e1f..000000000 --- a/scenarios/message-reactions/threading_helper.py +++ /dev/null @@ -1,169 +0,0 @@ -import asyncio -import itertools -import logging -import threading - -# pylint: disable=invalid-name -# pylint: disable=global-statement -try: - # Python 3.8 or newer has a suitable process watcher - asyncio.ThreadedChildWatcher -except AttributeError: - # backport the Python 3.8 threaded child watcher - import os - import warnings - - # Python 3.7 preferred API - _get_running_loop = getattr(asyncio, "get_running_loop", asyncio.get_event_loop) - - class _Py38ThreadedChildWatcher(asyncio.AbstractChildWatcher): - def __init__(self): - self._pid_counter = itertools.count(0) - self._threads = {} - - def is_active(self): - return True - - def close(self): - pass - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_val, exc_tb): - pass - - def __del__(self, _warn=warnings.warn): - threads = [t for t in list(self._threads.values()) if t.is_alive()] - if threads: - _warn( - f"{self.__class__} has registered but not finished child processes", - ResourceWarning, - source=self, - ) - - def add_child_handler(self, pid, callback, *args): - loop = _get_running_loop() - thread = threading.Thread( - target=self._do_waitpid, - name=f"waitpid-{next(self._pid_counter)}", - args=(loop, pid, callback, args), - daemon=True, - ) - self._threads[pid] = thread - thread.start() - - def remove_child_handler(self, pid): - # asyncio never calls remove_child_handler() !!! - # The method is no-op but is implemented because - # abstract base class requires it - return True - - def attach_loop(self, loop): - pass - - def _do_waitpid(self, loop, expected_pid, callback, args): - assert expected_pid > 0 - - try: - pid, status = os.waitpid(expected_pid, 0) - except ChildProcessError: - # The child process is already reaped - # (may happen if waitpid() is called elsewhere). - pid = expected_pid - returncode = 255 - logger.warning( - "Unknown child process pid %d, will report returncode 255", pid - ) - else: - if os.WIFSIGNALED(status): - returncode = -os.WTERMSIG(status) - elif os.WIFEXITED(status): - returncode = os.WEXITSTATUS(status) - else: - returncode = status - - if loop.get_debug(): - logger.debug( - "process %s exited with returncode %s", expected_pid, returncode - ) - - if loop.is_closed(): - logger.warning("Loop %r that handles pid %r is closed", loop, pid) - else: - loop.call_soon_threadsafe(callback, pid, returncode, *args) - - self._threads.pop(expected_pid) - - # add the watcher to the loop policy - asyncio.get_event_loop_policy().set_child_watcher(_Py38ThreadedChildWatcher()) - -__all__ = ["EventLoopThread", "get_event_loop", "stop_event_loop", "run_coroutine"] - -logger = logging.getLogger(__name__) - - -class EventLoopThread(threading.Thread): - loop = None - _count = itertools.count(0) - - def __init__(self): - name = f"{type(self).__name__}-{next(self._count)}" - super().__init__(name=name, daemon=True) - - def __repr__(self): - loop, r, c, d = self.loop, False, True, False - if loop is not None: - r, c, d = loop.is_running(), loop.is_closed(), loop.get_debug() - return ( - f"<{type(self).__name__} {self.name} id={self.ident} " - f"running={r} closed={c} debug={d}>" - ) - - def run(self): - self.loop = loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - - try: - loop.run_forever() - finally: - try: - shutdown_asyncgens = loop.shutdown_asyncgens() - except AttributeError: - pass - else: - loop.run_until_complete(shutdown_asyncgens) - loop.close() - asyncio.set_event_loop(None) - - def stop(self): - loop, self.loop = self.loop, None - if loop is None: - return - loop.call_soon_threadsafe(loop.stop) - self.join() - - -_lock = threading.Lock() -_loop_thread = None - - -def get_event_loop(): - global _loop_thread - with _lock: - if _loop_thread is None: - _loop_thread = EventLoopThread() - _loop_thread.start() - return _loop_thread.loop - - -def stop_event_loop(): - global _loop_thread - with _lock: - if _loop_thread is not None: - _loop_thread.stop() - _loop_thread = None - - -def run_coroutine(coro): - return asyncio.run_coroutine_threadsafe(coro, get_event_loop()) diff --git a/scenarios/roster/README.md b/scenarios/roster/README.md deleted file mode 100644 index 39f77916c..000000000 --- a/scenarios/roster/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# RosterBot - -Bot Framework v4 teams roster bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\roster` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/scenarios/roster/app.py b/scenarios/roster/app.py deleted file mode 100644 index f491845be..000000000 --- a/scenarios/roster/app.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import asyncio -import sys -from datetime import datetime -from types import MethodType - -from flask import Flask, request, Response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes - -from bots import RosterBot - -# Create the loop and Flask app -LOOP = asyncio.get_event_loop() -APP = Flask(__name__, instance_relative_config=True) -APP.config.from_object("config.DefaultConfig") - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error( # pylint: disable=unused-argument - self, context: TurnContext, error: Exception -): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = MethodType(on_error, ADAPTER) - -# Create the Bot -BOT = RosterBot() - -# Listen for incoming requests on /api/messages.s -@APP.route("/api/messages", methods=["POST"]) -def messages(): - # Main bot message handler. - if "application/json" in request.headers["Content-Type"]: - body = request.json - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = ( - request.headers["Authorization"] if "Authorization" in request.headers else "" - ) - - try: - task = LOOP.create_task( - ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - ) - LOOP.run_until_complete(task) - return Response(status=201) - except Exception as exception: - raise exception - - -if __name__ == "__main__": - try: - APP.run(debug=False, port=APP.config["PORT"]) # nosec debug - except Exception as exception: - raise exception diff --git a/scenarios/roster/bots/roster_bot.py b/scenarios/roster/bots/roster_bot.py deleted file mode 100644 index eab7b69e5..000000000 --- a/scenarios/roster/bots/roster_bot.py +++ /dev/null @@ -1,66 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from typing import List -from botbuilder.core import MessageFactory, TurnContext -from botbuilder.schema import ChannelAccount -from botbuilder.core.teams import TeamsActivityHandler, TeamsInfo - -class RosterBot(TeamsActivityHandler): - async def on_members_added_activity( - self, members_added: [ChannelAccount], turn_context: TurnContext - ): - for member in members_added: - if member.id != turn_context.activity.recipient.id: - await turn_context.send_activity( - "Hello and welcome!" - ) - - async def on_message_activity( - self, turn_context: TurnContext - ): - await turn_context.send_activity(MessageFactory.text(f"Echo: {turn_context.activity.text}")) - - text = turn_context.activity.text.strip() - if "members" in text: - await self._show_members(turn_context) - elif "channels" in text: - await self._show_channels(turn_context) - elif "details" in text: - await self._show_details(turn_context) - else: - await turn_context.send_activity(MessageFactory.text(f"Invalid command. Type \"Show channels\" to see a channel list. Type \"Show members\" to see a list of members in a team. Type \"Show details\" to see team information.")) - - async def _show_members( - self, turn_context: TurnContext - ): - members = await TeamsInfo.get_team_members(turn_context) - reply = MessageFactory.text(f"Total of {len(members)} members are currently in team") - await turn_context.send_activity(reply) - messages = list(map(lambda m: (f'{m.aad_object_id} --> {m.name} --> {m.user_principal_name}'), members)) - await self._send_in_batches(turn_context, messages) - - async def _show_channels( - self, turn_context: TurnContext - ): - channels = await TeamsInfo.get_team_channels(turn_context) - reply = MessageFactory.text(f"Total of {len(channels)} channels are currently in team") - await turn_context.send_activity(reply) - messages = list(map(lambda c: (f'{c.id} --> {c.name}'), channels)) - await self._send_in_batches(turn_context, messages) - - async def _show_details(self, turn_context: TurnContext): - team_details = await TeamsInfo.get_team_details(turn_context) - reply = MessageFactory.text(f"The team name is {team_details.name}. The team ID is {team_details.id}. The AADGroupID is {team_details.aad_group_id}.") - await turn_context.send_activity(reply) - - async def _send_in_batches(self, turn_context: TurnContext, messages: List[str]): - batch = [] - for msg in messages: - batch.append(msg) - if len(batch) == 10: - await turn_context.send_activity(MessageFactory.text("
".join(batch))) - batch = [] - - if len(batch) > 0: - await turn_context.send_activity(MessageFactory.text("
".join(batch))) \ No newline at end of file diff --git a/scenarios/roster/config.py b/scenarios/roster/config.py deleted file mode 100644 index 6b5116fba..000000000 --- a/scenarios/roster/config.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") diff --git a/scenarios/roster/requirements.txt b/scenarios/roster/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/roster/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/roster/teams_app_manifest/color.png b/scenarios/roster/teams_app_manifest/color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/roster/teams_app_manifest/color.png and /dev/null differ diff --git a/scenarios/roster/teams_app_manifest/manifest.json b/scenarios/roster/teams_app_manifest/manifest.json deleted file mode 100644 index 3078aeec2..000000000 --- a/scenarios/roster/teams_app_manifest/manifest.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0.0", - "id": "00000000-0000-0000-0000-000000000000", - "packageName": "com.teams.sample.roster", - "developer": { - "name": "TeamsRosterBot", - "websiteUrl": "https://www.microsoft.com", - "privacyUrl": "https://www.teams.com/privacy", - "termsOfUseUrl": "https://www.teams.com/termsofuser" - }, - "icons": { - "color": "color.png", - "outline": "outline.png" - }, - "name": { - "short": "TeamsRosterBot", - "full": "TeamsRosterBot" - }, - "description": { - "short": "TeamsRosterBot", - "full": "TeamsRosterBot" - }, - "accentColor": "#FFFFFF", - "bots": [ - { - "botId": "00000000-0000-0000-0000-000000000000", - "scopes": [ - "groupchat", - "team" - ], - "supportsFiles": false, - "isNotificationOnly": false - } - ], - "permissions": [ - "identity", - "messageTeamMembers" - ], - "validDomains": [] -} \ No newline at end of file diff --git a/scenarios/roster/teams_app_manifest/outline.png b/scenarios/roster/teams_app_manifest/outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/roster/teams_app_manifest/outline.png and /dev/null differ diff --git a/scenarios/search-based-messaging-extension/README.md b/scenarios/search-based-messaging-extension/README.md deleted file mode 100644 index 39f77916c..000000000 --- a/scenarios/search-based-messaging-extension/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# RosterBot - -Bot Framework v4 teams roster bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\roster` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/scenarios/search-based-messaging-extension/app.py b/scenarios/search-based-messaging-extension/app.py deleted file mode 100644 index 4b0440729..000000000 --- a/scenarios/search-based-messaging-extension/app.py +++ /dev/null @@ -1,83 +0,0 @@ -import json -import sys -from datetime import datetime - -from aiohttp import web -from aiohttp.web import Request, Response, json_response - -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) - -from botbuilder.schema import Activity, ActivityTypes -from bots import SearchBasedMessagingExtension -from config import DefaultConfig - -CONFIG = DefaultConfig() - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) -ADAPTER = BotFrameworkAdapter(SETTINGS) - -# Catch-all for errors. -async def on_error(context: TurnContext, error: Exception): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - -ADAPTER.on_turn_error = on_error - -# Create the Bot -BOT = SearchBasedMessagingExtension() - -# Listen for incoming requests on /api/messages -async def messages(req: Request) -> Response: - # Main bot message handler. - if "application/json" in req.headers["Content-Type"]: - body = await req.json() - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - - try: - response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - if response: - return json_response(data=response.body, status=response.status) - return Response(status=201) - except Exception as exception: - raise exception - -APP = web.Application() -APP.router.add_post("/api/messages", messages) - -if __name__ == "__main__": - try: - web.run_app(APP, host="localhost", port=CONFIG.PORT) - except Exception as error: - raise error diff --git a/scenarios/search-based-messaging-extension/bots/__init__.py b/scenarios/search-based-messaging-extension/bots/__init__.py deleted file mode 100644 index d35ade2a7..000000000 --- a/scenarios/search-based-messaging-extension/bots/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .search_based_messaging_extension import SearchBasedMessagingExtension - -__all__ = ["SearchBasedMessagingExtension"] diff --git a/scenarios/search-based-messaging-extension/bots/search_based_messaging_extension.py b/scenarios/search-based-messaging-extension/bots/search_based_messaging_extension.py deleted file mode 100644 index ff576fd85..000000000 --- a/scenarios/search-based-messaging-extension/bots/search_based_messaging_extension.py +++ /dev/null @@ -1,175 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import CardFactory, MessageFactory, TurnContext -from botbuilder.schema import ChannelAccount, ThumbnailCard, CardImage, HeroCard, Attachment, CardAction -from botbuilder.schema.teams import AppBasedLinkQuery, MessagingExtensionAttachment, MessagingExtensionQuery, MessagingExtensionResult, MessagingExtensionResponse -from botbuilder.core.teams import TeamsActivityHandler, TeamsInfo - -from typing import List -import requests - -class SearchBasedMessagingExtension(TeamsActivityHandler): - async def on_message_activity(self, turn_context: TurnContext): - await turn_context.send_activities(MessageFactory.text(f"Echo: {turn_context.activity.text}")) - - async def on_teams_messaging_extension_query(self, turn_context: TurnContext, query: MessagingExtensionQuery): - search_query = str(query.parameters[0].value) - response = requests.get(f"http://registry.npmjs.com/-/v1/search",params={"text":search_query}) - data = response.json() - - attachments = [] - - for obj in data["objects"]: - hero_card = HeroCard( - title=obj["package"]["name"], - tap=CardAction( - type="invoke", - value=obj["package"] - ), - preview=[CardImage(url=obj["package"]["links"]["npm"])] - ) - - attachment = MessagingExtensionAttachment( - content_type=CardFactory.content_types.hero_card, - content=HeroCard(title=obj["package"]["name"]), - preview=CardFactory.hero_card(hero_card) - ) - attachments.append(attachment) - return MessagingExtensionResponse( - compose_extension=MessagingExtensionResult( - type="result", - attachment_layout="list", - attachments=attachments - ) - ) - - - - async def on_teams_messaging_extension_select_item(self, turn_context: TurnContext, query) -> MessagingExtensionResponse: - hero_card = HeroCard( - title=query["name"], - subtitle=query["description"], - buttons=[ - CardAction( - type="openUrl", - value=query["links"]["npm"] - ) - ] - ) - attachment = MessagingExtensionAttachment( - content_type=CardFactory.content_types.hero_card, - content=hero_card - ) - - return MessagingExtensionResponse( - compose_extension=MessagingExtensionResult( - type="result", - attachment_layout="list", - attachments=[attachment] - ) - ) - - def _create_messaging_extension_result(self, attachments: List[MessagingExtensionAttachment]) -> MessagingExtensionResult: - return MessagingExtensionResult( - type="result", - attachment_layout="list", - attachments=attachments - ) - - def _create_search_result_attachment(self, search_query: str) -> MessagingExtensionAttachment: - card_text = f"You said {search_query}" - bf_logo = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU" - - button = CardAction( - type="openUrl", - title="Click for more Information", - value="https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-overview" - ) - - images = [CardImage(url=bf_logo)] - buttons = [button] - - hero_card = HeroCard( - title="You searched for:", - text=card_text, - images=images, - buttons=buttons - ) - - return MessagingExtensionAttachment( - content_type=CardFactory.content_types.hero_card, - content=hero_card, - preview=CardFactory.hero_card(hero_card) - ) - - def _create_dummy_search_result_attachment(self) -> MessagingExtensionAttachment: - card_text = "https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-overview" - bf_logo = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU" - - button = CardAction( - type = "openUrl", - title = "Click for more Information", - value = "https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-overview" - ) - - images = [CardImage(url=bf_logo)] - - buttons = [button] - - - hero_card = HeroCard( - title="Learn more about Teams:", - text=card_text, images=images, - buttons=buttons - ) - - preview = HeroCard( - title="Learn more about Teams:", - text=card_text, - images=images - ) - - return MessagingExtensionAttachment( - content_type = CardFactory.content_types.hero_card, - content = hero_card, - preview = CardFactory.hero_card(preview) - ) - - def _create_select_items_result_attachment(self, search_query: str) -> MessagingExtensionAttachment: - card_text = f"You said {search_query}" - bf_logo = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU" - - buttons = CardAction( - type="openUrl", - title="Click for more Information", - value="https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-overview" - ) - - images = [CardImage(url=bf_logo)] - buttons = [buttons] - - select_item_tap = CardAction( - type="invoke", - value={"query": search_query} - ) - - hero_card = HeroCard( - title="You searched for:", - text=card_text, - images=images, - buttons=buttons - ) - - preview = HeroCard( - title=card_text, - text=card_text, - images=images, - tap=select_item_tap - ) - - return MessagingExtensionAttachment( - content_type=CardFactory.content_types.hero_card, - content=hero_card, - preview=CardFactory.hero_card(preview) - ) \ No newline at end of file diff --git a/scenarios/search-based-messaging-extension/config.py b/scenarios/search-based-messaging-extension/config.py deleted file mode 100644 index 6b5116fba..000000000 --- a/scenarios/search-based-messaging-extension/config.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") diff --git a/scenarios/search-based-messaging-extension/requirements.txt b/scenarios/search-based-messaging-extension/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/search-based-messaging-extension/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/search-based-messaging-extension/teams_app_manifest/color.png b/scenarios/search-based-messaging-extension/teams_app_manifest/color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/search-based-messaging-extension/teams_app_manifest/color.png and /dev/null differ diff --git a/scenarios/search-based-messaging-extension/teams_app_manifest/manifest.json b/scenarios/search-based-messaging-extension/teams_app_manifest/manifest.json deleted file mode 100644 index 02c8164fc..000000000 --- a/scenarios/search-based-messaging-extension/teams_app_manifest/manifest.json +++ /dev/null @@ -1,49 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0", - "id": "<>", - "packageName": "com.microsoft.teams.samples.searchExtension", - "developer": { - "name": "Microsoft Corp", - "websiteUrl": "https://example.azurewebsites.net", - "privacyUrl": "https://example.azurewebsites.net/privacy", - "termsOfUseUrl": "https://example.azurewebsites.net/termsofuse" - }, - "name": { - "short": "search-extension-settings", - "full": "Microsoft Teams V4 Search Messaging Extension Bot and settings" - }, - "description": { - "short": "Microsoft Teams V4 Search Messaging Extension Bot and settings", - "full": "Sample Search Messaging Extension Bot using V4 Bot Builder SDK and V4 Microsoft Teams Extension SDK" - }, - "icons": { - "outline": "icon-outline.png", - "color": "icon-color.png" - }, - "accentColor": "#abcdef", - "composeExtensions": [ - { - "botId": "<>", - "canUpdateConfiguration": true, - "commands": [ - { - "id": "searchQuery", - "context": [ "compose", "commandBox" ], - "description": "Test command to run query", - "title": "Search", - "type": "query", - "parameters": [ - { - "name": "searchQuery", - "title": "Search Query", - "description": "Your search query", - "inputType": "text" - } - ] - } - ] - } - ] - } \ No newline at end of file diff --git a/scenarios/search-based-messaging-extension/teams_app_manifest/outline.png b/scenarios/search-based-messaging-extension/teams_app_manifest/outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/search-based-messaging-extension/teams_app_manifest/outline.png and /dev/null differ diff --git a/scenarios/task-module/bots/__init__.py b/scenarios/task-module/bots/__init__.py deleted file mode 100644 index 464ebfcd1..000000000 --- a/scenarios/task-module/bots/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .teams_task_module_bot import TaskModuleBot - -__all__ = ["TaskModuleBot"] diff --git a/scenarios/task-module/bots/teams_task_module_bot.py b/scenarios/task-module/bots/teams_task_module_bot.py deleted file mode 100644 index be0e8bf08..000000000 --- a/scenarios/task-module/bots/teams_task_module_bot.py +++ /dev/null @@ -1,90 +0,0 @@ -# Copyright (c) Microsoft Corp. All rights reserved. -# Licensed under the MIT License. - -import json -from typing import List -import random -from botbuilder.core import CardFactory, MessageFactory, TurnContext -from botbuilder.schema import ( - ChannelAccount, - HeroCard, - CardAction, - CardImage, - Attachment, -) -from botbuilder.schema.teams import ( - MessagingExtensionAction, - MessagingExtensionActionResponse, - MessagingExtensionAttachment, - MessagingExtensionResult, - TaskModuleResponse, - TaskModuleResponseBase, - TaskModuleContinueResponse, - TaskModuleMessageResponse, - TaskModuleTaskInfo, - TaskModuleRequest, -) -from botbuilder.core.teams import TeamsActivityHandler, TeamsInfo -from botbuilder.azure import CosmosDbPartitionedStorage -from botbuilder.core.teams.teams_helper import deserializer_helper - -class TaskModuleBot(TeamsActivityHandler): - async def on_message_activity(self, turn_context: TurnContext): - reply = MessageFactory.attachment(self._get_task_module_hero_card()) - await turn_context.send_activity(reply) - - def _get_task_module_hero_card(self) -> Attachment: - task_module_action = CardAction( - type="invoke", - title="Adaptive Card", - value={"type": "task/fetch", "data": "adaptivecard"}, - ) - card = HeroCard( - title="Task Module Invocation from Hero Card", - subtitle="This is a hero card with a Task Module Action button. Click the button to show an Adaptive Card within a Task Module.", - buttons=[task_module_action], - ) - return CardFactory.hero_card(card) - - async def on_teams_task_module_fetch( - self, turn_context: TurnContext, task_module_request: TaskModuleRequest - ) -> TaskModuleResponse: - reply = MessageFactory.text( - f"OnTeamsTaskModuleFetchAsync TaskModuleRequest: {json.dumps(task_module_request.data)}" - ) - await turn_context.send_activity(reply) - - # base_response = TaskModuleResponseBase(type='continue') - card = CardFactory.adaptive_card( - { - "version": "1.0.0", - "type": "AdaptiveCard", - "body": [ - {"type": "TextBlock", "text": "Enter Text Here",}, - { - "type": "Input.Text", - "id": "usertext", - "placeholder": "add some text and submit", - "IsMultiline": "true", - }, - ], - "actions": [{"type": "Action.Submit", "title": "Submit",}], - } - ) - - task_info = TaskModuleTaskInfo( - card=card, title="Adaptive Card: Inputs", height=200, width=400 - ) - continue_response = TaskModuleContinueResponse(type="continue", value=task_info) - return TaskModuleResponse(task=continue_response) - - async def on_teams_task_module_submit( - self, turn_context: TurnContext, task_module_request: TaskModuleRequest - ) -> TaskModuleResponse: - reply = MessageFactory.text( - f"on_teams_messaging_extension_submit_action_activity MessagingExtensionAction: {json.dumps(task_module_request.data)}" - ) - await turn_context.send_activity(reply) - - message_response = TaskModuleMessageResponse(type="message", value="Thanks!") - return TaskModuleResponse(task=message_response) diff --git a/scenarios/task-module/config.py b/scenarios/task-module/config.py deleted file mode 100644 index 9496963d3..000000000 --- a/scenarios/task-module/config.py +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get( - "MicrosoftAppPassword", "" - ) diff --git a/scenarios/task-module/requirements.txt b/scenarios/task-module/requirements.txt deleted file mode 100644 index 87eba6848..000000000 --- a/scenarios/task-module/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -botbuilder-core>=4.7.1 -flask>=1.0.3 diff --git a/scenarios/task-module/teams_app_manifest/icon-color.png b/scenarios/task-module/teams_app_manifest/icon-color.png deleted file mode 100644 index 48a2de133..000000000 Binary files a/scenarios/task-module/teams_app_manifest/icon-color.png and /dev/null differ diff --git a/scenarios/task-module/teams_app_manifest/icon-outline.png b/scenarios/task-module/teams_app_manifest/icon-outline.png deleted file mode 100644 index dbfa92772..000000000 Binary files a/scenarios/task-module/teams_app_manifest/icon-outline.png and /dev/null differ diff --git a/scenarios/task-module/teams_app_manifest/manifest.json b/scenarios/task-module/teams_app_manifest/manifest.json deleted file mode 100644 index 19d241ba5..000000000 --- a/scenarios/task-module/teams_app_manifest/manifest.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", - "manifestVersion": "1.5", - "version": "1.0.0", - "id": "<>", - "packageName": "com.microsoft.teams.samples", - "developer": { - "name": "Microsoft", - "websiteUrl": "https://example.azurewebsites.net", - "privacyUrl": "https://example.azurewebsites.net/privacy", - "termsOfUseUrl": "https://example.azurewebsites.net/termsofuse" - }, - "icons": { - "color": "color.png", - "outline": "outline.png" - }, - "name": { - "short": "Task Module", - "full": "Simple Task Module" - }, - "description": { - "short": "Test Task Module Scenario", - "full": "Simple Task Module Scenario Test" - }, - "accentColor": "#FFFFFF", - "bots": [ - { - "botId": "<>", - "scopes": [ - "personal", - "team", - "groupchat" - ], - "supportsFiles": false, - "isNotificationOnly": false - } - ], - "permissions": [ - "identity", - "messageTeamMembers" - ] -} \ No newline at end of file diff --git a/libraries/swagger/.gitignore b/swagger/.gitignore similarity index 100% rename from libraries/swagger/.gitignore rename to swagger/.gitignore diff --git a/libraries/swagger/ConnectorAPI.json b/swagger/ConnectorAPI.json similarity index 99% rename from libraries/swagger/ConnectorAPI.json rename to swagger/ConnectorAPI.json index bae96e716..af940d70d 100644 --- a/libraries/swagger/ConnectorAPI.json +++ b/swagger/ConnectorAPI.json @@ -919,7 +919,7 @@ }, "role": { "$ref": "#/definitions/RoleTypes", - "description": "Role of the entity behind the account (Example: User, Bot, etc.)" + "description": "Role of the entity behind the account (Example: User, Bot, Skill, etc.)" } } }, @@ -1154,7 +1154,7 @@ }, "role": { "$ref": "#/definitions/RoleTypes", - "description": "Role of the entity behind the account (Example: User, Bot, etc.)" + "description": "Role of the entity behind the account (Example: User, Bot, Skill, etc.)" } } }, @@ -2277,10 +2277,11 @@ } }, "RoleTypes": { - "description": "Role of the entity behind the account (Example: User, Bot, etc.)", + "description": "Role of the entity behind the account (Example: User, Bot, Skill, etc.)", "enum": [ "user", - "bot" + "bot", + "skill" ], "type": "string", "properties": {}, diff --git a/libraries/swagger/README.md b/swagger/README.md similarity index 100% rename from libraries/swagger/README.md rename to swagger/README.md diff --git a/libraries/swagger/TokenAPI.json b/swagger/TokenAPI.json similarity index 100% rename from libraries/swagger/TokenAPI.json rename to swagger/TokenAPI.json diff --git a/libraries/swagger/generateClient.cmd b/swagger/generateClient.cmd similarity index 100% rename from libraries/swagger/generateClient.cmd rename to swagger/generateClient.cmd diff --git a/swagger/package-lock.json b/swagger/package-lock.json new file mode 100644 index 000000000..f9a8accd5 --- /dev/null +++ b/swagger/package-lock.json @@ -0,0 +1,339 @@ +{ + "name": "generateclient", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@microsoft.azure/autorest.python": { + "version": "4.0.68", + "resolved": "https://registry.npmjs.org/@microsoft.azure/autorest.python/-/autorest.python-4.0.68.tgz", + "integrity": "sha512-+hSGll3lCRoy1Q6puvyCYvgLyfDYNzAZkWmJRJp6eEVVk3/QbaGvNOZUChwhnI3fkvCCdhO1jZV4LOiXw5dmZw==", + "dev": true, + "requires": { + "dotnet-2.0.0": "^1.4.4" + } + }, + "ansi-regex": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "dev": true + }, + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "dev": true, + "requires": { + "color-convert": "^1.9.0" + } + }, + "autorest": { + "version": "3.0.5165", + "resolved": "https://registry.npmjs.org/autorest/-/autorest-3.0.5165.tgz", + "integrity": "sha512-EarkJLUHiVLQrSrxYa2oHIrCoagyaxNj0RgEB9K+n4n0XnZdYlneIYhdc/V1RDQyiN7eB2CgaV/8LjKMfN9CdA==", + "dev": true + }, + "balanced-match": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "dev": true + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "dev": true + }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "cliui": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz", + "integrity": "sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==", + "dev": true, + "requires": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^6.2.0" + } + }, + "color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "dev": true, + "requires": { + "color-name": "1.1.3" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==", + "dev": true + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", + "dev": true + }, + "decamelize": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", + "integrity": "sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==", + "dev": true + }, + "dotnet-2.0.0": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/dotnet-2.0.0/-/dotnet-2.0.0-1.4.4.tgz", + "integrity": "sha512-KDbUncVUhwkJH2wjL9gbUWQ5NcZIe+PFEI0CGTMtX5TImFG6Nlt9CABVGBBG+oWf13zLARaBVenkD20moz1NPw==", + "dev": true + }, + "emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", + "dev": true + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==", + "dev": true + }, + "find-up": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", + "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==", + "dev": true, + "requires": { + "locate-path": "^5.0.0", + "path-exists": "^4.0.0" + } + }, + "get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", + "dev": true + }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==", + "dev": true + }, + "is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true + }, + "locate-path": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", + "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==", + "dev": true, + "requires": { + "p-locate": "^4.1.0" + } + }, + "minimatch": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.5.tgz", + "integrity": "sha512-tUpxzX0VAzJHjLu0xUfFv1gwVp9ba3IOuRAVH2EGuRW8a5emA2FlACLqiT/lDVtS1W+TGNwqz3sWaNyLgDJWuw==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "p-limit": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", + "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==", + "dev": true, + "requires": { + "p-try": "^2.0.0" + } + }, + "p-locate": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz", + "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==", + "dev": true, + "requires": { + "p-limit": "^2.2.0" + } + }, + "p-try": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", + "dev": true + }, + "path-exists": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", + "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", + "dev": true + }, + "replace": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/replace/-/replace-1.2.2.tgz", + "integrity": "sha512-C4EDifm22XZM2b2JOYe6Mhn+lBsLBAvLbK8drfUQLTfD1KYl/n3VaW/CDju0Ny4w3xTtegBpg8YNSpFJPUDSjA==", + "dev": true, + "requires": { + "chalk": "2.4.2", + "minimatch": "3.0.5", + "yargs": "^15.3.1" + } + }, + "require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==", + "dev": true + }, + "require-main-filename": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", + "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==", + "dev": true + }, + "set-blocking": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", + "integrity": "sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==", + "dev": true + }, + "string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dev": true, + "requires": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + } + }, + "strip-ansi": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dev": true, + "requires": { + "ansi-regex": "^5.0.1" + } + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + }, + "which-module": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz", + "integrity": "sha512-B+enWhmw6cjfVC7kS8Pj9pCrKSc5txArRyaYGe088shv/FGWH+0Rjx/xPgtsWfsUtS27FkP697E4DDhgrgoc0Q==", + "dev": true + }, + "wrap-ansi": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz", + "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==", + "dev": true, + "requires": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "dependencies": { + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "requires": { + "color-convert": "^2.0.1" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + } + } + }, + "y18n": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.3.tgz", + "integrity": "sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==", + "dev": true + }, + "yargs": { + "version": "15.4.1", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz", + "integrity": "sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==", + "dev": true, + "requires": { + "cliui": "^6.0.0", + "decamelize": "^1.2.0", + "find-up": "^4.1.0", + "get-caller-file": "^2.0.1", + "require-directory": "^2.1.1", + "require-main-filename": "^2.0.0", + "set-blocking": "^2.0.0", + "string-width": "^4.2.0", + "which-module": "^2.0.0", + "y18n": "^4.0.0", + "yargs-parser": "^18.1.2" + } + }, + "yargs-parser": { + "version": "18.1.3", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz", + "integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==", + "dev": true, + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + } + } + } +} diff --git a/libraries/swagger/package.json b/swagger/package.json similarity index 90% rename from libraries/swagger/package.json rename to swagger/package.json index 140121b5c..da19c0f8f 100644 --- a/libraries/swagger/package.json +++ b/swagger/package.json @@ -7,7 +7,7 @@ "devDependencies": { "@microsoft.azure/autorest.python": "^4.0.67", "autorest": "^3.0.5165", - "replace": "^1.0.0" + "replace": "^1.2.2" }, "dependencies": {} } diff --git a/libraries/swagger/tokenAPI.md b/swagger/tokenAPI.md similarity index 100% rename from libraries/swagger/tokenAPI.md rename to swagger/tokenAPI.md diff --git a/samples/experimental/sso/child/adapter_with_error_handler.py b/tests/experimental/sso/child/adapter_with_error_handler.py similarity index 100% rename from samples/experimental/sso/child/adapter_with_error_handler.py rename to tests/experimental/sso/child/adapter_with_error_handler.py diff --git a/samples/experimental/sso/child/app.py b/tests/experimental/sso/child/app.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/child/app.py rename to tests/experimental/sso/child/app.py diff --git a/samples/experimental/sso/child/bots/__init__.py b/tests/experimental/sso/child/bots/__init__.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/child/bots/__init__.py rename to tests/experimental/sso/child/bots/__init__.py diff --git a/samples/experimental/sso/child/bots/child_bot.py b/tests/experimental/sso/child/bots/child_bot.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/child/bots/child_bot.py rename to tests/experimental/sso/child/bots/child_bot.py diff --git a/samples/experimental/sso/child/config.py b/tests/experimental/sso/child/config.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/child/config.py rename to tests/experimental/sso/child/config.py diff --git a/samples/experimental/sso/child/dialogs/__init__.py b/tests/experimental/sso/child/dialogs/__init__.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/child/dialogs/__init__.py rename to tests/experimental/sso/child/dialogs/__init__.py diff --git a/samples/experimental/sso/child/dialogs/main_dialog.py b/tests/experimental/sso/child/dialogs/main_dialog.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/child/dialogs/main_dialog.py rename to tests/experimental/sso/child/dialogs/main_dialog.py diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/__init__.py b/tests/experimental/sso/child/helpers/__init__.py similarity index 100% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/__init__.py rename to tests/experimental/sso/child/helpers/__init__.py diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/dialog_helper.py b/tests/experimental/sso/child/helpers/dialog_helper.py similarity index 100% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/dialog_helper.py rename to tests/experimental/sso/child/helpers/dialog_helper.py diff --git a/samples/experimental/sso/parent/ReadMeForSSOTesting.md b/tests/experimental/sso/parent/ReadMeForSSOTesting.md similarity index 100% rename from samples/experimental/sso/parent/ReadMeForSSOTesting.md rename to tests/experimental/sso/parent/ReadMeForSSOTesting.md diff --git a/samples/experimental/sso/parent/app.py b/tests/experimental/sso/parent/app.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/parent/app.py rename to tests/experimental/sso/parent/app.py diff --git a/samples/experimental/sso/parent/bots/__init__.py b/tests/experimental/sso/parent/bots/__init__.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/parent/bots/__init__.py rename to tests/experimental/sso/parent/bots/__init__.py diff --git a/samples/experimental/sso/parent/bots/parent_bot.py b/tests/experimental/sso/parent/bots/parent_bot.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/parent/bots/parent_bot.py rename to tests/experimental/sso/parent/bots/parent_bot.py diff --git a/samples/experimental/sso/parent/config.py b/tests/experimental/sso/parent/config.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/parent/config.py rename to tests/experimental/sso/parent/config.py diff --git a/samples/experimental/sso/parent/dialogs/__init__.py b/tests/experimental/sso/parent/dialogs/__init__.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/parent/dialogs/__init__.py rename to tests/experimental/sso/parent/dialogs/__init__.py diff --git a/samples/experimental/sso/parent/dialogs/main_dialog.py b/tests/experimental/sso/parent/dialogs/main_dialog.py similarity index 100% rename from samples/experimental/sso/parent/dialogs/main_dialog.py rename to tests/experimental/sso/parent/dialogs/main_dialog.py diff --git a/samples/experimental/sso/child/helpers/__init__.py b/tests/experimental/sso/parent/helpers/__init__.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/child/helpers/__init__.py rename to tests/experimental/sso/parent/helpers/__init__.py diff --git a/samples/experimental/sso/child/helpers/dialog_helper.py b/tests/experimental/sso/parent/helpers/dialog_helper.py old mode 100755 new mode 100644 similarity index 100% rename from samples/experimental/sso/child/helpers/dialog_helper.py rename to tests/experimental/sso/parent/helpers/dialog_helper.py diff --git a/samples/experimental/sso/parent/skill_client.py b/tests/experimental/sso/parent/skill_client.py similarity index 100% rename from samples/experimental/sso/parent/skill_client.py rename to tests/experimental/sso/parent/skill_client.py diff --git a/samples/experimental/test-protocol/app.py b/tests/experimental/test-protocol/app.py similarity index 100% rename from samples/experimental/test-protocol/app.py rename to tests/experimental/test-protocol/app.py diff --git a/samples/experimental/test-protocol/config.py b/tests/experimental/test-protocol/config.py similarity index 96% rename from samples/experimental/test-protocol/config.py rename to tests/experimental/test-protocol/config.py index 9a6ec94ea..a6a419f17 100644 --- a/samples/experimental/test-protocol/config.py +++ b/tests/experimental/test-protocol/config.py @@ -1,18 +1,18 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - -""" Bot Configuration """ - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3428 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") - NEXT = "http://localhost:3978/api/messages" - SERVICE_URL = "http://localhost:3428/api/connector" - SKILL_APP_ID = "" +#!/usr/bin/env python3 +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os + +""" Bot Configuration """ + + +class DefaultConfig: + """ Bot Configuration """ + + PORT = 3428 + APP_ID = os.environ.get("MicrosoftAppId", "") + APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") + NEXT = "http://localhost:3978/api/messages" + SERVICE_URL = "http://localhost:3428/api/connector" + SKILL_APP_ID = "" diff --git a/samples/experimental/test-protocol/routing_handler.py b/tests/experimental/test-protocol/routing_handler.py similarity index 97% rename from samples/experimental/test-protocol/routing_handler.py rename to tests/experimental/test-protocol/routing_handler.py index 9b9bd346e..8d13b45a2 100644 --- a/samples/experimental/test-protocol/routing_handler.py +++ b/tests/experimental/test-protocol/routing_handler.py @@ -1,134 +1,134 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from typing import List - -from botbuilder.core import ChannelServiceHandler -from botbuilder.schema import ( - Activity, - ChannelAccount, - ConversationParameters, - ConversationResourceResponse, - ConversationsResult, - PagedMembersResult, - ResourceResponse -) -from botframework.connector.aio import ConnectorClient -from botframework.connector.auth import ( - AuthenticationConfiguration, - ChannelProvider, - ClaimsIdentity, - CredentialProvider, - MicrosoftAppCredentials -) - -from routing_id_factory import RoutingIdFactory - - -class RoutingHandler(ChannelServiceHandler): - def __init__( - self, - conversation_id_factory: RoutingIdFactory, - credential_provider: CredentialProvider, - auth_configuration: AuthenticationConfiguration, - channel_provider: ChannelProvider = None - ): - super().__init__(credential_provider, auth_configuration, channel_provider) - self._factory = conversation_id_factory - self._credentials = MicrosoftAppCredentials(None, None) - - async def on_reply_to_activity( - self, - claims_identity: ClaimsIdentity, - conversation_id: str, - activity_id: str, - activity: Activity, - ) -> ResourceResponse: - back_conversation_id, back_service_url = self._factory.get_conversation_info(conversation_id) - connector_client = self._get_connector_client(back_service_url) - activity.conversation.id = back_conversation_id - activity.service_url = back_service_url - - return await connector_client.conversations.send_to_conversation(back_conversation_id, activity) - - async def on_send_to_conversation( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity: Activity, - ) -> ResourceResponse: - back_conversation_id, back_service_url = self._factory.get_conversation_info(conversation_id) - connector_client = self._get_connector_client(back_service_url) - activity.conversation.id = back_conversation_id - activity.service_url = back_service_url - - return await connector_client.conversations.send_to_conversation(back_conversation_id, activity) - - async def on_update_activity( - self, - claims_identity: ClaimsIdentity, - conversation_id: str, - activity_id: str, - activity: Activity, - ) -> ResourceResponse: - back_conversation_id, back_service_url = self._factory.get_conversation_info(conversation_id) - connector_client = self._get_connector_client(back_service_url) - activity.conversation.id = back_conversation_id - activity.service_url = back_service_url - - return await connector_client.conversations.update_activity(back_conversation_id, activity.id, activity) - - async def on_delete_activity( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str, - ): - back_conversation_id, back_service_url = self._factory.get_conversation_info(conversation_id) - connector_client = self._get_connector_client(back_service_url) - - return await connector_client.conversations.delete_activity(back_conversation_id, activity_id) - - async def on_create_conversation( - self, claims_identity: ClaimsIdentity, parameters: ConversationParameters, - ) -> ConversationResourceResponse: - # This call will be used in Teams scenarios. - - # Scenario #1 - creating a thread with an activity in a Channel in a Team - # In order to know the serviceUrl in the case of Teams we would need to look it up based upon the - # TeamsChannelData. - # The inbound activity will contain the TeamsChannelData and so will the ConversationParameters. - - # Scenario #2 - starting a one on one conversation with a particular user - # - needs further analysis - - - back_service_url = "http://tempuri" - connector_client = self._get_connector_client(back_service_url) - - return await connector_client.conversations.create_conversation(parameters) - - async def on_delete_conversation_member( - self, claims_identity: ClaimsIdentity, conversation_id: str, member_id: str, - ): - return await super().on_delete_conversation_member(claims_identity, conversation_id, member_id) - - async def on_get_activity_members( - self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str, - ) -> List[ChannelAccount]: - return await super().on_get_activity_members(claims_identity, conversation_id, activity_id) - - async def on_get_conversation_members( - self, claims_identity: ClaimsIdentity, conversation_id: str, - ) -> List[ChannelAccount]: - return await super().on_get_conversation_members(claims_identity, conversation_id) - - async def on_get_conversations( - self, claims_identity: ClaimsIdentity, continuation_token: str = "", - ) -> ConversationsResult: - return await super().on_get_conversations(claims_identity, continuation_token) - - async def on_get_conversation_paged_members( - self, - claims_identity: ClaimsIdentity, - conversation_id: str, - page_size: int = None, - continuation_token: str = "", - ) -> PagedMembersResult: - return await super().on_get_conversation_paged_members(claims_identity, conversation_id, continuation_token) - - def _get_connector_client(self, service_url: str): - return ConnectorClient(self._credentials, service_url) +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List + +from botbuilder.core import ChannelServiceHandler +from botbuilder.schema import ( + Activity, + ChannelAccount, + ConversationParameters, + ConversationResourceResponse, + ConversationsResult, + PagedMembersResult, + ResourceResponse +) +from botframework.connector.aio import ConnectorClient +from botframework.connector.auth import ( + AuthenticationConfiguration, + ChannelProvider, + ClaimsIdentity, + CredentialProvider, + MicrosoftAppCredentials +) + +from routing_id_factory import RoutingIdFactory + + +class RoutingHandler(ChannelServiceHandler): + def __init__( + self, + conversation_id_factory: RoutingIdFactory, + credential_provider: CredentialProvider, + auth_configuration: AuthenticationConfiguration, + channel_provider: ChannelProvider = None + ): + super().__init__(credential_provider, auth_configuration, channel_provider) + self._factory = conversation_id_factory + self._credentials = MicrosoftAppCredentials(None, None) + + async def on_reply_to_activity( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, + activity: Activity, + ) -> ResourceResponse: + back_conversation_id, back_service_url = self._factory.get_conversation_info(conversation_id) + connector_client = self._get_connector_client(back_service_url) + activity.conversation.id = back_conversation_id + activity.service_url = back_service_url + + return await connector_client.conversations.send_to_conversation(back_conversation_id, activity) + + async def on_send_to_conversation( + self, claims_identity: ClaimsIdentity, conversation_id: str, activity: Activity, + ) -> ResourceResponse: + back_conversation_id, back_service_url = self._factory.get_conversation_info(conversation_id) + connector_client = self._get_connector_client(back_service_url) + activity.conversation.id = back_conversation_id + activity.service_url = back_service_url + + return await connector_client.conversations.send_to_conversation(back_conversation_id, activity) + + async def on_update_activity( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + activity_id: str, + activity: Activity, + ) -> ResourceResponse: + back_conversation_id, back_service_url = self._factory.get_conversation_info(conversation_id) + connector_client = self._get_connector_client(back_service_url) + activity.conversation.id = back_conversation_id + activity.service_url = back_service_url + + return await connector_client.conversations.update_activity(back_conversation_id, activity.id, activity) + + async def on_delete_activity( + self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str, + ): + back_conversation_id, back_service_url = self._factory.get_conversation_info(conversation_id) + connector_client = self._get_connector_client(back_service_url) + + return await connector_client.conversations.delete_activity(back_conversation_id, activity_id) + + async def on_create_conversation( + self, claims_identity: ClaimsIdentity, parameters: ConversationParameters, + ) -> ConversationResourceResponse: + # This call will be used in Teams scenarios. + + # Scenario #1 - creating a thread with an activity in a Channel in a Team + # In order to know the serviceUrl in the case of Teams we would need to look it up based upon the + # TeamsChannelData. + # The inbound activity will contain the TeamsChannelData and so will the ConversationParameters. + + # Scenario #2 - starting a one on one conversation with a particular user + # - needs further analysis - + + back_service_url = "http://tempuri" + connector_client = self._get_connector_client(back_service_url) + + return await connector_client.conversations.create_conversation(parameters) + + async def on_delete_conversation_member( + self, claims_identity: ClaimsIdentity, conversation_id: str, member_id: str, + ): + return await super().on_delete_conversation_member(claims_identity, conversation_id, member_id) + + async def on_get_activity_members( + self, claims_identity: ClaimsIdentity, conversation_id: str, activity_id: str, + ) -> List[ChannelAccount]: + return await super().on_get_activity_members(claims_identity, conversation_id, activity_id) + + async def on_get_conversation_members( + self, claims_identity: ClaimsIdentity, conversation_id: str, + ) -> List[ChannelAccount]: + return await super().on_get_conversation_members(claims_identity, conversation_id) + + async def on_get_conversations( + self, claims_identity: ClaimsIdentity, continuation_token: str = "", + ) -> ConversationsResult: + return await super().on_get_conversations(claims_identity, continuation_token) + + async def on_get_conversation_paged_members( + self, + claims_identity: ClaimsIdentity, + conversation_id: str, + page_size: int = None, + continuation_token: str = "", + ) -> PagedMembersResult: + return await super().on_get_conversation_paged_members(claims_identity, conversation_id, continuation_token) + + def _get_connector_client(self, service_url: str): + return ConnectorClient(self._credentials, service_url) diff --git a/samples/experimental/test-protocol/routing_id_factory.py b/tests/experimental/test-protocol/routing_id_factory.py similarity index 97% rename from samples/experimental/test-protocol/routing_id_factory.py rename to tests/experimental/test-protocol/routing_id_factory.py index c5ddb7524..0460f2df9 100644 --- a/samples/experimental/test-protocol/routing_id_factory.py +++ b/tests/experimental/test-protocol/routing_id_factory.py @@ -1,22 +1,22 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from uuid import uuid4 -from typing import Dict, Tuple - - -class RoutingIdFactory: - def __init__(self): - self._forward_x_ref: Dict[str, str] = {} - self._backward_x_ref: Dict[str, Tuple[str, str]] = {} - - def create_skill_conversation_id(self, conversation_id: str, service_url: str) -> str: - result = self._forward_x_ref.get(conversation_id, str(uuid4())) - - self._forward_x_ref[conversation_id] = result - self._backward_x_ref[result] = (conversation_id, service_url) - - return result - - def get_conversation_info(self, encoded_conversation_id) -> Tuple[str, str]: - return self._backward_x_ref[encoded_conversation_id] +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from uuid import uuid4 +from typing import Dict, Tuple + + +class RoutingIdFactory: + def __init__(self): + self._forward_x_ref: Dict[str, str] = {} + self._backward_x_ref: Dict[str, Tuple[str, str]] = {} + + def create_skill_conversation_id(self, conversation_id: str, service_url: str) -> str: + result = self._forward_x_ref.get(conversation_id, str(uuid4())) + + self._forward_x_ref[conversation_id] = result + self._backward_x_ref[result] = (conversation_id, service_url) + + return result + + def get_conversation_info(self, encoded_conversation_id) -> Tuple[str, str]: + return self._backward_x_ref[encoded_conversation_id] diff --git a/libraries/functional-tests/functionaltestbot/Dockerfile b/tests/functional-tests/functionaltestbot/Dockerfile similarity index 96% rename from libraries/functional-tests/functionaltestbot/Dockerfile rename to tests/functional-tests/functionaltestbot/Dockerfile index 3364fc380..9691bc470 100644 --- a/libraries/functional-tests/functionaltestbot/Dockerfile +++ b/tests/functional-tests/functionaltestbot/Dockerfile @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -FROM tiangolo/uwsgi-nginx-flask:python3.6 +FROM mcr.microsoft.com/oryx/python:3.10 RUN mkdir /functionaltestbot diff --git a/libraries/functional-tests/functionaltestbot/Dockfile b/tests/functional-tests/functionaltestbot/Dockfile similarity index 100% rename from libraries/functional-tests/functionaltestbot/Dockfile rename to tests/functional-tests/functionaltestbot/Dockfile diff --git a/libraries/functional-tests/functionaltestbot/client_driver/README.md b/tests/functional-tests/functionaltestbot/client_driver/README.md similarity index 100% rename from libraries/functional-tests/functionaltestbot/client_driver/README.md rename to tests/functional-tests/functionaltestbot/client_driver/README.md diff --git a/libraries/functional-tests/functionaltestbot/flask_bot_app/__init__.py b/tests/functional-tests/functionaltestbot/flask_bot_app/__init__.py similarity index 100% rename from libraries/functional-tests/functionaltestbot/flask_bot_app/__init__.py rename to tests/functional-tests/functionaltestbot/flask_bot_app/__init__.py diff --git a/libraries/functional-tests/functionaltestbot/flask_bot_app/app.py b/tests/functional-tests/functionaltestbot/flask_bot_app/app.py similarity index 100% rename from libraries/functional-tests/functionaltestbot/flask_bot_app/app.py rename to tests/functional-tests/functionaltestbot/flask_bot_app/app.py diff --git a/libraries/functional-tests/functionaltestbot/flask_bot_app/bot_app.py b/tests/functional-tests/functionaltestbot/flask_bot_app/bot_app.py similarity index 100% rename from libraries/functional-tests/functionaltestbot/flask_bot_app/bot_app.py rename to tests/functional-tests/functionaltestbot/flask_bot_app/bot_app.py diff --git a/libraries/functional-tests/functionaltestbot/flask_bot_app/default_config.py b/tests/functional-tests/functionaltestbot/flask_bot_app/default_config.py similarity index 90% rename from libraries/functional-tests/functionaltestbot/flask_bot_app/default_config.py rename to tests/functional-tests/functionaltestbot/flask_bot_app/default_config.py index 96c277e09..95a060d12 100644 --- a/libraries/functional-tests/functionaltestbot/flask_bot_app/default_config.py +++ b/tests/functional-tests/functionaltestbot/flask_bot_app/default_config.py @@ -5,7 +5,7 @@ class DefaultConfig: - """ Bot Configuration """ + """Bot Configuration""" PORT: int = 3978 APP_ID: str = environ.get("MicrosoftAppId", "") diff --git a/libraries/functional-tests/functionaltestbot/flask_bot_app/my_bot.py b/tests/functional-tests/functionaltestbot/flask_bot_app/my_bot.py similarity index 100% rename from libraries/functional-tests/functionaltestbot/flask_bot_app/my_bot.py rename to tests/functional-tests/functionaltestbot/flask_bot_app/my_bot.py diff --git a/libraries/functional-tests/functionaltestbot/functionaltestbot/README.md b/tests/functional-tests/functionaltestbot/functionaltestbot/README.md similarity index 100% rename from libraries/functional-tests/functionaltestbot/functionaltestbot/README.md rename to tests/functional-tests/functionaltestbot/functionaltestbot/README.md diff --git a/libraries/functional-tests/functionaltestbot/functionaltestbot/about.py b/tests/functional-tests/functionaltestbot/functionaltestbot/about.py similarity index 100% rename from libraries/functional-tests/functionaltestbot/functionaltestbot/about.py rename to tests/functional-tests/functionaltestbot/functionaltestbot/about.py diff --git a/libraries/functional-tests/functionaltestbot/functionaltestbot/app.py b/tests/functional-tests/functionaltestbot/functionaltestbot/app.py similarity index 99% rename from libraries/functional-tests/functionaltestbot/functionaltestbot/app.py rename to tests/functional-tests/functionaltestbot/functionaltestbot/app.py index 071a17d2b..fc975093a 100644 --- a/libraries/functional-tests/functionaltestbot/functionaltestbot/app.py +++ b/tests/functional-tests/functionaltestbot/functionaltestbot/app.py @@ -25,6 +25,7 @@ SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) ADAPTER = BotFrameworkAdapter(SETTINGS) + # Catch-all for errors. # pylint: disable=unused-argument async def on_error(self, context: TurnContext, error: Exception): @@ -46,6 +47,7 @@ async def on_error(self, context: TurnContext, error: Exception): # Create the main dialog BOT = MyBot() + # Listen for incoming requests on GET / for Azure monitoring @APP.route("/", methods=["GET"]) def ping(): diff --git a/libraries/functional-tests/functionaltestbot/functionaltestbot/bot.py b/tests/functional-tests/functionaltestbot/functionaltestbot/bot.py similarity index 100% rename from libraries/functional-tests/functionaltestbot/functionaltestbot/bot.py rename to tests/functional-tests/functionaltestbot/functionaltestbot/bot.py diff --git a/libraries/functional-tests/functionaltestbot/functionaltestbot/config.py b/tests/functional-tests/functionaltestbot/functionaltestbot/config.py similarity index 90% rename from libraries/functional-tests/functionaltestbot/functionaltestbot/config.py rename to tests/functional-tests/functionaltestbot/functionaltestbot/config.py index a3bd72174..e79075c24 100644 --- a/libraries/functional-tests/functionaltestbot/functionaltestbot/config.py +++ b/tests/functional-tests/functionaltestbot/functionaltestbot/config.py @@ -6,7 +6,7 @@ class DefaultConfig: - """ Bot Configuration """ + """Bot Configuration""" PORT = 443 APP_ID = os.environ.get("MicrosoftAppId", "") diff --git a/libraries/functional-tests/functionaltestbot/functionaltestbot/requirements.txt b/tests/functional-tests/functionaltestbot/functionaltestbot/requirements.txt similarity index 100% rename from libraries/functional-tests/functionaltestbot/functionaltestbot/requirements.txt rename to tests/functional-tests/functionaltestbot/functionaltestbot/requirements.txt diff --git a/libraries/functional-tests/functionaltestbot/init.sh b/tests/functional-tests/functionaltestbot/init.sh similarity index 100% rename from libraries/functional-tests/functionaltestbot/init.sh rename to tests/functional-tests/functionaltestbot/init.sh diff --git a/libraries/functional-tests/functionaltestbot/requirements.txt b/tests/functional-tests/functionaltestbot/requirements.txt similarity index 70% rename from libraries/functional-tests/functionaltestbot/requirements.txt rename to tests/functional-tests/functionaltestbot/requirements.txt index a348b59af..ce98b3838 100644 --- a/libraries/functional-tests/functionaltestbot/requirements.txt +++ b/tests/functional-tests/functionaltestbot/requirements.txt @@ -1,5 +1,5 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -botbuilder-core>=4.5.0.b4 -flask==1.1.1 +botbuilder-core>=4.9.0 +flask==2.2.5 diff --git a/libraries/functional-tests/functionaltestbot/runserver.py b/tests/functional-tests/functionaltestbot/runserver.py similarity index 100% rename from libraries/functional-tests/functionaltestbot/runserver.py rename to tests/functional-tests/functionaltestbot/runserver.py diff --git a/libraries/functional-tests/functionaltestbot/setup.py b/tests/functional-tests/functionaltestbot/setup.py similarity index 95% rename from libraries/functional-tests/functionaltestbot/setup.py rename to tests/functional-tests/functionaltestbot/setup.py index 1378ac4b0..3abf311eb 100644 --- a/libraries/functional-tests/functionaltestbot/setup.py +++ b/tests/functional-tests/functionaltestbot/setup.py @@ -5,8 +5,8 @@ from setuptools import setup REQUIRES = [ - "botbuilder-core>=4.5.0.b4", - "flask==1.1.1", + "botbuilder-core>=4.9.0", + "flask==2.2.5", ] root = os.path.abspath(os.path.dirname(__file__)) diff --git a/libraries/functional-tests/functionaltestbot/sshd_config b/tests/functional-tests/functionaltestbot/sshd_config similarity index 100% rename from libraries/functional-tests/functionaltestbot/sshd_config rename to tests/functional-tests/functionaltestbot/sshd_config diff --git a/libraries/functional-tests/functionaltestbot/template/linux/template.json b/tests/functional-tests/functionaltestbot/template/linux/template.json similarity index 100% rename from libraries/functional-tests/functionaltestbot/template/linux/template.json rename to tests/functional-tests/functionaltestbot/template/linux/template.json diff --git a/libraries/functional-tests/functionaltestbot/test.sh b/tests/functional-tests/functionaltestbot/test.sh similarity index 100% rename from libraries/functional-tests/functionaltestbot/test.sh rename to tests/functional-tests/functionaltestbot/test.sh diff --git a/tests/functional-tests/requirements.txt b/tests/functional-tests/requirements.txt new file mode 100644 index 000000000..d00d7a830 --- /dev/null +++ b/tests/functional-tests/requirements.txt @@ -0,0 +1,2 @@ +requests==2.32.0 +aiounittest==1.3.0 diff --git a/libraries/functional-tests/tests/direct_line_client.py b/tests/functional-tests/tests/direct_line_client.py similarity index 100% rename from libraries/functional-tests/tests/direct_line_client.py rename to tests/functional-tests/tests/direct_line_client.py diff --git a/libraries/functional-tests/tests/test_py_bot.py b/tests/functional-tests/tests/test_py_bot.py similarity index 100% rename from libraries/functional-tests/tests/test_py_bot.py rename to tests/functional-tests/tests/test_py_bot.py diff --git a/samples/experimental/skills-buffered/child/app.py b/tests/skills/skills-buffered/child/app.py similarity index 100% rename from samples/experimental/skills-buffered/child/app.py rename to tests/skills/skills-buffered/child/app.py diff --git a/samples/experimental/skills-buffered/child/bots/__init__.py b/tests/skills/skills-buffered/child/bots/__init__.py similarity index 100% rename from samples/experimental/skills-buffered/child/bots/__init__.py rename to tests/skills/skills-buffered/child/bots/__init__.py diff --git a/samples/experimental/skills-buffered/child/bots/child_bot.py b/tests/skills/skills-buffered/child/bots/child_bot.py similarity index 100% rename from samples/experimental/skills-buffered/child/bots/child_bot.py rename to tests/skills/skills-buffered/child/bots/child_bot.py diff --git a/samples/experimental/skills-buffered/child/config.py b/tests/skills/skills-buffered/child/config.py similarity index 100% rename from samples/experimental/skills-buffered/child/config.py rename to tests/skills/skills-buffered/child/config.py diff --git a/samples/experimental/skills-buffered/child/requirements.txt b/tests/skills/skills-buffered/child/requirements.txt similarity index 60% rename from samples/experimental/skills-buffered/child/requirements.txt rename to tests/skills/skills-buffered/child/requirements.txt index 20f8f8fe5..9e79c8115 100644 --- a/samples/experimental/skills-buffered/child/requirements.txt +++ b/tests/skills/skills-buffered/child/requirements.txt @@ -1,2 +1,2 @@ botbuilder-core>=4.7.1 -aiohttp +aiohttp==3.*.* diff --git a/samples/experimental/skills-buffered/parent/app.py b/tests/skills/skills-buffered/parent/app.py similarity index 100% rename from samples/experimental/skills-buffered/parent/app.py rename to tests/skills/skills-buffered/parent/app.py diff --git a/samples/experimental/skills-buffered/parent/bots/__init__.py b/tests/skills/skills-buffered/parent/bots/__init__.py similarity index 100% rename from samples/experimental/skills-buffered/parent/bots/__init__.py rename to tests/skills/skills-buffered/parent/bots/__init__.py diff --git a/samples/experimental/skills-buffered/parent/bots/parent_bot.py b/tests/skills/skills-buffered/parent/bots/parent_bot.py similarity index 100% rename from samples/experimental/skills-buffered/parent/bots/parent_bot.py rename to tests/skills/skills-buffered/parent/bots/parent_bot.py diff --git a/samples/experimental/skills-buffered/parent/config.py b/tests/skills/skills-buffered/parent/config.py similarity index 100% rename from samples/experimental/skills-buffered/parent/config.py rename to tests/skills/skills-buffered/parent/config.py diff --git a/samples/experimental/skills-buffered/parent/requirements.txt b/tests/skills/skills-buffered/parent/requirements.txt similarity index 60% rename from samples/experimental/skills-buffered/parent/requirements.txt rename to tests/skills/skills-buffered/parent/requirements.txt index 20f8f8fe5..9e79c8115 100644 --- a/samples/experimental/skills-buffered/parent/requirements.txt +++ b/tests/skills/skills-buffered/parent/requirements.txt @@ -1,2 +1,2 @@ botbuilder-core>=4.7.1 -aiohttp +aiohttp==3.*.* diff --git a/samples/experimental/skills-buffered/parent/skill_conversation_id_factory.py b/tests/skills/skills-buffered/parent/skill_conversation_id_factory.py similarity index 100% rename from samples/experimental/skills-buffered/parent/skill_conversation_id_factory.py rename to tests/skills/skills-buffered/parent/skill_conversation_id_factory.py diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/README.md b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/README.md similarity index 97% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/README.md rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/README.md index 40e84f525..f1a48af72 100644 --- a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/README.md +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/README.md @@ -1,30 +1,30 @@ -# EchoBot - -Bot Framework v4 echo bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) +# EchoBot + +Bot Framework v4 echo bot sample. + +This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. + +## Running the sample +- Clone the repository +```bash +git clone https://github.com/Microsoft/botbuilder-python.git +``` +- Activate your desired virtual environment +- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder +- In the terminal, type `pip install -r requirements.txt` +- In the terminal, type `python app.py` + +## Testing the bot using Bot Framework Emulator +[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. + +- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) + +### Connect to bot using Bot Framework Emulator +- Launch Bot Framework Emulator +- Paste this URL in the emulator window - http://localhost:3978/api/messages + +## Further reading + +- [Bot Framework Documentation](https://docs.botframework.com) +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/app.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/app.py similarity index 96% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/app.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/app.py index 95fd89577..96ffb9b2b 100644 --- a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/app.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/app.py @@ -1,98 +1,98 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import sys -import traceback -from datetime import datetime - -from aiohttp import web -from aiohttp.web import Request, Response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - ConversationState, - MemoryStorage, - UserState, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes - -from bots import AuthBot -from dialogs import MainDialog -from config import DefaultConfig - -CONFIG = DefaultConfig() - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) -ADAPTER = BotFrameworkAdapter(SETTINGS) - -STORAGE = MemoryStorage() - -CONVERSATION_STATE = ConversationState(STORAGE) -USER_STATE = UserState(STORAGE) - - -# Catch-all for errors. -async def on_error(context: TurnContext, error: Exception): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - traceback.print_exc() - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = on_error - -DIALOG = MainDialog(CONFIG) - - -# Listen for incoming requests on /api/messages -async def messages(req: Request) -> Response: - # Create the Bot - bot = AuthBot(CONVERSATION_STATE, USER_STATE, DIALOG) - - # Main bot message handler. - if "application/json" in req.headers["Content-Type"]: - body = await req.json() - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - - try: - await ADAPTER.process_activity(activity, auth_header, bot.on_turn) - return Response(status=201) - except Exception as exception: - raise exception - - -APP = web.Application() -APP.router.add_post("/api/messages", messages) - -if __name__ == "__main__": - try: - web.run_app(APP, host="localhost", port=CONFIG.PORT) - except Exception as error: - raise error +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import sys +import traceback +from datetime import datetime + +from aiohttp import web +from aiohttp.web import Request, Response +from botbuilder.core import ( + BotFrameworkAdapterSettings, + ConversationState, + MemoryStorage, + UserState, + TurnContext, + BotFrameworkAdapter, +) +from botbuilder.schema import Activity, ActivityTypes + +from bots import AuthBot +from dialogs import MainDialog +from config import DefaultConfig + +CONFIG = DefaultConfig() + +# Create adapter. +# See https://aka.ms/about-bot-adapter to learn more about how bots work. +SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) +ADAPTER = BotFrameworkAdapter(SETTINGS) + +STORAGE = MemoryStorage() + +CONVERSATION_STATE = ConversationState(STORAGE) +USER_STATE = UserState(STORAGE) + + +# Catch-all for errors. +async def on_error(context: TurnContext, error: Exception): + # This check writes out errors to console log .vs. app insights. + # NOTE: In production environment, you should consider logging this to Azure + # application insights. + print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) + traceback.print_exc() + + # Send a message to the user + await context.send_activity("The bot encountered an error or bug.") + await context.send_activity( + "To continue to run this bot, please fix the bot source code." + ) + # Send a trace activity if we're talking to the Bot Framework Emulator + if context.activity.channel_id == "emulator": + # Create a trace activity that contains the error object + trace_activity = Activity( + label="TurnError", + name="on_turn_error Trace", + timestamp=datetime.utcnow(), + type=ActivityTypes.trace, + value=f"{error}", + value_type="https://www.botframework.com/schemas/error", + ) + # Send a trace activity, which will be displayed in Bot Framework Emulator + await context.send_activity(trace_activity) + + +ADAPTER.on_turn_error = on_error + +DIALOG = MainDialog(CONFIG) + + +# Listen for incoming requests on /api/messages +async def messages(req: Request) -> Response: + # Create the Bot + bot = AuthBot(CONVERSATION_STATE, USER_STATE, DIALOG) + + # Main bot message handler. + if "application/json" in req.headers["Content-Type"]: + body = await req.json() + else: + return Response(status=415) + + activity = Activity().deserialize(body) + auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" + + try: + await ADAPTER.process_activity(activity, auth_header, bot.on_turn) + return Response(status=201) + except Exception as exception: + raise exception + + +APP = web.Application() +APP.router.add_post("/api/messages", messages) + +if __name__ == "__main__": + try: + web.run_app(APP, host="localhost", port=CONFIG.PORT) + except Exception as error: + raise error diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/bots/__init__.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/bots/__init__.py similarity index 97% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/bots/__init__.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/bots/__init__.py index 9fae5bf38..9b49815be 100644 --- a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/bots/__init__.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/bots/__init__.py @@ -1,6 +1,6 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -from .dialog_bot import DialogBot -from .auth_bot import AuthBot - -__all__ = ["DialogBot", "AuthBot"] +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +from .dialog_bot import DialogBot +from .auth_bot import AuthBot + +__all__ = ["DialogBot", "AuthBot"] diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/bots/auth_bot.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/bots/auth_bot.py similarity index 97% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/bots/auth_bot.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/bots/auth_bot.py index ec0325fda..e72b681c1 100644 --- a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/bots/auth_bot.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/bots/auth_bot.py @@ -1,42 +1,42 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from typing import List - -from botbuilder.core import MessageFactory, TurnContext -from botbuilder.schema import ActivityTypes, ChannelAccount - -from helpers.dialog_helper import DialogHelper -from bots import DialogBot - - -class AuthBot(DialogBot): - async def on_turn(self, turn_context: TurnContext): - if turn_context.activity.type == ActivityTypes.invoke: - await DialogHelper.run_dialog( - self.dialog, - turn_context, - self.conversation_state.create_property("DialogState") - ) - else: - await super().on_turn(turn_context) - - async def on_members_added_activity( - self, members_added: List[ChannelAccount], turn_context: TurnContext - ): - for member in members_added: - if member.id != turn_context.activity.recipient.id: - await turn_context.send_activity( - MessageFactory.text("Hello and welcome!") - ) - - async def on_token_response_event( - self, turn_context: TurnContext - ): - print("on token: Running dialog with Message Activity.") - - return await DialogHelper.run_dialog( - self.dialog, - turn_context, - self.conversation_state.create_property("DialogState") - ) +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import List + +from botbuilder.core import MessageFactory, TurnContext +from botbuilder.schema import ActivityTypes, ChannelAccount + +from helpers.dialog_helper import DialogHelper +from bots import DialogBot + + +class AuthBot(DialogBot): + async def on_turn(self, turn_context: TurnContext): + if turn_context.activity.type == ActivityTypes.invoke: + await DialogHelper.run_dialog( + self.dialog, + turn_context, + self.conversation_state.create_property("DialogState") + ) + else: + await super().on_turn(turn_context) + + async def on_members_added_activity( + self, members_added: List[ChannelAccount], turn_context: TurnContext + ): + for member in members_added: + if member.id != turn_context.activity.recipient.id: + await turn_context.send_activity( + MessageFactory.text("Hello and welcome!") + ) + + async def on_token_response_event( + self, turn_context: TurnContext + ): + print("on token: Running dialog with Message Activity.") + + return await DialogHelper.run_dialog( + self.dialog, + turn_context, + self.conversation_state.create_property("DialogState") + ) diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/bots/dialog_bot.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/bots/dialog_bot.py similarity index 97% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/bots/dialog_bot.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/bots/dialog_bot.py index 12576303e..eb9b355f6 100644 --- a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/bots/dialog_bot.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/bots/dialog_bot.py @@ -1,29 +1,29 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import ActivityHandler, ConversationState, UserState, TurnContext -from botbuilder.dialogs import Dialog - -from helpers.dialog_helper import DialogHelper - - -class DialogBot(ActivityHandler): - def __init__(self, conversation_state: ConversationState, user_state: UserState, dialog: Dialog): - self.conversation_state = conversation_state - self._user_state = user_state - self.dialog = dialog - - async def on_turn(self, turn_context: TurnContext): - await super().on_turn(turn_context) - - await self.conversation_state.save_changes(turn_context, False) - await self._user_state.save_changes(turn_context, False) - - async def on_message_activity(self, turn_context: TurnContext): - print("on message: Running dialog with Message Activity.") - - return await DialogHelper.run_dialog( - self.dialog, - turn_context, - self.conversation_state.create_property("DialogState") - ) +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import ActivityHandler, ConversationState, UserState, TurnContext +from botbuilder.dialogs import Dialog + +from helpers.dialog_helper import DialogHelper + + +class DialogBot(ActivityHandler): + def __init__(self, conversation_state: ConversationState, user_state: UserState, dialog: Dialog): + self.conversation_state = conversation_state + self._user_state = user_state + self.dialog = dialog + + async def on_turn(self, turn_context: TurnContext): + await super().on_turn(turn_context) + + await self.conversation_state.save_changes(turn_context, False) + await self._user_state.save_changes(turn_context, False) + + async def on_message_activity(self, turn_context: TurnContext): + print("on message: Running dialog with Message Activity.") + + return await DialogHelper.run_dialog( + self.dialog, + turn_context, + self.conversation_state.create_property("DialogState") + ) diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/config.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/config.py similarity index 95% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/config.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/config.py index 97a5625bf..3c064f3ff 100644 --- a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/config.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/config.py @@ -1,16 +1,16 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - -""" Bot Configuration """ - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") - CONNECTION_NAME = "" +#!/usr/bin/env python3 +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os + +""" Bot Configuration """ + + +class DefaultConfig: + """ Bot Configuration """ + + PORT = 3978 + APP_ID = os.environ.get("MicrosoftAppId", "") + APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") + CONNECTION_NAME = "" diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/__init__.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/__init__.py similarity index 94% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/__init__.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/__init__.py index f8117421c..6ec3374f3 100644 --- a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/__init__.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/__init__.py @@ -1,7 +1,7 @@ -from .logout_dialog import LogoutDialog -from .main_dialog import MainDialog - -__all__ = [ - "LogoutDialog", - "MainDialog" -] +from .logout_dialog import LogoutDialog +from .main_dialog import MainDialog + +__all__ = [ + "LogoutDialog", + "MainDialog" +] diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/logout_dialog.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/logout_dialog.py similarity index 97% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/logout_dialog.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/logout_dialog.py index 2e4a6c653..6855b8710 100644 --- a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/logout_dialog.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/logout_dialog.py @@ -1,47 +1,47 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.dialogs import ( - ComponentDialog, - DialogTurnResult, -) -from botbuilder.dialogs import DialogContext -from botbuilder.core import BotFrameworkAdapter, MessageFactory -from botbuilder.schema import ActivityTypes - - -class LogoutDialog(ComponentDialog): - def __init__( - self, dialog_id: str, connection_name: str, - ): - super().__init__(dialog_id) - - self.connection_name = connection_name - - async def on_begin_dialog( - self, inner_dc: DialogContext, options: object - ) -> DialogTurnResult: - result = await self._interrupt(inner_dc) - if result: - return result - - return await super().on_begin_dialog(inner_dc, options) - - async def on_continue_dialog(self, inner_dc: DialogContext) -> DialogTurnResult: - result = await self._interrupt(inner_dc) - if result: - return result - - return await super().on_continue_dialog(inner_dc) - - async def _interrupt(self, inner_dc: DialogContext): - if inner_dc.context.activity.type == ActivityTypes.message: - text = inner_dc.context.activity.text.lower() - - if text == "logout": - bot_adapter: BotFrameworkAdapter = inner_dc.context.adapter - await bot_adapter.sign_out_user(inner_dc.context, self.connection_name) - await inner_dc.context.send_activity(MessageFactory.text("You have been signed out.")) - return await inner_dc.cancel_all_dialogs() - - return None +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.dialogs import ( + ComponentDialog, + DialogTurnResult, +) +from botbuilder.dialogs import DialogContext +from botbuilder.core import BotFrameworkAdapter, MessageFactory +from botbuilder.schema import ActivityTypes + + +class LogoutDialog(ComponentDialog): + def __init__( + self, dialog_id: str, connection_name: str, + ): + super().__init__(dialog_id) + + self.connection_name = connection_name + + async def on_begin_dialog( + self, inner_dc: DialogContext, options: object + ) -> DialogTurnResult: + result = await self._interrupt(inner_dc) + if result: + return result + + return await super().on_begin_dialog(inner_dc, options) + + async def on_continue_dialog(self, inner_dc: DialogContext) -> DialogTurnResult: + result = await self._interrupt(inner_dc) + if result: + return result + + return await super().on_continue_dialog(inner_dc) + + async def _interrupt(self, inner_dc: DialogContext): + if inner_dc.context.activity.type == ActivityTypes.message: + text = inner_dc.context.activity.text.lower() + + if text == "logout": + bot_adapter: BotFrameworkAdapter = inner_dc.context.adapter + await bot_adapter.sign_out_user(inner_dc.context, self.connection_name) + await inner_dc.context.send_activity(MessageFactory.text("You have been signed out.")) + return await inner_dc.cancel_all_dialogs() + + return None diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/main_dialog.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/main_dialog.py similarity index 97% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/main_dialog.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/main_dialog.py index e851bbe38..afdf3727a 100644 --- a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/main_dialog.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/dialogs/main_dialog.py @@ -1,72 +1,72 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.dialogs import ( - WaterfallDialog, - WaterfallStepContext, - DialogTurnResult, -) -from botbuilder.dialogs.prompts import ConfirmPrompt, PromptOptions, OAuthPrompt, OAuthPromptSettings -from botbuilder.core import MessageFactory -from dialogs import LogoutDialog - - -class MainDialog(LogoutDialog): - def __init__( - self, configuration, - ): - super().__init__(MainDialog.__name__, configuration.CONNECTION_NAME) - - self.add_dialog( - OAuthPrompt( - OAuthPrompt.__name__, - OAuthPromptSettings( - connection_name=self.connection_name, - text="Please Sign In", - title="Sign In", - timeout=30000, - ) - ) - ) - self.add_dialog(ConfirmPrompt(ConfirmPrompt.__name__)) - self.add_dialog( - WaterfallDialog( - "WFDialog", - [self.prompt_step, self.login_step, self.display_token_phase_one, self.display_token_phase_two] - ) - ) - - self.initial_dialog_id = "WFDialog" - - async def prompt_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: - return await step_context.begin_dialog( - OAuthPrompt.__name__ - ) - - async def login_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: - token_response = step_context.result - if token_response: - await step_context.context.send_activity(MessageFactory.text("You are now logged in.")) - return await step_context.prompt( - ConfirmPrompt.__name__, - PromptOptions(prompt=MessageFactory.text("Would you like to view your token?")) - ) - - await step_context.context.send_activity(MessageFactory.text("Login was not successful please try again.")) - return await step_context.end_dialog() - - async def display_token_phase_one(self, step_context: WaterfallStepContext) -> DialogTurnResult: - await step_context.context.send_activity(MessageFactory.text("Thank you")) - - result = step_context.result - if result: - return await step_context.begin_dialog(OAuthPrompt.__name__) - - return await step_context.end_dialog() - - async def display_token_phase_two(self, step_context: WaterfallStepContext) -> DialogTurnResult: - token_response = step_context.result - if token_response: - await step_context.context.send_activity(MessageFactory.text(f"Here is your token {token_response.token}")) - - return await step_context.end_dialog() +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.dialogs import ( + WaterfallDialog, + WaterfallStepContext, + DialogTurnResult, +) +from botbuilder.dialogs.prompts import ConfirmPrompt, PromptOptions, OAuthPrompt, OAuthPromptSettings +from botbuilder.core import MessageFactory +from dialogs import LogoutDialog + + +class MainDialog(LogoutDialog): + def __init__( + self, configuration, + ): + super().__init__(MainDialog.__name__, configuration.CONNECTION_NAME) + + self.add_dialog( + OAuthPrompt( + OAuthPrompt.__name__, + OAuthPromptSettings( + connection_name=self.connection_name, + text="Please Sign In", + title="Sign In", + timeout=30000, + ) + ) + ) + self.add_dialog(ConfirmPrompt(ConfirmPrompt.__name__)) + self.add_dialog( + WaterfallDialog( + "WFDialog", + [self.prompt_step, self.login_step, self.display_token_phase_one, self.display_token_phase_two] + ) + ) + + self.initial_dialog_id = "WFDialog" + + async def prompt_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: + return await step_context.begin_dialog( + OAuthPrompt.__name__ + ) + + async def login_step(self, step_context: WaterfallStepContext) -> DialogTurnResult: + token_response = step_context.result + if token_response: + await step_context.context.send_activity(MessageFactory.text("You are now logged in.")) + return await step_context.prompt( + ConfirmPrompt.__name__, + PromptOptions(prompt=MessageFactory.text("Would you like to view your token?")) + ) + + await step_context.context.send_activity(MessageFactory.text("Login was not successful please try again.")) + return await step_context.end_dialog() + + async def display_token_phase_one(self, step_context: WaterfallStepContext) -> DialogTurnResult: + await step_context.context.send_activity(MessageFactory.text("Thank you")) + + result = step_context.result + if result: + return await step_context.begin_dialog(OAuthPrompt.__name__) + + return await step_context.end_dialog() + + async def display_token_phase_two(self, step_context: WaterfallStepContext) -> DialogTurnResult: + token_response = step_context.result + if token_response: + await step_context.context.send_activity(MessageFactory.text(f"Here is your token {token_response.token}")) + + return await step_context.end_dialog() diff --git a/samples/experimental/sso/parent/helpers/__init__.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/__init__.py old mode 100755 new mode 100644 similarity index 96% rename from samples/experimental/sso/parent/helpers/__init__.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/__init__.py index a824eb8f4..8dba0e6d6 --- a/samples/experimental/sso/parent/helpers/__init__.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/__init__.py @@ -1,6 +1,6 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from . import dialog_helper - -__all__ = ["dialog_helper"] +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from . import dialog_helper + +__all__ = ["dialog_helper"] diff --git a/samples/experimental/101.corebot-bert-bidaf/bot/helpers/dialog_helper.py b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/dialog_helper.py similarity index 81% rename from samples/experimental/101.corebot-bert-bidaf/bot/helpers/dialog_helper.py rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/dialog_helper.py index 56ba5b05f..062271fd8 100644 --- a/samples/experimental/101.corebot-bert-bidaf/bot/helpers/dialog_helper.py +++ b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/helpers/dialog_helper.py @@ -1,18 +1,15 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -"""Utility to run dialogs.""" + from botbuilder.core import StatePropertyAccessor, TurnContext from botbuilder.dialogs import Dialog, DialogSet, DialogTurnStatus class DialogHelper: - """Dialog Helper implementation.""" - @staticmethod async def run_dialog( dialog: Dialog, turn_context: TurnContext, accessor: StatePropertyAccessor - ): # pylint: disable=line-too-long - """Run dialog.""" + ): dialog_set = DialogSet(accessor) dialog_set.add(dialog) diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/requirements.txt b/tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/requirements.txt similarity index 100% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/requirements.txt rename to tests/skills/skills-prototypes/dialog-to-dialog/authentication-bot/requirements.txt diff --git a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/README.md b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/README.md similarity index 97% rename from samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/README.md rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/README.md index 40e84f525..f1a48af72 100644 --- a/samples/experimental/skills-prototypes/dialog-to-dialog/authentication-bot/README.md +++ b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/README.md @@ -1,30 +1,30 @@ -# EchoBot - -Bot Framework v4 echo bot sample. - -This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. - -## Running the sample -- Clone the repository -```bash -git clone https://github.com/Microsoft/botbuilder-python.git -``` -- Activate your desired virtual environment -- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder -- In the terminal, type `pip install -r requirements.txt` -- In the terminal, type `python app.py` - -## Testing the bot using Bot Framework Emulator -[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. - -- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) - -### Connect to bot using Bot Framework Emulator -- Launch Bot Framework Emulator -- Paste this URL in the emulator window - http://localhost:3978/api/messages - -## Further reading - -- [Bot Framework Documentation](https://docs.botframework.com) -- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) -- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) +# EchoBot + +Bot Framework v4 echo bot sample. + +This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. + +## Running the sample +- Clone the repository +```bash +git clone https://github.com/Microsoft/botbuilder-python.git +``` +- Activate your desired virtual environment +- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder +- In the terminal, type `pip install -r requirements.txt` +- In the terminal, type `python app.py` + +## Testing the bot using Bot Framework Emulator +[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. + +- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) + +### Connect to bot using Bot Framework Emulator +- Launch Bot Framework Emulator +- Paste this URL in the emulator window - http://localhost:3978/api/messages + +## Further reading + +- [Bot Framework Documentation](https://docs.botframework.com) +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/app.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/app.py similarity index 96% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/app.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/app.py index cfa375aac..d1964743e 100644 --- a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/app.py +++ b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/app.py @@ -1,85 +1,85 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import sys -from datetime import datetime - -from aiohttp import web -from aiohttp.web import Request, Response -from botbuilder.core import ( - BotFrameworkAdapterSettings, - TurnContext, - BotFrameworkAdapter, -) -from botbuilder.schema import Activity, ActivityTypes - -from bots import EchoBot -from config import DefaultConfig - -CONFIG = DefaultConfig() - -# Create adapter. -# See https://aka.ms/about-bot-adapter to learn more about how bots work. -SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) -ADAPTER = BotFrameworkAdapter(SETTINGS) - - -# Catch-all for errors. -async def on_error(context: TurnContext, error: Exception): - # This check writes out errors to console log .vs. app insights. - # NOTE: In production environment, you should consider logging this to Azure - # application insights. - print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) - - # Send a message to the user - await context.send_activity("The bot encountered an error or bug.") - await context.send_activity( - "To continue to run this bot, please fix the bot source code." - ) - # Send a trace activity if we're talking to the Bot Framework Emulator - if context.activity.channel_id == "emulator": - # Create a trace activity that contains the error object - trace_activity = Activity( - label="TurnError", - name="on_turn_error Trace", - timestamp=datetime.utcnow(), - type=ActivityTypes.trace, - value=f"{error}", - value_type="https://www.botframework.com/schemas/error", - ) - # Send a trace activity, which will be displayed in Bot Framework Emulator - await context.send_activity(trace_activity) - - -ADAPTER.on_turn_error = on_error - -# Create the Bot -BOT = EchoBot() - - -# Listen for incoming requests on /api/messages -async def messages(req: Request) -> Response: - # Main bot message handler. - if "application/json" in req.headers["Content-Type"]: - body = await req.json() - else: - return Response(status=415) - - activity = Activity().deserialize(body) - auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - - try: - await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - return Response(status=201) - except Exception as exception: - raise exception - - -APP = web.Application() -APP.router.add_post("/api/messages", messages) - -if __name__ == "__main__": - try: - web.run_app(APP, host="localhost", port=CONFIG.PORT) - except Exception as error: - raise error +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import sys +from datetime import datetime + +from aiohttp import web +from aiohttp.web import Request, Response +from botbuilder.core import ( + BotFrameworkAdapterSettings, + TurnContext, + BotFrameworkAdapter, +) +from botbuilder.schema import Activity, ActivityTypes + +from bots import EchoBot +from config import DefaultConfig + +CONFIG = DefaultConfig() + +# Create adapter. +# See https://aka.ms/about-bot-adapter to learn more about how bots work. +SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) +ADAPTER = BotFrameworkAdapter(SETTINGS) + + +# Catch-all for errors. +async def on_error(context: TurnContext, error: Exception): + # This check writes out errors to console log .vs. app insights. + # NOTE: In production environment, you should consider logging this to Azure + # application insights. + print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) + + # Send a message to the user + await context.send_activity("The bot encountered an error or bug.") + await context.send_activity( + "To continue to run this bot, please fix the bot source code." + ) + # Send a trace activity if we're talking to the Bot Framework Emulator + if context.activity.channel_id == "emulator": + # Create a trace activity that contains the error object + trace_activity = Activity( + label="TurnError", + name="on_turn_error Trace", + timestamp=datetime.utcnow(), + type=ActivityTypes.trace, + value=f"{error}", + value_type="https://www.botframework.com/schemas/error", + ) + # Send a trace activity, which will be displayed in Bot Framework Emulator + await context.send_activity(trace_activity) + + +ADAPTER.on_turn_error = on_error + +# Create the Bot +BOT = EchoBot() + + +# Listen for incoming requests on /api/messages +async def messages(req: Request) -> Response: + # Main bot message handler. + if "application/json" in req.headers["Content-Type"]: + body = await req.json() + else: + return Response(status=415) + + activity = Activity().deserialize(body) + auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" + + try: + await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) + return Response(status=201) + except Exception as exception: + raise exception + + +APP = web.Application() +APP.router.add_post("/api/messages", messages) + +if __name__ == "__main__": + try: + web.run_app(APP, host="localhost", port=CONFIG.PORT) + except Exception as error: + raise error diff --git a/scenarios/roster/bots/__init__.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/__init__.py similarity index 62% rename from scenarios/roster/bots/__init__.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/__init__.py index a2e035b9f..e41ca32ac 100644 --- a/scenarios/roster/bots/__init__.py +++ b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/__init__.py @@ -1,6 +1,6 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from .roster_bot import RosterBot - -__all__ = ["RosterBot"] +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .echo_bot import EchoBot + +__all__ = ["EchoBot"] diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/echo_bot.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/echo_bot.py similarity index 97% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/echo_bot.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/echo_bot.py index 91c3febb0..e82cebb51 100644 --- a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/echo_bot.py +++ b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/echo_bot.py @@ -1,27 +1,27 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -from botbuilder.core import ActivityHandler, MessageFactory, TurnContext -from botbuilder.schema import Activity, ActivityTypes, EndOfConversationCodes - - -class EchoBot(ActivityHandler): - async def on_message_activity(self, turn_context: TurnContext): - if "end" in turn_context.activity.text or "exit" in turn_context.activity.text: - # Send End of conversation at the end. - await turn_context.send_activity( - MessageFactory.text("Ending conversation from the skill...") - ) - - end_of_conversation = Activity(type=ActivityTypes.end_of_conversation) - end_of_conversation.code = EndOfConversationCodes.completed_successfully - await turn_context.send_activity(end_of_conversation) - else: - await turn_context.send_activity( - MessageFactory.text(f"Echo: {turn_context.activity.text}") - ) - await turn_context.send_activity( - MessageFactory.text( - f'Say "end" or "exit" and I\'ll end the conversation and back to the parent.' - ) - ) +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import ActivityHandler, MessageFactory, TurnContext +from botbuilder.schema import Activity, ActivityTypes, EndOfConversationCodes + + +class EchoBot(ActivityHandler): + async def on_message_activity(self, turn_context: TurnContext): + if "end" in turn_context.activity.text or "exit" in turn_context.activity.text: + # Send End of conversation at the end. + await turn_context.send_activity( + MessageFactory.text("Ending conversation from the skill...") + ) + + end_of_conversation = Activity(type=ActivityTypes.end_of_conversation) + end_of_conversation.code = EndOfConversationCodes.completed_successfully + await turn_context.send_activity(end_of_conversation) + else: + await turn_context.send_activity( + MessageFactory.text(f"Echo: {turn_context.activity.text}") + ) + await turn_context.send_activity( + MessageFactory.text( + f'Say "end" or "exit" and I\'ll end the conversation and back to the parent.' + ) + ) diff --git a/scenarios/activity-update-and-delete/config.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/config.py similarity index 88% rename from scenarios/activity-update-and-delete/config.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/config.py index 6b5116fba..ed68df254 100644 --- a/scenarios/activity-update-and-delete/config.py +++ b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/config.py @@ -1,13 +1,15 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3978 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") +#!/usr/bin/env python3 +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os + +""" Bot Configuration """ + + +class DefaultConfig: + """ Bot Configuration """ + + PORT = 3978 + APP_ID = os.environ.get("MicrosoftAppId", "") + APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/requirements.txt b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/requirements.txt similarity index 100% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/requirements.txt rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-child-bot/requirements.txt diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/app.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/app.py similarity index 100% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/app.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/app.py diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/__init__.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/__init__.py similarity index 93% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/__init__.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/__init__.py index be7e157a7..5cf1c3615 100644 --- a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/__init__.py +++ b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/__init__.py @@ -1,4 +1,4 @@ -from .root_bot import RootBot - - -__all__ = ["RootBot"] +from .root_bot import RootBot + + +__all__ = ["RootBot"] diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/root_bot.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/root_bot.py similarity index 100% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/root_bot.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/bots/root_bot.py diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/config.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/config.py similarity index 96% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/config.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/config.py index f2a9e1f6e..af0df9c81 100644 --- a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/config.py +++ b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/config.py @@ -1,32 +1,32 @@ -#!/usr/bin/env python3 -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -import os -from typing import Dict -from botbuilder.core.skills import BotFrameworkSkill - -""" Bot Configuration """ - - -class DefaultConfig: - """ Bot Configuration """ - - PORT = 3428 - APP_ID = os.environ.get("MicrosoftAppId", "") - APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") - SKILL_HOST_ENDPOINT = "http://localhost:3428/api/skills" - SKILLS = [ - { - "id": "SkillBot", - "app_id": "", - "skill_endpoint": "http://localhost:3978/api/messages", - }, - ] - - -class SkillConfiguration: - SKILL_HOST_ENDPOINT = DefaultConfig.SKILL_HOST_ENDPOINT - SKILLS: Dict[str, BotFrameworkSkill] = { - skill["id"]: BotFrameworkSkill(**skill) for skill in DefaultConfig.SKILLS - } +#!/usr/bin/env python3 +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os +from typing import Dict +from botbuilder.core.skills import BotFrameworkSkill + +""" Bot Configuration """ + + +class DefaultConfig: + """ Bot Configuration """ + + PORT = 3428 + APP_ID = os.environ.get("MicrosoftAppId", "") + APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") + SKILL_HOST_ENDPOINT = "http://localhost:3428/api/skills" + SKILLS = [ + { + "id": "SkillBot", + "app_id": "", + "skill_endpoint": "http://localhost:3978/api/messages", + }, + ] + + +class SkillConfiguration: + SKILL_HOST_ENDPOINT = DefaultConfig.SKILL_HOST_ENDPOINT + SKILLS: Dict[str, BotFrameworkSkill] = { + skill["id"]: BotFrameworkSkill(**skill) for skill in DefaultConfig.SKILLS + } diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/__init__.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/__init__.py similarity index 95% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/__init__.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/__init__.py index c23b52ce2..b4c3cd2cf 100644 --- a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/__init__.py +++ b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/__init__.py @@ -1,4 +1,4 @@ -from .dummy_middleware import DummyMiddleware - - -__all__ = ["DummyMiddleware"] +from .dummy_middleware import DummyMiddleware + + +__all__ = ["DummyMiddleware"] diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/dummy_middleware.py b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/dummy_middleware.py similarity index 96% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/dummy_middleware.py rename to tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/dummy_middleware.py index 4d38fe79f..82eb34707 100644 --- a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/dummy_middleware.py +++ b/tests/skills/skills-prototypes/simple-bot-to-bot/simple-root-bot/middleware/dummy_middleware.py @@ -1,32 +1,32 @@ -from typing import Awaitable, Callable, List - -from botbuilder.core import Middleware, TurnContext -from botbuilder.schema import Activity, ResourceResponse - - -class DummyMiddleware(Middleware): - def __init__(self, label: str): - self._label = label - - async def on_turn( - self, context: TurnContext, logic: Callable[[TurnContext], Awaitable] - ): - message = f"{self._label} {context.activity.type} {context.activity.text}" - print(message) - - # Register outgoing handler - context.on_send_activities(self._outgoing_handler) - - await logic() - - async def _outgoing_handler( - self, - context: TurnContext, # pylint: disable=unused-argument - activities: List[Activity], - logic: Callable[[TurnContext], Awaitable[List[ResourceResponse]]], - ): - for activity in activities: - message = f"{self._label} {activity.type} {activity.text}" - print(message) - - return await logic() +from typing import Awaitable, Callable, List + +from botbuilder.core import Middleware, TurnContext +from botbuilder.schema import Activity, ResourceResponse + + +class DummyMiddleware(Middleware): + def __init__(self, label: str): + self._label = label + + async def on_turn( + self, context: TurnContext, logic: Callable[[TurnContext], Awaitable] + ): + message = f"{self._label} {context.activity.type} {context.activity.text}" + print(message) + + # Register outgoing handler + context.on_send_activities(self._outgoing_handler) + + await logic() + + async def _outgoing_handler( + self, + context: TurnContext, # pylint: disable=unused-argument + activities: List[Activity], + logic: Callable[[TurnContext], Awaitable[List[ResourceResponse]]], + ): + for activity in activities: + message = f"{self._label} {activity.type} {activity.text}" + print(message) + + return await logic() diff --git a/scenarios/link-unfurling/app.py b/tests/skills/streamming-extensions/app.py similarity index 85% rename from scenarios/link-unfurling/app.py rename to tests/skills/streamming-extensions/app.py index 8bbf90feb..450c22b17 100644 --- a/scenarios/link-unfurling/app.py +++ b/tests/skills/streamming-extensions/app.py @@ -1,21 +1,21 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -import json import sys +import traceback from datetime import datetime from aiohttp import web from aiohttp.web import Request, Response, json_response - from botbuilder.core import ( BotFrameworkAdapterSettings, TurnContext, BotFrameworkAdapter, ) - +from botbuilder.core.integration import aiohttp_error_middleware from botbuilder.schema import Activity, ActivityTypes -from bots import LinkUnfurlingBot + +from bots import EchoBot from config import DefaultConfig CONFIG = DefaultConfig() @@ -25,12 +25,14 @@ SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) ADAPTER = BotFrameworkAdapter(SETTINGS) + # Catch-all for errors. async def on_error(context: TurnContext, error: Exception): # This check writes out errors to console log .vs. app insights. # NOTE: In production environment, you should consider logging this to Azure # application insights. print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) + traceback.print_exc() # Send a message to the user await context.send_activity("The bot encountered an error or bug.") @@ -48,14 +50,15 @@ async def on_error(context: TurnContext, error: Exception): value=f"{error}", value_type="https://www.botframework.com/schemas/error", ) - # Send a trace activity, which will be displayed in Bot Framework Emulator await context.send_activity(trace_activity) + ADAPTER.on_turn_error = on_error # Create the Bot -BOT = LinkUnfurlingBot() +BOT = EchoBot() + # Listen for incoming requests on /api/messages async def messages(req: Request) -> Response: @@ -68,15 +71,13 @@ async def messages(req: Request) -> Response: activity = Activity().deserialize(body) auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" - try: - response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) - if response: - return json_response(data=response.body, status=response.status) - return Response(status=201) - except Exception as exception: - raise exception - -APP = web.Application() + response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) + if response: + return json_response(data=response.body, status=response.status) + return Response(status=201) + + +APP = web.Application(middlewares=[aiohttp_error_middleware]) APP.router.add_post("/api/messages", messages) if __name__ == "__main__": diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/__init__.py b/tests/skills/streamming-extensions/bots/__init__.py similarity index 100% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/bots/__init__.py rename to tests/skills/streamming-extensions/bots/__init__.py diff --git a/tests/skills/streamming-extensions/bots/echo_bot.py b/tests/skills/streamming-extensions/bots/echo_bot.py new file mode 100644 index 000000000..90a094640 --- /dev/null +++ b/tests/skills/streamming-extensions/bots/echo_bot.py @@ -0,0 +1,19 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import ActivityHandler, MessageFactory, TurnContext +from botbuilder.schema import ChannelAccount + + +class EchoBot(ActivityHandler): + async def on_members_added_activity( + self, members_added: [ChannelAccount], turn_context: TurnContext + ): + for member in members_added: + if member.id != turn_context.activity.recipient.id: + await turn_context.send_activity("Hello and welcome!") + + async def on_message_activity(self, turn_context: TurnContext): + return await turn_context.send_activity( + MessageFactory.text(f"Echo: {turn_context.activity.text}") + ) diff --git a/samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/config.py b/tests/skills/streamming-extensions/config.py similarity index 100% rename from samples/experimental/skills-prototypes/simple-bot-to-bot/simple-child-bot/config.py rename to tests/skills/streamming-extensions/config.py