Skip to content

Instantly share code, notes, and snippets.

@rido-min
Last active November 21, 2024 06:34
Show Gist options
  • Save rido-min/fa22d090f60440053e69e46345942bd0 to your computer and use it in GitHub Desktop.
Save rido-min/fa22d090f60440053e69e46345942bd0 to your computer and use it in GitHub Desktop.
minimal-bot
node_modules/
dist/
.env
.npmrc
package-lock.json

bb-agent

bot / agent starter

setup

Configure .npmrc

echo "registry=https://pkgs.dev.azure.com/dynamicscrm/OneCRM/_packaging/CopilotSDK-JS-Nightly/npm/registry/ " > .npmrc

Install

npm -dd i

Provision / Configure ABS

$botName = Read-Host "BotName in ABS"
$appId = az ad app create --display-name $botName --sign-in-audience "AzureADMyOrg" --query appId | ConvertFrom-Json
$secretJson = az ad app credential reset --id $appId | ConvertFrom-Json
MicrosoftAppType=SingleTenant
MicrosoftAppTenantId=69e9b82d-4842-4902-8d1e-abc5b98a55e8
MicrosoftAppId=2bcdb210-4ed5-4ae8-96c8-33668ad97ed1
MicrosoftAppPassword=

Run

node --env-file .env app.js
// @ts-check
const express = require('express')
const {
CloudAdapter, ConfigurationBotFrameworkAuthentication,
} = require('botbuilder')
const { EchoBot } = require('./bot')
const adapter = new CloudAdapter(new ConfigurationBotFrameworkAuthentication());
const myBot = new EchoBot()
const server = express()
server.use(express.json())
server.post('/api/messages',
async (req, res) => {
console.log(req.body)
await adapter.process(req, res, (context) => myBot.run(context));
}
)
const port = process.env.PORT || 3978
server.listen(port, () => {
console.log(`\n lisenting on ${ port } for bot ${ process.env.MicrosoftAppId }`);
})
const { ActivityHandler, MessageFactory } = require('botbuilder')
class EchoBot extends ActivityHandler {
constructor() {
super()
this.onMessage(async (context, next) => {
const replyText = `Echo: ${ context.activity.text }`
await context.sendActivity(MessageFactory.text(replyText, replyText))
await next()
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded
const welcomeText = 'Hello from CS and welcome!'
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity(MessageFactory.text(welcomeText, welcomeText))
}
}
await next()
})
}
}
module.exports.EchoBot = EchoBot
{
"dependencies": {
"botbuilder": "^4.23.1",
"express": "^5.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment