Exemple #1
0
    async def teams_message_handler(self, req: Request) -> Response:
        """Handle incoming webhooks from Teams."""
        if "application/json" in req.headers["Content-Type"]:
            body = await req.json()
            _LOGGER.debug(json.dumps(body))
        else:
            return Response(status=415)

        activity = Activity().deserialize(body)

        # Cache service endpoint for channel
        teams_channel_id = teams_get_channel_id(activity)
        if teams_channel_id not in self.service_endpoints:
            self.service_endpoints[teams_channel_id] = activity.service_url
            await self.opsdroid.memory.put("teams_service_endpoints",
                                           self.service_endpoints)

        if activity.type == "message":
            message = Message(
                text=activity.text,
                user=activity.from_property.name,
                target=TurnContext.get_conversation_reference(activity),
                connector=self,
                raw_event=TurnContext(self.adapter, activity),
            )
            await self.opsdroid.parse(message)
        else:
            _LOGGER.info(
                f"Recieved {activity.type} activity which is not currently supported."
            )

        return Response(status=200)
Exemple #2
0
    async def process_activities(self, msgs, logic: Callable):
        """
        Loops through array of strings to bot

        :param msgs:
        :param logic:
        :return:
        """
        for msg in msgs:
            if msg is None:
                pass
            else:
                self._next_id += 1
                activity = Activity(text=msg,
                                    channel_id='console',
                                    from_property=ChannelAccount(id='user', name='User1'),
                                    recipient=ChannelAccount(id='bot', name='Bot'),
                                    conversation=ConversationAccount(id='Convo1'),
                                    type=ActivityTypes.message,
                                    timestamp=datetime.datetime.now(),
                                    id=str(self._next_id))

                activity = TurnContext.apply_conversation_reference(activity, self.reference, True)
                context = TurnContext(self, activity)
                print(context.get_conversation_reference(activity))

                await self.run_middleware(context, logic)
Exemple #3
0
    async def send_message_to_teams_channel(
            turn_context: TurnContext, activity: Activity,
            teams_channel_id: str) -> Tuple[ConversationReference, str]:
        if not turn_context:
            raise ValueError("The turn_context cannot be None")
        if not activity:
            raise ValueError("The activity cannot be None")
        if not teams_channel_id:
            raise ValueError("The teams_channel_id cannot be None or empty")

        old_ref = TurnContext.get_conversation_reference(turn_context.activity)
        conversation_parameters = ConversationParameters(
            is_group=True,
            channel_data={"channel": {
                "id": teams_channel_id
            }},
            activity=activity,
        )

        result = await turn_context.adapter.create_conversation(
            old_ref, TeamsInfo._create_conversation_callback,
            conversation_parameters)
        return (result[0], result[1])
Exemple #4
0
 async def _create_conversation_callback(
     new_turn_context, ) -> Tuple[ConversationReference, str]:
     new_activity_id = new_turn_context.activity.id
     conversation_reference = TurnContext.get_conversation_reference(
         new_turn_context.activity)
     return (conversation_reference, new_activity_id)