39
loading...
This website collects cookies to deliver better user experience
import requests
url = "https://discordapp.com/api/channels/DISCORD_CHANNEL_ID/messages?limit=10"
payload={}
headers = {'Authorization': 'Bot BOT_TOKEN'}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
.
├── .funcignore
├── .gitignore
├── host.json
├── local.settings.json
├── PotatoNoticiasTrigger
│ ├── function.json
│ ├── __init__.py
│ └── sample.dat
├── requirements.txt
└── .vscode
├── extensions.json
├── launch.json
├── settings.json
└── tasks.json
func host start
Value cannot be null. Parameter name: Provider error on func start
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.3.0, 4.0.0)"
}
requests
ask-sdk-core
ask-sdk-webservice-support
pip install -r requirements.txt
import os
ALEXA_SKILL_ID = os.getenv("ALEXA_SKILL_ID", "")
DISCORD_CHANNEL_ID = os.getenv("DISCORD_CHANNEL_ID", "")
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN", "")
from typing import List
import requests
import json
from PotatoNoticiasTrigger.config import DISCORD_CHANNEL_ID, DISCORD_BOT_TOKEN
def get_n_messages(size: int = 5):
# type: (int) -> List
payload = {}
url = "https://discordapp.com/api/channels/{0}/messages?limit={1}".format(
DISCORD_CHANNEL_ID, size)
headers = {
'Authorization': 'Bot {0}'.format(DISCORD_BOT_TOKEN)
}
response = requests.request("GET", url, headers=headers, data=payload)
if (response.status_code == 200):
data = response.json()
return data
else:
return None
class LaunchRequestHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return is_request_type("LaunchRequest")(handler_input)
def handle(self, handler_input):
print("Ejecucion de la Skill.")
speech_text = "Bienvenido a Potato Noticias, puedes decir últimas noticias"
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Potato Noticias", speech_text)).set_should_end_session(False)
return handler_input.response_builder.response
class HelpIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("AMAZON.HelpIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
print("Ejecucion Intent Ayuda")
speech_text = "Puedes decir últimas noticias, últimos 2 mensajes"
handler_input.response_builder.speak(speech_text).ask(speech_text).set_card(
SimpleCard("Potato Noticias - Ayuda", speech_text))
return handler_input.response_builder.response
class CancelAndStopIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("AMAZON.CancelIntent")(handler_input) or is_intent_name("AMAZON.StopIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
print("Ejecución Intent Cancel o Stop")
speech_text = "Hasta luego Atentamente Potato Noticias!"
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Potato Noticias", speech_text)).set_should_end_session(True)
return handler_input.response_builder.response
class SessionEndedRequestHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_request_type("SessionEndedRequest")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
# any cleanup logic goes here
return handler_input.response_builder.response
class AllExceptionHandler(AbstractExceptionHandler):
def can_handle(self, handler_input, exception):
# type: (HandlerInput, Exception) -> bool
return True
def handle(self, handler_input, exception):
# type: (HandlerInput, Exception) -> Response
print(exception)
speech = "Lo siento, No pude procesarlo. Puedes intentarlo nuevamente o decir Ayuda"
handler_input.response_builder.speak(speech).ask(speech)
return handler_input.response_builder.response
from ask_sdk_core.dispatch_components import AbstractRequestHandler, AbstractExceptionHandler
from ask_sdk_core.utils import is_request_type, is_intent_name
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model import Response
from ask_sdk_model.ui import SimpleCard
class LaunchRequestHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return is_request_type("LaunchRequest")(handler_input)
def handle(self, handler_input):
print("Ejecucion de la Skill.")
speech_text = "Bienvenido a Potato Noticias, puedes decir últimas noticias"
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Potato Noticias", speech_text)).set_should_end_session(False)
return handler_input.response_builder.response
class HelpIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("AMAZON.HelpIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
print("Ejecucion Intent Ayuda")
speech_text = "Puedes decir últimas noticias, últimos 2 mensajes"
handler_input.response_builder.speak(speech_text).ask(speech_text).set_card(
SimpleCard("Potato Noticias - Ayuda", speech_text))
return handler_input.response_builder.response
class CancelAndStopIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("AMAZON.CancelIntent")(handler_input) or is_intent_name("AMAZON.StopIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
print("Ejecucion Intent Cancel o Stop")
speech_text = "Hasta luego Atentamente Potato Noticias!"
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Potato Noticias", speech_text)).set_should_end_session(True)
return handler_input.response_builder.response
class SessionEndedRequestHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_request_type("SessionEndedRequest")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
# any cleanup logic goes here
return handler_input.response_builder.response
class AllExceptionHandler(AbstractExceptionHandler):
def can_handle(self, handler_input, exception):
# type: (HandlerInput, Exception) -> bool
return True
def handle(self, handler_input, exception):
# type: (HandlerInput, Exception) -> Response
print(exception)
speech = "Lo siento, No pude procesarlo. Puedes intentarlo nuevamente o decir Ayuda"
handler_input.response_builder.speak(speech).ask(speech)
return handler_input.response_builder.response
def generate_response(messages):
# type: (List) -> Tuple[str,str]
speak_out = ""
speak_out_visible = ""
itemCount = 1
for message in messages:
speak_out += 'Mensaje {0}: <break time="0.5s"/> {1}. <break time="1s"/>'.format(
itemCount, message.get("content", ""))
speak_out_visible += '{0}: {1}.\n'.format(
itemCount, message.get("content", ""))
itemCount += 1
return speak_out, speak_out_visible
class NoticiasIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("NoticiasIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
cantidad = get_slot_value(handler_input, "cantidad")
print(cantidad)
if cantidad is not None:
messages = get_n_messages(size=cantidad)
else:
messages = get_n_messages()
if messages is None or len(messages) == 0:
speak_output = "No se encontraron mensajes"
visual_output = "No se encontraron mensajes :("
else:
speak_output,visual_output = generate_response(messages)
speak_output += " Esos son todos los mensajes!."
visual_output += " Esos son todos los mensajes!."
handler_input.response_builder.speak(speak_output).set_card(
SimpleCard("Potato Noticias", visual_output)).set_should_end_session(True)
return handler_input.response_builder.response
import json
import azure.functions as func
from ask_sdk_core.skill_builder import SkillBuilder
from ask_sdk_webservice_support.webservice_handler import WebserviceSkillHandler
from PotatoNoticiasTrigger.default_intents import AllExceptionHandler, CancelAndStopIntentHandler, HelpIntentHandler, LaunchRequestHandler, SessionEndedRequestHandler
from PotatoNoticiasTrigger.noticias_intent import NoticiasIntentHandler
from PotatoNoticiasTrigger.config import ALEXA_SKILL_ID
def main(req: func.HttpRequest) -> func.HttpResponse:
skill_builder = SkillBuilder()
skill_builder.skill_id = ALEXA_SKILL_ID
# Default Intents
skill_builder.add_request_handler(LaunchRequestHandler())
skill_builder.add_request_handler(HelpIntentHandler())
skill_builder.add_request_handler(CancelAndStopIntentHandler())
skill_builder.add_request_handler(SessionEndedRequestHandler())
skill_builder.add_exception_handler(AllExceptionHandler())
# Custom Intents
skill_builder.add_request_handler(NoticiasIntentHandler())
webservice_handler = WebserviceSkillHandler(skill=skill_builder.create())
response = webservice_handler.verify_request_and_dispatch(req.headers, req.get_body().decode("utf-8"))
return func.HttpResponse(json.dumps(response),mimetype="application/json")
func host start
http://localhost:7071/api/PotatoNoticiasTrigger
ngrok http 7071
Forwarding http://2dba-200-87-92-9.ngrok.io -> http://localhost:7071
Forwarding https://2dba-200-87-92-9.ngrok.io -> http://localhost:7071
https://2dba-200-87-92-9.ngrok.io/api/PotatoNoticiasTrigger
https://potatonoticias.azurewebsites.net/api/PotatoNoticiasTrigger
Un agradecimiento a la comunidad de Python La Paz por permitirme dar una charla sobre esto, síganlos en sus redes sociales Facebook y Twitter y pregunten por su servidor de Discord, para conocer más sobre futuras charlas y eventos, también pueden ser expositores y enseñarnos su conocimiento para crecer con esta bonita comunidad.