Андрей Повх Asked:2022-01-14 16:37:56 +0800 CST2022-01-14 16:37:56 +0800 CST 2022-01-14 16:37:56 +0800 CST 如何使用 Bitrix 24 中的电报机器人创建交易? 772 在电报中,有人打开机器人并出现“民意调查”,该人输入的数据必须转到 Bitrix24,并在那里创建交易。是真的做吗?告诉我往哪个方向挖。 telegram-bot 1 个回答 Voted Best Answer Violet 2022-02-01T02:15:37+08:002022-02-01T02:15:37+08:00 main.py: import telebot import requests bot = telebot.TeleBot('123:AAA') b24_url_token = 'http://localhost/b24' b24_url_deal = 'https://domain.bitrix24.ru/rest/crm.deal.add.json' b24_url_deal_product = 'https://domain.bitrix24.ru/rest/crm.deal.productrows.set.json' b24_token = requests.post(b24_url_token).json()['access_token'] @bot.message_handler(commands=['start']) def start(message): msg = bot.send_message(message.chat.id, 'Введите *название сделки*', parse_mode='Markdown') bot.register_next_step_handler(msg, start_2) def start_2(message): msg = bot.send_message(message.chat.id, 'Введите *название продукта стоимость количество*\n_прим. Пряник 12 4', parse_mode='Markdown') bot.register_next_step_handler(msg, start_3, message.text) def start_3(message, title_lead): product = message.text.split()[0] price = message.text.split()[1] quantity = message.text.split()[2] b24_crm_add = requests.post(b24_url_deal, json={'fields': {'TITLE': title_lead, 'ASSIGNED_BY_ID': 1}, 'auth': b24_token}) b24_crm_add_product = requests.post(b24_url_deal_product, json={'id': 1, 'rows': [{"PRODUCT_ID": 1, 'PRODUCT_NAME': product, "PRICE": float(price), "QUANTITY": quantity}], 'auth': b24_token}) http://localhost/b24: conf_b24_oauth_token.php: <?php return [ 'production' => [ //идентификатор приложения 'client_id' => 'local.5f***9.7***8', //секретный код приложения 'client_secret' => '8cq***Sj7', 'scope' => 'crm', //домен третьего уровня клиентского проекта в Bitrix24 'domain' => 'domain.bitrix24.ru', //данные пользователя bitrix24 'login' => 'user', 'password' => 'password', ] ]; ?> index.php: <?php $config = require __DIR__.'/conf_b24_oauth_token.php'; $config = $config['production']; $_url = 'https://'.$config['domain']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $_url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $res = curl_exec($ch); $l = ''; if(preg_match('#Location: (.*)#', $res, $r)) { $l = trim($r[1]); } //echo $l.PHP_EOL; curl_setopt($ch, CURLOPT_URL, $l); $res = curl_exec($ch); preg_match('#name="backurl" value="(.*)"#', $res, $math); $post = http_build_query([ 'AUTH_FORM' => 'Y', 'TYPE' => 'AUTH', /* 'backurl' => $math[1], */ 'USER_LOGIN' => $config['login'], 'USER_PASSWORD' => $config['password'], 'USER_REMEMBER' => 'Y' ]); curl_setopt($ch, CURLOPT_URL, 'https://www.bitrix24.net/auth/'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $res = curl_exec($ch); $l = ''; if(preg_match('#Location: (.*)#', $res, $r)) { $l = trim($r[1]); } //echo $l.PHP_EOL; curl_setopt($ch, CURLOPT_URL, $l); $res = curl_exec($ch); $l = ''; if(preg_match('#Location: (.*)#', $res, $r)) { $l = trim($r[1]); } //echo $l.PHP_EOL; curl_setopt($ch, CURLOPT_URL, $l); $res = curl_exec($ch); //end autorize curl_setopt($ch, CURLOPT_URL, 'https://'.$config['domain'].'/oauth/authorize/?response_type=code&client_id='.$config['client_id']); $res = curl_exec($ch); $parts = parse_url($res); parse_str($parts['query'], $query); $code = $query['code']; curl_setopt($ch, CURLOPT_URL, 'https://'.$config['domain'].'/oauth/token/?grant_type=authorization_code&client_id='.$config['client_id'].'&client_secret='.$config['client_secret'].'&code='.$code.'&scope=crm'); curl_setopt($ch, CURLOPT_HEADER, false); $res = curl_exec($ch); curl_close($ch); echo $res; ?> pyTelegramBotAPI crm_deal_add crm_deal_productrows_set
main.py
:http://localhost/b24:
conf_b24_oauth_token.php
:index.php
: