microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Handlers/MessageHandler.cs
104lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.RegularExpressions; |
| 5 | using Microsoft.Teams.Bot.Apps.Routing; |
| 6 | using Microsoft.Teams.Bot.Apps.Schema; |
| 7 | |
| 8 | namespace Microsoft.Teams.Bot.Apps.Handlers; |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Delegate for handling message activities. |
| 12 | /// </summary> |
| 13 | /// <param name="context"></param> |
| 14 | /// <param name="cancellationToken"></param> |
| 15 | /// <returns></returns> |
| 16 | public delegate Task MessageHandler(Context<MessageActivity> context, CancellationToken cancellationToken = default); |
| 17 | |
| 18 | /// <summary> |
| 19 | /// Extension methods for registering message activity handlers. |
| 20 | /// </summary> |
| 21 | public static class MessageExtensions |
| 22 | { |
| 23 | /// <summary> |
| 24 | /// Registers a handler for message activities. |
| 25 | /// </summary> |
| 26 | /// <remarks> |
| 27 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 28 | /// </remarks> |
| 29 | /// <param name="app"></param> |
| 30 | /// <param name="handler"></param> |
| 31 | /// <returns></returns> |
| 32 | public static TeamsBotApplication OnMessage(this TeamsBotApplication app, MessageHandler handler) |
| 33 | { |
| 34 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 35 | app.Router.Register(new Route<MessageActivity> |
| 36 | { |
| 37 | |
| 38 | Name = TeamsActivityType.Message, |
| 39 | Selector = _ => true, |
| 40 | Handler = async (ctx, cancellationToken) => |
| 41 | { |
| 42 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | return app; |
| 47 | } |
| 48 | |
| 49 | /// <summary> |
| 50 | /// Registers a handler for message activities matching the specified pattern. |
| 51 | /// </summary> |
| 52 | /// <remarks> |
| 53 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 54 | /// </remarks> |
| 55 | /// <param name="app"></param> |
| 56 | /// <param name="pattern"></param> |
| 57 | /// <param name="handler"></param> |
| 58 | /// <returns></returns> |
| 59 | public static TeamsBotApplication OnMessage(this TeamsBotApplication app, string pattern, MessageHandler handler) |
| 60 | { |
| 61 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 62 | Regex regex = new(pattern); |
| 63 | |
| 64 | app.Router.Register(new Route<MessageActivity> |
| 65 | { |
| 66 | Name = string.Join("/", [TeamsActivityType.Message, pattern]), |
| 67 | Selector = msg => regex.IsMatch(msg.Text ?? ""), |
| 68 | Handler = async (ctx, cancellationToken) => |
| 69 | { |
| 70 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 71 | } |
| 72 | }); |
| 73 | |
| 74 | return app; |
| 75 | } |
| 76 | |
| 77 | /// <summary> |
| 78 | /// Registers a handler for message activities matching the specified regex. |
| 79 | /// </summary> |
| 80 | /// <remarks> |
| 81 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 82 | /// </remarks> |
| 83 | /// <param name="app"></param> |
| 84 | /// <param name="regex"></param> |
| 85 | /// <param name="handler"></param> |
| 86 | /// <returns></returns> |
| 87 | public static TeamsBotApplication OnMessage(this TeamsBotApplication app, Regex regex, MessageHandler handler) |
| 88 | { |
| 89 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 90 | ArgumentNullException.ThrowIfNull(regex, nameof(regex)); |
| 91 | app.Router.Register(new Route<MessageActivity> |
| 92 | { |
| 93 | Name = string.Join("/", [TeamsActivityType.Message, regex.ToString()]), |
| 94 | Selector = msg => regex.IsMatch(msg.Text ?? ""), |
| 95 | Handler = async (ctx, cancellationToken) => |
| 96 | { |
| 97 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | return app; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |