Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ Simple Message Notification/ Best Practices/ Using FunctionGraph to Forward SMN Messages to Slack

Using FunctionGraph to Forward SMN Messages to Slack

Updated on 2024-12-17 GMT+08:00

Scenarios

This section described how to use FunctionGraph to forward SMN messages to Slack. Slack, developed by Slack Technologies, is cloud-based instant messaging software. Slack does not have an API to receive HTTPS messages sent from SMN. So, you cannot directly add a Slack subscription to receive SMN notifications. Instead, you need to use FunctionGraph to forward messages to a specified URL.

Prerequisites

  • You have created an SMN message topic, for example, smn-test. For details, see Creating a Topic.
  • You have downloaded Slack.

Procedure

To use FunctionGraph to send SMN messages to Slack, perform the following operations:

  1. Configuring Slack
  2. Configuring FunctionGraph
  3. Configuring SMN

Configuring Slack

NOTE:

The following operations are for reference only. For details, visit the Slack website.

  1. Create or select a channel on Slack.
    Figure 1 Creating a channel
  2. Choose Apps > Manage > Browse apps.
    Figure 2 Choosing Apps
  3. Search for Incoming Webhooks and click Add.
    Figure 3 Adding Incoming Webhooks
  4. Click Configuration to go to the configuration page and obtain the Webhook URL.
    Figure 4 Clicking Configuration
    Figure 5 Obtaining the Webhook URL
  5. Save the obtained Webhook URL.
    Figure 6 Saving the Webhook URL

Configuring FunctionGraph

  1. Log in to the FunctionGraph console. In the navigation pane, choose Functions > Function List.
  2. Click Create Function in the upper right corner and choose Select template.
  3. Select the template shown in Figure 7 and click Configure.
    Figure 7 Selecting a template
  4. Set the function name, select an agency, retain default values for other parameters, and click Create Function.
    Figure 8 Configuring basic information
  5. On the configuration details page, click the Code tab and edit the code in index.py.
    Figure 9 Editing the code

    The following is an example of the code sent to Slack: (In actual use, set webhook_url to the Webhook URL obtained from Slack or perform other operations as required.)

    # -*- coding:utf-8 -*-
    import json
    import requests
    
    def handler (event, context):
        # Common part: Construct the body fields required by the destination address. event contains all fields sent by SMN.
        payload = {'text': event["record"][0]["smn"]["message"]}
    
        # Forwarding part: Call a Slack API to forward messages to the Slack platform.
        try:
            webhook_url = 'Webhook URL obtained from Slack'
            response = requests.post(webhook_url, json=payload)
            response.raise_for_status()
            print('Message sent successfully')
        except requests.exceptions.RequestException as e:
            print(f"Error sending message to Slack: {e}")
    
        return event
  6. After the editing is complete, press Ctrl+S to save the modification and update the code.
  7. Click Create Trigger.
  8. Set the following parameters:
    • Trigger Type: Select Simple Message Notification (SMN).
    • Topic Name: Select a topic, for example, smn-test.
    Figure 10 Creating a trigger
  9. Click OK. After the topic is created, all messages sent to the topic are forwarded to the destination address through FunctionGraph.

Configuring SMN

  1. Log in to the SMN console. In the navigation pane, choose Topic Management > Topics.
  2. Locate the topic smn-test and click Add Subscription in the Operation column.
  3. Set the following parameters:
    • Protocol: Select FunctionGraph (function).
    • Endpoint: Select a created function, for example, smn-test.
    Figure 11 Adding a subscription
  4. Click OK.
  5. Locate the topic smn-test and click Publish Message in the Operation column.
  6. Set the following parameters:
    • Message Format: Text
    • Message Content: Enter the message content.
  7. Click OK.

Viewing the Received Message

After a message is published, you can receive the message on Slack. Figure 12 shows an example.

Figure 12 Message received on Slack

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback