ctermdocs HomeBlogSign in

Microsoft Teams

cterm reads and writes Teams as you in one of two ways. When your organisation's admin registered an app for it (Microsoft calls it an app registration, with the device-code sign-in), cterm talks to Microsoft Graph, Teams' documented API, signed in as you with a code from Microsoft. Where no app is allowed, it does what you would: it opens Teams on the web in a browser of its own, signed in as you, reads what the page shows and types replies into the message box. Graph comes first while it is signed in, and the browser is the way back. Either way, anything a session wants to send waits in the project's queue until you approve it.

What it is for

  • A session that looks after a support channel reads the new messages, opens the files people attached (a spreadsheet, a PDF) and drafts the answer.
  • You approve the draft from the project page, or from your phone, and cterm posts it in the thread as you, with the files the session attached.
  • Notes to yourself (your own chat in Teams) go at once, with no approval, and so does everything a project you switched to direct sends writes. A project can also let its messages leave by themselves after a few minutes unless you touch them: Messages that leave after a delay.
  • Your own chat can also be a source of a project's desk, so that what the desk would post (a reply signed by a team member, one you approve) is tried there first: Support says how.

How to use it

Signing in to Microsoft Graph

Your admin gives you two things for the app: your organisation's tenant (its domain, or its id) and the app's client id. Set them once, from a terminal on the Mac where cterm runs or from an admin's session; cterm keeps them in its own store, nowhere else:

cterm teams app <tenant> <client id>
cterm teams login

cterm teams login prints an address and a short code. Open the address in any browser (your phone's will do), enter the code and sign in as yourself, with your second factor. The command waits and then says who signed in. From then on cterm keeps the sign-in and renews it by itself, across restarts and from one day to the next, until Microsoft ends it (your password changed, your admin withdrew the app); then Teams goes through the browser again and cterm teams status says the sign-in ended and to run cterm teams login. cterm teams logout forgets it.

Only an admin signs in, from a terminal or from one of their own sessions; a user's session is refused. Use the tenant's own domain or id: the shared organizations sign-in does not work for an app of one organisation.

cterm teams status says which way Teams goes now, who is signed in to Graph and since when, the scopes your admin consented, the browser's state, and the way the last call went (with the reason when it took the browser).

Signing in to the browser

The browser is the way back, and the only way where no app exists. Sign it in once:

cterm teams login --browser

A Chrome window opens at Teams. Sign in as usual, with your second factor; the window closes by itself once your chats show. From then on cterm uses that browser without a window when Graph cannot be used.

A conversation can ask for your password again on its own while the rest of Teams works: a send to it then fails saying so. Give the login that conversation (its id or a link copied from Teams), and the window opens on it and stays until its message box shows, so you can answer there:

cterm teams login --browser <to>

Reading and sending

Then, from a terminal or from a session (yours, as an admin):

  • cterm teams read <to> prints the messages of a chat, a channel or one thread, oldest first, each with its time, author and files. --since 2h (or a message id, or a time) and --limit 20 keep the newest. A channel with no posts yet reads as no messages.
  • cterm teams react <to> --message <id> puts a 👍 on one message (--reaction ✅, 👀, ❤️ and a few more), at once and with no yes, through Graph alone; a link copied from the message names it without --message. The support desk's coordinator uses it to acknowledge a message that needs no reply.
  • cterm teams download <file address> saves an attachment, or an image someone pasted into the chat, using the address read prints under the message.
  • cterm teams send <to> "<text>" sends a message, --file <path> attaches a file (several times for several), --thread <post id> replies in a channel's thread (without it, a message to a channel is a new post), and --dry only checks that the conversation and its message box open. Write @[Sandra Camacho] for a mention: cterm types the name and picks the person from Teams' list, as you would. A name written with accents the list lacks is tried again without them (@[Mónica Estrada] finds the Monica Estrada Teams lists). If nobody by that name is offered there, nothing is sent and the box is left empty, with no draft of the name. Before anything goes, cterm reads the box and then the message Teams is about to post: when either is missing a word you wrote, a mention or a web address in its place, nothing is posted, and the command fails saying what it held. The lines are checked too: each line you wrote on a line of its own, one that starts with "- " included. A word that starts with @ and is no @[Name] (an @company.com, an @handle) goes in as plain text, pinging nobody: Teams opens its list of people on it, and cterm closes that list before typing on. A web address (https://… or http://…) arrives as a link the reader clicks, the way Teams links one you paste; cterm waits for each pasted address to show in the box where you wrote it before typing on, so two addresses never end up glued together at the end. A comma, full stop or closing bracket right after it stays text. A blank line between paragraphs stays one.

<to> is self for your own chat, a conversation's id, or a link you copied from Teams (Copy link on a channel or on a message): a message's link replies in its thread.

Through Graph the commands work the same, and each says which way it went. A send is one request: your lines kept, each web address a link, each @[Nombre Apellido] a mention of the one person your organisation's directory has by that name (accents and capitals do not matter). A name that matches nobody, or several people, sends nothing and says so. Teams' answer, the new message's id, is what makes cterm say it was sent; when Graph refuses the message itself it says Graph's status and reason and sends nothing, by Graph or otherwise. An image you attach goes inside the message. Any other file (a spreadsheet, a CSV, a PDF) goes through Graph too once your sign-in holds the right to write files (Files.ReadWrite or Files.ReadWrite.All, which cterm teams status lists among the scopes): cterm uploads it where Teams itself keeps it and attaches it to the same message, beside the text and the images.

  • A new post or a reply in a channel's thread: into the channel's own Files folder, so the people of the channel open it as any file posted there.
  • A chat, your own included: into the Microsoft Teams Chat Files folder of your OneDrive (made if it is not there yet), shared with the chat's other people and nobody else, so each of them can open it.

A file of a name already in that folder is never written over: it arrives under a free name (informe 1.xlsx), the one there stays as it was, and the message carries the file under its new name. A large file goes up in parts, whole. The command lists each file attached with its address, and says the send went through Graph and why. A spreadsheet, document or slide deck is often stored larger than it was sent, since SharePoint and OneDrive write their own properties into it; that is fine. Only a file stored with fewer bytes than were sent counts as a cut upload.

When an upload fails, nothing is posted and the command says Graph's status and reason, naming any file an earlier upload of the same send left in the folder. A send through Graph that fails also leaves a [teams] line in cterm's log with the conversation, Graph's status and reason, and the files left in the folder. When Graph refuses an upload or the sharing for want of a right, the send goes through the browser instead, saying so. When Graph refuses the message after its files went up, it is not tried again another way, and the command names each file left in the folder with its address. Without the right to write files, a send with a file goes through the browser as before, and the command says that was why. --dry finds the mentions and shows the message as it would go, uploading and sending nothing.

Approving a reply

When a session runs cterm teams send to anything but your own chat, nothing is sent yet. The message goes to the project's queue, titled with where it goes, with the whole text and the files in its why, shown whole where other entries are cut at four lines. On the project page:

  • Approve sends exactly that message, as you; the session is told it was sent, so it does not send it again.
  • Changes… sends your words back to the session, which drafts it again and queues it again.
  • Bin… sends nothing, and the session reads your reason.

If the send fails at the approval (Teams signed out, a file gone), the entry says why and the session hears it.

Sending without a yes

When the point is that nobody waits for you, let a project's sessions send at once: set Teams to Direct in the project's Settings, Sends section, or from a terminal:

cterm teams direct on <owner>/<project>

From then on its sessions' messages go to Teams as soon as they write them, as you, with nothing in the queue. Only you switch it, from the Settings tab or a terminal: a session cannot. The same section offers Leave after a delay, where each message waits a few minutes with a countdown you can stop (Messages that leave after a delay). A session sends at most 500 messages an hour this way, so one that goes wrong cannot flood a channel without end; past that its send is refused and nothing goes. cterm teams direct lists the projects that send so, and cterm teams direct off <owner>/<project> puts the queue back.

How it works

Through Microsoft Graph

The sign-in's renewal token is kept in cterm's store, readable only by your user on the Mac, and never written to a log; the short-lived token Graph takes lives in memory and is renewed before it runs out. Each call waits out Graph's limits when Graph asks it to slow down, and a call Graph refuses keeps Graph's status and reason in its error and in cterm's log.

A chat is read by what changed since the last read. A channel is read with every post's replies, and Graph lists a channel's posts by the last change of their whole thread, so a new reply in a thread however old brings its post back to the top: cterm reads until the first thread older than the last round, and each reply names the post it answers, so on a desk it lands on that post's case and opens it again if it was resolved. A thread is read whole, however many replies it has. A file is downloaded through Graph from SharePoint or OneDrive, and an image someone pasted into a message from that message.

A read goes through the browser when Graph is not signed in or fails, except when Graph says to slow down, which is said as such. A send goes through the browser only when Graph is not signed in, lacks the right for it (an upload's or a share's included), or carries a file other than an image while the sign-in may not write files; never after Graph refused the message, nor after a send that got no answer, since it may have gone. A desk's round says on its run which way it read.

Through the browser

cterm keeps a Chrome profile signed in as you in its own state folder. Treat it like a password: whoever can read it can read your Teams. The browser opens at the first call, does one thing at a time, and closes after ten idle minutes.

It reads what Teams draws on the page and never calls Teams' internal services itself. A channel's page shows its last twenty or so posts, so cterm scrolls it back over the last seven days; a post whose replies changed has its thread opened and read, so every reply comes under its post. A long thread's page shows a screen of it at a time, so cterm scrolls it down to its newest reply; when it still reads fewer replies than the post says it has, it does not count the thread as read and opens it again on a later read. A link to a reply opens its thread at that reply. A desk also has the threads of its open cases followed whatever their age, a few on each read, so an answer in an older thread still arrives. Each read opens eight threads at most, which keeps it under a minute, and cterm remembers which threads it has read across a restart. Before a message leaves, cterm checks that Teams is posting it to the conversation and thread you named, and stops it otherwise. Attachments are downloaded only from SharePoint, where Teams keeps them. A download that gets no file says why: the file's SharePoint site asked for a sign-in (named, since Teams itself may still be signed in), its page closed, or no file came within 30 seconds.

It follows Teams' page as it is today, in English or Spanish. If Microsoft renames the message box or the attach button, a call fails and says what it did not find, rather than typing somewhere else. A page counts as open once it shows a message, a message box, or, in a channel with no posts yet, its New post button or its empty state; a page that shows none of them within 45 seconds fails saying so, and one that lands on Microsoft's sign-in says Teams is signed out. When Teams itself refuses a message, the send says what Teams answered, its status and its reason ("Teams answered 400: … ; nothing sent"), cterm's log keeps a line for it, and cterm teams status shows it as the last error until a later call goes through.

A new post in a channel goes into the box the channel draws for it: "Message in <channel>" when the channel shows its posts as threads, or, when it shows them as posts, the box that opens after cterm clicks Post in channel (New post, Start a post), sent with that box's Post button, never Enter, which only starts a new line there. A channel that holds a draft of a new post shows the draft's words and Continue your draft where Post in channel was: cterm opens it, clears the old words and subject, and posts its own; a --dry says what the draft held. When no box can be reached, the send says what the page showed: no New post button, or the button it clicked and no box after it, the text boxes and the buttons that might start a post, and Teams' own words when the channel does not let you post (only owners or moderators). When the post does not appear within 30 seconds (a minute for a message with files, which Teams shares with the people of the conversation first), the send says whether cterm clicked Post or pressed Enter, what the box held then (cleared, so no draft is left behind), whether Teams shows the message as sending or failed (so it may still go out when Teams opens again) and which message requests Teams made, each with its answer and whether it was a message or only Teams' typing signal. A file whose name you sent before arrives under Teams' own numbered name (informe 1.xlsx). The same lines go to cterm's log and to cterm teams status. Where Teams does not let you post, answer in a thread instead.

Written in docs/site/pages/teams.md and built by docs/site/build.mjs.