advertisement
Call Dingding interface to realize robot push message
1. Summary
In real transactions, in order to know the trading status of the FMZ Quant robot in time, sometimes we need to send the transaction results executed by the robot to WeChat, email, SMS, etc. However, there are hundreds of different types of information every day, making it insensitive to this information, leading to failure in timely collection of important information. Therefore, this article implements the robot's push message by calling the Dingding group interface.
2. Dingding Group Robot
Dingding group robot is an advanced extension function. As long as you have a Dingding account, you can use it. You can add third-party information to the Dingding group to achieve automatic information synchronization. Supports custom access to the Webhook protocol and adds reminders, alerts and other information to the Dingding group through the FMZ Quant robot. Three message formats and five message types are supported: text, link, and markdown. The same message can also be sent to multiple Dingding groups at the same time.
Please check the official link: https://ding-doc.dingtalk.com/doc#/serverapi2/ye8tup
3. Create robot
Step 1: Create a Dingding group
Each custom robot created in the Dingding pool will generate a unique link address, which is called a WebHook address. The Dingding group will receive a message by sending a message to the WebHook address. Take the PC version of Dingding as an example. First, click the "+" sign at the top left to start a group chat. If you just want to accept the message yourself, you can select two people and kick them out. Fill in the group name: "FMZ Robot" and select a normal group as the group type.
Step 2: Add Dingding Group Robot
Click the avatar, select Robot Management, then select Custom and click Add. Custom robot name: "FMZ", added to the newly created Dingding group. The robot supports three security settings:
- User-defined keyword: information will be synchronized only if it contains this keyword.
- Signature: equivalent to setting a password.
- IP address: the IP address segment of third-party fixed information.
If only used as a reminder or alert, select the user-defined keyword. The keyword we define here is ":", that is, when the information sent by FMZ Quant Robot contains ":", the information will be sent to the Dingding group. Then click Accept to complete the agreement. Finally, copy the Webhook address as a backup.
4. Code implementation
After obtaining the address of the Webhook, we can send the information to the Dingding group by sending an HTTP POST request to the address in the FMZ Quant strategy. Note that the character set encoding must be set to UTF-8 when a POST request is initiated.
import requests
import json
from datetime import datetime, timedelta, timezone
# Send information to the Dingding group
def message(text):
token="0303627a118e739e628bcde104e19cf5463f61a4a127e4f2376e6a8aa1156ef1"
headers = {'Content-Type': 'application/json;charset=utf-8'} # Request header
api_url = f"https://oapi.dingtalk.com/robot/send?access_token={token}"
json_text = {
"msgtype": "text", # message type
"text": {
"content": text
}
}
# Send and print messages
Registration(requests.post(api_url, json.dumps(json_text), headers=headers).content)
# Test functions
def onTick():
arr = ['BTC', 'ETH', 'XRP', 'BCH', 'LTC'] # Mainstream digital currencies
# Get Eastern Time 8
bj_dt = str(datetime.now().astimezone(timezone(timedelta(hours=8))))
bj_dt = bj_dt.split('.')[0] # Processing time
text = f'{bj_dt}\n' # Define information content
for i in arr: # conventional digital currency array loop
exchange.IO("currency", f"{i}_USDT") # Change trading pairs
ticker = exchange.GetTicker().Last # Get the last price
if i == 'LTC':
complete = ':'
more:
complete = ':'
text = t
advertisement
Related Articles
advertisement