microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Samples/Samples.Tab/Web/src/App.tsx
103lines · modecode
| 1 | import React from 'react'; |
| 2 | import * as teamsJs from '@microsoft/teams-js'; |
| 3 | import * as client from '@microsoft/teams.client'; |
| 4 | import * as endpoints from '@microsoft/teams.graph-endpoints'; |
| 5 | import { ConsoleLogger } from '@microsoft/teams.common'; |
| 6 | |
| 7 | import './App.css'; |
| 8 | |
| 9 | const clientId = import.meta.env.VITE_CLIENT_ID; |
| 10 | |
| 11 | export default function App() { |
| 12 | const [content, setContent] = React.useState(''); |
| 13 | const [app, setApp] = React.useState<client.App | null>(null); |
| 14 | |
| 15 | React.useEffect(() => { |
| 16 | (async () => { |
| 17 | // initialize the app and prompt for Graph scope consent, if not already granted |
| 18 | const app = new client.App(clientId, { |
| 19 | logger: new ConsoleLogger('@tests/tab', { level: 'debug' }), |
| 20 | }); |
| 21 | |
| 22 | await app.start(); |
| 23 | app.log.info('app started'); |
| 24 | setApp(app); |
| 25 | })(); |
| 26 | }, []); |
| 27 | |
| 28 | const showTeamsJsContext = React.useCallback(async () => { |
| 29 | if (!app) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | const context = await teamsJs.app.getContext(); |
| 34 | setContent(JSON.stringify(context, null, 2)); |
| 35 | }, [app]); |
| 36 | |
| 37 | |
| 38 | const postChatMessage = React.useCallback(async () => { |
| 39 | if (!app) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // get the bot to post a message to the current chat, whichever that is |
| 44 | const { conversationId } = await app.exec<{ conversationId: string }>('post-to-chat', { message: 'Hello from the client!' }); |
| 45 | setContent(`Message posted to conversation ${conversationId}`); |
| 46 | }, [app]); |
| 47 | |
| 48 | |
| 49 | const whoAmI = React.useCallback(async () => { |
| 50 | if (!app) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // get the current user from the Microsoft Graph |
| 55 | const me = await app.graph.call(endpoints.me.get); |
| 56 | setContent(JSON.stringify(me, null, 2)); |
| 57 | }, [app]); |
| 58 | |
| 59 | |
| 60 | const togglePresentationMode = React.useCallback(async () => { |
| 61 | if (!app) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | // get current presence from the Microsoft Graph |
| 66 | const { availability } = await app.graph.call(endpoints.me.presence.get); |
| 67 | const isAvailable = availability === 'Available'; |
| 68 | |
| 69 | // toggle between Dnd/Presenting and Available/Available |
| 70 | const newPresence = { |
| 71 | sessionId: clientId, |
| 72 | availability: isAvailable ? 'DoNotDisturb' : 'Available', |
| 73 | activity: isAvailable ? 'Presenting' : 'Available' |
| 74 | }; |
| 75 | await app.graph.call(endpoints.me.presence.setPresence.create, newPresence); |
| 76 | setContent(`You're now ${newPresence.activity}`); |
| 77 | |
| 78 | }, [app]); |
| 79 | |
| 80 | return ( |
| 81 | <div className="App"> |
| 82 | <h1>👋 Welcome</h1> |
| 83 | <p>This test app lets you try out some of the features offered by Teams AI Library v2 for Teams Tab app developers.</p> |
| 84 | |
| 85 | <div className="actions"> |
| 86 | <button disabled={!app} onClick={showTeamsJsContext}>Show TeamsJs context</button> |
| 87 | <button disabled={!app} onClick={postChatMessage}>Post chat message</button> |
| 88 | <button disabled={!app} onClick={whoAmI}>Who am I?</button> |
| 89 | <button disabled={!app} onClick={togglePresentationMode}>Toggle presentation mode</button> |
| 90 | </div> |
| 91 | |
| 92 | {content && ( |
| 93 | <div className='result'> |
| 94 | <pre> |
| 95 | <code>{content}</code> |
| 96 | </pre> |
| 97 | </div> |
| 98 | )} |
| 99 | |
| 100 | <p>For more information, please refer to the <a href='https://microsoft.github.io/teams-ai' rel='noopener noreferrer' target='_blank'>Teams AI documentation</a>.</p> |
| 101 | </div> |
| 102 | ); |
| 103 | } |