Beispiel #1
0
 async def process_activity(self, logic: Callable):
     """
     Begins listening to console input.
     :param logic:
     :return:
     """
     while True:
         #user's input is set as variable msg
         msg = input()
         #Nothing happens if the user inputs a blank input
         if msg is None:
             pass
         else:
             #increases _next_id value by one
             self._next_id += 1
             # creating an object for communication with the Bot. Used as an initial message for a new conversation
             activity = Activity(
                 text=msg,
                 channel_id='console',
                 #Uniquely identifies ID of user and their name
                 from_property=ChannelAccount(id='user', name='User1'),
                 #identifies the bot as recipient and their name as Bot
                 recipient=ChannelAccount(id='bot', name='Bot'),
                 #identification of the conversation
                 conversation=ConversationAccount(id='Convo1'),
                 type=ActivityTypes.message,
                 timestamp=datetime.datetime.now(),
                 id=str(self._next_id))
             #send to the bot
             activity = TurnContext.apply_conversation_reference(
                 activity, self.reference, True)
             context = TurnContext(self, activity)
             # output of the function
             #logs message from user
             await self.run_middleware(context, logic)
Beispiel #2
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)
Beispiel #3
0
    async def process_activity(self, logic: Callable):
        """
        Begins listening to console input.
        :param logic:
        :return:
        """
        while True:
            msg = input()
            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)
                await self.run_pipeline(context, logic)
    async def on_prompt(self, turn_context: TurnContext, state: Dict[str,
                                                                     object],
                        options: PromptOptions, is_retry: bool):
        if not turn_context:
            raise TypeError(
                'ConfirmPrompt.on_prompt(): turn_context cannot be None.')
        if not options:
            raise TypeError(
                'ConfirmPrompt.on_prompt(): options cannot be None.')

        # Format prompt to send
        channel_id = turn_context.activity.channel_id
        culture = self.determine_culture(turn_context.activity)
        defaults = self.choice_defaults[culture]
        choice_opts = self.choice_options if self.choice_options != None else defaults[
            2]
        confirms = self.confirm_choices if self.confirm_choices != None else (
            defaults[0], defaults[1])
        choices = {confirms[0], confirms[1]}
        if is_retry == True and options.retry_prompt != None:
            prompt = self.append_choices(options.retry_prompt)
        else:
            prompt = self.append_choices(options.prompt, channel_id, choices,
                                         self.style, choice_opts)
        turn_context.send_activity(prompt)
Beispiel #5
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)
Beispiel #6
0
 async def on_prompt(self, turn_context: TurnContext, state: Dict[str, object], options: PromptOptions, is_retry: bool):
     if not turn_context:
         raise TypeError('NumberPrompt.on_prompt(): turn_context cannot be None.')
     if not options:
         raise TypeError('NumberPrompt.on_prompt(): options cannot be None.')
     
     if is_retry == True and options.retry_prompt != None:
         prompt = turn_context.send_activity(options.retry_prompt)  
     else:
         if options.prompt != None:
             turn_context.send_activity(options.prompt)
Beispiel #7
0
    async def on_prompt(
        self,
        turn_context: TurnContext,
        state: Dict[str, object],
        options: PromptOptions,
        is_retry: bool,
    ):
        if not turn_context:
            raise TypeError(
                "NumberPrompt.on_prompt(): turn_context cannot be None.")
        if not options:
            raise TypeError(
                "NumberPrompt.on_prompt(): options cannot be None.")

        if is_retry and options.retry_prompt is not None:
            turn_context.send_activity(options.retry_prompt)
        elif options.prompt is not None:
            await turn_context.send_activity(options.prompt)
Beispiel #8
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])
Beispiel #9
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)