Bot Framework SDK4 C# - send proactive message to a user in Teams / REST response timeout - botframework

Based on a GitHub sample, I created a dialog bot in Teams that collects information from a user and sends that to a Flow (Microsoft Flow/Power Automate) with a HTTP POST call.
After that the Bot waits for a response and sends it back to the user. This generally would be sufficient if the timeout for the call wasn't limited to 2 minutes - sometimes it takes longer to get the complete actions in Flow and get the response.
My question is how I could accomplish the same without getting the timeout. REST seemed the easiest as I'm not a programmer..
I checked the GitHub sample for proactive messaging (https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/16.proactive-messages), and that worked in the Emulator when I enter http://localhost:3978/api/notify, but I have no idea how to use it published to Azure. What would be the endpoint and how I could pass the message text?
Thanks

You should do some code modify if you want to send an message based on proactive-messages demo .
You can refer to my previous post which will meet your requirement.
After you modify the NotifyController.cs file , you can use send messages to a specific user with steps below :
Connect to your bot get current user ID :
Send message to this user by rest client tool or post man :
Result :
If you publish your bot to Azure , lets assume your Azure App service host is :https://xxxxxx.azurewebsites.net , then your bot message endpoint will be :https://xxxxxx.azurewebsites.net/api/messages and your /notify function endpoint will be : https://xxxxxx.azurewebsites.net/api/notify . In brief , just use your Azure App service endpoint to replace http://localhost:3978 will be fine .
Hope it helps . If there is anything unclear pls feel free to let me know .

Related

How to use Google Oauth2.0 to authenticate user via Telegram Bot

This is my first time interacting with Google API and I'm using python3.9 with this library Python Telegram Bot
I want to access a user Google API Calendar via a telegram bot and I can't seem to find any article to guide me through it. My key problem (I think) is redirecting the success authorization flow back to telegram bot.
This is what I have in mind:
In the telegram app, user send '/send' to bot
Bot receive message and return a google an authorization link to user
User clink on authorization link and allow access
Bot receive authorization access and completes the Oauth flow
The problem lies betweeen step 3 and 4. A standard authorization link is
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=<clientid>&redirect_uri=<redirect_uri>&scope=<scope>&state<state>&access_type=offline
How do I send the authorization link back to my telegram bot? Should I create another API endpoint to receive that authorization link? Or can I send telegram api send_message() in the <redirect_uri> to redirect the success message to my bot.
Update 1
Thanks to CallMeStag, I manage to figure out a way to complete the oauth process. For people who faced the same problem, this is what I did
Pre-requisite: Credentials is created in google console api - Web application. redirect_uri set as localhost:8000 (During development phase)
User send '/send' to bot
Bot receive message and return an authorization link https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=<clientid>&redirect_uri=http://localhost:8000/&scope=<scope>&state<state>&access_type=offline
User click on link to authenticate and it will redirect to http://localhost:8000. Used fastapi as a webhook to receive the message. Capture the authorization code, use google.oauthlib.flow to complete the authorization process. Next, redirect user back to telegram link https://t.me/<botname>
Start using user google calendar
It's currently indeed not very straight forward for a PTB-application to listen for external updates (the auth verification in this cas) - see also this issue. Currently it might be easiest for you to set up a custom webhook application that runs in parallel to the Updater - e.g. using flask/django/starlette/fastapi/…. Alternatively, if you're using webhooks for your bot anyway, you can patch the Updater to do the job for you. Although that requires some manual work - see here for an example.
Once you are able to listen to updates coming from Google, handling them can be done via PTBs usual handler setup, specifically via the TypeHandler or even a custom Handler subclass - see this FAQ entry.
Regarding the redirect url: You'll want to redirect your user back to your bot, so you'll have to provide a link that does that. Bot.link should probably do the trick.
Disclaimer: I'm currently the maintainer of python-telegram-bot.

MS Teams Incoming Webhook issue

I've created an incoming Teams Webhook connector within a Teams group (using the method below).
I can successfully curl to the webhook internally and get the message to display ok.
MS have checked my tenant and access is as expected.
When I apply my URL for the webhook to any external service they eventually come back stating there was a problem detected with the webhook (Please make sure that your webhook endpoint of xxx is responding with a 2xx response code within 30 seconds of initial connection.)
Can anyone advise what else may need to be done?
Thanks
In Microsoft Teams, choose More options (⋯) next to the channel name and then choose Connectors.
Scroll through the list of Connectors to Incoming Webhook, and choose Add.
Enter a name for the webhook, upload an image to associate with data from the webhook, and choose Create.
Copy the webhook to the clipboard and save it. You'll need the webhook URL for sending information to Microsoft Teams.
Choose Done.
Incoming webhooks are special type of Connector in Teams that provide a simple way for an external app to share content in team channels and are often used as tracking and notification tools. Teams provides a unique URL to which you send a JSON payload with the message that you want to POST, typically in a card format. Cards are user-interface (UI) containers that contain content and actions related to a single topic and are a way to present message data in a consistent way. Please test the incoming webhook url using postman and let us know the payload result or status code. Would be help full for us to understand more.

Message sent by Microsoft Teams bot is not displayed in the conversation

I have tried to send MS Teams bot message request via rest api:
End point: https://smba.trafficmanager.net/ca/apis/v3/conversations/{conversation id}/activities
{"text":"test","type":"message"}
And got empty response:
{}
The message i sent did not appear in the conversation.
Any idea where i could have misconfigured?
It is possible that trustServiceUrl needs to be run before a message is sent, i can't find equivalent rest api to call.
You definitely do need call trustServiceUrl before sending the message. In addition, you need to have additional properties on your activity, like the destination user or tenant id. Please refer to the documents on this, and good starting places are here and here.

Sending a message to users as a bot in bot channel of Microsoft Teams using the Graph API

I have created a bot by following the steps mentioned in the doc.I have authenticated user using oauth 2.0 (auth code grant) as mentioned in the doc and in reverse I got a access token. But when I send message to channel in the teams using (/teams/{id}/channels/{id}/messages) API the message was sent on behalf of me. But I want my bot as the sender of message. Here is the image of the message that I have sent using the above API. and is there any way to send direct message to user as a bot?
Instead of using the Graph, there's another approach using the Bot Framework itself, to send a message to a team channel, a group chat, or a 1-1 conversation. The code doesn't even need to live inside the bot itself, it just needs to leverage the bot framework under the covers (for example, I have several Azure Functions that pro-actively message users). This idea is called "Proactive messaging" and you can read more about it in the docs here.
You do need to get certain fields when the user first installs the bot though, or any time the bot receives a message. I've described that more at Programmatically sending a message to a bot in Microsoft Teams. You haven't said what language you're using, but there are examples for a bunch of them - I can send you links if you let me know what you're using.

Is possible to send an activity to a bot from a conversation from an other direcline client?

I have a bot, a client start a conversation with it using directline 3.0.
The bot follow a waterfall flow and at one step start an external process that do something. The process, finished the task use directline to start a new connection. i would like to send a message to the bot using a specific connection as the bot receive the message and can continue the flow. Is it possible?
Yes, this is possible using proactive messages. There is no need to use direct line, specifically, to create a new connection to send an activity. In short, in your bot's you will create a new API that external services can ping. As part of the process, when the API is hit, a conversation reference is created that, in short, allows you to pass any received data as an activity to the bot. When the bot receives the activity, you can setup logic to determine what the bot does next.
There is a SO post here that addresses this issue that you can reference. Additionally, you can refer to the BotBuilder-Samples GitHub sample for additional clarification.
Hope of help!

Resources